From 40a45be05ef3175cebfe6f929c853d067f9cfcf0 Mon Sep 17 00:00:00 2001 From: WayofTime Date: Thu, 6 Nov 2014 21:25:53 -0500 Subject: [PATCH] Fixed check on unbreakable blocks for Crusher ritual, added event for items draining the SN --- .../AlchemicalWizardry.java | 5 + .../api/event/ItemDrainNetworkEvent.java | 29 ++++ .../api/event/PlayerDrainNetworkEvent.java | 21 +++ .../api/event/SoulNetworkEvent.java | 27 ++++ .../soulNetwork/ComplexNetworkHandler.java | 129 ++++++++++++++++++ .../api/soulNetwork/SoulNetworkHandler.java | 52 ++++++- .../common/items/BoundArmour.java | 4 +- .../common/items/BoundAxe.java | 12 +- .../common/items/BoundPickaxe.java | 11 +- .../common/items/BoundShovel.java | 13 +- .../common/items/EnergyItems.java | 2 +- .../common/rituals/RitualEffectCrushing.java | 4 + 12 files changed, 293 insertions(+), 16 deletions(-) create mode 100644 src/main/java/WayofTime/alchemicalWizardry/api/event/ItemDrainNetworkEvent.java create mode 100644 src/main/java/WayofTime/alchemicalWizardry/api/event/PlayerDrainNetworkEvent.java create mode 100644 src/main/java/WayofTime/alchemicalWizardry/api/event/SoulNetworkEvent.java create mode 100644 src/main/java/WayofTime/alchemicalWizardry/api/soulNetwork/ComplexNetworkHandler.java diff --git a/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java b/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java index c603c20a..618ef3f6 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java +++ b/src/main/java/WayofTime/alchemicalWizardry/AlchemicalWizardry.java @@ -10,6 +10,7 @@ import WayofTime.alchemicalWizardry.api.harvest.HarvestRegistry; import WayofTime.alchemicalWizardry.api.items.ShapedBloodOrbRecipe; import WayofTime.alchemicalWizardry.api.items.ShapelessBloodOrbRecipe; import WayofTime.alchemicalWizardry.api.rituals.Rituals; +import WayofTime.alchemicalWizardry.api.soulNetwork.ComplexNetworkHandler; import WayofTime.alchemicalWizardry.api.summoningRegistry.SummoningRegistry; import WayofTime.alchemicalWizardry.common.*; import WayofTime.alchemicalWizardry.common.alchemy.CombinedPotionRegistry; @@ -67,8 +68,10 @@ import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.RecipeSorter; import net.minecraftforge.oredict.RecipeSorter.Category; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; + import thaumcraft.api.ItemApi; import thaumcraft.api.ThaumcraftApi; import thaumcraft.api.aspects.Aspect; @@ -274,6 +277,8 @@ public class AlchemicalWizardry TEDemonPortal.loadBuildingList(); + ComplexNetworkHandler.load(); + MinecraftForge.EVENT_BUS.register(new LifeBucketHandler()); BloodMagicConfiguration.init(new File(event.getModConfigurationDirectory(), "AWWayofTime.cfg")); diff --git a/src/main/java/WayofTime/alchemicalWizardry/api/event/ItemDrainNetworkEvent.java b/src/main/java/WayofTime/alchemicalWizardry/api/event/ItemDrainNetworkEvent.java new file mode 100644 index 00000000..d75fd7dc --- /dev/null +++ b/src/main/java/WayofTime/alchemicalWizardry/api/event/ItemDrainNetworkEvent.java @@ -0,0 +1,29 @@ +package WayofTime.alchemicalWizardry.api.event; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import cpw.mods.fml.common.eventhandler.Cancelable; + +@Cancelable +public class ItemDrainNetworkEvent extends PlayerDrainNetworkEvent +{ + public final ItemStack itemStack; + public boolean shouldDamage; //If true, will damage regardless of if the network had enough inside it + public float damageAmount; //Amount of damage that would incur if the network could not drain properly + + /** + * Set result to deny the action i.e. damage/drain anyways. Cancelling event prevents action without penalties + * + * @param player Player using the item + * @param ownerNetwork Network that the item is tied to + * @param itemStack Item used + * @param drainAmount Original drain amount - change to alter cost + */ + public ItemDrainNetworkEvent(EntityPlayer player, String ownerNetwork, ItemStack itemStack, int drainAmount) + { + super(player, ownerNetwork, drainAmount); + this.itemStack = itemStack; + this.shouldDamage = false; + this.damageAmount = (float)(drainAmount) / 100.0f; + } +} \ No newline at end of file diff --git a/src/main/java/WayofTime/alchemicalWizardry/api/event/PlayerDrainNetworkEvent.java b/src/main/java/WayofTime/alchemicalWizardry/api/event/PlayerDrainNetworkEvent.java new file mode 100644 index 00000000..d339d20b --- /dev/null +++ b/src/main/java/WayofTime/alchemicalWizardry/api/event/PlayerDrainNetworkEvent.java @@ -0,0 +1,21 @@ +package WayofTime.alchemicalWizardry.api.event; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import cpw.mods.fml.common.eventhandler.Cancelable; + +@Cancelable +public class PlayerDrainNetworkEvent extends SoulNetworkEvent +{ + public final EntityPlayer player; //Player that activated the event + public PlayerDrainNetworkEvent(EntityPlayer player, String ownerNetwork, int drainAmount) + { + super(ownerNetwork, drainAmount); + this.player = player; + } + + public EntityPlayer getPlayer() + { + return player; + } +} diff --git a/src/main/java/WayofTime/alchemicalWizardry/api/event/SoulNetworkEvent.java b/src/main/java/WayofTime/alchemicalWizardry/api/event/SoulNetworkEvent.java new file mode 100644 index 00000000..fccf4d5e --- /dev/null +++ b/src/main/java/WayofTime/alchemicalWizardry/api/event/SoulNetworkEvent.java @@ -0,0 +1,27 @@ +package WayofTime.alchemicalWizardry.api.event; + +import net.minecraft.entity.player.EntityPlayer; +import cpw.mods.fml.common.eventhandler.Event; + +public class SoulNetworkEvent extends Event +{ + public String ownerNetwork; + public int drainAmount; + + public SoulNetworkEvent(String ownerNetwork, int drainAmount) + { + super(); + this.ownerNetwork = ownerNetwork; + this.drainAmount = drainAmount; + } + + public String getOwnerNetwork() + { + return this.ownerNetwork; + } + + public int getDrainAmount() + { + return this.drainAmount; + } +} diff --git a/src/main/java/WayofTime/alchemicalWizardry/api/soulNetwork/ComplexNetworkHandler.java b/src/main/java/WayofTime/alchemicalWizardry/api/soulNetwork/ComplexNetworkHandler.java new file mode 100644 index 00000000..00c799e7 --- /dev/null +++ b/src/main/java/WayofTime/alchemicalWizardry/api/soulNetwork/ComplexNetworkHandler.java @@ -0,0 +1,129 @@ +package WayofTime.alchemicalWizardry.api.soulNetwork; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.util.HashMap; +import java.util.Map.Entry; +import java.util.UUID; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.World; +import WayofTime.alchemicalWizardry.AlchemicalWizardry; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.mojang.authlib.GameProfile; + + +/** + * Temporary class to hash-out how to create a network not completely tied to the player. + */ +public class ComplexNetworkHandler +{ + public static String fileName = "config/BloodMagic/soulnetworkKeys"; + static HashMap keyMap = new HashMap(); + public static UUID getUUIDFromPlayer(EntityPlayer player) + { + return player.getPersistentID(); + } + + public static EntityPlayer getPlayerFromUUID(UUID uuid) + { + MinecraftServer server = MinecraftServer.getServer(); + GameProfile gameProfile; + gameProfile = server.func_152358_ax().func_152652_a(uuid); + String str = uuid.toString(); + //TODO ServerConfigurationManager d.createPlayerForUser + UUID.fromString(str); + return null; + } + + public static String getKeyForPlayer(EntityPlayer player) + { + return ""; + } + + public static UUID getUUIDForKey(String key) + { +// if (MinecraftServer.getServer() == null) +// { +// return null; +// } +// +// World world = MinecraftServer.getServer().worldServers[0]; +// UUIDKeyMap data = (UUIDKeyMap) world.loadItemData(UUIDKeyMap.class, key); +// +// if (data == null) +// { +// data = new UUIDKeyMap(key); +// world.setItemData(key, data); +// } + return null; + } + + public static String assignKeyToPlayer(EntityPlayer player) + { + return ""; + } + + public static void save() + { + keyMap.put(new UUID(0, 0), "test"); + + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String json = gson.toJson(keyMap); + AlchemicalWizardry.logger.info("Here, too!"); + Writer writer; + try + { + writer = new FileWriter(fileName + ".json"); + writer.write(json); + writer.close(); + } catch (IOException e) + { + e.printStackTrace(); + } + } + + public static void load() + { + File save = new File(fileName + ".json"); + + if(save.canRead()) + { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + BufferedReader br; + + try + { + br = new BufferedReader(new FileReader(save)); + HashMap schema = gson.fromJson(br, keyMap.getClass()); + + keyMap = schema; + + if(keyMap != null) + { + for(Entry entry : keyMap.entrySet()) + { + System.out.println("" + entry.getValue() + " gave: "+ entry.getKey()); + } + } + } catch (FileNotFoundException e) + { + e.printStackTrace(); + } + } + + else + { + keyMap = null; + } + } +} diff --git a/src/main/java/WayofTime/alchemicalWizardry/api/soulNetwork/SoulNetworkHandler.java b/src/main/java/WayofTime/alchemicalWizardry/api/soulNetwork/SoulNetworkHandler.java index 88c71edd..f8820dfa 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/api/soulNetwork/SoulNetworkHandler.java +++ b/src/main/java/WayofTime/alchemicalWizardry/api/soulNetwork/SoulNetworkHandler.java @@ -1,6 +1,7 @@ package WayofTime.alchemicalWizardry.api.soulNetwork; -import com.mojang.authlib.GameProfile; +import java.util.UUID; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -9,8 +10,12 @@ import net.minecraft.potion.PotionEffect; import net.minecraft.server.MinecraftServer; import net.minecraft.util.DamageSource; import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; +import WayofTime.alchemicalWizardry.api.event.ItemDrainNetworkEvent; -import java.util.UUID; +import com.mojang.authlib.GameProfile; + +import cpw.mods.fml.common.eventhandler.Event; public class SoulNetworkHandler { @@ -71,18 +76,38 @@ public class SoulNetworkHandler * @param ist Owned itemStack * @param player Player using the item * @param damageToBeDone - * @return True if server-sided, false if client-sided + * @return True if the action should be executed and false if it should not. Always returns false if client-sided. */ - public static boolean syphonAndDamageFromNetwork(ItemStack ist, EntityPlayer player, int damageToBeDone) + public static boolean syphonAndDamageFromNetwork(ItemStack ist, EntityPlayer player, int drain) { - if (player.worldObj.isRemote) + if (player.worldObj.isRemote) { return false; } + + if (ist.getTagCompound() != null && !(ist.getTagCompound().getString("ownerName").equals(""))) + { + String ownerName = ist.getTagCompound().getString("ownerName"); + + ItemDrainNetworkEvent event = new ItemDrainNetworkEvent(player, ownerName, ist, drain); + + if(MinecraftForge.EVENT_BUS.post(event)) + { + return false; + } + + int drainAmount = syphonFromNetwork(event.ownerNetwork, event.drainAmount); + if(drainAmount == 0 || event.shouldDamage) + { + hurtPlayer(player, event.damageAmount); + } + + return (event.getResult() != Event.Result.DENY); //The event has been told to prevent the action but allow all repercussions of using the item. + } - int amount = SoulNetworkHandler.syphonFromNetwork(ist, damageToBeDone); + int amount = SoulNetworkHandler.syphonFromNetwork(ist, drain); - hurtPlayer(player, damageToBeDone - amount); + hurtPlayer(player, drain - amount); return true; } @@ -238,6 +263,19 @@ public class SoulNetworkHandler } } } + + public static void hurtPlayer(EntityPlayer user, float damage) + { + if (!user.capabilities.isCreativeMode) + { + user.setHealth((user.getHealth() - damage)); + + if (user.getHealth() <= 0.0005f) + { + user.onDeath(DamageSource.generic); + } + } + } public static void checkAndSetItemOwner(ItemStack item, EntityPlayer player) { diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundArmour.java b/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundArmour.java index 6a64bfee..7459c74f 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundArmour.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundArmour.java @@ -375,8 +375,8 @@ public class BoundArmour extends ItemArmor implements IAlchemyGoggles, ISpecialA if (!player.capabilities.isCreativeMode) { - EnergyItems.syphonBatteries(itemStack, player, itemStack.getItemDamage() * 75); - itemStack.setItemDamage(0); + if( EnergyItems.syphonBatteries(itemStack, player, itemStack.getItemDamage() * 75)) + itemStack.setItemDamage(0); } } diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundAxe.java b/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundAxe.java index 04eb4386..0c5512eb 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundAxe.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundAxe.java @@ -123,6 +123,11 @@ public class BoundAxe extends ItemAxe implements IBindable { return par1ItemStack; } + + if(!EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 10000)) + { + return par1ItemStack; + } Vec3 blockVec = SpellHelper.getEntityBlockVector(par3EntityPlayer); int posX = (int) (blockVec.xCoord); @@ -176,7 +181,7 @@ public class BoundAxe extends ItemAxe implements IBindable } } - EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 10000); + return par1ItemStack; } @@ -198,7 +203,10 @@ public class BoundAxe extends ItemAxe implements IBindable { if (!par3EntityPlayer.capabilities.isCreativeMode) { - EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 20); + if(!EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 20)) + { + this.setActivated(par1ItemStack, false); + } } } diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundPickaxe.java b/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundPickaxe.java index 3d85614e..41936178 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundPickaxe.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundPickaxe.java @@ -130,6 +130,11 @@ public class BoundPickaxe extends ItemPickaxe implements IBindable { return par1ItemStack; } + + if(!EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 10000)) + { + return par1ItemStack; + } Vec3 blockVec = SpellHelper.getEntityBlockVector(par3EntityPlayer); int posX = (int) (blockVec.xCoord); @@ -184,7 +189,6 @@ public class BoundPickaxe extends ItemPickaxe implements IBindable } } - EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 10000); return par1ItemStack; } @@ -207,7 +211,10 @@ public class BoundPickaxe extends ItemPickaxe implements IBindable { if (!par3EntityPlayer.capabilities.isCreativeMode) { - EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 20); + if(!EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 20)) + { + this.setActivated(par1ItemStack, false); + } } } diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundShovel.java b/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundShovel.java index feffcb7c..ec29b98d 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundShovel.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/items/BoundShovel.java @@ -3,7 +3,9 @@ package WayofTime.alchemicalWizardry.common.items; import WayofTime.alchemicalWizardry.AlchemicalWizardry; import WayofTime.alchemicalWizardry.api.items.interfaces.IBindable; import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper; + import com.google.common.collect.Multimap; + import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.block.Block; @@ -127,6 +129,11 @@ public class BoundShovel extends ItemSpade implements IBindable { return par1ItemStack; } + + if(!EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 10000)) + { + return par1ItemStack; + } Vec3 blockVec = SpellHelper.getEntityBlockVector(par3EntityPlayer); int posX = (int) (blockVec.xCoord); @@ -181,7 +188,6 @@ public class BoundShovel extends ItemSpade implements IBindable } } - EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 10000); return par1ItemStack; } @@ -203,7 +209,10 @@ public class BoundShovel extends ItemSpade implements IBindable { if (!par3EntityPlayer.capabilities.isCreativeMode) { - EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 20); + if(!EnergyItems.syphonBatteries(par1ItemStack, par3EntityPlayer, 20)) + { + this.setActivated(par1ItemStack, false); + } } } diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/items/EnergyItems.java b/src/main/java/WayofTime/alchemicalWizardry/common/items/EnergyItems.java index 13d4ebf1..1416afd9 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/items/EnergyItems.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/items/EnergyItems.java @@ -66,7 +66,7 @@ public class EnergyItems extends Item implements IBindable { if (!player.worldObj.isRemote) { - return syphonAndDamageWhileInContainer(ist, player, damageToBeDone); + return SoulNetworkHandler.syphonAndDamageFromNetwork(ist, player, damageToBeDone); } else { World world = player.worldObj; diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/rituals/RitualEffectCrushing.java b/src/main/java/WayofTime/alchemicalWizardry/common/rituals/RitualEffectCrushing.java index ce0cc54c..7d4eb890 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/rituals/RitualEffectCrushing.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/rituals/RitualEffectCrushing.java @@ -109,6 +109,10 @@ public class RitualEffectCrushing extends RitualEffect { Block block = world.getBlock(x + i, y + j, z + k); int meta = world.getBlockMetadata(x + i, y + j, z + k); + if(block.getBlockHardness(world, x + i, y + j, z + k) == -1) + { + continue; + } if (block != null && !world.isAirBlock(x + i, y + j, z + k)) {