2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2014-03-20 15:54:07 +00:00
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
2011-08-21 18:02:01 +00:00
|
|
|
import java.util.List;
|
2012-08-31 17:52:40 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2013-02-11 23:33:30 +00:00
|
|
|
import java.util.regex.Pattern;
|
2013-10-11 02:44:41 +00:00
|
|
|
import net.ess3.api.IEssentials;
|
2011-11-25 04:49:57 +00:00
|
|
|
import org.bukkit.Material;
|
2012-11-04 17:31:57 +00:00
|
|
|
import org.bukkit.entity.*;
|
2012-01-20 04:20:37 +00:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
2013-03-16 09:16:33 +00:00
|
|
|
import org.bukkit.event.entity.*;
|
2013-04-16 19:58:13 +00:00
|
|
|
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
|
2011-04-04 01:26:45 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2011-03-19 22:39:51 +00:00
|
|
|
|
|
|
|
|
2012-01-20 04:20:37 +00:00
|
|
|
public class EssentialsEntityListener implements Listener
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2013-12-07 20:03:05 +00:00
|
|
|
private static final Logger LOGGER = Logger.getLogger("Essentials");
|
2013-02-11 23:33:30 +00:00
|
|
|
private static final transient Pattern powertoolPlayer = Pattern.compile("\\{player\\}");
|
2013-11-07 02:22:32 +00:00
|
|
|
private final IEssentials ess;
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2011-06-01 10:40:12 +00:00
|
|
|
public EssentialsEntityListener(IEssentials ess)
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2011-06-01 10:40:12 +00:00
|
|
|
this.ess = ess;
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
|
2012-10-06 02:31:34 +00:00
|
|
|
// This method does something undocumented reguarding certain bucket types #EasterEgg
|
2012-06-10 17:33:07 +00:00
|
|
|
@EventHandler(priority = EventPriority.LOW)
|
2012-03-02 16:54:12 +00:00
|
|
|
public void onEntityDamage(final EntityDamageByEntityEvent event)
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2012-03-02 16:54:12 +00:00
|
|
|
final Entity eAttack = event.getDamager();
|
|
|
|
final Entity eDefend = event.getEntity();
|
2012-12-30 03:31:05 +00:00
|
|
|
if (eAttack instanceof Player)
|
2011-04-04 01:26:45 +00:00
|
|
|
{
|
2013-10-16 19:59:39 +00:00
|
|
|
final User attacker = ess.getUser((Player)eAttack);
|
2012-12-30 03:31:05 +00:00
|
|
|
if (eDefend instanceof Player)
|
2011-04-04 01:26:45 +00:00
|
|
|
{
|
2012-12-30 03:31:05 +00:00
|
|
|
onPlayerVsPlayerDamage(event, (Player)eDefend, attacker);
|
2011-04-04 01:26:45 +00:00
|
|
|
}
|
2012-12-30 03:31:05 +00:00
|
|
|
else if (eDefend instanceof Ageable)
|
2011-11-25 04:49:57 +00:00
|
|
|
{
|
2014-04-13 00:01:49 +00:00
|
|
|
final ItemStack hand = attacker.getBase().getItemInHand();
|
2012-08-28 01:24:01 +00:00
|
|
|
if (hand != null && hand.getType() == Material.MILK_BUCKET)
|
|
|
|
{
|
|
|
|
((Ageable)eDefend).setBaby();
|
|
|
|
hand.setType(Material.BUCKET);
|
2014-04-13 00:01:49 +00:00
|
|
|
attacker.getBase().setItemInHand(hand);
|
|
|
|
attacker.getBase().updateInventory();
|
2012-08-28 01:24:01 +00:00
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
2011-11-25 04:49:57 +00:00
|
|
|
}
|
2012-12-30 03:31:05 +00:00
|
|
|
attacker.updateActivity(true);
|
|
|
|
}
|
|
|
|
else if (eAttack instanceof Projectile && eDefend instanceof Player)
|
2014-05-18 20:32:07 +00:00
|
|
|
{
|
2014-03-19 00:01:14 +00:00
|
|
|
final Projectile projectile = (Projectile)event.getDamager();
|
2014-02-19 00:58:20 +00:00
|
|
|
//This should return a ProjectileSource on 1.7.3 beta +
|
2014-03-19 00:01:14 +00:00
|
|
|
final Object shooter = projectile.getShooter();
|
2012-12-30 03:31:05 +00:00
|
|
|
if (shooter instanceof Player)
|
|
|
|
{
|
2013-10-16 19:59:39 +00:00
|
|
|
final User attacker = ess.getUser((Player)shooter);
|
2012-12-30 03:31:05 +00:00
|
|
|
onPlayerVsPlayerDamage(event, (Player)eDefend, attacker);
|
|
|
|
attacker.updateActivity(true);
|
|
|
|
}
|
2011-04-04 01:26:45 +00:00
|
|
|
}
|
2012-03-02 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
2012-12-30 03:31:05 +00:00
|
|
|
private void onPlayerVsPlayerDamage(final EntityDamageByEntityEvent event, final Player defender, final User attacker)
|
2012-11-04 17:31:57 +00:00
|
|
|
{
|
2013-02-11 23:33:30 +00:00
|
|
|
if (ess.getSettings().getLoginAttackDelay() > 0
|
|
|
|
&& (System.currentTimeMillis() < (attacker.getLastLogin() + ess.getSettings().getLoginAttackDelay()))
|
|
|
|
&& !attacker.isAuthorized("essentials.pvpdelay.exempt"))
|
2012-11-04 17:31:57 +00:00
|
|
|
{
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
2012-12-30 03:31:05 +00:00
|
|
|
if (!defender.equals(attacker.getBase()) && (attacker.hasInvulnerabilityAfterTeleport() || ess.getUser(defender).hasInvulnerabilityAfterTeleport()))
|
2012-11-04 17:31:57 +00:00
|
|
|
{
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attacker.isGodModeEnabled() && !attacker.isAuthorized("essentials.god.pvp"))
|
|
|
|
{
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attacker.isHidden() && !attacker.isAuthorized("essentials.vanish.pvp"))
|
|
|
|
{
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
2012-12-30 03:31:05 +00:00
|
|
|
onPlayerVsPlayerPowertool(event, defender, attacker);
|
2012-11-04 17:31:57 +00:00
|
|
|
}
|
|
|
|
|
2012-12-30 03:31:05 +00:00
|
|
|
private void onPlayerVsPlayerPowertool(final EntityDamageByEntityEvent event, final Player defender, final User attacker)
|
2012-11-04 17:31:57 +00:00
|
|
|
{
|
2014-04-13 00:01:49 +00:00
|
|
|
final List<String> commandList = attacker.getPowertool(attacker.getBase().getItemInHand());
|
2012-11-04 17:31:57 +00:00
|
|
|
if (commandList != null && !commandList.isEmpty())
|
|
|
|
{
|
2013-01-19 15:15:43 +00:00
|
|
|
for (final String tempCommand : commandList)
|
2012-11-04 17:31:57 +00:00
|
|
|
{
|
2013-02-11 23:33:30 +00:00
|
|
|
final String command = powertoolPlayer.matcher(tempCommand).replaceAll(defender.getName());
|
2013-01-19 15:15:43 +00:00
|
|
|
if (command != null && !command.isEmpty() && !command.equals(tempCommand))
|
2012-11-04 17:31:57 +00:00
|
|
|
{
|
2014-05-18 20:32:07 +00:00
|
|
|
|
|
|
|
class PowerToolInteractTask implements Runnable
|
2013-10-11 02:44:41 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
attacker.getServer().dispatchCommand(attacker.getBase(), command);
|
|
|
|
LOGGER.log(Level.INFO, String.format("[PT] %s issued server command: /%s", attacker.getName(), command));
|
|
|
|
}
|
2014-05-18 20:32:07 +00:00
|
|
|
}
|
|
|
|
ess.scheduleSyncDelayedTask(new PowerToolInteractTask());
|
2012-11-04 17:31:57 +00:00
|
|
|
|
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 17:33:07 +00:00
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2012-03-02 16:54:12 +00:00
|
|
|
public void onEntityDamage(final EntityDamageEvent event)
|
|
|
|
{
|
2013-10-16 19:59:39 +00:00
|
|
|
if (event.getEntity() instanceof Player && ess.getUser((Player)event.getEntity()).isGodModeEnabled())
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2011-08-27 18:39:54 +00:00
|
|
|
final Player player = (Player)event.getEntity();
|
|
|
|
player.setFireTicks(0);
|
|
|
|
player.setRemainingAir(player.getMaximumAir());
|
|
|
|
event.setCancelled(true);
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 17:33:07 +00:00
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2012-03-02 16:54:12 +00:00
|
|
|
public void onEntityCombust(final EntityCombustEvent event)
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2013-10-16 19:59:39 +00:00
|
|
|
if (event.getEntity() instanceof Player && ess.getUser((Player)event.getEntity()).isGodModeEnabled())
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-20 04:20:37 +00:00
|
|
|
@EventHandler(priority = EventPriority.LOWEST)
|
2012-03-02 16:54:12 +00:00
|
|
|
public void onPlayerDeathEvent(final PlayerDeathEvent event)
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2012-03-02 16:54:12 +00:00
|
|
|
final User user = ess.getUser(event.getEntity());
|
|
|
|
if (user.isAuthorized("essentials.back.ondeath") && !ess.getSettings().isCommandDisabled("back"))
|
2011-03-19 22:39:51 +00:00
|
|
|
{
|
2012-03-02 16:54:12 +00:00
|
|
|
user.setLastLocation();
|
2014-03-20 15:54:07 +00:00
|
|
|
user.sendMessage(tl("backAfterDeath"));
|
2012-03-02 16:54:12 +00:00
|
|
|
}
|
|
|
|
if (!ess.getSettings().areDeathMessagesEnabled())
|
|
|
|
{
|
|
|
|
event.setDeathMessage("");
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-25 20:40:22 +00:00
|
|
|
|
2012-07-29 16:15:44 +00:00
|
|
|
@EventHandler(priority = EventPriority.LOW)
|
|
|
|
public void onPlayerDeathExpEvent(final PlayerDeathEvent event)
|
|
|
|
{
|
|
|
|
final User user = ess.getUser(event.getEntity());
|
|
|
|
if (user.isAuthorized("essentials.keepxp"))
|
|
|
|
{
|
|
|
|
event.setKeepLevel(true);
|
|
|
|
event.setDroppedExp(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 17:33:07 +00:00
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2012-03-02 16:54:12 +00:00
|
|
|
public void onFoodLevelChange(final FoodLevelChangeEvent event)
|
2011-10-09 18:59:06 +00:00
|
|
|
{
|
2013-05-09 16:44:10 +00:00
|
|
|
if (event.getEntity() instanceof Player)
|
2011-10-09 18:59:06 +00:00
|
|
|
{
|
2013-10-16 19:59:39 +00:00
|
|
|
final User user = ess.getUser((Player)event.getEntity());
|
2013-05-09 16:44:10 +00:00
|
|
|
if (user.isGodModeEnabled())
|
|
|
|
{
|
|
|
|
if (user.isGodModeEnabledRaw())
|
|
|
|
{
|
2014-04-13 00:01:49 +00:00
|
|
|
user.getBase().setFoodLevel(20);
|
|
|
|
user.getBase().setSaturation(10);
|
2013-05-09 16:44:10 +00:00
|
|
|
}
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
2011-09-22 08:47:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-24 14:37:44 +00:00
|
|
|
|
2012-06-10 17:33:07 +00:00
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2012-03-02 16:54:12 +00:00
|
|
|
public void onEntityRegainHealth(final EntityRegainHealthEvent event)
|
2011-10-24 14:37:44 +00:00
|
|
|
{
|
2011-10-25 15:21:56 +00:00
|
|
|
if (event.getRegainReason() == RegainReason.SATIATED && event.getEntity() instanceof Player
|
2013-10-16 19:59:39 +00:00
|
|
|
&& ess.getUser((Player)event.getEntity()).isAfk() && ess.getSettings().getFreezeAfkPlayers())
|
2011-10-24 14:37:44 +00:00
|
|
|
{
|
2011-10-25 15:21:56 +00:00
|
|
|
event.setCancelled(true);
|
2011-10-24 14:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-28 19:35:13 +00:00
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
|
|
|
public void onPotionSplashEvent(final PotionSplashEvent event)
|
|
|
|
{
|
2012-09-28 19:45:56 +00:00
|
|
|
for (LivingEntity entity : event.getAffectedEntities())
|
2012-09-28 19:35:13 +00:00
|
|
|
{
|
2013-10-16 19:59:39 +00:00
|
|
|
if (entity instanceof Player && ess.getUser((Player)entity).isGodModeEnabled())
|
2012-09-28 19:45:56 +00:00
|
|
|
{
|
2012-09-28 19:55:37 +00:00
|
|
|
event.setIntensity(entity, 0d);
|
2012-09-28 19:45:56 +00:00
|
|
|
}
|
2012-09-28 19:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-09 16:44:10 +00:00
|
|
|
|
2013-04-16 19:58:13 +00:00
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2013-05-09 16:44:10 +00:00
|
|
|
public void onEntityShootBow(EntityShootBowEvent event)
|
2013-04-16 19:58:13 +00:00
|
|
|
{
|
2013-04-16 20:16:07 +00:00
|
|
|
if (event.getEntity() instanceof Player)
|
2013-04-16 19:58:13 +00:00
|
|
|
{
|
2013-10-16 19:59:39 +00:00
|
|
|
final User user = ess.getUser((Player)event.getEntity());
|
2013-04-16 20:16:07 +00:00
|
|
|
if (user.isAfk())
|
|
|
|
{
|
|
|
|
user.updateActivity(true);
|
|
|
|
}
|
2013-04-16 19:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-28 19:55:37 +00:00
|
|
|
}
|