2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
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;
|
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-01-17 19:25:19 +00:00
|
|
|
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
|
2013-03-16 09:16:33 +00:00
|
|
|
import org.bukkit.event.entity.*;
|
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
|
|
|
{
|
2012-08-31 17:52:40 +00:00
|
|
|
private static final Logger LOGGER = Logger.getLogger("Minecraft");
|
2011-06-01 10:40:12 +00:00
|
|
|
private final IEssentials ess;
|
2013-02-11 23:33:30 +00:00
|
|
|
private static final transient Pattern powertoolPlayer = Pattern.compile("\\{player\\}");
|
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
|
|
|
{
|
2012-03-02 16:54:12 +00:00
|
|
|
final User attacker = ess.getUser(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
|
|
|
{
|
2012-12-30 03:31:05 +00:00
|
|
|
final ItemStack hand = attacker.getItemInHand();
|
2012-08-28 01:24:01 +00:00
|
|
|
if (hand != null && hand.getType() == Material.MILK_BUCKET)
|
|
|
|
{
|
|
|
|
((Ageable)eDefend).setBaby();
|
|
|
|
hand.setType(Material.BUCKET);
|
2012-12-30 03:31:05 +00:00
|
|
|
attacker.setItemInHand(hand);
|
|
|
|
attacker.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)
|
|
|
|
{
|
|
|
|
Entity shooter = ((Projectile)event.getDamager()).getShooter();
|
|
|
|
if (shooter instanceof Player)
|
|
|
|
{
|
|
|
|
final User attacker = ess.getUser(shooter);
|
|
|
|
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
|
|
|
{
|
|
|
|
final List<String> commandList = attacker.getPowertool(attacker.getItemInHand());
|
|
|
|
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
|
|
|
{
|
|
|
|
ess.scheduleSyncDelayedTask(
|
|
|
|
new Runnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2013-01-19 15:15:43 +00:00
|
|
|
attacker.getServer().dispatchCommand(attacker.getBase(), command);
|
2012-11-04 17:31:57 +00:00
|
|
|
LOGGER.log(Level.INFO, String.format("[PT] %s issued server command: /%s", attacker.getName(), command));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2011-08-27 18:39:54 +00:00
|
|
|
if (event.getEntity() instanceof Player && ess.getUser(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
|
|
|
{
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
if (event.getEntity() instanceof Player && ess.getUser(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();
|
|
|
|
user.sendMessage(_("backAfterDeath"));
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2011-09-22 08:47:39 +00:00
|
|
|
if (event.getEntity() instanceof Player && ess.getUser(event.getEntity()).isGodModeEnabled())
|
2011-10-09 18:59:06 +00:00
|
|
|
{
|
2013-02-05 23:33:22 +00:00
|
|
|
final Player player = (Player)event.getEntity();
|
|
|
|
player.setFoodLevel(20);
|
|
|
|
player.setSaturation(10);
|
2011-10-04 08:05:04 +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
|
|
|
|
&& ess.getUser(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
|
|
|
{
|
2012-09-28 19:55:37 +00:00
|
|
|
if (entity instanceof Player && ess.getUser(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
|
|
|
}
|
|
|
|
}
|
2012-09-28 19:55:37 +00:00
|
|
|
}
|