2018-02-15 18:49:01 -08:00
|
|
|
package WayofTime.bloodmagic.util.helper;
|
2015-10-29 20:22:14 -07:00
|
|
|
|
2018-02-27 16:59:51 -08:00
|
|
|
import WayofTime.bloodmagic.core.data.Binding;
|
2018-06-28 18:24:17 -07:00
|
|
|
import WayofTime.bloodmagic.core.data.SoulTicket;
|
2018-02-15 18:49:01 -08:00
|
|
|
import WayofTime.bloodmagic.event.SoulNetworkEvent;
|
2018-06-28 18:24:17 -07:00
|
|
|
import WayofTime.bloodmagic.iface.IBindable;
|
2018-02-15 18:49:01 -08:00
|
|
|
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;
|
2015-10-29 20:22:14 -07:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.world.World;
|
2015-12-31 20:10:57 -05:00
|
|
|
import net.minecraftforge.common.DimensionManager;
|
2015-10-29 20:22:14 -07:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
|
2016-03-17 13:00:44 -07:00
|
|
|
import java.util.UUID;
|
2015-12-28 20:13:11 -08:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class NetworkHelper {
|
2015-11-02 09:51:11 -08:00
|
|
|
// 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) {
|
2015-12-31 20:10:57 -05:00
|
|
|
World world = DimensionManager.getWorld(0);
|
2016-06-12 13:41:02 -07:00
|
|
|
if (world == null || world.getMapStorage() == null) //Hack-ish way to fix the lava crystal.
|
|
|
|
return new BMWorldSavedData().getNetwork(UUID.fromString(uuid));
|
2016-02-15 08:11:18 -05:00
|
|
|
|
2016-06-12 13:41:02 -07:00
|
|
|
BMWorldSavedData saveData = (BMWorldSavedData) world.getMapStorage().getOrLoadData(BMWorldSavedData.class, BMWorldSavedData.ID);
|
2015-11-02 09:51:11 -08:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (saveData == null) {
|
2016-06-12 13:41:02 -07:00
|
|
|
saveData = new BMWorldSavedData();
|
|
|
|
world.getMapStorage().setData(BMWorldSavedData.ID, saveData);
|
2015-11-02 09:51:11 -08:00
|
|
|
}
|
|
|
|
|
2016-06-12 13:41:02 -07:00
|
|
|
return saveData.getNetwork(UUID.fromString(uuid));
|
2015-11-02 09:51:11 -08:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:13:11 -08:00
|
|
|
/**
|
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)
|
2015-12-28 20:13:11 -08:00
|
|
|
*/
|
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());
|
2015-12-28 20:13:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-15 21:30:48 -07:00
|
|
|
* @param player - The Player
|
2016-01-01 13:14:49 -05:00
|
|
|
* @see NetworkHelper#getSoulNetwork(String)
|
2015-12-28 20:13:11 -08:00
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public static SoulNetwork getSoulNetwork(EntityPlayer player) {
|
2015-12-31 20:10:57 -05:00
|
|
|
return getSoulNetwork(PlayerHelper.getUUIDFromPlayer(player));
|
2015-12-28 20:13:11 -08:00
|
|
|
}
|
|
|
|
|
2018-02-27 16:59:51 -08:00
|
|
|
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) {
|
2015-11-22 13:43:15 -08:00
|
|
|
return soulNetwork.getOrbTier();
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public static int getMaximumForTier(int tier) {
|
2016-03-14 18:57:39 -07:00
|
|
|
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();
|
|
|
|
}
|
2016-03-14 18:57:39 -07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:22:14 -07:00
|
|
|
// Syphon
|
|
|
|
|
2015-11-22 13:43:15 -08:00
|
|
|
/**
|
2015-12-30 15:34:40 -05:00
|
|
|
* Syphons from the player and damages them if there was not enough stored
|
|
|
|
* LP.
|
2017-08-15 21:30:48 -07:00
|
|
|
* <p>
|
2015-11-22 13:43:15 -08:00
|
|
|
* 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
|
2015-11-22 13:43:15 -08:00
|
|
|
* @return - Whether the action should be performed.
|
2018-06-28 18:24:17 -07:00
|
|
|
* @deprecated Use {@link #getSoulNetwork(EntityPlayer)} and {@link SoulNetwork#syphonAndDamage(EntityPlayer, SoulTicket)}
|
2015-11-22 13:43:15 -08:00
|
|
|
*/
|
2016-12-19 17:32:03 -08:00
|
|
|
@Deprecated
|
2017-08-15 21:30:48 -07:00
|
|
|
public static boolean syphonAndDamage(SoulNetwork soulNetwork, EntityPlayer user, int toSyphon) {
|
2015-11-22 13:43:15 -08:00
|
|
|
|
2018-02-27 16:59:51 -08:00
|
|
|
// if (soulNetwork.getNewOwner() == null)
|
2015-12-31 20:10:57 -05:00
|
|
|
// {
|
|
|
|
// soulNetwork.syphon(toSyphon);
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
|
|
|
|
return soulNetwork.syphonAndDamage(user, toSyphon);
|
2015-11-22 13:43:15 -08:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-06-26 17:15:45 -07:00
|
|
|
public static boolean syphonFromContainer(ItemStack stack, int toSyphon)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2018-02-27 16:59:51 -08:00
|
|
|
if (!(stack.getItem() instanceof IBindable))
|
|
|
|
return false;
|
2015-11-22 13:43:15 -08:00
|
|
|
|
2018-02-27 16:59:51 -08:00
|
|
|
Binding binding = ((IBindable) stack.getItem()).getBinding(stack);
|
|
|
|
if (binding == null)
|
2015-11-22 13:43:15 -08:00
|
|
|
return false;
|
|
|
|
|
2018-02-27 16:59:51 -08:00
|
|
|
SoulNetwork network = getSoulNetwork(binding);
|
2018-06-28 18:24:17 -07:00
|
|
|
SoulNetworkEvent.Syphon.Item event = new SoulNetworkEvent.Syphon.Item(network, new SoulTicket(toSyphon), stack);
|
2015-11-22 13:43:15 -08:00
|
|
|
|
2018-06-28 18:24:17 -07:00
|
|
|
return !MinecraftForge.EVENT_BUS.post(event) && network.syphon(event.getTicket(), true) >= toSyphon;
|
2015-11-22 13:43:15 -08:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-02-27 16:59:51 -08:00
|
|
|
if (!(stack.getItem() instanceof IBindable))
|
|
|
|
return false;
|
2015-12-31 20:47:01 -05:00
|
|
|
|
2018-02-27 16:59:51 -08:00
|
|
|
Binding binding = ((IBindable) stack.getItem()).getBinding(stack);
|
|
|
|
if (binding == null)
|
2015-12-31 20:47:01 -05:00
|
|
|
return false;
|
|
|
|
|
2018-02-27 16:59:51 -08:00
|
|
|
SoulNetwork network = getSoulNetwork(binding);
|
2015-12-31 20:47:01 -05:00
|
|
|
return network.getCurrentEssence() >= toSyphon;
|
|
|
|
}
|
|
|
|
|
2015-11-22 13:43:15 -08:00
|
|
|
// Set
|
|
|
|
|
2015-12-01 21:55:56 -08:00
|
|
|
/**
|
2015-12-30 15:34:40 -05: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) {
|
2015-11-22 13:43:15 -08:00
|
|
|
soulNetwork.setOrbTier(Math.max(maxOrb, soulNetwork.getOrbTier()));
|
2015-10-29 20:22:14 -07:00
|
|
|
}
|
|
|
|
}
|