Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Essentials/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,11 @@
<version>2.16.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>LocaleApiProvider</artifactId>
<version>2.16.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
29 changes: 28 additions & 1 deletion Essentials/src/com/earth2me/essentials/CommandSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import static com.earth2me.essentials.I18n.tl;


public class CommandSource {
protected CommandSender sender;
protected User user = null;

public CommandSource(final CommandSender base) {
this.sender = base;
}

public CommandSource(final User user) {
this.sender = user.getBase();
this.user = user;
}

public final CommandSender getSender() {
return sender;
}
Expand All @@ -30,10 +38,29 @@ public final CommandSender setSender(final CommandSender base) {
return this.sender = base;
}


public void sendMessage(String message) {
if (!message.isEmpty()) {
sender.sendMessage(message);
}
}

public void setUser(User user) {
this.user = user;
}

public User getUser() {
return user;
}

public void sendTl(String string, Object... objects) {
sendMessage(tl(string, objects));
}

public String tl(String string, Object... objects) {
if (user != null) {
return I18n.tl(user.getApplicableLocale(), string, objects);
} else {
return I18n.tl(string, objects);
}
}
}
10 changes: 8 additions & 2 deletions Essentials/src/com/earth2me/essentials/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.bukkit.Server;
import org.bukkit.command.CommandSender;

import java.util.Locale;


public final class Console implements IMessageRecipient {
public static final String NAME = "Console";
Expand Down Expand Up @@ -54,11 +56,11 @@ public CommandSender getCommandSender() {
@Override public boolean isReachable() {
return true;
}



/* ================================
* >> DELEGATE METHODS
* ================================ */

@Override public MessageResponse sendMessage(IMessageRecipient recipient, String message) {
return this.messageRecipient.sendMessage(recipient, message);
}
Expand All @@ -74,4 +76,8 @@ public CommandSender getCommandSender() {
@Override public void setReplyRecipient(IMessageRecipient recipient) {
this.messageRecipient.setReplyRecipient(recipient);
}

@Override public Locale getLocale() {
return this.messageRecipient.getLocale();
}
}
111 changes: 83 additions & 28 deletions Essentials/src/com/earth2me/essentials/Essentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
import net.ess3.api.*;
import net.ess3.api.IEssentials;
import net.ess3.api.ISettings;
import net.ess3.nms.PlayerLocaleProvider;
import net.ess3.nms.PotionMetaProvider;
import net.ess3.nms.SpawnEggProvider;
import net.ess3.nms.SpawnerProvider;
import net.ess3.nms.flattened.FlatSpawnEggProvider;
import net.ess3.nms.legacy.LegacyPotionMetaProvider;
import net.ess3.nms.legacy.LegacySpawnEggProvider;
import net.ess3.nms.legacy.LegacySpawnerProvider;
import net.ess3.nms.localeapi.ApiPlayerLocaleProvider;
import net.ess3.nms.refl.ReflPlayerLocaleProvider;
import net.ess3.nms.refl.ReflSpawnEggProvider;
import net.ess3.nms.updatedmeta.BasePotionDataProvider;
import net.ess3.nms.updatedmeta.BlockMetaSpawnerProvider;
Expand Down Expand Up @@ -110,6 +113,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
private transient SpawnerProvider spawnerProvider;
private transient SpawnEggProvider spawnEggProvider;
private transient PotionMetaProvider potionMetaProvider;
private transient PlayerLocaleProvider playerLocaleProvider;
private transient Kits kits;

public Essentials() {
Expand Down Expand Up @@ -246,6 +250,12 @@ public void onEnable() {
BasePotionDataProvider.class,
LegacyPotionMetaProvider.class
), "potion meta").getProvider();
playerLocaleProvider = new ProviderFactory<>(getLogger(),
Arrays.asList(
ApiPlayerLocaleProvider.class,
ReflPlayerLocaleProvider.class
)
, "player locale").getProvider();
execTimer.mark("Init(Providers)");
reload();

Expand Down Expand Up @@ -343,7 +353,7 @@ public void onDisable() {
for (User user : getOnlineUsers()) {
if (user.isVanished()) {
user.setVanished(false);
user.sendMessage(tl("unvanishedReload"));
user.sendTl("unvanishedReload");
}
user.stopTransaction();
}
Expand Down Expand Up @@ -400,13 +410,15 @@ public List<String> onTabCompleteEssentials(final CommandSender cSender, final C
}
}

try {
// Note: The tab completer is always a player, even when tab-completing in a command block
User user = null;
if (cSender instanceof Player) {
user = getUser((Player) cSender);
}
// Note: The tab completer is always a player, even when tab-completing in a command block
User user = null;
if (cSender instanceof Player) {
user = getUser((Player) cSender);
}

Locale locale = user != null ? user.getApplicableLocale() : getI18n().getCurrentLocale();

try {
CommandSource sender = new CommandSource(cSender);

// Check for disabled commands
Expand All @@ -420,8 +432,8 @@ public List<String> onTabCompleteEssentials(final CommandSender cSender, final C
cmd.setEssentials(this);
cmd.setEssentialsModule(module);
} catch (Exception ex) {
sender.sendMessage(tl("commandNotLoaded", commandLabel));
LOGGER.log(Level.SEVERE, tl("commandNotLoaded", commandLabel), ex);
sender.sendMessage(tl(locale, "commandNotLoaded", commandLabel));
LOGGER.log(Level.SEVERE, tl(locale, "commandNotLoaded", commandLabel), ex);
return Collections.emptyList();
}

Expand All @@ -444,11 +456,11 @@ public List<String> onTabCompleteEssentials(final CommandSender cSender, final C
} catch (Exception ex) {
showError(sender, ex, commandLabel);
// Tab completion shouldn't fail
LOGGER.log(Level.SEVERE, tl("commandFailed", commandLabel), ex);
LOGGER.log(Level.SEVERE, tl(locale, "commandFailed", commandLabel), ex);
return Collections.emptyList();
}
} catch (Throwable ex) {
LOGGER.log(Level.SEVERE, tl("commandFailed", commandLabel), ex);
LOGGER.log(Level.SEVERE, tl(locale, "commandFailed", commandLabel), ex);
return Collections.emptyList();
}
}
Expand Down Expand Up @@ -476,11 +488,12 @@ public boolean onCommandEssentials(final CommandSender cSender, final Command co
}

try {

User user = null;
Block bSenderBlock = null;
User user = null;
CommandSource sender = new CommandSource(cSender);
if (cSender instanceof Player) {
user = getUser((Player) cSender);
sender.setUser(user);
} else if (cSender instanceof BlockCommandSender) {
BlockCommandSender bsender = (BlockCommandSender) cSender;
bSenderBlock = bsender.getBlock();
Expand All @@ -492,8 +505,6 @@ public boolean onCommandEssentials(final CommandSender cSender, final Command co
Bukkit.getLogger().log(Level.INFO, "{0} issued server command: /{1} {2}", new Object[]{cSender.getName(), commandLabel, EssentialsCommand.getFinalArg(args, 0)});
}

CommandSource sender = new CommandSource(cSender);

// New mail notification
if (user != null && !getSettings().isCommandDisabled("mail") && !command.getName().equals("mail") && user.isAuthorized("essentials.mail")) {
user.notifyOfMail();
Expand All @@ -516,23 +527,23 @@ public boolean onCommandEssentials(final CommandSender cSender, final Command co
cmd.setEssentials(this);
cmd.setEssentialsModule(module);
} catch (Exception ex) {
sender.sendMessage(tl("commandNotLoaded", commandLabel));
sender.sendTl("commandNotLoaded", commandLabel);
LOGGER.log(Level.SEVERE, tl("commandNotLoaded", commandLabel), ex);
return true;
}

// Check authorization
if (user != null && !user.isAuthorized(cmd, permissionPrefix)) {
LOGGER.log(Level.INFO, tl("deniedAccessCommand", user.getName()));
user.sendMessage(tl("noAccessCommand"));
sender.sendTl("noAccessCommand");
return true;
}

if (user != null && user.isJailed() && !user.isAuthorized(cmd, "essentials.jail.allow.")) {
if (user.getJailTimeout() > 0) {
user.sendMessage(tl("playerJailedFor", user.getName(), DateUtil.formatDateDiff(user.getJailTimeout())));
sender.sendTl("playerJailedFor", user.getName(), DateUtil.formatDateDiff(sender, user.getJailTimeout()));
} else {
user.sendMessage(tl("jailMessage"));
sender.sendTl("jailMessage");
}
return true;
}
Expand Down Expand Up @@ -586,7 +597,7 @@ public void cleanupOpenInventories() {

@Override
public void showError(final CommandSource sender, final Throwable exception, final String commandLabel) {
sender.sendMessage(tl("errorWithMessage", exception.getMessage()));
sender.sendTl("errorWithMessage", exception.getMessage());
if (getSettings().isDebug()) {
LOGGER.log(Level.INFO, tl("errorCallingCommand", commandLabel), exception);
}
Expand Down Expand Up @@ -762,23 +773,62 @@ private int broadcastMessage(final IUser sender, final String permission, final

IText broadcast = new SimpleTextInput(message);

final Collection<Player> players = getOnlinePlayers();

for (Player player : players) {
sendBroadcast(sender != null ? sender.getSource() : null, permission, keywords, getUser(player), broadcast);
}

return players.size();
}

@Override
public int broadcastTl(final String string, final Object... objects) {
return broadcastTl(null, null, string, true, objects);
}

@Override
public int broadcastTl(final IUser sender, final String string, final Object... objects) {
return broadcastTl(sender.getSource(), null, string, false, objects);
}

@Override
public int broadcastTl(final CommandSource sender, final String string, final Object... objects) {
return broadcastTl(sender, null, string, false, objects);
}

@Override
public int broadcastTl(final String permission, final String string, final Object... objects) {
return broadcastTl(null, permission, string, false, objects);
}

private int broadcastTl(final CommandSource sender, final String permission, final String string, final boolean keywords, final Object... objects) {
if (sender != null && sender.getUser() != null && sender.getUser().isHidden()) {
return 0;
}


final Collection<Player> players = getOnlinePlayers();

for (Player player : players) {
final User user = getUser(player);
if ((permission == null && (sender == null || !user.isIgnoredPlayer(sender))) || (permission != null && user.isAuthorized(permission))) {
if (keywords) {
broadcast = new KeywordReplacer(broadcast, new CommandSource(player), this, false);
}
for (String messageText : broadcast.getLines()) {
user.sendMessage(messageText);
}
}
sendBroadcast(sender, permission, keywords, user, new SimpleTextInput(user.tl(string, objects)));
}

return players.size();
}

private void sendBroadcast(final CommandSource sender, final String permission, final boolean keywords, final User user, IText broadcast) {
if ((permission == null && (sender == null || sender.getUser() == null || !user.isIgnoredPlayer(sender.getUser()))) || (permission != null && user.isAuthorized(permission))) {
if (keywords) {
broadcast = new KeywordReplacer(broadcast, user.getSource(), this, false);
}
for (String messageText : broadcast.getLines()) {
user.sendMessage(messageText);
}
}
}

@Override
public BukkitTask runTaskAsynchronously(final Runnable run) {
return this.getScheduler().runTaskAsynchronously(this, run);
Expand Down Expand Up @@ -895,6 +945,11 @@ public PotionMetaProvider getPotionMetaProvider() {
return potionMetaProvider;
}

@Override
public PlayerLocaleProvider getPlayerLocaleProvider() {
return playerLocaleProvider;
}

private static void addDefaultBackPermissionsToWorld(World w) {
String permName = "essentials.back.into." + w.getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void onPlayerDeathEvent(final PlayerDeathEvent event) {
final User user = ess.getUser(event.getEntity());
if (user.isAuthorized("essentials.back.ondeath") && !ess.getSettings().isCommandDisabled("back")) {
user.setLastLocation();
user.sendMessage(tl("backAfterDeath"));
user.sendMessage(user.tl( "backAfterDeath"));
}
if (!ess.getSettings().areDeathMessagesEnabled()) {
event.setDeathMessage("");
Expand Down
Loading