Skip to content

Commit

Permalink
Release 1.7. Add support for real players.
Browse files Browse the repository at this point in the history
  • Loading branch information
gamerforEA committed Feb 28, 2017
1 parent 87ac07b commit 26c5b4d
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apply plugin: 'forge'

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

version = "1.6"
version = "1.7"
group= "com.gamerforea"
archivesBaseName = "EventHelper"

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/gamerforea/eventhelper/EventHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ public static final void callEvent(Event event)
throwable.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +22,8 @@ public abstract class FakePlayerContainer
public GameProfile profile;
private FakePlayer player;

private WeakReference<EntityPlayer> realPlayer;

protected FakePlayerContainer(FakePlayer modFake)
{
this.modFakeProfile = modFake.getGameProfile();
Expand All @@ -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)
Expand All @@ -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<EntityPlayer>(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)
Expand Down Expand Up @@ -70,4 +140,4 @@ private static final GameProfile readProfile(NBTTagCompound nbt, String nameKey,

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public final World getWorld()
{
return this.entity.worldObj;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public final World getWorld()
{
return this.tile.getWorldObj();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ public final World getWorld()
{
return this.world;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ public static final BlockFace toBukkitFace(ForgeDirection direction)
throw new RuntimeException("Failed hooking CraftBukkit methods!", throwable);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public static final Class<?> getCraftClass(String name) throws ClassNotFoundExce
{
return Class.forName((EventHelper.craftPackage + '/' + name).replace("//", ".").replace('/', '.'));
}
}
}
12 changes: 10 additions & 2 deletions src/main/java/com/gamerforea/eventhelper/util/EventUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -141,4 +149,4 @@ private static final void err(String format, Object... args)
{
System.err.println(String.format(format, args));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ private static final World getEntityWorld()
{
return getServer().getEntityWorld();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public static final Boolean isInPrivateInj(World world, int x, int y, int z)
return false;
}
}
}
}

0 comments on commit 26c5b4d

Please sign in to comment.