Rewrite Binding system to be fully automated

No more manual handling of binding items to players. Retains all previous functionality.

I have tested on both the Client and Server and it seems to work just fine. If any issues arise from this, do not hesitate to yell at me.

The deprecated methods will be removed after beta.
This commit is contained in:
Nick 2016-02-03 23:14:26 -08:00
parent e219b50589
commit 8c1eaddb97
14 changed files with 152 additions and 165 deletions

View file

@ -8,43 +8,107 @@ import net.minecraftforge.common.MinecraftForge;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.api.event.ItemBindEvent;
import WayofTime.bloodmagic.api.iface.IBindable;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
public class BindableHelper
{
/**
* Bind an item to a player. Handles checking if the player was an
* instanceof {@link net.minecraftforge.common.util.FakePlayer} or other
* type of Fake Player.
*
* Sets the Owner Name of the item without checking if it is already bound. Also
* bypasses {@link ItemBindEvent}.
*
* @param stack
* - The ItemStack to bind
* @param ownerName
* - The username to bind the ItemStack to
*/
public static void setItemOwnerName(ItemStack stack, String ownerName)
{
stack = NBTHelper.checkNBT(stack);
stack.getTagCompound().setString(Constants.NBT.OWNER_NAME, ownerName);
}
/**
* Sets the Owner UUID of the item without checking if it is already bound. Also
* bypasses {@link ItemBindEvent}.
*
* @param stack
* - The ItemStack to bind
* @param ownerUUID
* - The UUID to bind the ItemStack to
*/
public static void setItemOwnerUUID(ItemStack stack, String ownerUUID)
{
stack = NBTHelper.checkNBT(stack);
stack.getTagCompound().setString(Constants.NBT.OWNER_UUID, ownerUUID);
}
/**
* Used to safely obtain the username of the ItemStack's owner
*
* @param stack
* - The ItemStack to check the owner of
*
* @return - The username of the ItemStack's owner
*/
public static String getOwnerName(ItemStack stack)
{
stack = NBTHelper.checkNBT(stack);
return PlayerHelper.getUsernameFromStack(stack);
}
/**
* Used to safely obtain the UUID of the ItemStack's owner
*
* @param stack
* - The ItemStack to check the owner of
*
* @return - The UUID of the ItemStack's owner
*/
public static String getOwnerUUID(ItemStack stack)
{
stack = NBTHelper.checkNBT(stack);
return stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
}
// Everything below is to be removed.
/**
* Deprecated.
*
* Now handled automatically with {@link WayofTime.bloodmagic.util.handler.EventHandler#interactEvent(PlayerInteractEvent)}
*
* @param stack
* - The ItemStack to bind
* @param player
* - The Player to bind the ItemStack to
*
*
* @return - Whether binding was successful
*/
@Deprecated
public static boolean checkAndSetItemOwner(ItemStack stack, EntityPlayer player)
{
return !PlayerHelper.isFakePlayer(player) && checkAndSetItemOwner(stack, PlayerHelper.getUUIDFromPlayer(player), player.getName());
}
/**
* Bind an item to a username.
*
* Requires the Item contained in the ItemStack to be an instanceof
* {@link IBindable}
*
* Fires {@link ItemBindEvent}.
*
* Deprecated.
*
* Now handled automatically with {@link WayofTime.bloodmagic.util.handler.EventHandler#interactEvent(PlayerInteractEvent)}
*
* @param stack
* - The ItemStack to bind
* @param uuid
* - The username to bind the ItemStack to
* @param currentUsername
* - The current name of the player.
*
*
* @return - Whether the binding was successful
*/
@Deprecated
public static boolean checkAndSetItemOwner(ItemStack stack, String uuid, String currentUsername)
{
stack = NBTHelper.checkNBT(stack);
@ -70,8 +134,10 @@ public class BindableHelper
}
/**
* @see BindableHelper#checkAndSetItemOwner(ItemStack, String)
*
* Deprecated.
*
* Now handled automatically with {@link WayofTime.bloodmagic.util.handler.EventHandler#interactEvent(PlayerInteractEvent)}
*
* @param stack
* - ItemStack to check
* @param uuid
@ -79,54 +145,25 @@ public class BindableHelper
* @param currentUsername
* - The current name of the player.
*/
@Deprecated
public static boolean checkAndSetItemOwner(ItemStack stack, UUID uuid, String currentUsername)
{
return checkAndSetItemOwner(stack, uuid.toString(), currentUsername);
}
/**
* Sets the Owner of the item without checking if it is already bound. Also
* bypasses {@link ItemBindEvent}.
*
* Deprecated.
*
* @see #setItemOwnerName(ItemStack, String)
*
* @param stack
* - The ItemStack to bind
* @param ownerName
* - The username to bind the ItemStack to
*/
@Deprecated
public static void setItemOwner(ItemStack stack, String ownerName)
{
stack = NBTHelper.checkNBT(stack);
stack.getTagCompound().setString(Constants.NBT.OWNER_UUID, ownerName);
}
/**
* Used to safely obtain the username of the ItemStack's owner
*
* @param stack
* - The ItemStack to check the owner of
*
* @return - The username of the ItemStack's owner
*/
public static String getOwnerName(ItemStack stack)
{
stack = NBTHelper.checkNBT(stack);
return PlayerHelper.getUsernameFromStack(stack);
}
/**
* Used to safely obtain the UUID of the ItemStack's owner
*
* @param stack
* - The ItemStack to check the owner of
*
* @return - The UUID of the ItemStack's owner
*/
public static String getOwnerUUID(ItemStack stack)
{
stack = NBTHelper.checkNBT(stack);
return stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
setItemOwnerName(stack, ownerName);
}
}

View file

@ -41,13 +41,9 @@ public class BlockTeleposer extends BlockContainer
ItemStack playerItem = player.getCurrentEquippedItem();
if (playerItem != null && playerItem.getItem() instanceof ItemTelepositionFocus)
{
BindableHelper.checkAndSetItemOwner(playerItem, player);
((ItemTelepositionFocus) playerItem.getItem()).setBlockPos(playerItem, world, pos);
} else if (world.getTileEntity(pos) instanceof TileTeleposer)
{
else if (world.getTileEntity(pos) instanceof TileTeleposer)
player.openGui(BloodMagic.instance, Constants.Gui.TELEPOSER_GUI, world, pos.getX(), pos.getY(), pos.getZ());
}
return true;
}

View file

@ -154,52 +154,6 @@ public class ItemBindable extends Item implements IBindable
tooltip.add(TextHelper.localizeEffect("tooltip.BloodMagic.currentOwner", PlayerHelper.getUsernameFromStack(stack)));
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
BindableHelper.checkAndSetItemOwner(stack, player);
return stack;
}
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
BindableHelper.checkAndSetItemOwner(stack, player);
return false;
}
protected void damagePlayer(World world, EntityPlayer player, int damage)
{
if (world != null)
{
double posX = player.posX;
double posY = player.posY;
double posZ = player.posZ;
world.playSoundEffect((double) ((float) posX + 0.5F), (double) ((float) posY + 0.5F), (double) ((float) posZ + 0.5F), "random.fizz", 0.5F, 2.6F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.8F);
float f = 1.0F;
float f1 = f * 0.6F + 0.4F;
float f2 = f * f * 0.7F - 0.5F;
float f3 = f * f * 0.6F - 0.7F;
for (int l = 0; l < 8; ++l)
world.spawnParticle(EnumParticleTypes.REDSTONE, posX + Math.random() - Math.random(), posY + Math.random() - Math.random(), posZ + Math.random() - Math.random(), f1, f2, f3);
}
for (int i = 0; i < damage; i++)
{
player.attackEntityFrom(BloodMagicAPI.getDamageSource(), 0F); // Emulate
// an
// attack
player.setHealth(player.getHealth() - 1);
if (player.getHealth() <= 0.0005)
{
player.inventory.dropAllItems();
break;
}
}
}
public int getLPUsed()
{
return this.lpUsed;

View file

@ -45,8 +45,6 @@ public class ItemBloodOrb extends ItemBindable implements IBloodOrb, IBindable
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
super.onItemRightClick(stack, world, player);
if (world == null)
return stack;

View file

@ -37,8 +37,6 @@ public class ItemBoundSword extends ItemSword implements IBindable
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
BindableHelper.checkAndSetItemOwner(stack, player);
if (!player.isSneaking() && getActivated(stack))
player.setItemInUse(stack, this.getMaxItemUseDuration(stack));

View file

@ -110,8 +110,6 @@ public class ItemBoundTool extends ItemBindable
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
BindableHelper.checkAndSetItemOwner(stack, player);
// if (!world.isRemote)
{
if (player.isSneaking())
@ -136,15 +134,6 @@ public class ItemBoundTool extends ItemBindable
return stack;
}
@Override
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos blockPos, EnumFacing side, float hitX, float hitY, float hitZ)
{
if (BindableHelper.checkAndSetItemOwner(stack, player) && ItemBindable.syphonNetwork(stack, player, getLPUsed()))
return false;
return false;
}
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{

View file

@ -52,16 +52,13 @@ public class ItemTelepositionFocus extends ItemBindable
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
if (BindableHelper.checkAndSetItemOwner(stack, player))
if (player.isSneaking())
{
if (player.isSneaking())
{
MovingObjectPosition mop = getMovingObjectPositionFromPlayer(world, player, false);
MovingObjectPosition mop = getMovingObjectPositionFromPlayer(world, player, false);
if (mop != null && mop.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK)
{
setBlockPos(stack, world, mop.getBlockPos());
}
if (mop != null && mop.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK)
{
setBlockPos(stack, world, mop.getBlockPos());
}
}

View file

@ -48,14 +48,6 @@ public class ItemSigilBase extends ItemBindable implements ISigil
return stack;
}
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
super.onItemRightClick(stack, world, player);
return false;
}
@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltip, boolean advanced)

View file

@ -23,22 +23,17 @@ public class ItemSigilBloodLight extends ItemSigilBase
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
if (BindableHelper.checkAndSetItemOwner(stack, player) && ItemBindable.syphonNetwork(stack, player, getLPUsed() * 5) && !world.isRemote)
MovingObjectPosition mop = this.getMovingObjectPositionFromPlayer(world, player, false);
if (mop != null && mop.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK)
{
MovingObjectPosition mop = this.getMovingObjectPositionFromPlayer(world, player, false);
BlockPos blockPos = mop.getBlockPos().offset(mop.sideHit);
if (mop != null && mop.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK)
{
BlockPos blockPos = mop.getBlockPos().offset(mop.sideHit);
if (world.isAirBlock(blockPos))
{
world.setBlockState(blockPos, ModBlocks.bloodLight.getDefaultState());
}
} else
{
world.spawnEntityInWorld(new EntityBloodLight(world, player));
}
if (world.isAirBlock(blockPos))
world.setBlockState(blockPos, ModBlocks.bloodLight.getDefaultState());
} else
{
world.spawnEntityInWorld(new EntityBloodLight(world, player));
}
return stack;
@ -52,15 +47,10 @@ public class ItemSigilBloodLight extends ItemSigilBase
if (world.isRemote)
return false;
if (BindableHelper.checkAndSetItemOwner(stack, player) && ItemBindable.syphonNetwork(stack, player, getLPUsed()))
{
BlockPos newPos = blockPos.offset(side);
BlockPos newPos = blockPos.offset(side);
if (world.isAirBlock(newPos))
{
world.setBlockState(newPos, ModBlocks.bloodLight.getDefaultState());
}
}
if (world.isAirBlock(newPos))
world.setBlockState(newPos, ModBlocks.bloodLight.getDefaultState());
return true;
}

View file

@ -74,7 +74,7 @@ public class ItemSigilLava extends ItemSigilBase
{
super.onItemUse(stack, player, world, blockPos, side, hitX, hitY, hitZ);
if (world.isRemote || !BindableHelper.checkAndSetItemOwner(stack, player) || player.isSneaking() || isUnusable(stack))
if (world.isRemote || player.isSneaking() || isUnusable(stack))
{
return false;
}

View file

@ -37,7 +37,6 @@ public class ItemSigilToggleable extends ItemSigilBase
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
BindableHelper.checkAndSetItemOwner(stack, player);
if (!world.isRemote && !isUnusable(stack))
{
if (player.isSneaking())
@ -52,12 +51,7 @@ public class ItemSigilToggleable extends ItemSigilBase
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos blockPos, EnumFacing side, float hitX, float hitY, float hitZ)
{
super.onItemUse(stack, player, world, blockPos, side, hitX, hitY, hitZ);
if (BindableHelper.checkAndSetItemOwner(stack, player) && ItemBindable.syphonNetwork(stack, player, getLPUsed()))
return onSigilUse(stack, player, world, blockPos, side, hitX, hitY, hitZ);
return false;
return ItemBindable.syphonNetwork(stack, player, getLPUsed()) && onSigilUse(stack, player, world, blockPos, side, hitX, hitY, hitZ);
}
public boolean onSigilUse(ItemStack itemStack, EntityPlayer player, World world, BlockPos blockPos, EnumFacing side, float hitX, float hitY, float hitZ)

View file

@ -76,7 +76,7 @@ public class ItemSigilVoid extends ItemSigilBase
{
super.onItemUse(stack, player, world, blockPos, side, hitX, hitY, hitZ);
if (world.isRemote || !BindableHelper.checkAndSetItemOwner(stack, player) || player.isSneaking() || isUnusable(stack))
if (world.isRemote || player.isSneaking() || isUnusable(stack))
{
return false;
}

View file

@ -74,7 +74,7 @@ public class ItemSigilWater extends ItemSigilBase
{
super.onItemUse(stack, player, world, blockPos, side, hitX, hitY, hitZ);
if (world.isRemote || !BindableHelper.checkAndSetItemOwner(stack, player) || player.isSneaking() || isUnusable(stack))
if (world.isRemote || player.isSneaking() || isUnusable(stack))
{
return false;
}

View file

@ -3,8 +3,13 @@ package WayofTime.bloodmagic.util.handler;
import java.util.List;
import java.util.Random;
import WayofTime.bloodmagic.api.event.ItemBindEvent;
import WayofTime.bloodmagic.api.iface.IBindable;
import WayofTime.bloodmagic.api.util.helper.BindableHelper;
import WayofTime.bloodmagic.api.util.helper.NBTHelper;
import com.google.common.base.Strings;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
@ -19,6 +24,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.AnvilUpdateEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
@ -182,6 +188,42 @@ public class EventHandler
}
}
@SubscribeEvent
public void interactEvent(PlayerInteractEvent event)
{
if (event.world.isRemote)
return;
EntityPlayer player = event.entityPlayer;
if (PlayerHelper.isFakePlayer(player))
return;
if (event.useBlock == Result.DENY && event.useItem != Result.DENY)
{
ItemStack held = player.getHeldItem();
if (held != null && held.getItem() instanceof IBindable)
{
held = NBTHelper.checkNBT(held);
if (Strings.isNullOrEmpty(BindableHelper.getOwnerUUID(held)))
{
IBindable bindable = (IBindable) held.getItem();
if (bindable.onBind(player, held))
{
String uuid = PlayerHelper.getUUIDFromPlayer(player).toString();
ItemBindEvent toPost = new ItemBindEvent(player, uuid, held);
if (MinecraftForge.EVENT_BUS.post(toPost) || toPost.getResult() == Result.DENY)
return;
BindableHelper.setItemOwnerUUID(held, uuid);
BindableHelper.setItemOwnerName(held, player.getDisplayNameString());
}
} else if (BindableHelper.getOwnerUUID(held).equals(PlayerHelper.getUUIDFromPlayer(player).toString()) && !BindableHelper.getOwnerName(held).equals(player.getDisplayNameString()))
BindableHelper.setItemOwnerName(held, player.getDisplayNameString());
}
}
}
@SubscribeEvent
public void selfSacrificeEvent(SacrificeKnifeUsedEvent event)
{