1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-29 09:19:51 +02:00

Events WIP

This commit is contained in:
snowleo
2012-07-29 12:33:35 +02:00
parent 8c4eb152a3
commit e6bda76b12
26 changed files with 466 additions and 49 deletions

View File

@@ -0,0 +1,22 @@
package net.ess3.api.ondemand;
import java.lang.ref.WeakReference;
public abstract class OnDemand<T>
{
protected WeakReference<T> reference = null;
public final T get()
{
T obj = reference == null ? null : reference.get();
if (obj == null)
{
obj = getNew();
reference = new WeakReference<T>(obj);
}
return obj;
}
protected abstract T getNew();
}

View File

@@ -0,0 +1,29 @@
package net.ess3.api.ondemand;
import net.ess3.api.IUser;
import net.ess3.api.server.Player;
import net.ess3.api.server.Server;
public class UserOnDemand extends OnDemand<IUser>
{
private final String name;
private final Server server;
public UserOnDemand(String name, Server server)
{
this.name = name;
this.server = server;
}
@Override
protected IUser getNew()
{
Player player = server.getPlayer(name);
if (player == null)
{
return null;
}
return player.getUser();
}
}

View File

@@ -0,0 +1,24 @@
package net.ess3.api.server;
import lombok.Delegate;
public class Block {
public Block(ItemStack stack, Location location)
{
this.stack = stack;
this.location = location;
}
@Delegate
private final ItemStack stack;
@Delegate
private final Location location;
public ItemStack convertToItem()
{
final ItemStack is = ItemStack.create(this.getType(), 1, this.getDurability());
return this.getType().convertToItem(is);
}
}

View File

@@ -26,12 +26,18 @@ public abstract class ItemStack implements Cloneable
}
public abstract Material getType();
public abstract void setType(Material mat);
public abstract void setType(int id);
public abstract int getAmount();
public abstract void setAmount(int value);
public abstract short getDurability();
public abstract short getDurability();
public abstract void setDurability(short value);
public abstract int getMaxStackSize();

View File

@@ -34,4 +34,6 @@ public abstract class Material {
public abstract int getMaxDurability();
protected abstract Material matchMaterial(String string);
public abstract ItemStack convertToItem(ItemStack is);
}

View File

@@ -58,4 +58,6 @@ public interface Player extends CommandSender
void setCompassTarget(Location loc);
void damage(int value);
boolean isInSurvivalMode();
}

View File

@@ -34,4 +34,6 @@ public interface Plugin
void callSuicideEvent(Player player);
Logger getLogger();
Server getServer();
}

View File

@@ -25,4 +25,6 @@ public interface Server
String getVersion();
public void unbanIP(String string);
public Player getPlayer(String name);
}

View File

@@ -0,0 +1,7 @@
package net.ess3.api.server.events;
public interface EventFactory
{
public void register(EventListener listener, EventType type, EventPriority priority, boolean ignoreCancelled);
}

View File

@@ -0,0 +1,21 @@
package net.ess3.api.server.events;
import net.ess3.api.IUser;
import net.ess3.api.ondemand.OnDemand;
import net.ess3.api.server.Block;
public abstract class EventListener
{
private static EventFactory eventFactory;
public final void register(EventType type, EventPriority priority, boolean ignoreCancelled)
{
eventFactory.register(this, type, priority, ignoreCancelled);
}
public boolean onBlockPlace(Block block, OnDemand<IUser> user)
{
return true;
}
}

View File

@@ -0,0 +1,12 @@
package net.ess3.api.server.events;
public enum EventPriority
{
LOWEST,
LOW,
NORMAL,
HIGH,
HIGHEST,
MONITOR
}

View File

@@ -0,0 +1,7 @@
package net.ess3.api.server.events;
public enum EventType
{
PLACE_BLOCK
}

View File

@@ -0,0 +1,13 @@
package net.ess3.bukkit;
import net.ess3.api.server.Block;
import net.ess3.api.server.ItemStack;
import net.ess3.api.server.Location;
public class BukkitBlockFactory {
public static Block convert(org.bukkit.block.Block block) {
Location loc = Location.create(block.getWorld().getName(), block.getX(), block.getY(), block.getZ());
return new Block(ItemStack.create(block.getTypeId(), 1, block.getData()), loc);
}
}

View File

@@ -53,6 +53,18 @@ public class BukkitItemStack extends net.ess3.api.server.ItemStack
{
return Material.get(itemStack.getTypeId());
}
@Override
public void setType(Material type)
{
itemStack.setTypeId(type.getId());
}
@Override
public void setType(int id)
{
itemStack.setTypeId(id);
}
@Override
public boolean isAir()

View File

@@ -1,8 +1,9 @@
package net.ess3.bukkit;
import net.ess3.api.server.Material;
import java.util.EnumMap;
import lombok.Delegate;
import net.ess3.api.server.ItemStack;
import net.ess3.api.server.Material;
public class BukkitMaterial extends Material
@@ -16,6 +17,7 @@ public class BukkitMaterial extends Material
materials.put(material, new BukkitMaterial(material));
}
}
private interface Excludes {
short getMaxDurability();
}
@@ -59,4 +61,86 @@ public class BukkitMaterial extends Material
{
return (short)this.material.getMaxDurability();
}
@Override
public ItemStack convertToItem(ItemStack is)
{
switch (org.bukkit.Material.getMaterial(is.getType().getId()))
{
case WOODEN_DOOR:
is.setType(org.bukkit.Material.WOOD_DOOR.getId());
is.setDurability((short)0);
break;
case IRON_DOOR_BLOCK:
is.setType(org.bukkit.Material.IRON_DOOR.getId());
is.setDurability((short)0);
break;
case SIGN_POST:
case WALL_SIGN:
is.setType(org.bukkit.Material.SIGN.getId());
is.setDurability((short)0);
break;
case CROPS:
is.setType(org.bukkit.Material.SEEDS.getId());
is.setDurability((short)0);
break;
case CAKE_BLOCK:
is.setType(org.bukkit.Material.CAKE.getId());
is.setDurability((short)0);
break;
case BED_BLOCK:
is.setType(org.bukkit.Material.BED.getId());
is.setDurability((short)0);
break;
case REDSTONE_WIRE:
is.setType(org.bukkit.Material.REDSTONE.getId());
is.setDurability((short)0);
break;
case REDSTONE_TORCH_OFF:
case REDSTONE_TORCH_ON:
is.setType(org.bukkit.Material.REDSTONE_TORCH_ON.getId());
is.setDurability((short)0);
break;
case DIODE_BLOCK_OFF:
case DIODE_BLOCK_ON:
is.setType(org.bukkit.Material.DIODE.getId());
is.setDurability((short)0);
break;
case DOUBLE_STEP:
is.setType(org.bukkit.Material.STEP.getId());
break;
case TORCH:
case RAILS:
case LADDER:
case WOOD_STAIRS:
case COBBLESTONE_STAIRS:
case LEVER:
case STONE_BUTTON:
case FURNACE:
case DISPENSER:
case PUMPKIN:
case JACK_O_LANTERN:
case WOOD_PLATE:
case STONE_PLATE:
case PISTON_STICKY_BASE:
case PISTON_BASE:
case IRON_FENCE:
case THIN_GLASS:
case TRAP_DOOR:
case FENCE:
case FENCE_GATE:
case NETHER_FENCE:
is.setDurability((short)0);
break;
case FIRE:
return null;
case PUMPKIN_STEM:
is.setType(org.bukkit.Material.PUMPKIN_SEEDS.getId());
break;
case MELON_STEM:
is.setType(org.bukkit.Material.MELON_SEEDS.getId());
break;
}
return is;
}
}

View File

@@ -7,8 +7,8 @@ import net.ess3.api.server.IInventory;
import net.ess3.api.server.Location;
import net.ess3.api.server.Player;
import net.ess3.api.server.World;
import org.bukkit.GameMode;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.LivingEntity;
public class BukkitPlayer extends BukkitCommandSender implements Player
@@ -33,7 +33,7 @@ public class BukkitPlayer extends BukkitCommandSender implements Player
}
@Delegate(types =
{
org.bukkit.entity.Player.class, LivingEntity.class
org.bukkit.entity.Player.class, org.bukkit.entity.LivingEntity.class
}, excludes =
{
OfflinePlayer.class, org.bukkit.command.CommandSender.class, Excludes.class
@@ -50,6 +50,7 @@ public class BukkitPlayer extends BukkitCommandSender implements Player
@Getter
private transient OfflinePlayer safePlayer;
private final transient BukkitServer server;
private transient IUser user = null;
public BukkitPlayer(final OfflinePlayer player, final BukkitServer server)
{
@@ -85,7 +86,7 @@ public class BukkitPlayer extends BukkitCommandSender implements Player
@Override
public IUser getUser()
{
throw new UnsupportedOperationException("Not supported yet.");
return user;
}
@Override
@@ -202,4 +203,11 @@ public class BukkitPlayer extends BukkitCommandSender implements Player
{
onlinePlayer.setCompassTarget(((BukkitLocation)loc).getBukkitLocation());
}
@Override
public boolean isInSurvivalMode()
{
return onlinePlayer.getGameMode() == GameMode.SURVIVAL;
}
}

View File

@@ -5,6 +5,8 @@ import net.ess3.api.server.Plugin;
import net.ess3.api.server.Location;
import java.io.File;
import lombok.Delegate;
import lombok.Getter;
import net.ess3.api.server.Server;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPluginLoader;
@@ -12,66 +14,73 @@ import org.bukkit.plugin.java.JavaPluginLoader;
public class BukkitPlugin implements Plugin
{
@Delegate
private final org.bukkit.plugin.Plugin plugin;
private interface Excludes {
public org.bukkit.Server getServer();
}
@Delegate(excludes={Excludes.class})
@Getter
private final org.bukkit.plugin.Plugin bukkitPlugin;
@Getter
private final Server server;
public BukkitPlugin(final org.bukkit.plugin.Plugin plugin)
public BukkitPlugin(final org.bukkit.plugin.Plugin plugin, final Server server)
{
this.plugin = plugin;
this.bukkitPlugin = plugin;
this.server = server;
}
@Override
public int scheduleAsyncDelayedTask(final Runnable run)
{
return plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, run);
return bukkitPlugin.getServer().getScheduler().scheduleAsyncDelayedTask(bukkitPlugin, run);
}
@Override
public int scheduleSyncDelayedTask(final Runnable run)
{
return plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, run);
return bukkitPlugin.getServer().getScheduler().scheduleSyncDelayedTask(bukkitPlugin, run);
}
@Override
public int scheduleSyncDelayedTask(final Runnable run, final long delay)
{
return plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, run, delay);
return bukkitPlugin.getServer().getScheduler().scheduleSyncDelayedTask(bukkitPlugin, run, delay);
}
@Override
public int scheduleSyncRepeatingTask(final Runnable run, final long delay, final long period)
{
return plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, run, delay, period);
return bukkitPlugin.getServer().getScheduler().scheduleSyncRepeatingTask(bukkitPlugin, run, delay, period);
}
@Override
public int scheduleAsyncRepeatingTask(final Runnable run, final long delay, final long period)
{
return plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, run, delay, period);
return bukkitPlugin.getServer().getScheduler().scheduleAsyncRepeatingTask(bukkitPlugin, run, delay, period);
}
@Override
public File getRootFolder()
{
return plugin.getDataFolder().getParentFile().getParentFile();
return bukkitPlugin.getDataFolder().getParentFile().getParentFile();
}
@Override
public void cancelTask(final int taskId)
{
plugin.getServer().getScheduler().cancelTask(taskId);
bukkitPlugin.getServer().getScheduler().cancelTask(taskId);
}
@Override
public String getVersion()
{
return plugin.getDescription().getVersion();
return bukkitPlugin.getDescription().getVersion();
}
@Override
public Class getClassByName(final String name)
{
final JavaPluginLoader jpl = (JavaPluginLoader)plugin.getPluginLoader();
final JavaPluginLoader jpl = (JavaPluginLoader)bukkitPlugin.getPluginLoader();
return jpl.getClassByName(name);
}
@@ -79,7 +88,7 @@ public class BukkitPlugin implements Plugin
public Location callRespawnEvent(Player player, Location loc, boolean bedSpawn)
{
final PlayerRespawnEvent pre = new PlayerRespawnEvent(((BukkitPlayer)player).getOnlinePlayer(), ((BukkitLocation)loc).getBukkitLocation() , bedSpawn);
getServer().getPluginManager().callEvent(pre);
getBukkitServer().getPluginManager().callEvent(pre);
return new BukkitLocation(pre.getRespawnLocation());
}
@@ -87,7 +96,11 @@ public class BukkitPlugin implements Plugin
public void callSuicideEvent(Player player)
{
EntityDamageEvent ede = new EntityDamageEvent(((BukkitPlayer)player).getOnlinePlayer(), EntityDamageEvent.DamageCause.SUICIDE, 1000);
getServer().getPluginManager().callEvent(ede);
getBukkitServer().getPluginManager().callEvent(ede);
}
public org.bukkit.Server getBukkitServer()
{
return bukkitPlugin.getServer();
}
}

View File

@@ -30,6 +30,8 @@ public class BukkitServer implements Server, Listener
org.bukkit.entity.Player[] getOnlinePlayers();
BukkitCommandSender getConsoleSender();
org.bukkit.entity.Player getPlayer(String name);
}
@Delegate(excludes = Excludes.class)
private final org.bukkit.Server server;
@@ -81,6 +83,7 @@ public class BukkitServer implements Server, Listener
{
final HashMap<String, Player> oplayersMap = new HashMap<String, Player>(onlinePlayersMap);
BukkitPlayer p = new BukkitPlayer(event.getPlayer(), this);
oplayersMap.put(event.getPlayer().getName(), p);
onlinePlayersMap = Collections.unmodifiableMap(oplayersMap);
onlinePlayers = Collections.unmodifiableCollection(oplayersMap.values());
@@ -121,6 +124,11 @@ public class BukkitServer implements Server, Listener
}
public Player getPlayer(final org.bukkit.entity.Player player) {
return onlinePlayersMap.get(player);
return onlinePlayersMap.get(player.getName());
}
@Override
public Player getPlayer(final String playerName) {
return onlinePlayersMap.get(playerName);
}
}

View File

@@ -1,11 +1,10 @@
package net.ess3.bukkit;
import java.util.List;
import net.ess3.api.server.World;
import net.ess3.api.server.ItemStack;
import net.ess3.api.server.Location;
import lombok.Delegate;
import lombok.Getter;
import net.ess3.api.server.ItemStack;
import net.ess3.api.server.Location;
import net.ess3.api.server.World;
import org.bukkit.TreeType;
public class BukkitWorld implements World {

View File

@@ -1,9 +1,9 @@
package net.ess3.bukkit;
import java.util.logging.Level;
import net.ess3.Essentials;
import static net.ess3.I18n._;
import net.ess3.api.server.*;
import java.util.logging.Level;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -33,7 +33,7 @@ public class EssentialsPlugin extends JavaPlugin
BukkitServer server = new BukkitServer(getServer());
final PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(server, this);
ess = new Essentials(server, getLogger(), new BukkitPlugin(this));
ess = new Essentials(server, getLogger(), new BukkitPlugin(this, server));
if (VersionCheck.checkVersion(this))
{
try

View File

@@ -0,0 +1,21 @@
package net.ess3.bukkit.events;
import net.ess3.api.server.events.EventListener;
import org.bukkit.event.Event;
import org.bukkit.event.EventException;
import org.bukkit.event.Listener;
import org.bukkit.plugin.EventExecutor;
public abstract class BaseEventExecutor implements EventExecutor
{
protected final EventListener listener;
public BaseEventExecutor(final EventListener listener)
{
this.listener = listener;
}
@Override
public abstract void execute(Listener ll, Event event) throws EventException;
}

View File

@@ -0,0 +1,25 @@
package net.ess3.bukkit.events;
import net.ess3.api.server.Plugin;
import net.ess3.api.server.events.EventFactory;
import net.ess3.api.server.events.EventListener;
import net.ess3.api.server.events.EventPriority;
import net.ess3.api.server.events.EventType;
public class BukkitEventFactory implements EventFactory
{
private Plugin plugin;
public BukkitEventFactory(Plugin plugin)
{
this.plugin = plugin;
}
@Override
public void register(EventListener listener, EventType type, EventPriority priority, boolean ignoreCancelled)
{
BukkitListener bukkitListener = new BukkitListener(plugin);
bukkitListener.register(listener, type, priority, ignoreCancelled);
}
}

View File

@@ -0,0 +1,70 @@
package net.ess3.bukkit.events;
import net.ess3.api.server.Plugin;
import net.ess3.api.server.events.EventListener;
import net.ess3.api.server.events.EventPriority;
import net.ess3.api.server.events.EventType;
import net.ess3.bukkit.BukkitPlugin;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.plugin.EventExecutor;
public class BukkitListener implements Listener
{
private Plugin plugin;
public BukkitListener(final Plugin plugin)
{
this.plugin = plugin;
}
public void register(EventListener listener, EventType type, EventPriority priority, boolean ignoreCancelled)
{
Class<? extends Event> event = getEventClass(type);
org.bukkit.event.EventPriority bukkitPriority = getEventPriority(priority);
EventExecutor executor = getEventExecutor(type, listener);
((BukkitPlugin)plugin).getBukkitServer().getPluginManager().registerEvent(event, this, bukkitPriority, executor, ((BukkitPlugin)plugin).getBukkitPlugin(), ignoreCancelled);
}
private static Class<? extends Event> getEventClass(final EventType type)
{
switch (type)
{
case PLACE_BLOCK:
return BlockPlaceEvent.class;
}
throw new RuntimeException("Missing Event Class");
}
private static org.bukkit.event.EventPriority getEventPriority(final EventPriority priority)
{
switch (priority)
{
case LOWEST:
return org.bukkit.event.EventPriority.LOWEST;
case LOW:
return org.bukkit.event.EventPriority.LOW;
case NORMAL:
return org.bukkit.event.EventPriority.NORMAL;
case HIGH:
return org.bukkit.event.EventPriority.HIGH;
case HIGHEST:
return org.bukkit.event.EventPriority.HIGHEST;
case MONITOR:
return org.bukkit.event.EventPriority.MONITOR;
}
throw new RuntimeException("Missing Event Priority");
}
private EventExecutor getEventExecutor(final EventType type, final EventListener listener)
{
switch (type)
{
case PLACE_BLOCK:
return new PlaceBlockExecutor(listener, plugin.getServer());
}
throw new RuntimeException("Missing Event Executor");
}
}

View File

@@ -0,0 +1,31 @@
package net.ess3.bukkit.events;
import net.ess3.api.ondemand.UserOnDemand;
import net.ess3.api.server.Block;
import net.ess3.api.server.Server;
import net.ess3.api.server.events.EventListener;
import net.ess3.bukkit.BukkitBlockFactory;
import org.bukkit.event.Event;
import org.bukkit.event.EventException;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class PlaceBlockExecutor extends BaseEventExecutor
{
private final Server server;
public PlaceBlockExecutor(EventListener listener, Server server)
{
super(listener);
this.server = server;
}
@Override
public void execute(Listener ll, Event event) throws EventException
{
org.bukkit.block.Block bukkitBlock = ((BlockPlaceEvent)event).getBlockPlaced();
Block block = BukkitBlockFactory.convert(bukkitBlock);
String playername = ((BlockPlaceEvent)event).getPlayer().getName();
listener.onBlockPlace(block, new UserOnDemand(playername, server));
}
}

View File

@@ -2,16 +2,15 @@ package net.ess3.listener;
import net.ess3.api.IEssentials;
import net.ess3.api.IUser;
import net.ess3.utils.Util;
import org.bukkit.GameMode;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import net.ess3.api.ondemand.OnDemand;
import net.ess3.api.server.Block;
import net.ess3.api.server.ItemStack;
import net.ess3.api.server.events.EventListener;
import net.ess3.api.server.events.EventPriority;
import net.ess3.api.server.events.EventType;
public class EssentialsBlockListener implements Listener
public class EssentialsBlockListener extends EventListener
{
private final transient IEssentials ess;
@@ -19,21 +18,20 @@ public class EssentialsBlockListener implements Listener
{
super();
this.ess = ess;
register(EventType.PLACE_BLOCK, EventPriority.LOW, true);
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onBlockPlace(final BlockPlaceEvent event)
@Override
public boolean onBlockPlace(final Block placedBlock, final OnDemand<IUser> user)
{
// Do not rely on getItemInHand();
// http://leaky.bukkit.org/issues/663
final ItemStack itemstack = Util.convertBlockToItem(event.getBlockPlaced());
if (itemstack == null)
final ItemStack itemstack = placedBlock.convertToItem();
if (placedBlock == null)
{
return;
return true;
}
final IUser user = ess.getUserMap().getUser(event.getPlayer());
final boolean unlimitedForUser = user.getData().hasUnlimited(itemstack.getType());
if (unlimitedForUser && user.getGameMode() == GameMode.SURVIVAL)
final boolean unlimitedForUser = user.get().getData().hasUnlimited(itemstack.getType());
if (unlimitedForUser && user.get().isInSurvivalMode())
{
ess.getPlugin().scheduleSyncDelayedTask(
new Runnable()
@@ -41,10 +39,11 @@ public class EssentialsBlockListener implements Listener
@Override
public void run()
{
user.getInventory().addItem(itemstack);
user.updateInventory();
user.get().getInventory().addItem(itemstack);
user.get().updateInventory();
}
});
}
return true;
}
}

View File

@@ -750,6 +750,4 @@ public class User extends UserBase implements IUser
final boolean set = !vanished;
this.setVanished(set);
}
}