Skip to content

Commit

Permalink
Added SendWarningOnJoin config option. defaults to true.
Browse files Browse the repository at this point in the history
Updated to SpongeAPI 4.1.0
  • Loading branch information
ryantheleach committed Aug 17, 2016
1 parent 4fc838f commit 954beb0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'

group 'au.id.rleach'
version '1.1.2'
version '1.2.0'

sourceCompatibility = 1.8

Expand Down Expand Up @@ -40,5 +40,5 @@ dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
shadow translator
compile translator
compile 'org.spongepowered:spongeapi:4.0.3'
compile 'org.spongepowered:spongeapi:4.1.0'
}
33 changes: 28 additions & 5 deletions src/main/java/au/id/rleach/translate/TranslateWithBing.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package au.id.rleach.translate;


import au.id.rleach.translate.data.ImmutableLanguageData;
import au.id.rleach.translate.data.LanguageData;
import au.id.rleach.translate.data.TranslateKeys;
Expand All @@ -14,7 +15,10 @@
import ninja.leaping.configurate.loader.ConfigurationLoader;
import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.config.DefaultConfig;
Expand All @@ -23,6 +27,7 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.game.GameReloadEvent;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.message.MessageChannelEvent;
Expand All @@ -46,7 +51,7 @@
import javax.inject.Inject;


@Plugin(id="translatewithbing", name="TranslateWithBing", version="1.1.2")
@Plugin(id="translatewithbing", name="TranslateWithBing", version="1.2.0", description = "Translates chat using Microsoft Azure")
public class TranslateWithBing {

@Inject
Expand All @@ -59,6 +64,7 @@ public class TranslateWithBing {

private final URL jarConfigFile = this.getClass().getResource("default.conf");
private ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder().setURL(jarConfigFile).build();
private boolean sendWarningOnJoin = true;

@Listener
public void onPreInit(GamePreInitializationEvent event) {
Expand All @@ -82,8 +88,14 @@ private void initMap(){
}
}

@Listener
public void reloadGame(final GameReloadEvent reloadEvent){
reload();
}

@Listener
public void onPlayerJoin(final ClientConnectionEvent.Join event){
if(!this.sendWarningOnJoin) return;
if(Sponge.getServer().getOnlinePlayers().stream().anyMatch(p->!p.getLocale().equals(event.getTargetEntity().getLocale()))) {
Sponge.getServer().getBroadcastChannel().send(languageWarning);
}
Expand All @@ -97,7 +109,7 @@ public void onCommandInitTime(final GameInitializationEvent event) {
.permission(LANGUAGE_OVERRIDE_PERMISSION)
.executor((src, context) -> {
Optional<Player> p = context.getOne("player");
Optional<Language> opt = context.getOne("options.language");
Optional<Language> opt = context.getOne(commandKey);
if (!p.isPresent()) {
return CommandResult.empty();
}
Expand All @@ -107,8 +119,7 @@ public void onCommandInitTime(final GameInitializationEvent event) {
try {
src.sendMessage(Text.of("Players language set to : ", opt.get().getName(opt.get())));
} catch (Exception e) {
src.sendMessage(Text.of("An error occured",e.getMessage()));
return CommandResult.empty();
throw new CommandException(Text.of(e.getMessage()));
}
return CommandResult.success();
} else {
Expand All @@ -129,7 +140,7 @@ public void onCommandInitTime(final GameInitializationEvent event) {
.build();
final CommandSpec reloadConfigSpec = CommandSpec.builder()
.permission(CONFIG_RELOAD_PERMISSION)
.executor((src,args)->{setupPlugin();src.sendMessage(Text.of("Reloaded translator config"));return CommandResult.success();})
.executor((src,args)->{return doReload(src,args);})
.build();

Sponge.getCommandManager().register(this, overrideLanguageSpec, "language");
Expand All @@ -154,6 +165,16 @@ public void onCommandInitTime(final GameInitializationEvent event) {

}

private CommandResult doReload(final CommandSource src, final CommandContext args) {
reload();
src.sendMessage(Text.of("Reloaded translator config"));
return CommandResult.success();
}

private void reload() {
setupPlugin();
}

@Listener
public final void serverStarted(final GamePreInitializationEvent event){
setupPlugin();
Expand All @@ -176,10 +197,12 @@ private void setupPlugin(){
final CommentedConfigurationNode secret = rootNode.getNode("ClientSecret");
final String sID = id.getString(defNode.getNode("ClientID").getString());
final String sSecret = secret.getString(defNode.getNode("ClientSecret").getString());
final Boolean sSendWarningOnJoin = rootNode.getNode("SendWarningOnJoin").getBoolean(true);
if("UNSET".equals(sSecret)) {
throw new RuntimeException("You need to register a ClientID & Client Secret to use this plugin, see https://msdn.microsoft.com/en-us/library/mt146806.aspx and fill in the config");
}
try {
sendWarningOnJoin = Preconditions.checkNotNull(sSendWarningOnJoin);
Translate.setClientId(Preconditions.checkNotNull(sID));
Translate.setClientSecret(Preconditions.checkNotNull(sSecret));
} catch (final RuntimeException e){
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/au/id/rleach/translate/default.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#Get your Client ID and Client Secret by signing up for Microsofts Translator Service, Free tier = 2M characters a month.
#https://msdn.microsoft.com/en-us/library/mt146806.aspx
ClientID = TAAMC
ClientSecret = "UNSET"
ClientSecret = "UNSET"
SendWarningOnJoin = true

0 comments on commit 954beb0

Please sign in to comment.