From e0b073b8b70e8f298c7103206680a42ae82228ee Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 22 Nov 2015 13:43:15 -0800 Subject: [PATCH] Cleanup the SoulNetwork a bit. Should work now? --- .../WayofTime/bloodmagic/api/NBTHolder.java | 2 +- .../api/event/SoulNetworkEvent.java | 2 +- .../bloodmagic/api/network/SoulNetwork.java | 107 +++++++++++++----- .../api/util/helper/NetworkHelper.java | 78 ++++++++----- .../bloodmagic/item/ItemBindable.java | 2 +- .../bloodmagic/item/ItemBloodOrb.java | 4 +- .../item/sigil/ItemSigilDivination.java | 4 +- 7 files changed, 130 insertions(+), 69 deletions(-) diff --git a/src/main/java/WayofTime/bloodmagic/api/NBTHolder.java b/src/main/java/WayofTime/bloodmagic/api/NBTHolder.java index 755f45c4..ec80694c 100644 --- a/src/main/java/WayofTime/bloodmagic/api/NBTHolder.java +++ b/src/main/java/WayofTime/bloodmagic/api/NBTHolder.java @@ -13,7 +13,7 @@ public class NBTHolder { public static final String NBT_COORDX = "xCoord"; public static final String NBT_COORDY = "yCoord"; public static final String NBT_COORDZ = "zCoord"; - public static final String NBT_MAXORB = "maxOrb"; + public static final String NBT_ORBTIER = "orbTier"; public static final String NBT_CURRENTESSENCE = "currentEssence"; public static final String NBT_CURRENTRITUAL = "currentRitual"; public static final String NBT_RUNNING = "isRunning"; diff --git a/src/main/java/WayofTime/bloodmagic/api/event/SoulNetworkEvent.java b/src/main/java/WayofTime/bloodmagic/api/event/SoulNetworkEvent.java index ee2ee53f..e25d2da5 100644 --- a/src/main/java/WayofTime/bloodmagic/api/event/SoulNetworkEvent.java +++ b/src/main/java/WayofTime/bloodmagic/api/event/SoulNetworkEvent.java @@ -7,7 +7,7 @@ import net.minecraftforge.fml.common.eventhandler.Event; public class SoulNetworkEvent extends Event { - public String ownerName; + public final String ownerName; public int syphon; public SoulNetworkEvent(String ownerName, int syphon) { diff --git a/src/main/java/WayofTime/bloodmagic/api/network/SoulNetwork.java b/src/main/java/WayofTime/bloodmagic/api/network/SoulNetwork.java index 3ae84d57..cedd2254 100644 --- a/src/main/java/WayofTime/bloodmagic/api/network/SoulNetwork.java +++ b/src/main/java/WayofTime/bloodmagic/api/network/SoulNetwork.java @@ -2,12 +2,19 @@ package WayofTime.bloodmagic.api.network; import WayofTime.bloodmagic.api.BloodMagicAPI; import WayofTime.bloodmagic.api.NBTHolder; +import WayofTime.bloodmagic.api.event.AddToNetworkEvent; import WayofTime.bloodmagic.api.event.SoulNetworkEvent; import WayofTime.bloodmagic.api.util.helper.PlayerHelper; +import com.google.common.base.Strings; +import com.sun.istack.internal.Nullable; import lombok.Getter; import lombok.Setter; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.potion.Potion; +import net.minecraft.potion.PotionEffect; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.World; import net.minecraft.world.WorldSavedData; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.eventhandler.Event; @@ -17,33 +24,61 @@ import net.minecraftforge.fml.common.eventhandler.Event; public class SoulNetwork extends WorldSavedData { private int currentEssence; - private int maxOrb; + private int orbTier; + @Nullable private final EntityPlayer player; public SoulNetwork(String name) { super(name); currentEssence = 0; - maxOrb = 0; + orbTier = 0; player = PlayerHelper.getPlayerFromUsername(name); } @Override public void readFromNBT(NBTTagCompound nbttagcompound) { currentEssence = nbttagcompound.getInteger(NBTHolder.NBT_CURRENTESSENCE); - maxOrb = nbttagcompound.getInteger(NBTHolder.NBT_MAXORB); + orbTier = nbttagcompound.getInteger(NBTHolder.NBT_ORBTIER); } @Override public void writeToNBT(NBTTagCompound nbttagcompound) { nbttagcompound.setInteger(NBTHolder.NBT_CURRENTESSENCE, currentEssence); - nbttagcompound.setInteger(NBTHolder.NBT_MAXORB, maxOrb); + nbttagcompound.setInteger(NBTHolder.NBT_ORBTIER, orbTier); + } + + public int addLifeEssence(int toAdd, int maximum) { + AddToNetworkEvent event = new AddToNetworkEvent(mapName, toAdd, maximum); + + if(MinecraftForge.EVENT_BUS.post(event)) + return 0; + + if (MinecraftServer.getServer() == null) + return 0; + + World world = MinecraftServer.getServer().worldServers[0]; + SoulNetwork data = (SoulNetwork) world.loadItemData(SoulNetwork.class, event.ownerNetwork); + + if (data == null) { + data = new SoulNetwork(event.ownerNetwork); + world.setItemData(event.ownerNetwork, data); + } + + int currEss = data.getCurrentEssence(); + + if (currEss >= event.maximum) + return 0; + + int newEss = Math.min(event.maximum, currEss + event.addedAmount); + if(event.getResult() != Event.Result.DENY) + data.setCurrentEssence(newEss); + + return newEss - currEss; } /** - * Used to syphon LP from the network. - * - * @return - . + * Used to syphon LP from the network */ public int syphon(int syphon) { if (getCurrentEssence() >= syphon) { @@ -55,44 +90,54 @@ public class SoulNetwork extends WorldSavedData { return 0; } + /** + * If the player exists on the server, syphon the given amount of LP from the player's LP network and + * damage for any remaining LP required. + * + * Always returns false on the client side. + * + * @return - Whether the action should be performed. + */ public boolean syphonAndDamage(int toSyphon) { -// SoulNetworkEvent.PlayerDrainNetworkEvent event = new SoulNetworkEvent.PlayerDrainNetworkEvent(getPlayer(), mapName, syphon); -// -// if (MinecraftForge.EVENT_BUS.post(event)) -// return false; -// -// syphon(syphon); -// int drain = Math.max(0, getCurrentEssence() - syphon); -// -// if (drain == 0 || event.shouldDamage) -// hurtPlayer(event.syphon); -// -// return event.getResult() != Event.Result.DENY; - - if (player.worldObj.isRemote) + if (getPlayer().worldObj.isRemote) return false; - int drain = syphon(toSyphon); - hurtPlayer(toSyphon - drain); + if (!Strings.isNullOrEmpty(mapName)) { + SoulNetworkEvent.ItemDrainNetworkEvent event = new SoulNetworkEvent.ItemDrainNetworkEvent(player, mapName, getPlayer().getHeldItem(), toSyphon); + + if(MinecraftForge.EVENT_BUS.post(event)) + return false; + + int drainAmount = syphon(event.syphon); + + if(drainAmount == 0 || event.shouldDamage) + hurtPlayer(event.syphon); + + return event.getResult() != Event.Result.DENY; + } + + int amount = syphon(toSyphon); + hurtPlayer(toSyphon - amount); return true; } public void hurtPlayer(int syphon) { + getPlayer().addPotionEffect(new PotionEffect(Potion.confusion.getId(), 20)); if (syphon < 100 && syphon > 0) { - if (!player.capabilities.isCreativeMode) { - player.setHealth((player.getHealth() - 1)); + if (!getPlayer().capabilities.isCreativeMode) { + getPlayer().setHealth((getPlayer().getHealth() - 1)); - if (player.getHealth() <= 0.0005f) - player.onDeath(BloodMagicAPI.getDamageSource()); + if (getPlayer().getHealth() <= 0.0005f) + getPlayer().onDeath(BloodMagicAPI.getDamageSource()); } } else if (syphon >= 100) { - if (!player.capabilities.isCreativeMode) { + if (!getPlayer().capabilities.isCreativeMode) { for (int i = 0; i < ((syphon + 99) / 100); i++) { - player.setHealth((player.getHealth() - 1)); + getPlayer().setHealth((getPlayer().getHealth() - 1)); - if (player.getHealth() <= 0.0005f) { - player.onDeath(BloodMagicAPI.getDamageSource()); + if (getPlayer().getHealth() <= 0.0005f) { + getPlayer().onDeath(BloodMagicAPI.getDamageSource()); break; } } diff --git a/src/main/java/WayofTime/bloodmagic/api/util/helper/NetworkHelper.java b/src/main/java/WayofTime/bloodmagic/api/util/helper/NetworkHelper.java index 69eb9562..66c23679 100644 --- a/src/main/java/WayofTime/bloodmagic/api/util/helper/NetworkHelper.java +++ b/src/main/java/WayofTime/bloodmagic/api/util/helper/NetworkHelper.java @@ -28,8 +28,47 @@ public class NetworkHelper { return network; } + public static int getCurrentMaxOrb(SoulNetwork soulNetwork) { + return soulNetwork.getOrbTier(); + } + // Syphon + /** + * Handles null-checking the player for you. + * + * @return - Whether the action should be performed. + */ + public static boolean syphonAndDamage(SoulNetwork soulNetwork, int toSyphon) { + if (soulNetwork.getPlayer() == null) { + soulNetwork.syphon(toSyphon); + return true; + } + + return soulNetwork.syphonAndDamage(toSyphon); + } + + public static boolean syphonFromContainer(ItemStack stack, SoulNetwork soulNetwork, int toSyphon) { + stack = NBTHolder.checkNBT(stack); + String ownerName = stack.getTagCompound().getString(NBTHolder.NBT_OWNER); + + if (Strings.isNullOrEmpty(ownerName)) + return false; + + SoulNetworkEvent.ItemDrainInContainerEvent event = new SoulNetworkEvent.ItemDrainInContainerEvent(stack, ownerName, toSyphon); + + return !(MinecraftForge.EVENT_BUS.post(event) || event.getResult() == Event.Result.DENY) && soulNetwork.syphon(event.syphon) >= toSyphon; + } + + // Set + + public static void setMaxOrbToMax(SoulNetwork soulNetwork, int maxOrb) { + soulNetwork.setOrbTier(Math.max(maxOrb, soulNetwork.getOrbTier())); + soulNetwork.markDirty(); + } + + // TODO - Remove everything below. It is deprecated and should not be used. + /** * Master method used to syphon from the player's network, and will damage them accordingly if they do not have enough LP. * Does not drain on the client side. @@ -39,6 +78,7 @@ public class NetworkHelper { * @param syphon * @return True if the action should be executed and false if it should not. Always returns false if client-sided. */ + @Deprecated public static boolean syphonAndDamageFromNetwork(ItemStack stack, EntityPlayer player, int syphon) { if (player.worldObj.isRemote) return false; @@ -68,6 +108,7 @@ public class NetworkHelper { return true; } + @Deprecated public static boolean syphonFromNetworkWhileInContainer(ItemStack stack, int syphon) { stack = NBTHolder.checkNBT(stack); String ownerName = stack.getTagCompound().getString(NBTHolder.NBT_OWNER); @@ -83,6 +124,7 @@ public class NetworkHelper { return syphonFromNetwork(event.ownerName, event.syphon) >= syphon; } + @Deprecated public static int syphonFromNetwork(ItemStack stack, int syphon) { stack = NBTHolder.checkNBT(stack); String ownerName = stack.getTagCompound().getString(NBTHolder.NBT_OWNER); @@ -92,6 +134,7 @@ public class NetworkHelper { return 0; } + @Deprecated public static int syphonFromNetwork(String ownerName, int syphon) { if (MinecraftServer.getServer() == null) return 0; @@ -120,6 +163,7 @@ public class NetworkHelper { * * @return amount added to the network */ + @Deprecated public static int addCurrentEssenceToMaximum(String ownerName, int addedEssence, int maximum) { AddToNetworkEvent event = new AddToNetworkEvent(ownerName, addedEssence, maximum); @@ -151,6 +195,7 @@ public class NetworkHelper { // Get + @Deprecated public static int getCurrentEssence(String ownerName) { if (MinecraftServer.getServer() == null) return 0; @@ -168,6 +213,7 @@ public class NetworkHelper { // Do damage + @Deprecated public static void hurtPlayer(EntityPlayer user, int energySyphoned) { if (energySyphoned < 100 && energySyphoned > 0) { if (!user.capabilities.isCreativeMode) { @@ -190,41 +236,11 @@ public class NetworkHelper { } } + @Deprecated public static void hurtPlayer(EntityPlayer user, float damage) { if (!user.capabilities.isCreativeMode) { user.attackEntityFrom(BloodMagicAPI.getDamageSource(), 0F); user.setHealth((user.getHealth() - damage)); } } - - public static void setMaxOrbToMax(String ownerName, int maxOrb) { - if (MinecraftServer.getServer() == null) - return; - - World world = MinecraftServer.getServer().worldServers[0]; - SoulNetwork network = (SoulNetwork) world.loadItemData(SoulNetwork.class, ownerName); - - if (network == null) { - network = new SoulNetwork(ownerName); - world.setItemData(ownerName, network); - } - - network.setMaxOrb(Math.max(maxOrb, network.getMaxOrb())); - network.markDirty(); - } - - public static int getCurrentMaxOrb(String ownerName) { - if (MinecraftServer.getServer() == null) - return 0; - - World world = MinecraftServer.getServer().worldServers[0]; - SoulNetwork network = (SoulNetwork) world.loadItemData(SoulNetwork.class, ownerName); - - if (network == null) { - network = new SoulNetwork(ownerName); - world.setItemData(ownerName, network); - } - - return network.getMaxOrb(); - } } diff --git a/src/main/java/WayofTime/bloodmagic/item/ItemBindable.java b/src/main/java/WayofTime/bloodmagic/item/ItemBindable.java index 01546785..71489232 100644 --- a/src/main/java/WayofTime/bloodmagic/item/ItemBindable.java +++ b/src/main/java/WayofTime/bloodmagic/item/ItemBindable.java @@ -58,7 +58,7 @@ public class ItemBindable extends Item implements IBindable { public static boolean syphonBatteries(ItemStack stack, EntityPlayer player, int damageToBeDone) { if (!player.worldObj.isRemote) { - return NetworkHelper.getSoulNetwork(BindableHelper.getOwnerName(stack), player.worldObj).syphonAndDamage(damageToBeDone); + return NetworkHelper.syphonAndDamage(NetworkHelper.getSoulNetwork(BindableHelper.getOwnerName(stack), player.worldObj), damageToBeDone); } else { double posX = player.posX; double posY = player.posY; diff --git a/src/main/java/WayofTime/bloodmagic/item/ItemBloodOrb.java b/src/main/java/WayofTime/bloodmagic/item/ItemBloodOrb.java index 710f0066..91499b67 100644 --- a/src/main/java/WayofTime/bloodmagic/item/ItemBloodOrb.java +++ b/src/main/java/WayofTime/bloodmagic/item/ItemBloodOrb.java @@ -63,9 +63,9 @@ public class ItemBloodOrb extends ItemBindable implements IBloodOrb, IBindable { return stack; if(stack.getTagCompound().getString(NBTHolder.NBT_OWNER).equals(PlayerHelper.getUsernameFromPlayer(player))) - NetworkHelper.setMaxOrbToMax(stack.getTagCompound().getString(NBTHolder.NBT_OWNER), getOrbLevel(stack.getItemDamage())); + NetworkHelper.setMaxOrbToMax(NetworkHelper.getSoulNetwork(stack.getTagCompound().getString(NBTHolder.NBT_OWNER), world), getOrbLevel(stack.getItemDamage())); - NetworkHelper.addCurrentEssenceToMaximum(stack.getTagCompound().getString(NBTHolder.NBT_OWNER), 200, getMaxEssence(stack.getItemDamage())); + NetworkHelper.getSoulNetwork(stack.getTagCompound().getString(NBTHolder.NBT_OWNER), world).addLifeEssence(200, getMaxEssence(stack.getItemDamage())); hurtPlayer(player, 200); return stack; } diff --git a/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilDivination.java b/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilDivination.java index 1bf51737..84eb3414 100644 --- a/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilDivination.java +++ b/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilDivination.java @@ -24,9 +24,9 @@ public class ItemSigilDivination extends ItemSigilBase implements ISigil, IAltar public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { super.onItemRightClick(stack, world, player); - if (!world.isRemote && syphonBatteries(stack, player, getEnergyUsed())) { + if (!world.isRemote) { MovingObjectPosition position = getMovingObjectPositionFromPlayer(world, player, false); - int currentEssence = NetworkHelper.getCurrentEssence(BindableHelper.getOwnerName(stack)); + int currentEssence = NetworkHelper.getSoulNetwork(BindableHelper.getOwnerName(stack), world).getCurrentEssence(); if (position == null) { ChatUtil.sendNoSpam(player, new ChatComponentText(TextHelper.localize(tooltipBase + "currentEssence", currentEssence)));