Rewrite IBindable to provide an object instead of storing 2 strings

This commit is contained in:
Nicholas Ignoffo 2018-02-27 16:59:51 -08:00
parent 941173dbf4
commit 2a43e53842
47 changed files with 416 additions and 510 deletions

View file

@ -3,6 +3,7 @@ package WayofTime.bloodmagic.util.handler.event;
import WayofTime.bloodmagic.BloodMagic;
import WayofTime.bloodmagic.ConfigHandler;
import WayofTime.bloodmagic.api.impl.BloodMagicAPI;
import WayofTime.bloodmagic.core.data.Binding;
import WayofTime.bloodmagic.util.Constants;
import WayofTime.bloodmagic.event.ItemBindEvent;
import WayofTime.bloodmagic.event.SacrificeKnifeUsedEvent;
@ -53,6 +54,7 @@ import net.minecraft.init.MobEffects;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
@ -79,10 +81,7 @@ import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.registry.EntityEntry;
import net.minecraftforge.fml.common.registry.EntityRegistry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.*;
@Mod.EventBusSubscriber(modid = BloodMagic.MODID)
public class GenericHandler {
@ -339,25 +338,25 @@ public class GenericHandler {
return;
ItemStack held = event.getItemStack();
if (!held.isEmpty() && held.getItem() instanceof IBindable) {
held = NBTHelper.checkNBT(held);
if (!held.isEmpty() && held.getItem() instanceof IBindable) { // Make sure it's bindable
IBindable bindable = (IBindable) held.getItem();
if (Strings.isNullOrEmpty(bindable.getOwnerUUID(held))) {
Binding binding = bindable.getBinding(held);
if (binding == null) { // If the binding is null, let's create one
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)
ItemBindEvent toPost = new ItemBindEvent(player, held);
if (MinecraftForge.EVENT_BUS.post(toPost)) // Allow cancellation of binding
return;
BindableHelper.setItemOwnerUUID(held, uuid);
BindableHelper.setItemOwnerName(held, player.getDisplayNameString());
BindableHelper.applyBinding(held, player); // Bind item to the player
}
} else if (bindable.getOwnerUUID(held).equals(PlayerHelper.getUUIDFromPlayer(player).toString()) && !bindable.getOwnerName(held).equals(player.getDisplayNameString()))
BindableHelper.setItemOwnerName(held, player.getDisplayNameString());
// If the binding exists, we'll check if the player's name has changed since they last used it and update that if so.
} else if (binding.getOwnerId().equals(player.getGameProfile().getId()) && !binding.getOwnerName().equals(player.getGameProfile().getName())) {
binding.setOwnerName(player.getGameProfile().getName());
BindableHelper.applyBinding(held, binding);
}
}
if (!held.isEmpty() && held.getItem() instanceof IBloodOrb) {
held = NBTHelper.checkNBT(held);
IBloodOrb bloodOrb = (IBloodOrb) held.getItem();
SoulNetwork network = NetworkHelper.getSoulNetwork(player);

View file

@ -1,17 +1,26 @@
package WayofTime.bloodmagic.util.helper;
import WayofTime.bloodmagic.core.data.Binding;
import WayofTime.bloodmagic.util.Constants;
import WayofTime.bloodmagic.event.ItemBindEvent;
import WayofTime.bloodmagic.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;
import net.minecraft.nbt.NBTTagCompound;
public class BindableHelper {
public static void applyBinding(ItemStack stack, EntityPlayer player) {
Binding binding = new Binding(player.getGameProfile().getId(), player.getGameProfile().getName());
applyBinding(stack, binding);
}
public static void applyBinding(ItemStack stack, Binding binding) {
if (!stack.hasTagCompound())
stack.setTagCompound(new NBTTagCompound());
stack.getTagCompound().setTag("binding", binding.serializeNBT());
}
/**
* Sets the Owner Name of the item without checking if it is already bound.
* Also bypasses {@link ItemBindEvent}.
@ -37,113 +46,4 @@ public class BindableHelper {
stack.getTagCompound().setString(Constants.NBT.OWNER_UUID, ownerUUID);
}
// Everything below is to be removed.
/**
* Deprecated.
* <p>
* Built into {@link IBindable} now.
*
* @param stack - The ItemStack to check the owner of
* @return - The username of the ItemStack's owner
*/
@Deprecated
public static String getOwnerName(ItemStack stack) {
stack = NBTHelper.checkNBT(stack);
return PlayerHelper.getUsernameFromStack(stack);
}
/**
* Deprecated.
* <p>
* Built into {@link IBindable} now.
*
* @param stack - The ItemStack to check the owner of
* @return - The UUID of the ItemStack's owner
*/
@Deprecated
public static String getOwnerUUID(ItemStack stack) {
stack = NBTHelper.checkNBT(stack);
return stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
}
/**
* Deprecated.
* <p>
* Now handled automatically with
* {@link GenericHandler#onInteract(PlayerInteractEvent.RightClickItem)}
*
* @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());
}
/**
* Deprecated.
* <p>
* Now handled automatically with
* {@link GenericHandler#onInteract(PlayerInteractEvent.RightClickItem)}
*
* @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);
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);
}
return true;
}
/**
* Deprecated.
* <p>
* Now handled automatically with
* {@link GenericHandler#onInteract(PlayerInteractEvent.RightClickItem)}
*
* @param stack - ItemStack to check
* @param uuid - UUID of the Player
* @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);
}
/**
* Deprecated.
*
* @param stack - The ItemStack to bind
* @param ownerName - The username to bind the ItemStack to
* @see #setItemOwnerName(ItemStack, String)
*/
@Deprecated
public static void setItemOwner(ItemStack stack, String ownerName) {
setItemOwnerName(stack, ownerName);
}
}

View file

@ -1,5 +1,7 @@
package WayofTime.bloodmagic.util.helper;
import WayofTime.bloodmagic.core.data.Binding;
import WayofTime.bloodmagic.iface.IBindable;
import WayofTime.bloodmagic.util.Constants;
import WayofTime.bloodmagic.event.SoulNetworkEvent;
import WayofTime.bloodmagic.orb.BloodOrb;
@ -57,6 +59,10 @@ public class NetworkHelper {
return getSoulNetwork(PlayerHelper.getUUIDFromPlayer(player));
}
public static SoulNetwork getSoulNetwork(Binding binding) {
return getSoulNetwork(binding.getOwnerId());
}
/**
* Gets the current orb tier of the SoulNetwork.
*
@ -99,7 +105,7 @@ public class NetworkHelper {
@Deprecated
public static boolean syphonAndDamage(SoulNetwork soulNetwork, EntityPlayer user, int toSyphon) {
// if (soulNetwork.getPlayer() == null)
// if (soulNetwork.getNewOwner() == null)
// {
// soulNetwork.syphon(toSyphon);
// return true;
@ -117,15 +123,16 @@ public class NetworkHelper {
*/
public static boolean syphonFromContainer(ItemStack stack, int toSyphon) //TODO: Change to a String, int?
{
stack = NBTHelper.checkNBT(stack);
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
if (Strings.isNullOrEmpty(ownerName))
if (!(stack.getItem() instanceof IBindable))
return false;
SoulNetwork network = getSoulNetwork(ownerName);
Binding binding = ((IBindable) stack.getItem()).getBinding(stack);
if (binding == null)
return false;
SoulNetworkEvent.ItemDrainInContainerEvent event = new SoulNetworkEvent.ItemDrainInContainerEvent(stack, ownerName, toSyphon);
SoulNetwork network = getSoulNetwork(binding);
SoulNetworkEvent.ItemDrainInContainerEvent event = new SoulNetworkEvent.ItemDrainInContainerEvent(stack, binding.getOwnerId(), toSyphon);
return !(MinecraftForge.EVENT_BUS.post(event) || event.getResult() == Event.Result.DENY) && network.syphon(event.syphon) >= toSyphon;
}
@ -138,13 +145,14 @@ public class NetworkHelper {
* @return - If syphoning is possible
*/
public static boolean canSyphonFromContainer(ItemStack stack, int toSyphon) {
stack = NBTHelper.checkNBT(stack);
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
if (Strings.isNullOrEmpty(ownerName))
if (!(stack.getItem() instanceof IBindable))
return false;
SoulNetwork network = getSoulNetwork(ownerName);
Binding binding = ((IBindable) stack.getItem()).getBinding(stack);
if (binding == null)
return false;
SoulNetwork network = getSoulNetwork(binding);
return network.getCurrentEssence() >= toSyphon;
}

View file

@ -1,12 +1,7 @@
package WayofTime.bloodmagic.util.helper;
import WayofTime.bloodmagic.util.Constants;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraftforge.common.UsernameCache;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.fml.common.FMLCommonHandler;
@ -23,43 +18,25 @@ public class PlayerHelper {
*/
private static final ArrayList<String> knownFakePlayers = Lists.newArrayList();
public static String getUsernameFromPlayer(EntityPlayer player) {
return player.getEntityWorld().isRemote ? "" : UsernameCache.getLastKnownUsername(getUUIDFromPlayer(player));
}
public static EntityPlayer getPlayerFromUsername(String username) {
public static EntityPlayer getPlayerFromId(UUID uuid) {
if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
return null;
return FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayerByUsername(username);
}
public static EntityPlayer getPlayerFromUUID(String uuid) {
return getPlayerFromUsername(getUsernameFromUUID(uuid));
return FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayerByUUID(uuid);
}
public static EntityPlayer getPlayerFromUUID(UUID uuid) {
return getPlayerFromUsername(getUsernameFromUUID(uuid));
return getPlayerFromId(uuid);
}
public static UUID getUUIDFromPlayer(EntityPlayer player) {
return player.getGameProfile().getId();
}
public static String getUsernameFromUUID(String uuid) {
return UsernameCache.getLastKnownUsername(UUID.fromString(uuid));
}
public static String getUsernameFromUUID(UUID uuid) {
return UsernameCache.getLastKnownUsername(uuid);
}
public static String getUsernameFromStack(ItemStack stack) {
stack = NBTHelper.checkNBT(stack);
return stack.getTagCompound().getString(Constants.NBT.OWNER_NAME);
}
/**
* Checks whether or not the given player is an "actual" player
*
@ -69,23 +46,4 @@ public class PlayerHelper {
public static boolean isFakePlayer(EntityPlayer player) {
return player instanceof FakePlayer || (player != null && knownFakePlayers.contains(player.getClass().getCanonicalName()));
}
public static void causeNauseaToPlayer(ItemStack stack) {
if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
return;
stack = NBTHelper.checkNBT(stack);
if (!Strings.isNullOrEmpty(stack.getTagCompound().getString(Constants.NBT.OWNER_UUID)))
causeNauseaToPlayer(stack.getTagCompound().getString(Constants.NBT.OWNER_UUID));
}
public static void causeNauseaToPlayer(String ownerName) {
EntityPlayer player = getPlayerFromUsername(ownerName);
if (player == null)
return;
player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 80));
}
}