Skip to content

Commit

Permalink
Added support for Velocity platform
Browse files Browse the repository at this point in the history
  • Loading branch information
MCMDEV committed Mar 13, 2022
1 parent 644f86a commit fe0fbbd
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 14 deletions.
11 changes: 11 additions & 0 deletions .idea/artifacts/i18n_velocity.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/google-java-format.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 19 additions & 14 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions i18n-velocity/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>i18n</artifactId>
<groupId>de.helixdevs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>i18n-velocity</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<repositories>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>de.helixdevs</groupId>
<artifactId>i18n-adventure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>3.1.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package de.helixdevs.i18n.velocity;

import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
import com.velocitypowered.api.command.RawCommand;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import de.helixdevs.i18n.adventure.AdventureTranslationConsumer;
import de.helixdevs.i18n.api.TranslationConsumer;
import de.helixdevs.i18n.common.command.api.Command;
import de.helixdevs.i18n.common.platform.I18nPlatform;
import de.helixdevs.i18n.common.platform.I18nPlugin;
import de.helixdevs.i18n.velocity.command.VelocitySender;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.yaml.snakeyaml.DumperOptions;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

@Plugin(
id = "i18n",
name = "I18n",
version = "1.0-SNAPSHOT",
description = "Velocity implementation of I18n",
authors = {"MCMDEV"}
)
public class I18nVelocityPlugin implements I18nPlugin {

private final ProxyServer proxyServer;
private final Logger logger;
private final Path dataDirectory;
private final I18nPlatform platform;

private ConfigurationNode configurationNode;

@Inject
public I18nVelocityPlugin(ProxyServer proxyServer, Logger logger, @DataDirectory Path dataDirectory) {
this.proxyServer = proxyServer;
this.logger = logger;
this.dataDirectory = dataDirectory;
this.platform = new I18nPlatform(this);
}

@Subscribe
private void onProxyInitialization(ProxyInitializeEvent event) throws IOException {
YAMLConfigurationLoader configurationLoader = YAMLConfigurationLoader.builder()
.setFlowStyle(DumperOptions.FlowStyle.FLOW)
.setIndent(2)
.setPath(dataDirectory.resolve("config.yml"))
.build();
this.configurationNode = configurationLoader.load();

platform.enable();
}

@Subscribe
private void onProxyShutdown(ProxyShutdownEvent event) {
platform.disable();
}

@Override
public String getConfigString(String key) {
return configurationNode.getNode(key).getString();
}

@Override
public List<String> getConfigStringList(String key) {
try {
return configurationNode.getNode(key).getList(TypeToken.of(String.class));
} catch (ObjectMappingException e) {
e.printStackTrace();
}
return Collections.emptyList();
}

@Override
public Set<TranslationConsumer> getConsumers() {
Set<TranslationConsumer> consumers = new HashSet<>();
consumers.add(new AdventureTranslationConsumer());
return consumers;
}

@Override
public Logger getLogger() {
return this.logger;
}

@Override
public void registerCommand(Command command) {
proxyServer.getCommandManager().register(command.getLabel(), new SimpleCommand() {
@Override
public void execute(Invocation invocation) {
command.execute(new VelocitySender(invocation.source()), invocation.arguments());
}

@Override
public boolean hasPermission(Invocation invocation) {
return invocation.source().hasPermission(command.getPermission());
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.helixdevs.i18n.velocity.command;

import com.velocitypowered.api.command.CommandSource;
import de.helixdevs.i18n.common.command.api.Sender;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

public class VelocitySender implements Sender {

private final CommandSource holder;

public VelocitySender(CommandSource holder) {
this.holder = holder;
}

@Override
public void sendMessage(String message) {
holder.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(message));
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<module>i18n-paper</module>
<module>i18n-api</module>
<module>i18n-adventure</module>
<module>i18n-velocity</module>
</modules>

<properties>
Expand Down

0 comments on commit fe0fbbd

Please sign in to comment.