Rewrite LP network data saving system

Instead of creating a new file for each player with their UUID as the name, we create a single file and store it all in a List. That List gets converted to a UUID -> SoulNetwork map when read from the file.

MigrateNetworkDataHandler is used to migrate players from the old system to the new one. It reads both data files and sets the LP of the new network to the LP of the old network (if the old network is larger). Once conversion is done, we delete the old file so that it doesn't happen again and overwrite player progress.

This is an API breaking change due to an import change.
This commit is contained in:
Nicholas Ignoffo 2016-06-12 13:41:02 -07:00
parent f8859dbf56
commit f99b21cffc
40 changed files with 423 additions and 283 deletions

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