2015-11-02 12:39:44 -08:00
|
|
|
package WayofTime.bloodmagic.api.util.helper;
|
2015-10-29 20:22:14 -07:00
|
|
|
|
2015-11-28 18:25:46 -08:00
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
2015-11-02 12:39:44 -08:00
|
|
|
import WayofTime.bloodmagic.api.event.ItemBindEvent;
|
|
|
|
import WayofTime.bloodmagic.api.iface.IBindable;
|
2015-10-29 20:22:14 -07:00
|
|
|
import com.google.common.base.Strings;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param stack - The ItemStack to bind
|
|
|
|
* @param player - The Player to bind the ItemStack to
|
2015-11-28 18:25:46 -08:00
|
|
|
*
|
2015-10-29 20:22:14 -07:00
|
|
|
* @return - Whether binding was successful
|
|
|
|
*/
|
|
|
|
public static boolean checkAndSetItemOwner(ItemStack stack, EntityPlayer player) {
|
|
|
|
return !PlayerHelper.isFakePlayer(player) && checkAndSetItemOwner(stack, player.getGameProfile().getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind an item to a username.
|
2015-12-01 21:14:26 -08:00
|
|
|
*
|
2015-10-29 20:22:14 -07:00
|
|
|
* Requires the Item contained in the ItemStack to be an instanceof {@link IBindable}
|
2015-12-01 21:14:26 -08:00
|
|
|
*
|
2015-10-29 20:22:14 -07:00
|
|
|
* Fires {@link ItemBindEvent}.
|
|
|
|
*
|
|
|
|
* @param stack - The ItemStack to bind
|
|
|
|
* @param ownerName - The username to bind the ItemStack to
|
2015-11-28 18:25:46 -08:00
|
|
|
*
|
2015-10-29 20:22:14 -07:00
|
|
|
* @return - Whether the binding was successful
|
|
|
|
*/
|
|
|
|
public static boolean checkAndSetItemOwner(ItemStack stack, String ownerName) {
|
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;
|
|
|
|
|
2015-11-28 18:25:46 -08:00
|
|
|
if (Strings.isNullOrEmpty(stack.getTagCompound().getString(Constants.NBT.OWNER_NAME))) {
|
2015-10-29 20:22:14 -07:00
|
|
|
MinecraftForge.EVENT_BUS.post(new ItemBindEvent(PlayerHelper.getPlayerFromUsername(ownerName), ownerName, stack));
|
|
|
|
((IBindable) stack.getItem()).onBind(PlayerHelper.getPlayerFromUsername(ownerName), stack);
|
2015-11-28 18:25:46 -08:00
|
|
|
stack.getTagCompound().setString(Constants.NBT.OWNER_NAME, ownerName);
|
2015-10-29 20:22:14 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Owner 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 setItemOwner(ItemStack stack, String ownerName) {
|
2015-11-28 18:25:46 -08:00
|
|
|
stack = NBTHelper.checkNBT(stack);
|
2015-10-29 20:22:14 -07:00
|
|
|
|
2015-11-28 18:25:46 -08:00
|
|
|
stack.getTagCompound().setString(Constants.NBT.OWNER_NAME, ownerName);
|
2015-10-29 20:22:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to safely obtain the username of the ItemStack's owner
|
|
|
|
*
|
|
|
|
* @param stack - The ItemStack to check the owner of
|
2015-11-28 18:25:46 -08:00
|
|
|
*
|
2015-10-29 20:22:14 -07:00
|
|
|
* @return - The username of the ItemStack's owner
|
|
|
|
*/
|
|
|
|
public static String getOwnerName(ItemStack stack) {
|
2015-11-28 18:25:46 -08:00
|
|
|
stack = NBTHelper.checkNBT(stack);
|
2015-10-29 20:22:14 -07:00
|
|
|
|
2015-11-28 18:25:46 -08:00
|
|
|
return stack.getTagCompound().getString(Constants.NBT.OWNER_NAME);
|
2015-10-29 20:22:14 -07:00
|
|
|
}
|
|
|
|
}
|