BloodMagic/src/main/java/WayofTime/bloodmagic/util/helper/NetworkHelper.java

168 lines
5.5 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.util.helper;
import WayofTime.bloodmagic.core.data.Binding;
import WayofTime.bloodmagic.iface.IBindable;
import WayofTime.bloodmagic.event.SoulNetworkEvent;
import WayofTime.bloodmagic.orb.BloodOrb;
import WayofTime.bloodmagic.orb.IBloodOrb;
import WayofTime.bloodmagic.core.registry.OrbRegistry;
import WayofTime.bloodmagic.core.data.BMWorldSavedData;
import WayofTime.bloodmagic.core.data.SoulNetwork;
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 java.util.UUID;
2017-08-15 21:30:48 -07:00
public class NetworkHelper {
// Get
2015-12-01 21:55:56 -08:00
/**
* Gets the SoulNetwork for the player.
2017-08-15 21:30:48 -07:00
*
* @param uuid - The UUID of the SoulNetwork owner - this is UUID.toString().
2015-12-01 21:55:56 -08:00
* @return - The SoulNetwork for the given name.
*/
2017-08-15 21:30:48 -07:00
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.
return new BMWorldSavedData().getNetwork(UUID.fromString(uuid));
BMWorldSavedData saveData = (BMWorldSavedData) world.getMapStorage().getOrLoadData(BMWorldSavedData.class, BMWorldSavedData.ID);
2017-08-15 21:30:48 -07:00
if (saveData == null) {
saveData = new BMWorldSavedData();
world.getMapStorage().setData(BMWorldSavedData.ID, saveData);
}
return saveData.getNetwork(UUID.fromString(uuid));
}
/**
2017-08-15 21:30:48 -07:00
* @param uuid - The Player's Mojang UUID
2016-01-01 13:14:49 -05:00
* @see NetworkHelper#getSoulNetwork(String)
*/
2017-08-15 21:30:48 -07:00
public static SoulNetwork getSoulNetwork(UUID uuid) {
2016-01-01 18:54:39 -08:00
return getSoulNetwork(uuid.toString());
}
/**
2017-08-15 21:30:48 -07:00
* @param player - The Player
2016-01-01 13:14:49 -05:00
* @see NetworkHelper#getSoulNetwork(String)
*/
2017-08-15 21:30:48 -07:00
public static SoulNetwork getSoulNetwork(EntityPlayer player) {
return getSoulNetwork(PlayerHelper.getUUIDFromPlayer(player));
}
public static SoulNetwork getSoulNetwork(Binding binding) {
return getSoulNetwork(binding.getOwnerId());
}
2015-12-01 21:55:56 -08:00
/**
* Gets the current orb tier of the SoulNetwork.
2017-08-15 21:30:48 -07:00
*
* @param soulNetwork - SoulNetwork to get the tier of.
2015-12-01 21:55:56 -08:00
* @return - The Orb tier of the given SoulNetwork
*/
2017-08-15 21:30:48 -07:00
public static int getCurrentMaxOrb(SoulNetwork soulNetwork) {
return soulNetwork.getOrbTier();
}
2017-08-15 21:30:48 -07:00
public static int getMaximumForTier(int tier) {
int ret = 0;
if (tier > OrbRegistry.getTierMap().size() || tier < 0)
return ret;
2017-08-15 20:21:54 -07:00
for (ItemStack orbStack : OrbRegistry.getOrbsForTier(tier)) {
BloodOrb orb = ((IBloodOrb) orbStack.getItem()).getOrb(orbStack);
if (orb.getCapacity() > ret)
ret = orb.getCapacity();
}
return ret;
}
// Syphon
/**
* Syphons from the player and damages them if there was not enough stored
* LP.
2017-08-15 21:30:48 -07:00
* <p>
* Handles null-checking the player for you.
2017-08-15 21:30:48 -07:00
*
* @param soulNetwork - SoulNetwork to syphon from
* @param user - User of the item.
* @param toSyphon - Amount of LP to syphon
* @return - Whether the action should be performed.
* @deprecated Use {@link #getSoulNetwork(EntityPlayer)} and {@link SoulNetwork#syphonAndDamage(EntityPlayer, int)}
*/
@Deprecated
2017-08-15 21:30:48 -07:00
public static boolean syphonAndDamage(SoulNetwork soulNetwork, EntityPlayer user, int toSyphon) {
// if (soulNetwork.getNewOwner() == null)
// {
// soulNetwork.syphon(toSyphon);
// return true;
// }
return soulNetwork.syphonAndDamage(user, toSyphon);
}
2015-12-01 21:55:56 -08:00
/**
* Syphons a player from within a container.
2017-08-15 21:30:48 -07:00
*
* @param stack - ItemStack in the Container.
* @param toSyphon - Amount of LP to syphon
2015-12-01 21:55:56 -08:00
* @return - If the syphon was successful.
*/
public static boolean syphonFromContainer(ItemStack stack, int toSyphon)
{
if (!(stack.getItem() instanceof IBindable))
return false;
Binding binding = ((IBindable) stack.getItem()).getBinding(stack);
if (binding == null)
return false;
SoulNetwork network = getSoulNetwork(binding);
SoulNetworkEvent.ItemDrainInContainerEvent event = new SoulNetworkEvent.ItemDrainInContainerEvent(stack, binding.getOwnerId(), toSyphon);
return !MinecraftForge.EVENT_BUS.post(event) && network.syphon(event.syphon) >= toSyphon;
}
2016-01-01 10:52:42 -08:00
/**
* Checks if the ItemStack has a user to be syphoned from.
2017-08-15 21:30:48 -07:00
*
* @param stack - ItemStack to check
* @param toSyphon - Amount of LP to syphon
2016-01-01 10:52:42 -08:00
* @return - If syphoning is possible
*/
2017-08-15 21:30:48 -07:00
public static boolean canSyphonFromContainer(ItemStack stack, int toSyphon) {
if (!(stack.getItem() instanceof IBindable))
return false;
Binding binding = ((IBindable) stack.getItem()).getBinding(stack);
if (binding == null)
return false;
SoulNetwork network = getSoulNetwork(binding);
return network.getCurrentEssence() >= toSyphon;
}
// Set
2015-12-01 21:55:56 -08:00
/**
* Sets the orb tier of the SoulNetwork to the given orb. Will not set if
* the given tier is lower than the current tier.
2017-08-15 21:30:48 -07:00
*
* @param soulNetwork - SoulNetwork to set the orb tier of
* @param maxOrb - Tier of orb to set to
2015-12-01 21:55:56 -08:00
*/
2017-08-15 21:30:48 -07:00
public static void setMaxOrb(SoulNetwork soulNetwork, int maxOrb) {
soulNetwork.setOrbTier(Math.max(maxOrb, soulNetwork.getOrbTier()));
}
}