Skip to content

Commit

Permalink
Release 1.4.
Browse files Browse the repository at this point in the history
Added fast event (non recommended).
  • Loading branch information
gamerforEA committed Nov 4, 2015
1 parent 25e3bcb commit 8290d5e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ apply plugin: 'forge'

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

version = "1.3"
version = "1.4"
group= "com.gamerforea"
archivesBaseName = "EventHelper"

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/gamerforea/eventhelper/EventHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@
import net.minecraftforge.common.config.Configuration;

@SideOnly(Side.SERVER)
@Mod(modid = "EventHelper", name = "EventHelper", version = "1.3", acceptableRemoteVersions = "*")
@Mod(modid = "EventHelper", name = "EventHelper", version = "1.4", acceptableRemoteVersions = "*")
public final class EventHelper
{
public static final File cfgDir = new File(Loader.instance().getConfigDir(), "Events");
public static final List<RegisteredListener> listeners = Lists.newArrayList();
public static boolean debug = false;
public static boolean fastEvents = false;

@EventHandler
public final void serverStarted(FMLServerStartedEvent event)
{
Configuration cfg = FastUtils.getConfig("EventHelper");
String[] plugins = cfg.getStringList("plugins", CATEGORY_GENERAL, new String[] { "WorldGuard" }, "Plugins for sending events");
boolean wgHooking = cfg.getBoolean("wgHooking", CATEGORY_GENERAL, true, "Hooking WorldGuard plugin (allow checking regions)");
debug = cfg.getBoolean("debug", CATEGORY_GENERAL, debug, "Debug enabled");
debug = cfg.getBoolean("debug", CATEGORY_GENERAL, debug, "Debugging enabled");
fastEvents = cfg.getBoolean("fastEvents", CATEGORY_GENERAL, fastEvents, "Fast events enabled (not recommended, work only on WorldGuard)");
cfg.save();

PluginManager plManager = Bukkit.getPluginManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.Bukkit;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;

public final class ConvertUtils
Expand All @@ -16,6 +17,11 @@ public static final org.bukkit.entity.Entity toBukkitEntity(Entity entity) throw
return (org.bukkit.entity.Entity) getBukkitEntity.invoke(entity);
}

public static final org.bukkit.entity.Player toBukkitEntity(EntityPlayer player) throws Exception
{
return (org.bukkit.entity.Player) getBukkitEntity.invoke(player);
}

public static final org.bukkit.World toBukkitWorld(World world)
{
return Bukkit.getWorld(world.getWorldInfo().getWorldName());
Expand Down
90 changes: 62 additions & 28 deletions src/main/java/com/gamerforea/eventhelper/util/EventUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import static com.gamerforea.eventhelper.util.ConvertUtils.toBukkitEntity;
import static com.gamerforea.eventhelper.util.ConvertUtils.toBukkitWorld;

import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;

import com.gamerforea.eventhelper.EventHelper;
import com.gamerforea.eventhelper.wg.WGFastEvents;
import com.gamerforea.eventhelper.wg.WGRegionChecker;

import net.minecraft.entity.Entity;
Expand All @@ -20,36 +20,70 @@ public final class EventUtils
{
public static final boolean cantBreak(EntityPlayer player, int x, int y, int z)
{
try
{
BlockBreakEvent event = new BlockBreakEvent(toBukkitWorld(player.worldObj).getBlockAt(x, y, z), (Player) toBukkitEntity(player));
EventHelper.callEvent(event);
return event.isCancelled();
}
catch (Throwable throwable)
{
System.err.println(String.format("Failed call BlockBreakEvent: [Player: %s, X:%d, Y:%d, Z:%d]", player.toString(), x, y, z));
if (EventHelper.debug)
throwable.printStackTrace();
return true;
}
if (!EventHelper.fastEvents)
try
{
BlockBreakEvent event = new BlockBreakEvent(toBukkitWorld(player.worldObj).getBlockAt(x, y, z), toBukkitEntity(player));
EventHelper.callEvent(event);
return event.isCancelled();
}
catch (Throwable throwable)
{
System.err.println(String.format("Failed call BlockBreakEvent: [Player: %s, X:%d, Y:%d, Z:%d]", player.toString(), x, y, z));
if (EventHelper.debug)
throwable.printStackTrace();
return true;
}
else
try
{
return !WGFastEvents.hasAccess(toBukkitEntity(player), x, y, z, false);
}
catch (Throwable throwable)
{
System.err.println(String.format("Failed use fast hasAccess(): [Player: %s, X:%d, Y:%d, Z:%d]", player.toString(), x, y, z));
if (EventHelper.debug)
throwable.printStackTrace();
return true;
}
}

public static final boolean cantDamage(Entity damager, Entity damagee)
{
try
{
EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(toBukkitEntity(damager), toBukkitEntity(damagee), DamageCause.ENTITY_ATTACK, 0D);
EventHelper.callEvent(event);
return event.isCancelled();
}
catch (Throwable throwable)
{
System.err.println(String.format("Failed call EntityDamageByEntityEvent [Damager: %s, Damagee: %s]", damager.toString(), damagee.toString()));
if (EventHelper.debug)
throwable.printStackTrace();
return true;
}
if (!EventHelper.fastEvents)
try
{
EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(toBukkitEntity(damager), toBukkitEntity(damagee), DamageCause.ENTITY_ATTACK, 0D);
EventHelper.callEvent(event);
return event.isCancelled();
}
catch (Throwable throwable)
{
System.err.println(String.format("Failed call EntityDamageByEntityEvent: [Damager: %s, Damagee: %s]", damager.toString(), damagee.toString()));
if (EventHelper.debug)
throwable.printStackTrace();
return true;
}
else
try
{
if (damager instanceof EntityPlayer)
{
int x = MathHelper.floor_double(damagee.posX);
int y = MathHelper.floor_double(damagee.posY);
int z = MathHelper.floor_double(damagee.posZ);
return !WGFastEvents.hasAccess(toBukkitEntity((EntityPlayer) damager), x, y, z, true);
}
else
return isInPrivate(damagee);
}
catch (Throwable throwable)
{
System.err.println(String.format("Failed use fast hasAccess(): [Damager: %s, Damagee: %s]", damager.toString(), damagee.toString()));
if (EventHelper.debug)
throwable.printStackTrace();
return true;
}
}

public static final boolean isInPrivate(World world, int x, int y, int z)
Expand All @@ -60,7 +94,7 @@ public static final boolean isInPrivate(World world, int x, int y, int z)
}
catch (Throwable throwable)
{
System.err.println(String.format("Failed check private [World: %s, X: %d, Y: %d, Z: %d]", world.getWorldInfo().getWorldName(), x, y, z));
System.err.println(String.format("Failed check private: [World: %s, X: %d, Y: %d, Z: %d]", world.getWorldInfo().getWorldName(), x, y, z));
if (EventHelper.debug)
throwable.printStackTrace();
return true;
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/gamerforea/eventhelper/wg/WGFastEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.gamerforea.eventhelper.wg;

import java.lang.reflect.Method;

import org.bukkit.entity.Player;

import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.bukkit.BukkitPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.StateFlag.State;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

public final class WGFastEvents
{
private static final Method hasAccess;

public static final boolean hasAccess(Player player, int x, int y, int z, boolean checkPvP) throws Throwable
{
return (Boolean) hasAccess.invoke(null, player, x, y, z);
}

static
{
try
{
Class<?> clazz = WGReflection.injectIntoWG(WGFastEvents.class);
hasAccess = clazz.getDeclaredMethod("hasAccessInj", Player.class, int.class, int.class, int.class, boolean.class);
hasAccess.setAccessible(true);
}
catch (Throwable throwable)
{
throw new RuntimeException("Failed injecting WGFastEvents$Inj.hasAccessInj() method!", throwable);
}
}

public static final class Inj
{
public static final boolean hasAccessInj(Player player, int x, int y, int z, boolean checkPvP)
{
BukkitPlayer bPlayer = new BukkitPlayer(WorldGuardPlugin.inst(), player);
for (ProtectedRegion region : WorldGuardPlugin.inst().getRegionManager(player.getWorld()).getApplicableRegions(new Vector(x, y, z)))
if (!region.getId().equals(ProtectedRegion.GLOBAL_REGION))
if (!region.isMember(bPlayer) && !region.isOwner(bPlayer))
return false;
else if (checkPvP && region.getFlag(DefaultFlag.PVP) == State.DENY)
return false;
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static final boolean isInPrivate(World world, int x, int y, int z) throws
}
catch (Throwable throwable)
{
throw new RuntimeException("Failed injecting WGRegionCheckerInj.isInPrivateInj() method!", throwable);
throw new RuntimeException("Failed injecting WGRegionChecker$Inj.isInPrivateInj() method!", throwable);
}
}

Expand Down

0 comments on commit 8290d5e

Please sign in to comment.