2011-09-28 23:38:56 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-12-01 13:42:39 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2011-11-30 01:51:02 +00:00
|
|
|
import com.earth2me.essentials.craftbukkit.SetBed;
|
2011-11-18 03:18:03 +00:00
|
|
|
import com.earth2me.essentials.textreader.IText;
|
|
|
|
import com.earth2me.essentials.textreader.KeywordReplacer;
|
|
|
|
import com.earth2me.essentials.textreader.TextInput;
|
|
|
|
import com.earth2me.essentials.textreader.TextPager;
|
|
|
|
import java.io.IOException;
|
2011-09-28 23:38:56 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2011-11-21 01:55:26 +00:00
|
|
|
import java.util.Locale;
|
2011-09-28 23:38:56 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.entity.Player;
|
2011-11-30 01:51:02 +00:00
|
|
|
import org.bukkit.event.block.Action;
|
2011-09-28 23:38:56 +00:00
|
|
|
import org.bukkit.event.player.PlayerLoginEvent.Result;
|
2011-11-18 17:42:26 +00:00
|
|
|
import org.bukkit.event.player.*;
|
2011-12-06 22:56:38 +00:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
2011-09-28 23:38:56 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
|
|
|
public class EssentialsPlayerListener extends PlayerListener
|
|
|
|
{
|
|
|
|
private static final Logger LOGGER = Logger.getLogger("Minecraft");
|
|
|
|
private final transient Server server;
|
|
|
|
private final transient IEssentials ess;
|
|
|
|
|
|
|
|
public EssentialsPlayerListener(final IEssentials parent)
|
|
|
|
{
|
|
|
|
this.ess = parent;
|
|
|
|
this.server = parent.getServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerRespawn(final PlayerRespawnEvent event)
|
|
|
|
{
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
updateCompass(user);
|
|
|
|
if (ess.getSettings().changeDisplayName())
|
|
|
|
{
|
2011-11-20 15:43:38 +00:00
|
|
|
user.setDisplayNick();
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerChat(final PlayerChatEvent event)
|
|
|
|
{
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
if (user.isMuted())
|
|
|
|
{
|
|
|
|
event.setCancelled(true);
|
2011-11-21 01:55:26 +00:00
|
|
|
user.sendMessage(_("playerMuted"));
|
|
|
|
LOGGER.info(_("mutedUserSpeaks", user.getName()));
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
|
|
|
final Iterator<Player> it = event.getRecipients().iterator();
|
|
|
|
while (it.hasNext())
|
|
|
|
{
|
|
|
|
final User u = ess.getUser(it.next());
|
|
|
|
if (u.isIgnoredPlayer(user.getName()))
|
|
|
|
{
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.updateActivity(true);
|
|
|
|
if (ess.getSettings().changeDisplayName())
|
|
|
|
{
|
2011-11-20 15:43:38 +00:00
|
|
|
user.setDisplayNick();
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerMove(final PlayerMoveEvent event)
|
|
|
|
{
|
|
|
|
if (event.isCancelled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
|
|
|
|
if (user.isAfk() && ess.getSettings().getFreezeAfkPlayers())
|
|
|
|
{
|
|
|
|
final Location from = event.getFrom();
|
|
|
|
final Location to = event.getTo().clone();
|
|
|
|
to.setX(from.getX());
|
2011-10-09 21:45:46 +00:00
|
|
|
to.setY(from.getY());
|
2011-09-28 23:38:56 +00:00
|
|
|
to.setZ(from.getZ());
|
2011-10-09 21:45:46 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
event.setTo(Util.getSafeDestination(to));
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
event.setTo(to);
|
|
|
|
}
|
2011-09-28 23:38:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-04 19:58:47 +00:00
|
|
|
final Location afk = user.getAfkPosition();
|
2011-09-28 23:38:56 +00:00
|
|
|
if (afk == null || !event.getTo().getWorld().equals(afk.getWorld()) || afk.distanceSquared(event.getTo()) > 9)
|
|
|
|
{
|
|
|
|
user.updateActivity(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerQuit(final PlayerQuitEvent event)
|
|
|
|
{
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
if (ess.getSettings().removeGodOnDisconnect() && user.isGodModeEnabled())
|
|
|
|
{
|
|
|
|
user.toggleGodModeEnabled();
|
|
|
|
}
|
|
|
|
if (user.getSavedInventory() != null)
|
|
|
|
{
|
2011-11-30 18:31:21 +00:00
|
|
|
user.getInventory().setContents(user.getSavedInventory());
|
2011-09-28 23:38:56 +00:00
|
|
|
user.setSavedInventory(null);
|
|
|
|
}
|
|
|
|
user.updateActivity(false);
|
|
|
|
user.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerJoin(final PlayerJoinEvent event)
|
|
|
|
{
|
|
|
|
ess.getBackup().onPlayerJoin();
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
|
|
|
|
if (ess.getSettings().changeDisplayName())
|
|
|
|
{
|
2011-11-20 15:43:38 +00:00
|
|
|
user.setDisplayNick();
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
2011-11-04 23:13:21 +00:00
|
|
|
user.setLastLoginAddress(user.getAddress().getAddress().getHostAddress());
|
2011-09-28 23:38:56 +00:00
|
|
|
user.updateActivity(false);
|
|
|
|
if (user.isAuthorized("essentials.sleepingignored"))
|
|
|
|
{
|
|
|
|
user.setSleepingIgnored(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ess.getSettings().isCommandDisabled("motd") && user.isAuthorized("essentials.motd"))
|
|
|
|
{
|
2011-11-18 03:18:03 +00:00
|
|
|
try
|
2011-09-28 23:38:56 +00:00
|
|
|
{
|
2011-11-18 03:18:03 +00:00
|
|
|
final IText input = new TextInput(user, "motd", true, ess);
|
|
|
|
final IText output = new KeywordReplacer(input, user, ess);
|
2011-11-24 04:57:01 +00:00
|
|
|
final TextPager pager = new TextPager(output, true);
|
2011-11-28 19:52:15 +00:00
|
|
|
pager.showPage("1", null, "motd", user);
|
2011-11-18 03:18:03 +00:00
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
2011-11-27 08:07:15 +00:00
|
|
|
if (ess.getSettings().isDebug())
|
|
|
|
{
|
|
|
|
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGGER.log(Level.WARNING, ex.getMessage());
|
|
|
|
}
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ess.getSettings().isCommandDisabled("mail") && user.isAuthorized("essentials.mail"))
|
|
|
|
{
|
|
|
|
final List<String> mail = user.getMails();
|
|
|
|
if (mail.isEmpty())
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
user.sendMessage(_("noNewMail"));
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
user.sendMessage(_("youHaveNewMail", mail.size()));
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerLogin(final PlayerLoginEvent event)
|
|
|
|
{
|
|
|
|
if (event.getResult() != Result.ALLOWED && event.getResult() != Result.KICK_FULL && event.getResult() != Result.KICK_BANNED)
|
|
|
|
{
|
|
|
|
LOGGER.log(Level.INFO, "Disconnecting user " + event.getPlayer().toString() + " due to " + event.getResult().toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
User user = ess.getUser(event.getPlayer());
|
|
|
|
user.setNPC(false);
|
|
|
|
|
|
|
|
final long currentTime = System.currentTimeMillis();
|
2011-11-04 15:28:51 +00:00
|
|
|
final boolean banExpired = user.checkBanTimeout(currentTime);
|
2011-09-28 23:38:56 +00:00
|
|
|
user.checkMuteTimeout(currentTime);
|
|
|
|
user.checkJailTimeout(currentTime);
|
|
|
|
|
|
|
|
if (banExpired == false && (user.isBanned() || event.getResult() == Result.KICK_BANNED))
|
|
|
|
{
|
|
|
|
final String banReason = user.getBanReason();
|
2011-11-21 01:55:26 +00:00
|
|
|
event.disallow(Result.KICK_BANNED, banReason != null && !banReason.isEmpty() && !banReason.equalsIgnoreCase("ban") ? banReason : _("defaultBanReason"));
|
2011-09-28 23:38:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server.getOnlinePlayers().length >= server.getMaxPlayers() && !user.isAuthorized("essentials.joinfullserver"))
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
event.disallow(Result.KICK_FULL, _("serverFull"));
|
2011-09-28 23:38:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.allow();
|
|
|
|
|
|
|
|
user.setLastLogin(System.currentTimeMillis());
|
|
|
|
updateCompass(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateCompass(final User user)
|
|
|
|
{
|
2011-11-26 21:30:40 +00:00
|
|
|
Location loc = user.getHome(user.getLocation());
|
2011-11-27 08:07:15 +00:00
|
|
|
if (loc == null)
|
|
|
|
{
|
2011-11-27 05:00:58 +00:00
|
|
|
loc = user.getBedSpawnLocation();
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
2011-11-27 08:07:15 +00:00
|
|
|
if (loc != null)
|
|
|
|
{
|
2011-11-26 21:30:40 +00:00
|
|
|
user.setCompassTarget(loc);
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerTeleport(PlayerTeleportEvent event)
|
2011-12-06 22:56:38 +00:00
|
|
|
{
|
2011-09-28 23:38:56 +00:00
|
|
|
if (event.isCancelled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-06 22:56:38 +00:00
|
|
|
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
//There is TeleportCause.COMMMAND but plugins have to actively pass the cause in on their teleports.
|
2011-12-07 01:12:36 +00:00
|
|
|
if((event.getCause() == TeleportCause.PLUGIN || event.getCause() == TeleportCause.COMMAND)&& ess.getSettings().registerBackInListener())
|
2011-12-06 22:56:38 +00:00
|
|
|
{
|
|
|
|
user.setLastLocation();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:38:56 +00:00
|
|
|
if (ess.getSettings().changeDisplayName())
|
|
|
|
{
|
2011-11-20 15:43:38 +00:00
|
|
|
user.setDisplayNick();
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|
|
|
|
updateCompass(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerEggThrow(final PlayerEggThrowEvent event)
|
|
|
|
{
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
final ItemStack is = new ItemStack(Material.EGG, 1);
|
|
|
|
if (user.hasUnlimited(is))
|
|
|
|
{
|
|
|
|
user.getInventory().addItem(is);
|
|
|
|
user.updateInventory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerBucketEmpty(final PlayerBucketEmptyEvent event)
|
|
|
|
{
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
if (user.hasUnlimited(new ItemStack(event.getBucket())))
|
|
|
|
{
|
|
|
|
event.getItemStack().setType(event.getBucket());
|
|
|
|
ess.scheduleSyncDelayedTask(new Runnable()
|
|
|
|
{
|
2011-11-18 18:07:28 +00:00
|
|
|
@Override
|
2011-09-28 23:38:56 +00:00
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
user.updateInventory();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerAnimation(final PlayerAnimationEvent event)
|
|
|
|
{
|
2011-10-23 20:51:38 +00:00
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
user.updateActivity(true);
|
2011-09-28 23:38:56 +00:00
|
|
|
usePowertools(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void usePowertools(final PlayerAnimationEvent event)
|
|
|
|
{
|
|
|
|
if (event.getAnimationType() != PlayerAnimationType.ARM_SWING)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
|
|
|
final ItemStack is = user.getItemInHand();
|
|
|
|
if (is == null || is.getType() == Material.AIR || !user.arePowerToolsEnabled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final List<String> commandList = user.getPowertool(is);
|
|
|
|
if (commandList == null || commandList.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to loop through each command and execute
|
|
|
|
for (String command : commandList)
|
|
|
|
{
|
|
|
|
if (command.matches(".*\\{player\\}.*"))
|
|
|
|
{
|
|
|
|
//user.sendMessage("Click a player to use this command");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (command.startsWith("c:"))
|
|
|
|
{
|
|
|
|
for (Player p : server.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
p.sendMessage(user.getDisplayName() + ":" + command.substring(2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
user.getServer().dispatchCommand(event.getPlayer(), command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)
|
|
|
|
{
|
|
|
|
if (event.isCancelled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final User user = ess.getUser(event.getPlayer());
|
2011-11-21 01:55:26 +00:00
|
|
|
final String cmd = event.getMessage().toLowerCase(Locale.ENGLISH).split(" ")[0].replace("/", "").toLowerCase(Locale.ENGLISH);
|
2011-09-28 23:38:56 +00:00
|
|
|
final List<String> commands = Arrays.asList("msg", "r", "mail", "m", "t", "emsg", "tell", "er", "reply", "ereply", "email");
|
|
|
|
if (commands.contains(cmd))
|
|
|
|
{
|
|
|
|
for (Player player : ess.getServer().getOnlinePlayers())
|
|
|
|
{
|
|
|
|
User spyer = ess.getUser(player);
|
|
|
|
if (spyer.isSocialSpyEnabled() && !user.equals(spyer))
|
|
|
|
{
|
|
|
|
player.sendMessage(user.getDisplayName() + " : " + event.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!cmd.equalsIgnoreCase("afk"))
|
|
|
|
{
|
|
|
|
user.updateActivity(true);
|
|
|
|
}
|
|
|
|
}
|
2011-11-25 06:12:21 +00:00
|
|
|
|
|
|
|
@Override
|
2011-11-26 21:30:40 +00:00
|
|
|
public void onPlayerChangedWorld(final PlayerChangedWorldEvent event)
|
2011-11-25 06:12:21 +00:00
|
|
|
{
|
2011-11-27 08:07:15 +00:00
|
|
|
if (ess.getSettings().getNoGodWorlds().contains(event.getPlayer().getLocation().getWorld().getName()))
|
|
|
|
{
|
2011-11-25 06:12:21 +00:00
|
|
|
User user = ess.getUser(event.getPlayer());
|
2011-11-27 08:07:15 +00:00
|
|
|
if (user.isGodModeEnabledRaw())
|
|
|
|
{
|
2011-11-25 06:12:21 +00:00
|
|
|
user.sendMessage(_("noGodWorldWarning"));
|
|
|
|
}
|
|
|
|
}
|
2011-11-27 08:07:15 +00:00
|
|
|
}
|
2011-11-30 01:51:02 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerInteract(final PlayerInteractEvent event)
|
|
|
|
{
|
|
|
|
if (event.isCancelled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.getAction() != Action.RIGHT_CLICK_BLOCK)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ess.getSettings().getUpdateBedAtDaytime() && event.getClickedBlock().getType() == Material.BED_BLOCK)
|
|
|
|
{
|
|
|
|
SetBed.setBed(event.getPlayer(), event.getClickedBlock());
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 23:38:56 +00:00
|
|
|
}
|