Merge branch '1.9-NetworkChange' into 1.9

This commit is contained in:
Nicholas Ignoffo 2016-06-18 13:11:50 -07:00
commit 7b653a4bb6
40 changed files with 423 additions and 283 deletions

View file

@ -5,6 +5,8 @@ import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.eventhandler.Cancelable;
import net.minecraftforge.fml.common.eventhandler.Event;
import javax.annotation.Nullable;
/**
* Base event class for Soul Network related events.
*
@ -64,6 +66,7 @@ public class SoulNetworkEvent extends Event
@Cancelable
public static class ItemDrainNetworkEvent extends PlayerDrainNetworkEvent
{
@Nullable
public final ItemStack itemStack;
/**
* Amount of damage that would incur if the network could not drain
@ -84,7 +87,7 @@ public class SoulNetworkEvent extends Event
* @param drainAmount
* Original drain amount - change to alter cost
*/
public ItemDrainNetworkEvent(EntityPlayer player, String ownerNetwork, ItemStack itemStack, int drainAmount)
public ItemDrainNetworkEvent(EntityPlayer player, String ownerNetwork, @Nullable ItemStack itemStack, int drainAmount)
{
super(player, ownerNetwork, drainAmount);
this.itemStack = itemStack;

View file

@ -0,0 +1,64 @@
package WayofTime.bloodmagic.api.saving;
import WayofTime.bloodmagic.api.util.helper.PlayerHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.WorldSavedData;
import java.util.*;
public class BMWorldSavedData extends WorldSavedData
{
public static final String ID = "BloodMagic-SoulNetworks";
private Map<UUID, SoulNetwork> soulNetworks = new HashMap<UUID, SoulNetwork>();
public BMWorldSavedData(String id)
{
super(id);
}
public BMWorldSavedData()
{
this(ID);
}
public SoulNetwork getNetwork(EntityPlayer player)
{
return getNetwork(PlayerHelper.getUUIDFromPlayer(player));
}
public SoulNetwork getNetwork(UUID playerId)
{
if (!soulNetworks.containsKey(playerId))
soulNetworks.put(playerId, SoulNetwork.newEmpty(playerId).setParent(this));
return soulNetworks.get(playerId);
}
@Override
public void readFromNBT(NBTTagCompound tagCompound)
{
NBTTagList networkData = tagCompound.getTagList("networkData", 10);
for (int i = 0; i < networkData.tagCount(); i++)
{
NBTTagCompound data = networkData.getCompoundTagAt(i);
SoulNetwork network = SoulNetwork.fromNBT(data);
network.setParent(this);
soulNetworks.put(network.getPlayerId(), network);
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tagCompound)
{
NBTTagList networkData = new NBTTagList();
for (SoulNetwork soulNetwork : soulNetworks.values())
networkData.appendTag(soulNetwork.serializeNBT());
tagCompound.setTag("networkData", networkData);
return tagCompound;
}
}

View file

@ -0,0 +1,220 @@
package WayofTime.bloodmagic.api.saving;
import WayofTime.bloodmagic.api.BloodMagicAPI;
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 lombok.Getter;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.Event;
import javax.annotation.Nullable;
import java.util.UUID;
@Getter
public class SoulNetwork implements INBTSerializable<NBTTagCompound>
{
private BMWorldSavedData parent;
private EntityPlayer cachedPlayer;
private UUID playerId;
private int currentEssence;
private int orbTier;
private SoulNetwork()
{
// No-op - For creation via NBT only
}
public int add(int toAdd, int maximum)
{
AddToNetworkEvent event = new AddToNetworkEvent(playerId.toString(), toAdd, maximum);
if (MinecraftForge.EVENT_BUS.post(event))
return 0;
if (FMLCommonHandler.instance().getMinecraftServerInstance() == null)
return 0;
int currEss = getCurrentEssence();
if (currEss >= event.maximum)
return 0;
int newEss = Math.min(event.maximum, currEss + event.addedAmount);
if (event.getResult() != Event.Result.DENY)
setCurrentEssence(newEss);
return newEss - currEss;
}
/**
* @deprecated - Please use {@link #add(int, int)}
*/
@Deprecated
public int addLifeEssence(int toAdd, int maximum)
{
return add(toAdd, maximum);
}
public int syphon(int syphon)
{
if (getCurrentEssence() >= syphon)
{
setCurrentEssence(getCurrentEssence() - syphon);
return syphon;
}
return 0;
}
public boolean syphonAndDamage(EntityPlayer user, int toSyphon)
{
if (user != null)
{
if (user.worldObj.isRemote)
return false;
if (!Strings.isNullOrEmpty(playerId.toString()))
{
SoulNetworkEvent.ItemDrainNetworkEvent event = new SoulNetworkEvent.ItemDrainNetworkEvent(user, playerId.toString(), null, toSyphon);
if (MinecraftForge.EVENT_BUS.post(event))
return false;
int drainAmount = syphon(event.syphon);
if (drainAmount <= 0 || event.shouldDamage)
hurtPlayer(user, event.syphon);
return event.getResult() != Event.Result.DENY;
}
int amount = syphon(toSyphon);
hurtPlayer(user, toSyphon - amount);
return true;
}
return false;
}
public void causeNausea()
{
if (getPlayer() != null)
getPlayer().addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 99));
}
/**
* @deprecated - Please use {@link #causeNausea()}
*/
@Deprecated
public void causeNauseaToPlayer()
{
causeNausea();
}
public void hurtPlayer(EntityPlayer user, float syphon)
{
if (user != null)
{
if (syphon < 100 && syphon > 0)
{
if (!user.capabilities.isCreativeMode)
{
user.hurtResistantTime = 0;
user.attackEntityFrom(BloodMagicAPI.getDamageSource(), 1.0F);
}
} else if (syphon >= 100)
{
if (!user.capabilities.isCreativeMode)
{
for (int i = 0; i < ((syphon + 99) / 100); i++)
{
user.hurtResistantTime = 0;
user.attackEntityFrom(BloodMagicAPI.getDamageSource(), 1.0F);
}
}
}
}
}
private void markDirty()
{
if (getParent() != null)
getParent().markDirty();
else
BloodMagicAPI.getLogger().error("A SoulNetwork was created, but a parent was not set to allow saving.");
}
@Nullable
public EntityPlayer getPlayer()
{
if (cachedPlayer == null)
cachedPlayer = PlayerHelper.getPlayerFromUUID(playerId);
return cachedPlayer;
}
public SoulNetwork setCurrentEssence(int currentEssence)
{
this.currentEssence = currentEssence;
markDirty();
return this;
}
public SoulNetwork setOrbTier(int orbTier)
{
this.orbTier = orbTier;
markDirty();
return this;
}
public SoulNetwork setParent(BMWorldSavedData parent)
{
this.parent = parent;
markDirty();
return this;
}
// INBTSerializable
@Override
public NBTTagCompound serializeNBT()
{
NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setString("playerId", getPlayerId().toString());
tagCompound.setInteger("currentEssence", getCurrentEssence());
tagCompound.setInteger("orbTier", getOrbTier());
return tagCompound;
}
@Override
public void deserializeNBT(NBTTagCompound nbt)
{
this.playerId = UUID.fromString(nbt.getString("playerId"));
this.currentEssence = nbt.getInteger("currentEssence");
this.orbTier = nbt.getInteger("orbTier");
}
public static SoulNetwork fromNBT(NBTTagCompound tagCompound)
{
SoulNetwork soulNetwork = new SoulNetwork();
soulNetwork.deserializeNBT(tagCompound);
return soulNetwork;
}
public static SoulNetwork newEmpty(UUID uuid)
{
SoulNetwork network = new SoulNetwork();
network.playerId = uuid;
return network;
}
}

View file

@ -1,19 +1,17 @@
package WayofTime.bloodmagic.api.util.helper;
import WayofTime.bloodmagic.api.BloodMagicAPI;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.api.event.AddToNetworkEvent;
import WayofTime.bloodmagic.api.event.SoulNetworkEvent;
import WayofTime.bloodmagic.api.network.SoulNetwork;
import WayofTime.bloodmagic.api.orb.IBloodOrb;
import WayofTime.bloodmagic.api.registry.OrbRegistry;
import WayofTime.bloodmagic.api.saving.BMWorldSavedData;
import WayofTime.bloodmagic.api.saving.SoulNetwork;
import com.google.common.base.Strings;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.Event;
import java.util.UUID;
@ -33,20 +31,18 @@ public class NetworkHelper
public static SoulNetwork getSoulNetwork(String uuid)
{
World world = DimensionManager.getWorld(0);
if (world == null || world.getMapStorage() == null) //Hack-ish way to fix the lava crystal.
if (world == null || world.getMapStorage() == null) //Hack-ish way to fix the lava crystal.
return new BMWorldSavedData().getNetwork(UUID.fromString(uuid));
BMWorldSavedData saveData = (BMWorldSavedData) world.getMapStorage().getOrLoadData(BMWorldSavedData.class, BMWorldSavedData.ID);
if (saveData == null)
{
return new SoulNetwork(uuid);
saveData = new BMWorldSavedData();
world.getMapStorage().setData(BMWorldSavedData.ID, saveData);
}
SoulNetwork network = (SoulNetwork) world.getMapStorage().getOrLoadData(SoulNetwork.class, uuid);
if (network == null)
{
network = new SoulNetwork(uuid);
world.getMapStorage().setData(uuid, network);
}
return network;
return saveData.getNetwork(UUID.fromString(uuid));
}
/**
@ -188,208 +184,5 @@ public class NetworkHelper
public static void setMaxOrb(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.
*
* @param stack
* Owned itemStack
* @param player
* Player using the item
*
* @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;
stack = NBTHelper.checkNBT(stack);
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
if (!Strings.isNullOrEmpty(ownerName))
{
SoulNetworkEvent.ItemDrainNetworkEvent event = new SoulNetworkEvent.ItemDrainNetworkEvent(player, ownerName, stack, syphon);
if (MinecraftForge.EVENT_BUS.post(event))
return false;
int drainAmount = syphonFromNetwork(event.ownerUUID, event.syphon);
if (drainAmount == 0 || event.shouldDamage)
hurtPlayer(player, event.syphon);
// The event has been told to prevent the action but allow all
// repercussions of using the item.
return event.getResult() != Event.Result.DENY;
}
int amount = NetworkHelper.syphonFromNetwork(stack, syphon);
hurtPlayer(player, syphon - amount);
return true;
}
@Deprecated
public static boolean syphonFromNetworkWhileInContainer(ItemStack stack, int syphon)
{
stack = NBTHelper.checkNBT(stack);
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
if (Strings.isNullOrEmpty(ownerName))
return false;
SoulNetworkEvent.ItemDrainInContainerEvent event = new SoulNetworkEvent.ItemDrainInContainerEvent(stack, ownerName, syphon);
if (MinecraftForge.EVENT_BUS.post(event) || event.getResult() == Event.Result.DENY)
return false;
return syphonFromNetwork(event.ownerUUID, event.syphon) >= syphon;
}
@Deprecated
public static int syphonFromNetwork(ItemStack stack, int syphon)
{
stack = NBTHelper.checkNBT(stack);
String ownerName = stack.getTagCompound().getString(Constants.NBT.OWNER_UUID);
if (!Strings.isNullOrEmpty(ownerName))
return syphonFromNetwork(ownerName, syphon);
return 0;
}
@Deprecated
public static int syphonFromNetwork(String ownerName, int syphon)
{
if (FMLCommonHandler.instance().getMinecraftServerInstance() == null)
return 0;
World world = FMLCommonHandler.instance().getMinecraftServerInstance().worldServers[0];
SoulNetwork network = (SoulNetwork) world.loadItemData(SoulNetwork.class, ownerName);
if (network == null)
{
network = new SoulNetwork(ownerName);
world.setItemData(ownerName, network);
}
if (network.getCurrentEssence() >= syphon)
{
network.setCurrentEssence(network.getCurrentEssence() - syphon);
network.markDirty();
return syphon;
}
return 0;
}
// Add
/**
* A method to add to an owner's network up to a maximum value.
*
* @return amount added to the network
*/
@Deprecated
public static int addCurrentEssenceToMaximum(String ownerName, int addedEssence, int maximum)
{
AddToNetworkEvent event = new AddToNetworkEvent(ownerName, addedEssence, maximum);
if (MinecraftForge.EVENT_BUS.post(event))
return 0;
if (FMLCommonHandler.instance().getMinecraftServerInstance() == null)
return 0;
World world = FMLCommonHandler.instance().getMinecraftServerInstance().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;
}
// Get
@Deprecated
public static int getCurrentEssence(String ownerName)
{
if (FMLCommonHandler.instance().getMinecraftServerInstance() == null)
return 0;
World world = FMLCommonHandler.instance().getMinecraftServerInstance().worldServers[0];
SoulNetwork network = (SoulNetwork) world.loadItemData(SoulNetwork.class, ownerName);
if (network == null)
{
network = new SoulNetwork(ownerName);
world.setItemData(ownerName, network);
}
return network.getCurrentEssence();
}
// Do damage
@Deprecated
public static void hurtPlayer(EntityPlayer user, int energySyphoned)
{
if (energySyphoned < 100 && energySyphoned > 0)
{
if (!user.capabilities.isCreativeMode)
{
user.setHealth((user.getHealth() - 1));
if (user.getHealth() <= 0.0005f)
user.onDeath(BloodMagicAPI.getDamageSource());
}
} else if (energySyphoned >= 100)
{
if (!user.capabilities.isCreativeMode)
{
for (int i = 0; i < ((energySyphoned + 99) / 100); i++)
{
user.setHealth((user.getHealth() - 1));
if (user.getHealth() <= 0.0005f)
{
user.onDeath(BloodMagicAPI.getDamageSource());
break;
}
}
}
}
}
@Deprecated
public static void hurtPlayer(EntityPlayer user, float damage)
{
if (!user.capabilities.isCreativeMode)
{
user.attackEntityFrom(BloodMagicAPI.getDamageSource(), 0F);
user.setHealth((user.getHealth() - damage));
}
}
}