Fixed check on unbreakable blocks for Crusher ritual, added event for items draining the SN

This commit is contained in:
WayofTime committed 2014-11-06 21:25:53 -05:00
1 parent 42afd64e30
commit 40a45be05e
12 files changed
+293 -16

No files matched your search

@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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<UUID, String> 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<UUID, String> entry : keyMap.entrySet())
{
System.out.println("" + entry.getValue() + " gave: "+ entry.getKey());
}
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
else
{
keyMap = null;
}
}
}
@@ -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)
{