Skip to content

Instantly share code, notes, and snippets.

@Xyene
Created January 30, 2013 18:44
Show Gist options
  • Save Xyene/4675610 to your computer and use it in GitHub Desktop.
Save Xyene/4675610 to your computer and use it in GitHub Desktop.
Class for (Canary -> Bukkit) event conversion.
package org.surgedev.legacy;
import net.minecraft.server.Entity;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.craftbukkit.entity.CraftItem;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.*;
import org.surgedev.legacy.canary.*;
import org.surgedev.legacy.canary.Block;
import org.surgedev.legacy.canary.Player;
import static org.surgedev.legacy.Canary.*;
import static org.surgedev.legacy.Canary.asBukkitCopy;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class CEW {
public static HookParametersLogincheck loginCheck(HookParametersLogincheck loginHook) {
try {
AsyncPlayerPreLoginEvent fire = new AsyncPlayerPreLoginEvent(loginHook.getPlayerName(), InetAddress.getByName(loginHook.getIp()));
fire.setKickMessage(loginHook.getKickReason());
throwEvent(fire);
loginHook.setKickReason(fire.getKickMessage());
} catch (UnknownHostException e) {
//Should never happen
throw new IllegalArgumentException(e);
}
return loginHook;
}
public static boolean generic(PluginLoader.Hook h, Object[] parameters) {
return ((GenericCanaryEvent) throwEvent(new GenericCanaryEvent(h, parameters))).isCancelled();
}
public static void playerMove(Player who, Location from, Location to) {
throwEvent(new PlayerMoveEvent(asBukkitCopy(who), asBukkitCopy(from), Canary.asBukkitCopy(to)));
}
public static boolean blockDestroyed(Player player, Block blockDestroyed) {
return ((BlockBreakEvent) throwEvent(new BlockBreakEvent(asBukkitCopy(blockDestroyed), asBukkitCopy(player)))).isCancelled();
}
public static boolean blockCreate(Player player, Block blockPlaced) {
return ((BlockPlaceEvent) throwEvent(new BlockBreakEvent(asBukkitCopy(blockPlaced), asBukkitCopy(player)))).isCancelled();
}
public static HookParametersChat chat(HookParametersChat hookParametersChat) {
Set<org.bukkit.entity.Player> recieversSet = new HashSet<org.bukkit.entity.Player>();
for (Player p : hookParametersChat.getReceivers())
recieversSet.add(asBukkitCopy(p));
AsyncPlayerChatEvent chat = new AsyncPlayerChatEvent(false, asBukkitCopy(hookParametersChat.getPlayer()), hookParametersChat.getMessage().toString(), recieversSet);
throwEvent(chat);
hookParametersChat.setCanceled(chat.isCancelled());
hookParametersChat.setMessage(new StringBuilder().append(chat.getMessage()));
List<Player> recieversList = new ArrayList<Player>();
for (org.bukkit.entity.Player p : chat.getRecipients())
recieversList.add(asCanaryCopy(p));
hookParametersChat.setReceivers(recieversList);
return hookParametersChat;
}
public static void disconnect(Player dis) {
throwEvent(new PlayerQuitEvent(asBukkitCopy(dis), "disconnected via Canary event!"));
}
public static void login(Player log) {
throwEvent(new PlayerLoginEvent(asBukkitCopy(log), "disconnected via Canary event!"));
}
public static void armSwing(Player who) {
throwEvent(new PlayerAnimationEvent(asBukkitCopy(who)));
}
public static boolean itemPickup(Player who, ItemEntity what) {
return ((PlayerPickupItemEvent) throwEvent(new PlayerPickupItemEvent(asBukkitCopy(who), new CraftItem((CraftServer) Bukkit.getServer(), what.getEntity()), 0))).isCancelled();
}
public static boolean itemDrop(Player who, ItemEntity what) {
return ((PlayerDropItemEvent) throwEvent(new PlayerDropItemEvent(asBukkitCopy(who), new CraftItem((CraftServer) Bukkit.getServer(), what.getEntity())))).isCancelled();
}
public static boolean teleport(Player who, Location from, Location to) {
return ((PlayerTeleportEvent) throwEvent(new PlayerTeleportEvent(asBukkitCopy(who), asBukkitCopy(from), asBukkitCopy(to)))).isCancelled();
}
public static boolean blockBreak(Player who, Block what) {
return ((BlockBreakEvent) throwEvent(new BlockBreakEvent(asBukkitCopy(what), asBukkitCopy(who)))).isCancelled();
}
public static boolean flow(Block from, Block to) {
org.bukkit.block.Block newBlock = asBukkitCopy(to);
return ((BlockSpreadEvent) throwEvent(new BlockSpreadEvent(asBukkitCopy(from), newBlock, newBlock.getState()))).isCancelled();
}
public static boolean ignite(Block what, Player who) {
//Canary does not give an ignite cause, so we can only approximate
return ((BlockIgniteEvent) throwEvent(new BlockIgniteEvent(asBukkitCopy(what),
who != null ? BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL : BlockIgniteEvent.IgniteCause.SPREAD, asBukkitCopy(who)))).isCancelled();
}
public static boolean explode(Block where, Entity who, HashSet<Block> what) {
List<org.bukkit.block.Block> destroyed = new ArrayList<org.bukkit.block.Block>();
for (Block b : what)
destroyed.add(asBukkitCopy(b));
//Yield is not passed, 1 is a safe estimate
return ((EntityExplodeEvent) throwEvent(new EntityExplodeEvent(who.getBukkitEntity(), asBukkitCopy(where.getLocation()), destroyed, 1))).isCancelled();
}
public static Event throwEvent(Event e) {
Bukkit.getPluginManager().callEvent(e);
return e; //For ease of use
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment