Merge branch '1.8-Rewrite' of https://github.com/WayofTime/BloodMagic into 1.8-Rewrite

This commit is contained in:
Arcaratus 2015-11-27 20:15:47 -05:00
commit 08e7844df1
15 changed files with 388 additions and 84 deletions

View file

@ -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";

View file

@ -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) {

View file

@ -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;
}
}

View file

@ -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();
}
}