From 26c5b4d0c7003717828e6a6d01390cac5c0dbd1d Mon Sep 17 00:00:00 2001 From: gamerforEA Date: Tue, 28 Feb 2017 19:41:14 +0400 Subject: [PATCH] Release 1.7. Add support for real players. --- build.gradle | 2 +- .../gamerforea/eventhelper/EventHelper.java | 2 +- .../eventhelper/fake/FakePlayerContainer.java | 72 ++++++++++++++++++- .../fake/FakePlayerContainerEntity.java | 2 +- .../fake/FakePlayerContainerTileEntity.java | 2 +- .../fake/FakePlayerContainerWorld.java | 2 +- .../eventhelper/util/ConvertUtils.java | 2 +- .../eventhelper/util/CraftUtils.java | 2 +- .../eventhelper/util/EventUtils.java | 12 +++- .../eventhelper/util/FastUtils.java | 2 +- .../eventhelper/wg/WGReflection.java | 2 +- .../eventhelper/wg/WGRegionChecker.java | 2 +- 12 files changed, 91 insertions(+), 13 deletions(-) diff --git a/build.gradle b/build.gradle index 1ebba65..81d2af1 100644 --- a/build.gradle +++ b/build.gradle @@ -25,7 +25,7 @@ apply plugin: 'forge' [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' -version = "1.6" +version = "1.7" group= "com.gamerforea" archivesBaseName = "EventHelper" diff --git a/src/main/java/com/gamerforea/eventhelper/EventHelper.java b/src/main/java/com/gamerforea/eventhelper/EventHelper.java index 4f16abe..cb54ba9 100644 --- a/src/main/java/com/gamerforea/eventhelper/EventHelper.java +++ b/src/main/java/com/gamerforea/eventhelper/EventHelper.java @@ -62,4 +62,4 @@ public static final void callEvent(Event event) throwable.printStackTrace(); } } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainer.java b/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainer.java index 84b8aeb..45cc0b4 100644 --- a/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainer.java +++ b/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainer.java @@ -1,10 +1,15 @@ package com.gamerforea.eventhelper.fake; +import java.lang.ref.WeakReference; import java.util.UUID; +import com.gamerforea.eventhelper.util.EventUtils; import com.gamerforea.eventhelper.util.FastUtils; import com.mojang.authlib.GameProfile; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.common.util.FakePlayer; @@ -17,6 +22,8 @@ public abstract class FakePlayerContainer public GameProfile profile; private FakePlayer player; + private WeakReference realPlayer; + protected FakePlayerContainer(FakePlayer modFake) { this.modFakeProfile = modFake.getGameProfile(); @@ -30,6 +37,20 @@ protected FakePlayerContainer(GameProfile modFakeProfile) public abstract World getWorld(); + public final EntityPlayer get() + { + if (this.realPlayer != null) + { + EntityPlayer p = this.realPlayer.get(); + if (p == null || p instanceof EntityPlayerMP && ((EntityPlayerMP) p).playerNetServerHandler == null) + this.realPlayer = null; + else + return p; + } + + return this.getPlayer(); + } + public final FakePlayer getPlayer() { if (this.player != null) @@ -42,6 +63,55 @@ else if (this.modFake != null) return this.modFake = FastUtils.getFake(this.getWorld(), this.modFakeProfile); } + public final void setRealPlayer(EntityPlayer player) + { + this.reset(); + if (player != null) + { + this.profile = player.getGameProfile(); + this.realPlayer = new WeakReference(player); + } + } + + public final void setParent(FakePlayerContainer container) + { + this.reset(); + if (container.profile != null) + { + this.profile = container.profile; + this.player = container.player; + this.realPlayer = container.realPlayer; + } + } + + public final GameProfile getProfile() + { + return this.profile; + } + + public final void setProfile(GameProfile profile) + { + this.reset(); + this.profile = profile; + } + + public final boolean cantBreak(int x, int y, int z) + { + return EventUtils.cantBreak(this.get(), x, y, z); + } + + public final boolean cantDamage(Entity target) + { + return EventUtils.cantDamage(this.get(), target); + } + + private final void reset() + { + this.profile = null; + this.player = null; + this.realPlayer = null; + } + public final void writeToNBT(NBTTagCompound nbt) { if (this.profile != null) @@ -70,4 +140,4 @@ private static final GameProfile readProfile(NBTTagCompound nbt, String nameKey, return null; } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerEntity.java b/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerEntity.java index 3962f1d..69b084e 100644 --- a/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerEntity.java +++ b/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerEntity.java @@ -27,4 +27,4 @@ public final World getWorld() { return this.entity.worldObj; } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerTileEntity.java b/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerTileEntity.java index a2962fa..a67074c 100644 --- a/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerTileEntity.java +++ b/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerTileEntity.java @@ -27,4 +27,4 @@ public final World getWorld() { return this.tile.getWorldObj(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerWorld.java b/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerWorld.java index deef1b0..dc14481 100644 --- a/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerWorld.java +++ b/src/main/java/com/gamerforea/eventhelper/fake/FakePlayerContainerWorld.java @@ -26,4 +26,4 @@ public final World getWorld() { return this.world; } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/util/ConvertUtils.java b/src/main/java/com/gamerforea/eventhelper/util/ConvertUtils.java index ed41cb5..b5f328d 100644 --- a/src/main/java/com/gamerforea/eventhelper/util/ConvertUtils.java +++ b/src/main/java/com/gamerforea/eventhelper/util/ConvertUtils.java @@ -75,4 +75,4 @@ public static final BlockFace toBukkitFace(ForgeDirection direction) throw new RuntimeException("Failed hooking CraftBukkit methods!", throwable); } } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/util/CraftUtils.java b/src/main/java/com/gamerforea/eventhelper/util/CraftUtils.java index 8519fd1..75bb18d 100644 --- a/src/main/java/com/gamerforea/eventhelper/util/CraftUtils.java +++ b/src/main/java/com/gamerforea/eventhelper/util/CraftUtils.java @@ -8,4 +8,4 @@ public static final Class getCraftClass(String name) throws ClassNotFoundExce { return Class.forName((EventHelper.craftPackage + '/' + name).replace("//", ".").replace('/', '.')); } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/util/EventUtils.java b/src/main/java/com/gamerforea/eventhelper/util/EventUtils.java index 9ea12f2..a1f67dc 100644 --- a/src/main/java/com/gamerforea/eventhelper/util/EventUtils.java +++ b/src/main/java/com/gamerforea/eventhelper/util/EventUtils.java @@ -43,6 +43,14 @@ public static final boolean cantBreak(EntityPlayer player, int x, int y, int z) } } + public static final boolean cantBreak(EntityPlayer player, double x, double y, double z) + { + int xx = MathHelper.floor_double(x); + int yy = MathHelper.floor_double(y); + int zz = MathHelper.floor_double(z); + return cantBreak(player, xx, yy, zz); + } + public static final boolean cantDamage(Entity damager, Entity damagee) { try @@ -64,7 +72,7 @@ public static final boolean cantInteract(EntityPlayer player, ItemStack stack, i { try { - org.bukkit.entity.Player bPlayer = toBukkitEntity(player); + Player bPlayer = toBukkitEntity(player); PlayerInteractEvent event = new PlayerInteractEvent(bPlayer, Action.RIGHT_CLICK_BLOCK, toBukkitItemStackMirror(stack), bPlayer.getWorld().getBlockAt(x, y, z), toBukkitFace(side)); EventHelper.callEvent(event); return event.isCancelled(); @@ -141,4 +149,4 @@ private static final void err(String format, Object... args) { System.err.println(String.format(format, args)); } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/util/FastUtils.java b/src/main/java/com/gamerforea/eventhelper/util/FastUtils.java index 7f17074..62f18d4 100644 --- a/src/main/java/com/gamerforea/eventhelper/util/FastUtils.java +++ b/src/main/java/com/gamerforea/eventhelper/util/FastUtils.java @@ -89,4 +89,4 @@ private static final World getEntityWorld() { return getServer().getEntityWorld(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/wg/WGReflection.java b/src/main/java/com/gamerforea/eventhelper/wg/WGReflection.java index f9a1b93..92edb41 100644 --- a/src/main/java/com/gamerforea/eventhelper/wg/WGReflection.java +++ b/src/main/java/com/gamerforea/eventhelper/wg/WGReflection.java @@ -39,4 +39,4 @@ public static final Class injectIntoWG(Class clazz) throws Throwable m.setAccessible(true); return (Class) m.invoke(getWGClassLoader(), null, bytes, 0, bytes.length); } -} \ No newline at end of file +} diff --git a/src/main/java/com/gamerforea/eventhelper/wg/WGRegionChecker.java b/src/main/java/com/gamerforea/eventhelper/wg/WGRegionChecker.java index c96e156..456dd70 100644 --- a/src/main/java/com/gamerforea/eventhelper/wg/WGRegionChecker.java +++ b/src/main/java/com/gamerforea/eventhelper/wg/WGRegionChecker.java @@ -41,4 +41,4 @@ public static final Boolean isInPrivateInj(World world, int x, int y, int z) return false; } } -} \ No newline at end of file +}