BloodMagic/src/main/java/WayofTime/bloodmagic/apibutnotreally/util/helper/BindableHelper.java

150 lines
5.1 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.apibutnotreally.util.helper;
import WayofTime.bloodmagic.apibutnotreally.Constants;
import WayofTime.bloodmagic.apibutnotreally.event.ItemBindEvent;
import WayofTime.bloodmagic.apibutnotreally.iface.IBindable;
import WayofTime.bloodmagic.util.handler.event.GenericHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import java.util.UUID;
2017-08-15 21:30:48 -07:00
public class BindableHelper {
/**
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}.
2017-08-15 21:30:48 -07:00
*
* @param stack - The ItemStack to bind
* @param ownerName - The username to bind the ItemStack to
*/
2017-08-15 21:30:48 -07:00
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}.
2017-08-15 21:30:48 -07:00
*
* @param stack - The ItemStack to bind
* @param ownerUUID - The UUID to bind the ItemStack to
*/
2017-08-15 21:30:48 -07:00
public static void setItemOwnerUUID(ItemStack stack, String ownerUUID) {
stack = NBTHelper.checkNBT(stack);
stack.getTagCompound().setString(Constants.NBT.OWNER_UUID, ownerUUID);
}
// Everything below is to be removed.
/**
* Deprecated.
2017-08-15 21:30:48 -07:00
* <p>
* Built into {@link IBindable} now.
2017-08-15 21:30:48 -07:00
*
* @param stack - The ItemStack to check the owner of
* @return - The username of the ItemStack's owner
*/
@Deprecated
2017-08-15 21:30:48 -07:00
public static String getOwnerName(ItemStack stack) {
stack = NBTHelper.checkNBT(stack);
return PlayerHelper.getUsernameFromStack(stack);
}
/**
* Deprecated.
2017-08-15 21:30:48 -07:00
* <p>
* Built into {@link IBindable} now.
2017-08-15 21:30:48 -07:00
*
* @param stack - The ItemStack to check the owner of
* @return - The UUID of the ItemStack's owner
*/
@Deprecated
2017-08-15 21:30:48 -07:00
public static String getOwnerUUID(ItemStack stack) {
stack = NBTHelper.checkNBT(stack);
return stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
}
/**
* Deprecated.
2017-08-15 21:30:48 -07:00
* <p>
2016-03-16 18:41:06 -04:00
* Now handled automatically with
* {@link GenericHandler#onInteract(PlayerInteractEvent.RightClickItem)}
2017-08-15 21:30:48 -07:00
*
* @param stack - The ItemStack to bind
* @param player - The Player to bind the ItemStack to
* @return - Whether binding was successful
*/
@Deprecated
2017-08-15 21:30:48 -07:00
public static boolean checkAndSetItemOwner(ItemStack stack, EntityPlayer player) {
return !PlayerHelper.isFakePlayer(player) && checkAndSetItemOwner(stack, PlayerHelper.getUUIDFromPlayer(player), player.getName());
}
/**
* Deprecated.
2017-08-15 21:30:48 -07:00
* <p>
2016-03-16 18:41:06 -04:00
* Now handled automatically with
* {@link GenericHandler#onInteract(PlayerInteractEvent.RightClickItem)}
2017-08-15 21:30:48 -07:00
*
* @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
2017-08-15 21:30:48 -07:00
public static boolean checkAndSetItemOwner(ItemStack stack, String uuid, String currentUsername) {
stack = NBTHelper.checkNBT(stack);
if (!(stack.getItem() instanceof IBindable))
return false;
String currentOwner = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
if (currentOwner == "") //The player has not been set yet, so set everything.
{
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);
stack.getTagCompound().setString(Constants.NBT.OWNER_NAME, currentUsername);
return true;
} 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-12-27 19:38:12 -05:00
return true;
}
/**
* Deprecated.
2017-08-15 21:30:48 -07:00
* <p>
2016-03-16 18:41:06 -04:00
* Now handled automatically with
* {@link GenericHandler#onInteract(PlayerInteractEvent.RightClickItem)}
2017-08-15 21:30:48 -07:00
*
* @param stack - ItemStack to check
* @param uuid - UUID of the Player
* @param currentUsername - The current name of the player.
*/
@Deprecated
2017-08-15 21:30:48 -07:00
public static boolean checkAndSetItemOwner(ItemStack stack, UUID uuid, String currentUsername) {
return checkAndSetItemOwner(stack, uuid.toString(), currentUsername);
}
/**
* Deprecated.
2017-08-15 21:30:48 -07:00
*
* @param stack - The ItemStack to bind
* @param ownerName - The username to bind the ItemStack to
* @see #setItemOwnerName(ItemStack, String)
*/
@Deprecated
2017-08-15 21:30:48 -07:00
public static void setItemOwner(ItemStack stack, String ownerName) {
setItemOwnerName(stack, ownerName);
}
}