Add basic event forwarding

Only supports 3 events for now:
- UseBlockEvent
- UseEntityEvent
- DamageEntityEvent
This commit is contained in:
Eric 2018-08-03 17:27:34 +02:00
parent ae7fcbb8aa
commit c9e5d0a65a
7 changed files with 329 additions and 1 deletions

View File

@ -1,5 +1,11 @@
package org.codemc.worldguardwrapper; package org.codemc.worldguardwrapper;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.codemc.worldguardwrapper.event.DamageEntityEvent;
import org.codemc.worldguardwrapper.event.UseBlockEvent;
import org.codemc.worldguardwrapper.event.UseEntityEvent;
import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation; import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation;
import lombok.experimental.Delegate; import lombok.experimental.Delegate;
@ -17,15 +23,30 @@ public class WorldGuardWrapper implements IWorldGuardImplementation {
@Delegate @Delegate
private IWorldGuardImplementation delegate; private IWorldGuardImplementation delegate;
private Listener eventListener;
private WorldGuardWrapper() { private WorldGuardWrapper() {
// TODO: better way to detect version // TODO: better way to detect version
try { try {
Class.forName("com.sk89q.worldguard.WorldGuard"); Class.forName("com.sk89q.worldguard.WorldGuard");
delegate = new org.codemc.worldguardwrapper.implementation.v7.WorldGuardImplementation(); delegate = new org.codemc.worldguardwrapper.implementation.v7.WorldGuardImplementation();
eventListener = new org.codemc.worldguardwrapper.implementation.v7.EventListener(
UseBlockEvent.class, UseEntityEvent.class, DamageEntityEvent.class);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
delegate = new org.codemc.worldguardwrapper.implementation.v6.WorldGuardImplementation(); delegate = new org.codemc.worldguardwrapper.implementation.v6.WorldGuardImplementation();
eventListener = new org.codemc.worldguardwrapper.implementation.v6.EventListener(
UseBlockEvent.class, UseEntityEvent.class, DamageEntityEvent.class);
} }
} }
/**
* Forward WorldGuard event calls to wrapped events to allow listening to them
* without having to use WorldGuard's events.
*
* @param plugin The plugin
*/
public void registerEvents(JavaPlugin plugin) {
Bukkit.getPluginManager().registerEvents(eventListener, plugin);
}
} }

View File

@ -0,0 +1,33 @@
package org.codemc.worldguardwrapper.event;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.codemc.worldguardwrapper.implementation.AbstractWrappedEvent;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class DamageEntityEvent extends AbstractWrappedEvent {
private static final HandlerList handlers = new HandlerList();
private final Event originalEvent;
private final Player player;
private final Location target;
private final Entity entity;
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,37 @@
package org.codemc.worldguardwrapper.event;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.codemc.worldguardwrapper.implementation.AbstractWrappedEvent;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class UseBlockEvent extends AbstractWrappedEvent {
private static final HandlerList handlers = new HandlerList();
private final Event originalEvent;
private final Player player;
private final World world;
private final List<Block> blocks;
private final Material effectiveMaterial;
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,33 @@
package org.codemc.worldguardwrapper.event;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.codemc.worldguardwrapper.implementation.AbstractWrappedEvent;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class UseEntityEvent extends AbstractWrappedEvent {
private static final HandlerList handlers = new HandlerList();
private final Event originalEvent;
private final Player player;
private final Location target;
private final Entity entity;
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,28 @@
package org.codemc.worldguardwrapper.implementation;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
public abstract class AbstractWrappedEvent extends Event implements Cancellable {
private Result result = Result.DEFAULT;
@Override
public boolean isCancelled() {
return result == Result.DENY;
}
@Override
public void setCancelled(boolean cancel) {
if (cancel) {
setResult(Result.DENY);
}
}
public void setResult(Result result) {
this.result = result;
}
public Result getResult() {
return result;
}
}

View File

@ -0,0 +1,88 @@
package org.codemc.worldguardwrapper.implementation.v6;
import java.util.List;
import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent;
import com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent;
import com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.codemc.worldguardwrapper.implementation.AbstractWrappedEvent;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class EventListener implements Listener {
private Class<? extends AbstractWrappedEvent> useBlockEventClass;
private Class<? extends AbstractWrappedEvent> useEntityEventClass;
private Class<? extends AbstractWrappedEvent> damageEntityEventClass;
@EventHandler
public void onUseBlock(UseBlockEvent e) {
Player player = e.getCause().getFirstPlayer();
if (player == null) {
// Only forward player events for now
return;
}
try {
AbstractWrappedEvent event = useBlockEventClass
.getConstructor(Event.class, Player.class, World.class, List.class, Material.class)
.newInstance(e.getOriginalEvent(), player, e.getWorld(), e.getBlocks(), e.getEffectiveMaterial());
Bukkit.getServer().getPluginManager().callEvent(event);
e.setResult(event.getResult());
} catch (ReflectiveOperationException ex) {
ex.printStackTrace(); // TODO: Handle differently
}
}
@EventHandler
public void onUseEntity(UseEntityEvent e) {
Player player = e.getCause().getFirstPlayer();
if (player == null) {
// Only forward player events for now
return;
}
try {
AbstractWrappedEvent event = useEntityEventClass
.getConstructor(Event.class, Player.class, Location.class, Entity.class)
.newInstance(e.getOriginalEvent(), player, e.getTarget(), e.getEntity());
Bukkit.getServer().getPluginManager().callEvent(event);
e.setResult(event.getResult());
} catch (ReflectiveOperationException ex) {
ex.printStackTrace(); // TODO: Handle differently
}
}
@EventHandler
public void onDamageEntity(DamageEntityEvent e) {
Player player = e.getCause().getFirstPlayer();
if (player == null) {
// Only forward player events for now
return;
}
try {
AbstractWrappedEvent event = damageEntityEventClass
.getConstructor(Event.class, Player.class, Location.class, Entity.class)
.newInstance(e.getOriginalEvent(), player, e.getTarget(), e.getEntity());
Bukkit.getServer().getPluginManager().callEvent(event);
e.setResult(event.getResult());
} catch (ReflectiveOperationException ex) {
ex.printStackTrace(); // TODO: Handle differently
}
}
}

View File

@ -0,0 +1,88 @@
package org.codemc.worldguardwrapper.implementation.v7;
import java.util.List;
import com.sk89q.worldguard.bukkit.event.block.UseBlockEvent;
import com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent;
import com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.codemc.worldguardwrapper.implementation.AbstractWrappedEvent;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class EventListener implements Listener {
private Class<? extends AbstractWrappedEvent> useBlockEventClass;
private Class<? extends AbstractWrappedEvent> useEntityEventClass;
private Class<? extends AbstractWrappedEvent> damageEntityEventClass;
@EventHandler
public void onUseBlock(UseBlockEvent e) {
Player player = e.getCause().getFirstPlayer();
if (player == null) {
// Only forward player events for now
return;
}
try {
AbstractWrappedEvent event = useBlockEventClass
.getConstructor(Event.class, Player.class, World.class, List.class, Material.class)
.newInstance(e.getOriginalEvent(), player, e.getWorld(), e.getBlocks(), e.getEffectiveMaterial());
Bukkit.getServer().getPluginManager().callEvent(event);
e.setResult(event.getResult());
} catch (ReflectiveOperationException ex) {
ex.printStackTrace(); // TODO: Handle differently
}
}
@EventHandler
public void onUseEntity(UseEntityEvent e) {
Player player = e.getCause().getFirstPlayer();
if (player == null) {
// Only forward player events for now
return;
}
try {
AbstractWrappedEvent event = useEntityEventClass
.getConstructor(Event.class, Player.class, Location.class, Entity.class)
.newInstance(e.getOriginalEvent(), player, e.getTarget(), e.getEntity());
Bukkit.getServer().getPluginManager().callEvent(event);
e.setResult(event.getResult());
} catch (ReflectiveOperationException ex) {
ex.printStackTrace(); // TODO: Handle differently
}
}
@EventHandler
public void onDamageEntity(DamageEntityEvent e) {
Player player = e.getCause().getFirstPlayer();
if (player == null) {
// Only forward player events for now
return;
}
try {
AbstractWrappedEvent event = damageEntityEventClass
.getConstructor(Event.class, Player.class, Location.class, Entity.class)
.newInstance(e.getOriginalEvent(), player, e.getTarget(), e.getEntity());
Bukkit.getServer().getPluginManager().callEvent(event);
e.setResult(event.getResult());
} catch (ReflectiveOperationException ex) {
ex.printStackTrace(); // TODO: Handle differently
}
}
}