2015-11-02 12:39:44 -08:00
|
|
|
package WayofTime.bloodmagic.api.util.helper;
|
2015-10-29 20:22:14 -07:00
|
|
|
|
2016-03-17 13:00:44 -07:00
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
|
|
|
import WayofTime.bloodmagic.api.event.ItemBindEvent;
|
|
|
|
import WayofTime.bloodmagic.api.iface.IBindable;
|
2016-05-30 18:20:31 -07:00
|
|
|
import WayofTime.bloodmagic.util.handler.event.GenericHandler;
|
2015-10-29 20:22:14 -07:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2016-02-25 08:54:18 -05:00
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
2016-03-17 13:00:44 -07:00
|
|
|
|
|
|
|
import java.util.UUID;
|
2015-12-28 20:13:11 -08:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public class BindableHelper
|
|
|
|
{
|
2015-10-29 20:22:14 -07:00
|
|
|
/**
|
2016-03-16 18:41:06 -04:00
|
|
|
* Sets the Owner Name of the item without checking if it is already bound.
|
|
|
|
* Also bypasses {@link ItemBindEvent}.
|
|
|
|
*
|
2016-02-03 23:14:26 -08:00
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-16 18:41:06 -04:00
|
|
|
* Sets the Owner UUID of the item without checking if it is already bound.
|
|
|
|
* Also bypasses {@link ItemBindEvent}.
|
|
|
|
*
|
2016-02-03 23:14:26 -08:00
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2016-02-04 00:25:37 -08:00
|
|
|
// Everything below is to be removed.
|
|
|
|
|
2016-02-03 23:14:26 -08:00
|
|
|
/**
|
2016-02-04 00:25:37 -08:00
|
|
|
* Deprecated.
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2016-02-04 00:25:37 -08:00
|
|
|
* Built into {@link IBindable} now.
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2016-02-03 23:14:26 -08:00
|
|
|
* @param stack
|
|
|
|
* - The ItemStack to check the owner of
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2016-02-03 23:14:26 -08:00
|
|
|
* @return - The username of the ItemStack's owner
|
|
|
|
*/
|
2016-02-04 00:25:37 -08:00
|
|
|
@Deprecated
|
2016-02-03 23:14:26 -08:00
|
|
|
public static String getOwnerName(ItemStack stack)
|
|
|
|
{
|
|
|
|
stack = NBTHelper.checkNBT(stack);
|
|
|
|
|
|
|
|
return PlayerHelper.getUsernameFromStack(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-04 00:25:37 -08:00
|
|
|
* Deprecated.
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2016-02-04 00:25:37 -08:00
|
|
|
* Built into {@link IBindable} now.
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2016-02-03 23:14:26 -08:00
|
|
|
* @param stack
|
|
|
|
* - The ItemStack to check the owner of
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2016-02-03 23:14:26 -08:00
|
|
|
* @return - The UUID of the ItemStack's owner
|
|
|
|
*/
|
2016-02-04 00:25:37 -08:00
|
|
|
@Deprecated
|
2016-02-03 23:14:26 -08:00
|
|
|
public static String getOwnerUUID(ItemStack stack)
|
|
|
|
{
|
|
|
|
stack = NBTHelper.checkNBT(stack);
|
|
|
|
|
|
|
|
return stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated.
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
|
|
|
* Now handled automatically with
|
2016-05-30 18:20:31 -07:00
|
|
|
* {@link GenericHandler#onInteract(PlayerInteractEvent.RightClickItem)}
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param stack
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The ItemStack to bind
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param player
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The Player to bind the ItemStack to
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2015-10-29 20:22:14 -07:00
|
|
|
* @return - Whether binding was successful
|
|
|
|
*/
|
2016-02-03 23:14:26 -08:00
|
|
|
@Deprecated
|
2015-12-30 15:34:40 -05:00
|
|
|
public static boolean checkAndSetItemOwner(ItemStack stack, EntityPlayer player)
|
|
|
|
{
|
2016-01-13 19:59:28 -05:00
|
|
|
return !PlayerHelper.isFakePlayer(player) && checkAndSetItemOwner(stack, PlayerHelper.getUUIDFromPlayer(player), player.getName());
|
2015-10-29 20:22:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-03 23:14:26 -08:00
|
|
|
* Deprecated.
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
|
|
|
* Now handled automatically with
|
2016-05-30 18:20:31 -07:00
|
|
|
* {@link GenericHandler#onInteract(PlayerInteractEvent.RightClickItem)}
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param stack
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The ItemStack to bind
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param uuid
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The username to bind the ItemStack to
|
2016-01-13 19:59:28 -05:00
|
|
|
* @param currentUsername
|
|
|
|
* - The current name of the player.
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2015-10-29 20:22:14 -07:00
|
|
|
* @return - Whether the binding was successful
|
|
|
|
*/
|
2016-02-03 23:14:26 -08:00
|
|
|
@Deprecated
|
2016-01-13 19:59:28 -05:00
|
|
|
public static boolean checkAndSetItemOwner(ItemStack stack, String uuid, String currentUsername)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2015-11-28 18:25:46 -08:00
|
|
|
stack = NBTHelper.checkNBT(stack);
|
2015-10-29 20:22:14 -07:00
|
|
|
|
|
|
|
if (!(stack.getItem() instanceof IBindable))
|
|
|
|
return false;
|
|
|
|
|
2016-01-13 19:59:28 -05:00
|
|
|
String currentOwner = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
|
|
|
|
|
|
|
|
if (currentOwner == "") //The player has not been set yet, so set everything.
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2015-12-28 20:13:11 -08:00
|
|
|
MinecraftForge.EVENT_BUS.post(new ItemBindEvent(PlayerHelper.getPlayerFromUUID(uuid), uuid, stack));
|
|
|
|
((IBindable) stack.getItem()).onBind(PlayerHelper.getPlayerFromUUID(uuid), stack);
|
|
|
|
stack.getTagCompound().setString(Constants.NBT.OWNER_UUID, uuid);
|
2016-01-13 19:59:28 -05:00
|
|
|
stack.getTagCompound().setString(Constants.NBT.OWNER_NAME, currentUsername);
|
2015-10-29 20:22:14 -07:00
|
|
|
return true;
|
2016-01-13 19:59:28 -05:00
|
|
|
} else if (currentOwner.equals(uuid)) //The player has been set, so this will simply update the display name
|
|
|
|
{
|
|
|
|
stack.getTagCompound().setString(Constants.NBT.OWNER_NAME, currentUsername);
|
2015-10-29 20:22:14 -07:00
|
|
|
}
|
|
|
|
|
2015-12-27 19:38:12 -05:00
|
|
|
return true;
|
2015-10-29 20:22:14 -07:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:13:11 -08:00
|
|
|
/**
|
2016-02-03 23:14:26 -08:00
|
|
|
* Deprecated.
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
|
|
|
* Now handled automatically with
|
2016-05-30 18:20:31 -07:00
|
|
|
* {@link GenericHandler#onInteract(PlayerInteractEvent.RightClickItem)}
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2016-01-01 10:52:42 -08:00
|
|
|
* @param stack
|
2016-01-02 17:56:37 -05:00
|
|
|
* - ItemStack to check
|
2016-01-01 10:52:42 -08:00
|
|
|
* @param uuid
|
2016-01-02 17:56:37 -05:00
|
|
|
* - UUID of the Player
|
2016-01-13 19:59:28 -05:00
|
|
|
* @param currentUsername
|
|
|
|
* - The current name of the player.
|
2015-12-28 20:13:11 -08:00
|
|
|
*/
|
2016-02-03 23:14:26 -08:00
|
|
|
@Deprecated
|
2016-01-13 19:59:28 -05:00
|
|
|
public static boolean checkAndSetItemOwner(ItemStack stack, UUID uuid, String currentUsername)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2016-01-13 19:59:28 -05:00
|
|
|
return checkAndSetItemOwner(stack, uuid.toString(), currentUsername);
|
2015-12-28 20:13:11 -08:00
|
|
|
}
|
|
|
|
|
2015-10-29 20:22:14 -07:00
|
|
|
/**
|
2016-02-03 23:14:26 -08:00
|
|
|
* Deprecated.
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2016-02-03 23:14:26 -08:00
|
|
|
* @see #setItemOwnerName(ItemStack, String)
|
2016-03-16 18:41:06 -04:00
|
|
|
*
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param stack
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The ItemStack to bind
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param ownerName
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The username to bind the ItemStack to
|
2015-10-29 20:22:14 -07:00
|
|
|
*/
|
2016-02-03 23:14:26 -08:00
|
|
|
@Deprecated
|
2015-12-30 15:34:40 -05:00
|
|
|
public static void setItemOwner(ItemStack stack, String ownerName)
|
|
|
|
{
|
2016-02-03 23:14:26 -08:00
|
|
|
setItemOwnerName(stack, ownerName);
|
2015-10-29 20:22:14 -07:00
|
|
|
}
|
|
|
|
}
|