Death to the old Transmutation Knowledge! Long live Player Knowledge!

This commit is contained in:
Pahimar 2016-05-22 13:21:46 -04:00
parent 8d942d49d3
commit 3290abb23e
19 changed files with 283 additions and 1350 deletions

View file

@ -7,7 +7,6 @@ import com.pahimar.ee3.handler.*;
import com.pahimar.ee3.init.*;
import com.pahimar.ee3.knowledge.AbilityRegistry;
import com.pahimar.ee3.knowledge.PlayerKnowledgeRegistry;
import com.pahimar.ee3.knowledge.TransmutationKnowledgeRegistry;
import com.pahimar.ee3.network.PacketHandler;
import com.pahimar.ee3.proxy.IProxy;
import com.pahimar.ee3.recipe.AludelRecipeManager;
@ -56,7 +55,6 @@ public class EquivalentExchange3
Files.updateFileReferences();
SerializationHelper.initModDataDirectories();
TransmutationKnowledgeRegistry.getInstance();
AbilityRegistry.getInstance().loadAbilityRegistryFromFile(ConfigurationHandler.Settings.onlyLoadFile);
event.registerServerCommand(new CommandEE());
}
@ -119,8 +117,6 @@ public class EquivalentExchange3
WorldEventHandler.hasInitilialized = false;
EnergyValueRegistry.INSTANCE.save();
TransmutationKnowledgeRegistry.getInstance().clear();
PlayerKnowledgeRegistry.INSTANCE.saveAll();
AbilityRegistry.getInstance().save();

View file

@ -71,11 +71,6 @@ public class CommandEE extends CommandBase
modCommands.add(new CommandPlayerForgetEverything());
modCommands.add(new CommandPlayerForgetItem());
modCommands.add(new CommandPlayerForgetCurrentItem());
modCommands.add(new CommandTemplateLearnItem());
modCommands.add(new CommandTemplateLearnCurrentItem());
modCommands.add(new CommandTemplateForgetEverything());
modCommands.add(new CommandTemplateForgetItem());
modCommands.add(new CommandTemplateForgetCurrentItem());
modCommands.add(new CommandSetItemLearnable());
modCommands.add(new CommandSetItemNotLearnable());
modCommands.add(new CommandSetItemRecoverable());

View file

@ -1,54 +0,0 @@
package com.pahimar.ee3.command;
import com.pahimar.ee3.knowledge.TransmutationKnowledgeRegistry;
import com.pahimar.ee3.reference.Messages;
import com.pahimar.ee3.reference.Names;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
public class CommandTemplateForgetCurrentItem extends CommandBase
{
@Override
public String getCommandName()
{
return Names.Commands.TEMPLATE_FORGET_CURRENT_ITEM;
}
@Override
public int getRequiredPermissionLevel()
{
return 2;
}
@Override
public String getCommandUsage(ICommandSender commandSender)
{
return Messages.Commands.TEMPLATE_FORGET_CURRENT_ITEM_USAGE;
}
@Override
public void processCommand(ICommandSender commandSender, String[] args)
{
if (args.length < 1)
{
throw new WrongUsageException(Messages.Commands.TEMPLATE_FORGET_CURRENT_ITEM_USAGE);
}
else
{
ItemStack itemStack = ((EntityPlayer) commandSender).getCurrentEquippedItem();
if (itemStack != null)
{
TransmutationKnowledgeRegistry.getInstance().makeTemplateForget(itemStack);
func_152373_a(commandSender, this, Messages.Commands.TEMPLATE_FORGET_CURRENT_ITEM_SUCCESS, new Object[]{commandSender.getCommandSenderName(), itemStack.func_151000_E()});
}
else
{
throw new WrongUsageException(Messages.Commands.NO_ITEM);
}
}
}
}

View file

@ -1,35 +0,0 @@
package com.pahimar.ee3.command;
import com.pahimar.ee3.knowledge.TransmutationKnowledgeRegistry;
import com.pahimar.ee3.reference.Messages;
import com.pahimar.ee3.reference.Names;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
public class CommandTemplateForgetEverything extends CommandBase
{
@Override
public String getCommandName()
{
return Names.Commands.TEMPLATE_FORGET_EVERYTHING;
}
@Override
public int getRequiredPermissionLevel()
{
return 2;
}
@Override
public String getCommandUsage(ICommandSender commandSender)
{
return Messages.Commands.TEMPLATE_FORGET_EVERYTHING_USAGE;
}
@Override
public void processCommand(ICommandSender commandSender, String[] args)
{
TransmutationKnowledgeRegistry.getInstance().makeTemplateForgetEverything();
func_152373_a(commandSender, this, Messages.Commands.TEMPLATE_FORGET_EVERYTHING_SUCCESS, new Object[]{commandSender.getCommandSenderName()});
}
}

View file

@ -1,94 +0,0 @@
package com.pahimar.ee3.command;
import com.pahimar.ee3.knowledge.TransmutationKnowledgeRegistry;
import com.pahimar.ee3.reference.Messages;
import com.pahimar.ee3.reference.Names;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import java.util.List;
public class CommandTemplateForgetItem extends CommandBase
{
@Override
public String getCommandName()
{
return Names.Commands.TEMPLATE_FORGET_ITEM;
}
@Override
public int getRequiredPermissionLevel()
{
return 2;
}
@Override
public String getCommandUsage(ICommandSender commandSender)
{
return Messages.Commands.TEMPLATE_FORGET_ITEM_USAGE;
}
@Override
public void processCommand(ICommandSender commandSender, String[] args)
{
if (args.length < 2)
{
throw new WrongUsageException(Messages.Commands.TEMPLATE_FORGET_ITEM_USAGE);
}
else
{
Item item = getItemByText(commandSender, args[1]);
int metaData = 0;
if (args.length >= 3)
{
metaData = parseInt(commandSender, args[2]);
}
ItemStack itemStack = new ItemStack(item, 1, metaData);
if (args.length >= 4)
{
String stringNBTData = func_147178_a(commandSender, args, 3).getUnformattedText();
try
{
NBTBase nbtBase = JsonToNBT.func_150315_a(stringNBTData);
if (!(nbtBase instanceof NBTTagCompound))
{
func_152373_a(commandSender, this, Messages.Commands.INVALID_NBT_TAG_ERROR, new Object[]{"Not a valid tag"});
return;
}
itemStack.setTagCompound((NBTTagCompound) nbtBase);
}
catch (Exception exception)
{
func_152373_a(commandSender, this, Messages.Commands.INVALID_NBT_TAG_ERROR, new Object[]{exception.getMessage()});
return;
}
}
TransmutationKnowledgeRegistry.getInstance().makeTemplateForget(itemStack);
func_152373_a(commandSender, this, Messages.Commands.TEMPLATE_FORGET_ITEM_SUCCESS, new Object[]{commandSender.getCommandSenderName(), itemStack.func_151000_E()});
}
}
@Override
public List addTabCompletionOptions(ICommandSender commandSender, String[] args)
{
if (args.length == 2)
{
return getListOfStringsFromIterableMatchingLastWord(args, Item.itemRegistry.getKeys());
}
return null;
}
}

View file

@ -1,54 +0,0 @@
package com.pahimar.ee3.command;
import com.pahimar.ee3.knowledge.TransmutationKnowledgeRegistry;
import com.pahimar.ee3.reference.Messages;
import com.pahimar.ee3.reference.Names;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
public class CommandTemplateLearnCurrentItem extends CommandBase
{
@Override
public String getCommandName()
{
return Names.Commands.TEMPLATE_LEARN_CURRENT_ITEM;
}
@Override
public int getRequiredPermissionLevel()
{
return 2;
}
@Override
public String getCommandUsage(ICommandSender commandSender)
{
return Messages.Commands.TEMPLATE_LEARN_CURRENT_ITEM_USAGE;
}
@Override
public void processCommand(ICommandSender commandSender, String[] args)
{
if (args.length < 1)
{
throw new WrongUsageException(Messages.Commands.TEMPLATE_LEARN_CURRENT_ITEM_USAGE);
}
else
{
ItemStack itemStack = ((EntityPlayer) commandSender).getCurrentEquippedItem();
if (itemStack != null)
{
TransmutationKnowledgeRegistry.getInstance().teachTemplate(itemStack);
func_152373_a(commandSender, this, Messages.Commands.TEMPLATE_LEARN_CURRENT_ITEM_SUCCESS, new Object[]{commandSender.getCommandSenderName(), itemStack.func_151000_E()});
}
else
{
throw new WrongUsageException(Messages.Commands.NO_ITEM);
}
}
}
}

View file

@ -1,98 +0,0 @@
package com.pahimar.ee3.command;
import com.pahimar.ee3.knowledge.AbilityRegistry;
import com.pahimar.ee3.knowledge.TransmutationKnowledgeRegistry;
import com.pahimar.ee3.reference.Messages;
import com.pahimar.ee3.reference.Names;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import java.util.List;
public class CommandTemplateLearnItem extends CommandBase
{
@Override
public String getCommandName()
{
return Names.Commands.TEMPLATE_LEARN_ITEM;
}
@Override
public int getRequiredPermissionLevel()
{
return 2;
}
@Override
public String getCommandUsage(ICommandSender commandSender)
{
return Messages.Commands.TEMPLATE_LEARN_ITEM_USAGE;
}
@Override
public void processCommand(ICommandSender commandSender, String[] args)
{
if (args.length < 2)
{
throw new WrongUsageException(Messages.Commands.TEMPLATE_LEARN_ITEM_USAGE);
}
else
{
Item item = getItemByText(commandSender, args[1]);
int metaData = 0;
if (args.length >= 3)
{
metaData = parseInt(commandSender, args[2]);
}
ItemStack itemStack = new ItemStack(item, 1, metaData);
if (args.length >= 4)
{
String stringNBTData = func_147178_a(commandSender, args, 3).getUnformattedText();
try
{
NBTBase nbtBase = JsonToNBT.func_150315_a(stringNBTData);
if (!(nbtBase instanceof NBTTagCompound))
{
func_152373_a(commandSender, this, Messages.Commands.INVALID_NBT_TAG_ERROR, new Object[]{"Not a valid tag"});
return;
}
itemStack.setTagCompound((NBTTagCompound) nbtBase);
}
catch (Exception exception)
{
func_152373_a(commandSender, this, Messages.Commands.INVALID_NBT_TAG_ERROR, new Object[]{exception.getMessage()});
return;
}
}
if (AbilityRegistry.getInstance().isLearnable(itemStack))
{
TransmutationKnowledgeRegistry.getInstance().teachTemplate(itemStack);
func_152373_a(commandSender, this, Messages.Commands.TEMPLATE_LEARN_ITEM_SUCCESS, new Object[]{commandSender.getCommandSenderName(), itemStack.func_151000_E()});
}
}
}
@Override
public List addTabCompletionOptions(ICommandSender commandSender, String[] args)
{
if (args.length == 2)
{
return getListOfStringsFromIterableMatchingLastWord(args, Item.itemRegistry.getKeys());
}
return null;
}
}

View file

@ -1,6 +1,5 @@
package com.pahimar.ee3.handler;
import com.pahimar.ee3.knowledge.TransmutationKnowledgeRegistry;
import com.pahimar.ee3.network.PacketHandler;
import com.pahimar.ee3.network.message.MessageChalkSettings;
import com.pahimar.ee3.network.message.MessageSyncEnergyValues;
@ -9,33 +8,13 @@ import com.pahimar.ee3.util.EntityHelper;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.event.entity.player.PlayerEvent;
public class PlayerEventHandler
{
@SubscribeEvent
public void onPlayerLoadFromFileEvent(PlayerEvent.LoadFromFile event)
{
if (!event.entityPlayer.worldObj.isRemote)
{
TransmutationKnowledgeRegistry.getInstance().loadPlayerFromDiskIfNeeded(event.entityPlayer);
}
}
public class PlayerEventHandler {
@SubscribeEvent
public void onPlayerSaveToFileEvent(PlayerEvent.SaveToFile event)
{
if (!event.entityPlayer.worldObj.isRemote)
{
TransmutationKnowledgeRegistry.getInstance().savePlayerKnowledgeToDisk(event.entityPlayer);
}
}
public void onPlayerLoggedIn(cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent event) {
@SubscribeEvent
public void onPlayerLoggedIn(cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent event)
{
if (event.player != null)
{
if (event.player != null) {
NBTTagCompound playerCustomData = EntityHelper.getCustomEntityData(event.player);
// Chalk Settings
@ -45,18 +24,7 @@ public class PlayerEventHandler
EntityHelper.saveCustomEntityData(event.player, playerCustomData);
PacketHandler.INSTANCE.sendTo(new MessageChalkSettings(chalkSettings), (EntityPlayerMP) event.player);
TransmutationKnowledgeRegistry.getInstance().loadPlayerFromDiskIfNeeded(event.player);
PacketHandler.INSTANCE.sendTo(new MessageSyncEnergyValues(), (EntityPlayerMP) event.player);
}
}
@SubscribeEvent
public void onPlayerLoggedOut(cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedOutEvent event)
{
if (!event.player.worldObj.isRemote)
{
TransmutationKnowledgeRegistry.getInstance().unloadPlayer(event.player);
}
}
}

View file

@ -1,9 +1,9 @@
package com.pahimar.ee3.inventory;
import com.pahimar.ee3.api.knowledge.PlayerKnowledgeRegistryProxy;
import com.pahimar.ee3.inventory.element.IElementButtonHandler;
import com.pahimar.ee3.inventory.element.IElementTextFieldHandler;
import com.pahimar.ee3.item.ItemAlchenomicon;
import com.pahimar.ee3.knowledge.TransmutationKnowledgeRegistry;
import com.pahimar.ee3.reference.Comparators;
import com.pahimar.ee3.util.FilterUtils;
import com.pahimar.ee3.util.ItemHelper;
@ -17,7 +17,6 @@ import net.minecraft.item.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.UUID;
public class ContainerAlchenomicon extends ContainerEE implements IElementButtonHandler, IElementTextFieldHandler
{
@ -31,12 +30,12 @@ public class ContainerAlchenomicon extends ContainerEE implements IElementButton
public ContainerAlchenomicon(EntityPlayer entityPlayer, ItemStack itemStack)
{
TreeSet<ItemStack> knownTransmutations = new TreeSet<ItemStack>(Comparators.DISPLAY_NAME_COMPARATOR);
TreeSet<ItemStack> knownTransmutations = new TreeSet<>(Comparators.DISPLAY_NAME_COMPARATOR);
if (itemStack.getItem() instanceof ItemAlchenomicon && ItemHelper.hasOwnerUUID(itemStack))
if (itemStack.getItem() instanceof ItemAlchenomicon && ItemHelper.hasOwnerName(itemStack))
{
UUID ownerUUID = ItemHelper.getOwnerUUID(itemStack);
knownTransmutations.addAll(TransmutationKnowledgeRegistry.getInstance().getPlayersKnownTransmutations(ownerUUID));
String playerName = ItemHelper.getOwnerName(itemStack);
knownTransmutations.addAll(PlayerKnowledgeRegistryProxy.getKnownItemStacks(playerName));
}
inventoryAlchenomicon = new InventoryAlchenomicon(knownTransmutations);
@ -188,7 +187,7 @@ public class ContainerAlchenomicon extends ContainerEE implements IElementButton
this.requiresUpdate = true;
boolean shouldUpdateInventory = false;
ItemStack[] newInventory = new ItemStack[80];
List<ItemStack> filteredList = new ArrayList(FilterUtils.filterByDisplayName(inventoryAlchenomicon.getKnownTransmutations(), searchTerm, FilterUtils.NameFilterType.CONTAINS, Comparators.DISPLAY_NAME_COMPARATOR));
List<ItemStack> filteredList = new ArrayList<>(FilterUtils.filterByDisplayName(inventoryAlchenomicon.getKnownTransmutations(), searchTerm, FilterUtils.NameFilterType.CONTAINS, Comparators.DISPLAY_NAME_COMPARATOR));
maxPageOffset = filteredList.size() / 80;
if (pageOffset > maxPageOffset)

View file

@ -2,16 +2,16 @@ package com.pahimar.ee3.inventory;
import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy;
import com.pahimar.ee3.api.knowledge.AbilityRegistryProxy;
import com.pahimar.ee3.api.knowledge.PlayerKnowledgeRegistryProxy;
import com.pahimar.ee3.inventory.element.IElementButtonHandler;
import com.pahimar.ee3.inventory.element.IElementSliderHandler;
import com.pahimar.ee3.inventory.element.IElementTextFieldHandler;
import com.pahimar.ee3.item.ItemAlchenomicon;
import com.pahimar.ee3.item.ItemMiniumStone;
import com.pahimar.ee3.item.ItemPhilosophersStone;
import com.pahimar.ee3.knowledge.TransmutationKnowledge;
import com.pahimar.ee3.knowledge.TransmutationKnowledgeRegistry;
import com.pahimar.ee3.knowledge.PlayerKnowledge;
import com.pahimar.ee3.network.PacketHandler;
import com.pahimar.ee3.network.message.MessageTransmutationKnowledgeUpdate;
import com.pahimar.ee3.network.message.MessagePlayerKnowledge;
import com.pahimar.ee3.reference.Comparators;
import com.pahimar.ee3.tileentity.TileEntityTransmutationTablet;
import com.pahimar.ee3.util.FilterUtils;
@ -48,9 +48,9 @@ public class ContainerTransmutationTablet extends ContainerEE implements IElemen
if (tileEntityTransmutationTablet.getStackInSlot(TileEntityTransmutationTablet.ALCHENOMICON_INDEX) != null)
{
ItemStack itemStack = tileEntityTransmutationTablet.getStackInSlot(TileEntityTransmutationTablet.ALCHENOMICON_INDEX);
if (itemStack.getItem() instanceof ItemAlchenomicon && ItemHelper.hasOwnerUUID(itemStack))
if (itemStack.getItem() instanceof ItemAlchenomicon && ItemHelper.hasOwnerName(itemStack))
{
knownTransmutations.addAll(TransmutationKnowledgeRegistry.getInstance().getPlayersKnownTransmutations(ItemHelper.getOwnerUUID(itemStack)));
knownTransmutations.addAll(PlayerKnowledgeRegistryProxy.getKnownItemStacks(ItemHelper.getOwnerName(itemStack)));
}
}
inventoryTransmutationTablet = new InventoryTransmutationTablet(knownTransmutations);
@ -189,11 +189,10 @@ public class ContainerTransmutationTablet extends ContainerEE implements IElemen
}
}
public void handleTransmutationKnowledgeUpdate(TransmutationKnowledge transmutationKnowledge)
{
if (transmutationKnowledge != null)
{
this.inventoryTransmutationTablet = new InventoryTransmutationTablet(transmutationKnowledge.getKnownTransmutations());
public void handlePlayerKnowledgeUpdate(PlayerKnowledge playerKnowledge) {
if (playerKnowledge != null) {
this.inventoryTransmutationTablet = new InventoryTransmutationTablet(playerKnowledge.getKnownItemStacks());
this.updateInventory();
}
}
@ -479,7 +478,7 @@ public class ContainerTransmutationTablet extends ContainerEE implements IElemen
if (!this.tileEntityTransmutationTablet.getWorldObj().isRemote && itemStack != null && itemStack.getItem() instanceof ItemAlchenomicon && ItemHelper.hasOwnerUUID(itemStack))
{
PacketHandler.INSTANCE.sendToAllAround(new MessageTransmutationKnowledgeUpdate(this.containerTransmutationTablet.tileEntityTransmutationTablet, null), new NetworkRegistry.TargetPoint(this.tileEntityTransmutationTablet.getWorldObj().provider.dimensionId, (double) this.tileEntityTransmutationTablet.xCoord, (double) this.tileEntityTransmutationTablet.yCoord, (double) this.tileEntityTransmutationTablet.zCoord, 5d));
PacketHandler.INSTANCE.sendToAllAround(new MessagePlayerKnowledge(this.containerTransmutationTablet.tileEntityTransmutationTablet, null), new NetworkRegistry.TargetPoint(this.tileEntityTransmutationTablet.getWorldObj().provider.dimensionId, (double) this.tileEntityTransmutationTablet.xCoord, (double) this.tileEntityTransmutationTablet.yCoord, (double) this.tileEntityTransmutationTablet.zCoord, 5d));
}
}
@ -488,12 +487,12 @@ public class ContainerTransmutationTablet extends ContainerEE implements IElemen
{
super.putStack(itemStack);
if (!this.tileEntityTransmutationTablet.getWorldObj().isRemote && itemStack != null && itemStack.getItem() instanceof ItemAlchenomicon && ItemHelper.hasOwnerUUID(itemStack))
if (!this.tileEntityTransmutationTablet.getWorldObj().isRemote && itemStack != null && itemStack.getItem() instanceof ItemAlchenomicon && ItemHelper.hasOwnerName(itemStack))
{
Set<ItemStack> knownTransmutations = TransmutationKnowledgeRegistry.getInstance().getPlayersKnownTransmutations(ItemHelper.getOwnerUUID(itemStack));
Set<ItemStack> knownTransmutations = PlayerKnowledgeRegistryProxy.getKnownItemStacks(ItemHelper.getOwnerName(itemStack));
this.containerTransmutationTablet.inventoryTransmutationTablet = new InventoryTransmutationTablet(knownTransmutations);
this.containerTransmutationTablet.updateInventory();
PacketHandler.INSTANCE.sendToAllAround(new MessageTransmutationKnowledgeUpdate(this.containerTransmutationTablet.tileEntityTransmutationTablet, knownTransmutations), new NetworkRegistry.TargetPoint(this.tileEntityTransmutationTablet.getWorldObj().provider.dimensionId, (double) this.tileEntityTransmutationTablet.xCoord, (double) this.tileEntityTransmutationTablet.yCoord, (double) this.tileEntityTransmutationTablet.zCoord, 5d));
PacketHandler.INSTANCE.sendToAllAround(new MessagePlayerKnowledge(this.containerTransmutationTablet.tileEntityTransmutationTablet, knownTransmutations), new NetworkRegistry.TargetPoint(this.tileEntityTransmutationTablet.getWorldObj().provider.dimensionId, (double) this.tileEntityTransmutationTablet.xCoord, (double) this.tileEntityTransmutationTablet.yCoord, (double) this.tileEntityTransmutationTablet.zCoord, 5d));
}
}
}

View file

@ -5,6 +5,7 @@ import com.pahimar.ee3.api.knowledge.AbilityRegistryProxy;
import com.pahimar.ee3.handler.ConfigurationHandler;
import com.pahimar.ee3.reference.Comparators;
import com.pahimar.ee3.reference.Files;
import com.pahimar.ee3.util.LogHelper;
import com.pahimar.ee3.util.SerializationHelper;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
@ -33,10 +34,13 @@ public class PlayerKnowledgeRegistry {
templatePlayerKnowledge = new PlayerKnowledge();
}
public PlayerKnowledge getTemplatePlayerKnowledge() {
return templatePlayerKnowledge;
}
/**
* TODO Finish JavaDoc
*
* @param player
* @param object
* @return
*/
public boolean doesPlayerKnow(EntityPlayer player, Object object) {
if (player != null) {
@ -46,6 +50,13 @@ public class PlayerKnowledgeRegistry {
return false;
}
/**
* TODO Finish JavaDoc
*
* @param playerName
* @param object
* @return
*/
public boolean doesPlayerKnow(String playerName, Object object) {
if (getPlayerKnowledge(playerName) != null) {
@ -55,6 +66,13 @@ public class PlayerKnowledgeRegistry {
return false;
}
/**
* TODO Finish JavaDoc
*
* @param entityPlayer
* @param object
* @return
*/
public boolean canPlayerLearn(EntityPlayer entityPlayer, Object object) {
if (entityPlayer != null) {
@ -64,6 +82,13 @@ public class PlayerKnowledgeRegistry {
return false;
}
/**
* TODO Finish JavaDoc
*
* @param playerName
* @param object
* @return
*/
public boolean canPlayerLearn(String playerName, Object object) {
if (getPlayerKnowledge(playerName) != null) {
@ -73,6 +98,12 @@ public class PlayerKnowledgeRegistry {
return false;
}
/**
* TODO Finish JavaDoc
*
* @param entityPlayer
* @param object
*/
public void teachPlayer(EntityPlayer entityPlayer, Object object) {
if (entityPlayer != null) {
@ -80,6 +111,12 @@ public class PlayerKnowledgeRegistry {
}
}
/**
* TODO Finish JavaDoc
*
* @param playerName
* @param object
*/
public void teachPlayer(String playerName, Object object) {
if (getPlayerKnowledge(playerName) != null) {
@ -88,6 +125,12 @@ public class PlayerKnowledgeRegistry {
}
}
/**
* TODO Finish JavaDoc
*
* @param entityPlayer
* @param objects
*/
public void teachPlayer(EntityPlayer entityPlayer, Collection<?> objects) {
if (entityPlayer != null) {
@ -95,6 +138,12 @@ public class PlayerKnowledgeRegistry {
}
}
/**
* TODO Finish JavaDoc
*
* @param playerName
* @param objects
*/
public void teachPlayer(String playerName, Collection<?> objects) {
if (objects != null) {
@ -102,12 +151,20 @@ public class PlayerKnowledgeRegistry {
PlayerKnowledge playerKnowledge = getPlayerKnowledge(playerName);
if (playerKnowledge != null) {
objects.forEach(playerKnowledge::learn);
for (Object object : objects){
getPlayerKnowledge(playerName).learn(object);
}
save(playerName);
}
}
}
/**
* TODO Finish JavaDoc
*
* @param entityPlayer
* @param object
*/
public void makePlayerForget(EntityPlayer entityPlayer, Object object) {
if (entityPlayer != null) {
@ -115,6 +172,12 @@ public class PlayerKnowledgeRegistry {
}
}
/**
* TODO Finish JavaDoc
*
* @param playerName
* @param object
*/
public void makePlayerForget(String playerName, Object object) {
if (getPlayerKnowledge(playerName) != null) {
@ -123,6 +186,12 @@ public class PlayerKnowledgeRegistry {
}
}
/**
* TODO Finish JavaDoc
*
* @param entityPlayer
* @param objects
*/
public void makePlayerForget(EntityPlayer entityPlayer, Collection<?> objects) {
if (entityPlayer != null) {
@ -130,6 +199,12 @@ public class PlayerKnowledgeRegistry {
}
}
/**
* TODO Finish JavaDoc
*
* @param playerName
* @param objects
*/
public void makePlayerForget(String playerName, Collection<?> objects) {
if (objects != null) {
@ -137,12 +212,19 @@ public class PlayerKnowledgeRegistry {
PlayerKnowledge playerKnowledge = getPlayerKnowledge(playerName);
if (playerKnowledge != null) {
objects.forEach(playerKnowledge::forget);
for (Object object : objects) {
getPlayerKnowledge(playerName).forget(object);
}
save(playerName);
}
}
}
/**
* TODO Finish JavaDoc
*
* @param entityPlayer
*/
public void makePlayerForgetAll(EntityPlayer entityPlayer) {
if (entityPlayer != null) {
@ -150,6 +232,11 @@ public class PlayerKnowledgeRegistry {
}
}
/**
* TODO Finish JavaDoc
*
* @param playerName
*/
public void makePlayerForgetAll(String playerName) {
if (playerName != null && !playerName.isEmpty()) {
@ -158,6 +245,12 @@ public class PlayerKnowledgeRegistry {
}
}
/**
* TODO Finish JavaDoc
*
* @param entityPlayer
* @return
*/
public Set<ItemStack> getKnownItemStacks(EntityPlayer entityPlayer) {
if (entityPlayer != null) {
@ -167,6 +260,12 @@ public class PlayerKnowledgeRegistry {
return Collections.EMPTY_SET;
}
/**
* TODO Finish JavaDoc
*
* @param playerName
* @return
*/
public Set<ItemStack> getKnownItemStacks(String playerName) {
if (getPlayerKnowledge(playerName) != null) {
@ -199,7 +298,6 @@ public class PlayerKnowledgeRegistry {
*/
protected PlayerKnowledge getPlayerKnowledge(String playerName) {
// TODO Logging
if (playerName != null && !playerName.isEmpty()) {
if (!playerKnowledgeMap.containsKey(playerName)) {
playerKnowledgeMap.put(playerName, load(getPlayerKnowledgeFile(playerName), false));
@ -275,17 +373,23 @@ public class PlayerKnowledgeRegistry {
PlayerKnowledge playerKnowledge = SerializationHelper.GSON.fromJson(jsonString, PlayerKnowledge.class);
if (playerKnowledge != null) {
return playerKnowledge;
return playerKnowledge;
}
}
catch (JsonSyntaxException | FileNotFoundException e) {
catch (JsonSyntaxException e) {
LogHelper.error("Unable to read player knowledge from file '{}'", file.getAbsoluteFile());
}
catch (FileNotFoundException e) {
LogHelper.warn("Unable to find file '{}'", file.getAbsoluteFile());
}
}
if (ConfigurationHandler.Settings.playerKnowledgeTemplateEnabled && !isTemplate) {
LogHelper.info("Unable to read player knowledge from {}, initializing a new one with template data", file.getName());
return new PlayerKnowledge(templatePlayerKnowledge);
}
else {
LogHelper.info("Unable to read player knowledge from {}, initializing a new empty one", file.getName());
return new PlayerKnowledge();
}
}

View file

@ -1,246 +0,0 @@
package com.pahimar.ee3.knowledge;
import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.pahimar.ee3.exchange.JsonItemStack;
import com.pahimar.ee3.reference.Comparators;
import com.pahimar.ee3.util.FilterUtils;
import com.pahimar.ee3.util.ItemHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import java.io.*;
import java.lang.reflect.Type;
import java.util.*;
public class TransmutationKnowledge implements JsonSerializer<TransmutationKnowledge>, JsonDeserializer<TransmutationKnowledge>
{
private static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(TransmutationKnowledge.class, new TransmutationKnowledge()).create();
private Set<ItemStack> knownTransmutations;
private boolean hasBeenModified = false;
public TransmutationKnowledge()
{
this(new TreeSet<ItemStack>(Comparators.ID_COMPARATOR));
}
public TransmutationKnowledge(Collection<ItemStack> knownTransmutations)
{
this.knownTransmutations = new TreeSet<ItemStack>(Comparators.ID_COMPARATOR);
this.knownTransmutations.addAll(knownTransmutations);
hasBeenModified = false;
}
public TransmutationKnowledge(ItemStack... knownTransmutations)
{
this(Arrays.asList(knownTransmutations));
}
public boolean isKnown(ItemStack itemStack)
{
ItemStack unitItemStack = itemStack.copy();
unitItemStack.stackSize = 1;
return this.knownTransmutations.contains(unitItemStack);
}
public Set<ItemStack> getKnownTransmutations()
{
return this.knownTransmutations;
}
public boolean learnTransmutation(ItemStack itemStack)
{
ItemStack unitItemStack = itemStack.copy();
unitItemStack.stackSize = 1;
if (!this.knownTransmutations.contains(unitItemStack))
{
hasBeenModified = true;
return this.knownTransmutations.add(unitItemStack);
}
return false;
}
public boolean forgetTransmutation(ItemStack itemStack)
{
ItemStack unitItemStack = itemStack.copy();
unitItemStack.stackSize = 1;
if (this.knownTransmutations.contains(unitItemStack))
{
hasBeenModified = true;
return this.knownTransmutations.remove(unitItemStack);
}
return false;
}
public void forgetAllTransmutations()
{
this.knownTransmutations.clear();
hasBeenModified = true;
}
public boolean hasBeenModified()
{
return hasBeenModified;
}
public Set<ItemStack> filterByNameStartsWith(String filterString)
{
return FilterUtils.filterByDisplayName(getKnownTransmutations(), filterString, FilterUtils.NameFilterType.STARTS_WITH);
}
public Set<ItemStack> filterByNameContains(String filterString)
{
return FilterUtils.filterByDisplayName(getKnownTransmutations(), filterString, FilterUtils.NameFilterType.CONTAINS);
}
@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (ItemStack itemStack : knownTransmutations)
{
stringBuilder.append(String.format("%s, ", ItemHelper.toString(itemStack)));
}
stringBuilder.append("]");
return stringBuilder.toString();
}
public static TransmutationKnowledge createFromJson(String jsonTransmutationKnowledge) throws JsonParseException
{
try
{
return jsonSerializer.fromJson(jsonTransmutationKnowledge, TransmutationKnowledge.class);
}
catch (JsonSyntaxException exception)
{
exception.printStackTrace();
}
catch (JsonParseException exception)
{
exception.printStackTrace();
}
return null;
}
public String toJson()
{
return jsonSerializer.toJson(this);
}
@Override
public TransmutationKnowledge deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
if (json.isJsonObject())
{
JsonObject jsonObject = (JsonObject) json;
Set<ItemStack> itemStacks = new TreeSet<ItemStack>(Comparators.ID_COMPARATOR);
if (jsonObject.has("knownTransmutations") && jsonObject.get("knownTransmutations").isJsonArray())
{
JsonArray jsonArray = (JsonArray) jsonObject.get("knownTransmutations");
Iterator<JsonElement> iterator = jsonArray.iterator();
while (iterator.hasNext())
{
JsonElement jsonElement = iterator.next();
if (jsonElement.isJsonObject())
{
try
{
JsonItemStack jsonItemStack = JsonItemStack.jsonSerializer.fromJson(jsonElement, JsonItemStack.class);
ItemStack itemStack = null;
Item item = (Item) Item.itemRegistry.getObject(jsonItemStack.itemName);
if (item != null)
{
itemStack = new ItemStack(item, 1, jsonItemStack.itemDamage);
if (jsonItemStack.itemNBTTagCompound != null)
{
itemStack.stackTagCompound = jsonItemStack.itemNBTTagCompound;
}
}
if (itemStack != null)
{
itemStacks.add(itemStack);
}
}
catch (JsonParseException e)
{
}
}
}
}
return new TransmutationKnowledge(itemStacks);
}
return null;
}
@Override
public JsonElement serialize(TransmutationKnowledge transmutationKnowledge, Type typeOfSrc, JsonSerializationContext context)
{
JsonObject jsonTransmutationKnowledge = new JsonObject();
JsonArray knownTransmutations = new JsonArray();
for (ItemStack itemStack : transmutationKnowledge.getKnownTransmutations())
{
knownTransmutations.add(JsonItemStack.jsonSerializer.toJsonTree(new JsonItemStack(itemStack)));
}
jsonTransmutationKnowledge.add("knownTransmutations", knownTransmutations);
return jsonTransmutationKnowledge;
}
public static void writeToFile(File file, TransmutationKnowledge transmutationKnowledge)
{
JsonWriter jsonWriter;
try
{
jsonWriter = new JsonWriter(new FileWriter(file));
jsonWriter.setIndent(" ");
jsonSerializer.toJson(transmutationKnowledge, TransmutationKnowledge.class, jsonWriter);
jsonWriter.close();
transmutationKnowledge.hasBeenModified = false;
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static TransmutationKnowledge readFromFile(File file)
{
JsonReader jsonReader;
try
{
jsonReader = new JsonReader(new FileReader(file));
TransmutationKnowledge transmutationKnowledge = jsonSerializer.fromJson(jsonReader, TransmutationKnowledge.class);
jsonReader.close();
return transmutationKnowledge;
}
catch (FileNotFoundException ignored)
{
// NOOP
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}

View file

@ -1,478 +0,0 @@
package com.pahimar.ee3.knowledge;
import com.pahimar.ee3.api.event.PlayerKnowledgeEvent;
import com.pahimar.ee3.api.event.TemplateKnowledgeEvent;
import com.pahimar.ee3.reference.Files;
import com.pahimar.ee3.util.FilterUtils;
import com.pahimar.ee3.util.SerializationHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import java.io.File;
import java.util.HashMap;
import java.util.Set;
import java.util.UUID;
public class TransmutationKnowledgeRegistry
{
private static TransmutationKnowledgeRegistry transmutationKnowledgeRegistry = null;
private static File playerKnowledgeDirectory, dataKnowledgeDirectory;
private static TransmutationKnowledge templateKnowledge;
private static HashMap<UUID, TransmutationKnowledge> playerKnowledgeMap;
private TransmutationKnowledgeRegistry()
{
playerKnowledgeDirectory = new File(SerializationHelper.getInstancePlayerDataDirectory(), "knowledge" + File.separator + "transmutation");
playerKnowledgeDirectory.mkdirs();
dataKnowledgeDirectory = new File(SerializationHelper.getInstanceDataDirectory(), "knowledge" + File.separator + "transmutation");
dataKnowledgeDirectory.mkdirs();
loadTemplateKnowledgeFromDisk();
playerKnowledgeMap = new HashMap<UUID, TransmutationKnowledge>();
}
public static TransmutationKnowledgeRegistry getInstance()
{
if (transmutationKnowledgeRegistry == null)
{
transmutationKnowledgeRegistry = new TransmutationKnowledgeRegistry();
}
return transmutationKnowledgeRegistry;
}
public TransmutationKnowledge getTemplateKnowledge()
{
return templateKnowledge;
}
/* Template Related Transmutation Knowledge */
public Set<ItemStack> getTemplatesKnownTransmutations()
{
if (templateKnowledge == null)
{
loadTemplateKnowledgeFromDisk();
}
return templateKnowledge.getKnownTransmutations();
}
public Set<ItemStack> getTemplatesKnownTransmutationsFilteredStartsWith(String filterString)
{
if (templateKnowledge == null)
{
loadTemplateKnowledgeFromDisk();
}
return FilterUtils.filterByDisplayName(templateKnowledge.getKnownTransmutations(), filterString, FilterUtils.NameFilterType.STARTS_WITH);
}
public Set<ItemStack> getTemplatesKnownTransmutationsFilteredContains(String filterString)
{
if (templateKnowledge == null)
{
loadTemplateKnowledgeFromDisk();
}
return FilterUtils.filterByDisplayName(templateKnowledge.getKnownTransmutations(), filterString, FilterUtils.NameFilterType.CONTAINS);
}
public boolean doesTemplateKnow(ItemStack itemStack)
{
if (templateKnowledge == null)
{
loadTemplateKnowledgeFromDisk();
}
return templateKnowledge.isKnown(itemStack);
}
public boolean canTemplateLearn(ItemStack itemStack)
{
if (AbilityRegistry.getInstance().isLearnable(itemStack))
{
if (templateKnowledge == null)
{
loadTemplateKnowledgeFromDisk();
}
return !templateKnowledge.isKnown(itemStack);
}
return false;
}
public void teachTemplate(ItemStack itemStack)
{
if (templateKnowledge == null)
{
loadTemplateKnowledgeFromDisk();
}
if (canTemplateLearn(itemStack) && !MinecraftForge.EVENT_BUS.post(new TemplateKnowledgeEvent.TemplateLearnKnowledgeEvent(itemStack)))
{
templateKnowledge.learnTransmutation(itemStack);
saveTemplateKnowledgeToDisk();
}
}
public void makeTemplateForget(ItemStack itemStack)
{
if (templateKnowledge == null)
{
loadTemplateKnowledgeFromDisk();
}
if (doesTemplateKnow(itemStack) && !MinecraftForge.EVENT_BUS.post(new TemplateKnowledgeEvent.TemplateForgetKnowledgeEvent(itemStack)))
{
templateKnowledge.forgetTransmutation(itemStack);
saveTemplateKnowledgeToDisk();
}
}
public void makeTemplateForgetEverything()
{
if (templateKnowledge == null)
{
loadTemplateKnowledgeFromDisk();
}
if (!MinecraftForge.EVENT_BUS.post(new TemplateKnowledgeEvent.TemplateForgetAllKnowledgeEvent()))
{
templateKnowledge.forgetAllTransmutations();
saveTemplateKnowledgeToDisk();
}
}
/* Player Related Transmutation Knowledge */
public Set<ItemStack> getPlayersKnownTransmutations(EntityPlayer entityPlayer)
{
if (entityPlayer != null)
{
return getPlayersKnownTransmutations(entityPlayer.getUniqueID());
}
return null;
}
public Set<ItemStack> getPlayersKnownTransmutations(UUID playerUUID)
{
if (playerUUID != null)
{
loadPlayerFromDiskIfNeeded(playerUUID);
if (playerKnowledgeMap.containsKey(playerUUID))
{
return playerKnowledgeMap.get(playerUUID).getKnownTransmutations();
}
}
return null;
}
public Set<ItemStack> getPlayersKnownTransmutationsFilteredStartsWith(EntityPlayer entityPlayer, String startsWith)
{
if (entityPlayer != null)
{
return getPlayersKnownTransmutationsFilteredStartsWith(entityPlayer.getUniqueID(), startsWith);
}
return null;
}
public Set<ItemStack> getPlayersKnownTransmutationsFilteredStartsWith(UUID playerUUID, String startsWith)
{
if (playerUUID != null)
{
loadPlayerFromDiskIfNeeded(playerUUID);
if (playerKnowledgeMap.containsKey(playerUUID))
{
return playerKnowledgeMap.get(playerUUID).filterByNameStartsWith(startsWith);
}
}
return null;
}
public Set<ItemStack> getPlayersKnownTransmutationsFilteredContains(EntityPlayer entityPlayer, String contains)
{
if (entityPlayer != null)
{
return getPlayersKnownTransmutationsFilteredContains(entityPlayer.getUniqueID(), contains);
}
return null;
}
public Set<ItemStack> getPlayersKnownTransmutationsFilteredContains(UUID playerUUID, String contains)
{
if (playerUUID != null)
{
loadPlayerFromDiskIfNeeded(playerUUID);
if (playerKnowledgeMap.containsKey(playerUUID))
{
return playerKnowledgeMap.get(playerUUID).filterByNameContains(contains);
}
}
return null;
}
public boolean doesPlayerKnow(EntityPlayer entityPlayer, ItemStack itemStack)
{
if (entityPlayer != null && itemStack != null)
{
loadPlayerFromDiskIfNeeded(entityPlayer);
if (playerKnowledgeMap.containsKey(entityPlayer.getUniqueID()))
{
return playerKnowledgeMap.get(entityPlayer.getUniqueID()).isKnown(itemStack);
}
}
return false;
}
public boolean doesPlayerKnow(UUID playerUUID, ItemStack itemStack)
{
if (playerUUID != null && itemStack != null)
{
loadPlayerFromDiskIfNeeded(playerUUID);
if (playerKnowledgeMap.containsKey(playerUUID))
{
return playerKnowledgeMap.get(playerUUID).isKnown(itemStack);
}
}
return false;
}
public boolean canPlayerLearn(EntityPlayer entityPlayer, ItemStack itemStack)
{
if (entityPlayer != null)
{
return canPlayerLearn(entityPlayer.getUniqueID(), itemStack);
}
return false;
}
public boolean canPlayerLearn(UUID playerUUID, ItemStack itemStack)
{
if (playerUUID != null && itemStack != null)
{
if (AbilityRegistry.getInstance().isLearnable(itemStack))
{
loadPlayerFromDiskIfNeeded(playerUUID);
if (playerKnowledgeMap.containsKey(playerUUID))
{
return !playerKnowledgeMap.get(playerUUID).isKnown(itemStack);
}
}
}
return false;
}
public void teachPlayer(EntityPlayer entityPlayer, ItemStack itemStack)
{
if (entityPlayer != null)
{
teachPlayer(entityPlayer.getUniqueID(), itemStack);
}
}
public void teachPlayer(UUID playerUUID, ItemStack itemStack)
{
if (playerUUID != null && itemStack != null && canPlayerLearn(playerUUID, itemStack))
{
loadPlayerFromDiskIfNeeded(playerUUID);
if (playerKnowledgeMap.containsKey(playerUUID) && !MinecraftForge.EVENT_BUS.post(new PlayerKnowledgeEvent.PlayerLearnKnowledgeEvent(playerUUID, itemStack)))
{
playerKnowledgeMap.get(playerUUID).learnTransmutation(itemStack);
savePlayerKnowledgeToDisk(playerUUID);
}
}
}
public void makePlayerForget(EntityPlayer entityPlayer, ItemStack itemStack)
{
if (entityPlayer != null)
{
makePlayerForget(entityPlayer.getUniqueID(), itemStack);
}
}
public void makePlayerForget(UUID playerUUID, ItemStack itemStack)
{
if (playerUUID != null && itemStack != null && doesPlayerKnow(playerUUID, itemStack))
{
loadPlayerFromDiskIfNeeded(playerUUID);
if (playerKnowledgeMap.containsKey(playerUUID) && !MinecraftForge.EVENT_BUS.post(new PlayerKnowledgeEvent.PlayerForgetKnowledgeEvent(playerUUID, itemStack)))
{
playerKnowledgeMap.get(playerUUID).forgetTransmutation(itemStack);
savePlayerKnowledgeToDisk(playerUUID);
}
}
}
public void makePlayerForgetEverything(EntityPlayer entityPlayer)
{
if (entityPlayer != null)
{
makePlayerForgetEverything(entityPlayer.getUniqueID());
}
}
public void makePlayerForgetEverything(UUID playerUUID)
{
if (playerUUID != null)
{
loadPlayerFromDiskIfNeeded(playerUUID);
if (playerKnowledgeMap.containsKey(playerUUID) && !MinecraftForge.EVENT_BUS.post(new PlayerKnowledgeEvent.PlayerForgetAllKnowledgeEvent(playerUUID)))
{
playerKnowledgeMap.get(playerUUID).forgetAllTransmutations();
savePlayerKnowledgeToDisk(playerUUID);
}
}
}
/* Serialization */
public void loadTemplateKnowledgeFromDisk()
{
if (dataKnowledgeDirectory != null)
{
File templateFile = new File(dataKnowledgeDirectory, Files.TEMPLATE_JSON_FILENAME);
if (!templateFile.exists())
{
templateKnowledge = new TransmutationKnowledge();
SerializationHelper.writeTransmutationKnowledgeToFile(dataKnowledgeDirectory, Files.TEMPLATE_JSON_FILENAME, templateKnowledge);
}
else
{
templateKnowledge = SerializationHelper.readTransmutationKnowledgeFromFile(dataKnowledgeDirectory, Files.TEMPLATE_JSON_FILENAME);
}
}
else
{
templateKnowledge = new TransmutationKnowledge();
}
}
public void saveTemplateKnowledgeToDisk()
{
if (dataKnowledgeDirectory != null)
{
if (templateKnowledge != null)
{
SerializationHelper.writeTransmutationKnowledgeToFile(dataKnowledgeDirectory, Files.TEMPLATE_JSON_FILENAME, templateKnowledge);
}
else
{
templateKnowledge = new TransmutationKnowledge();
SerializationHelper.writeTransmutationKnowledgeToFile(dataKnowledgeDirectory, Files.TEMPLATE_JSON_FILENAME, templateKnowledge);
}
}
}
public void loadPlayerFromDiskIfNeeded(EntityPlayer entityPlayer)
{
if (entityPlayer != null && entityPlayer.getUniqueID() != null)
{
loadPlayerFromDiskIfNeeded(entityPlayer.getUniqueID());
}
}
public void loadPlayerFromDiskIfNeeded(UUID playerUUID)
{
if (playerUUID != null && playerKnowledgeDirectory != null && !playerKnowledgeMap.containsKey(playerUUID))
{
TransmutationKnowledge playerTransmutationKnowledge = new TransmutationKnowledge();
File playerKnowledgeFile = new File(playerKnowledgeDirectory, playerUUID.toString() + ".json");
if (playerKnowledgeFile.exists() && playerKnowledgeFile.isFile())
{
playerTransmutationKnowledge = SerializationHelper.readTransmutationKnowledgeFromFile(playerKnowledgeDirectory, playerUUID.toString() + ".json");
}
playerKnowledgeMap.put(playerUUID, playerTransmutationKnowledge);
}
}
public void unloadPlayer(EntityPlayer entityPlayer)
{
if (entityPlayer != null)
{
unloadPlayer(entityPlayer.getUniqueID());
}
}
public void unloadPlayer(UUID playerUUID)
{
if (playerUUID != null)
{
if (playerKnowledgeMap.containsKey(playerUUID))
{
savePlayerKnowledgeToDisk(playerUUID);
playerKnowledgeMap.remove(playerUUID);
}
}
}
public void savePlayerKnowledgeToDisk(EntityPlayer entityPlayer)
{
if (entityPlayer != null && entityPlayer.getUniqueID() != null)
{
savePlayerKnowledgeToDisk(entityPlayer.getUniqueID());
}
}
public void savePlayerKnowledgeToDisk(UUID playerUUID)
{
if (playerUUID != null && playerKnowledgeDirectory != null)
{
if (playerKnowledgeMap.containsKey(playerUUID) && playerKnowledgeMap.get(playerUUID) != null)
{
if (playerKnowledgeMap.get(playerUUID).hasBeenModified())
{
SerializationHelper.writeTransmutationKnowledgeToFile(playerKnowledgeDirectory, playerUUID.toString() + ".json", playerKnowledgeMap.get(playerUUID));
}
}
else
{
loadPlayerFromDiskIfNeeded(playerUUID);
SerializationHelper.writeTransmutationKnowledgeToFile(playerKnowledgeDirectory, playerUUID.toString() + ".json", playerKnowledgeMap.get(playerUUID));
}
}
}
public void saveAll()
{
if (dataKnowledgeDirectory != null)
{
saveTemplateKnowledgeToDisk();
}
if (playerKnowledgeDirectory != null)
{
for (UUID playerUUID : playerKnowledgeMap.keySet())
{
SerializationHelper.writeTransmutationKnowledgeToFile(playerKnowledgeDirectory, playerUUID.toString() + ".json", playerKnowledgeMap.get(playerUUID));
}
}
}
public void clear()
{
saveAll();
this.transmutationKnowledgeRegistry = null;
}
}

View file

@ -28,7 +28,7 @@ public class PacketHandler
INSTANCE.registerMessage(MessageTileEntityTransmutationTablet.class, MessageTileEntityTransmutationTablet.class, 13, Side.CLIENT);
INSTANCE.registerMessage(MessageSingleParticleEvent.class, MessageSingleParticleEvent.class, 14, Side.CLIENT);
INSTANCE.registerMessage(MessageSliderElementUpdated.class, MessageSliderElementUpdated.class, 15, Side.SERVER);
INSTANCE.registerMessage(MessageTransmutationKnowledgeUpdate.class, MessageTransmutationKnowledgeUpdate.class, 16, Side.CLIENT);
INSTANCE.registerMessage(MessagePlayerKnowledge.class, MessagePlayerKnowledge.class, 16, Side.CLIENT);
INSTANCE.registerMessage(MessageTileEntityResearchStation.class, MessageTileEntityResearchStation.class, 17, Side.CLIENT);
}
}

View file

@ -0,0 +1,112 @@
package com.pahimar.ee3.network.message;
import com.google.gson.JsonSyntaxException;
import com.pahimar.ee3.inventory.ContainerTransmutationTablet;
import com.pahimar.ee3.knowledge.PlayerKnowledge;
import com.pahimar.ee3.tileentity.TileEntityTransmutationTablet;
import com.pahimar.ee3.util.CompressionHelper;
import com.pahimar.ee3.util.SerializationHelper;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import java.util.Set;
public class MessagePlayerKnowledge implements IMessage, IMessageHandler<MessagePlayerKnowledge, IMessage>
{
public int xCoord, yCoord, zCoord;
public PlayerKnowledge playerKnowledge;
public MessagePlayerKnowledge(){
}
public MessagePlayerKnowledge(TileEntityTransmutationTablet transmutationTablet, Set<ItemStack> knownItemStacks) {
if (transmutationTablet != null) {
this.xCoord = transmutationTablet.xCoord;
this.yCoord = transmutationTablet.yCoord;
this.zCoord = transmutationTablet.zCoord;
}
else {
this.xCoord = 0;
this.yCoord = Integer.MIN_VALUE;
this.zCoord = 0;
}
if (knownItemStacks != null) {
this.playerKnowledge = new PlayerKnowledge(knownItemStacks);
}
else {
this.playerKnowledge = new PlayerKnowledge();
}
}
@Override
public void fromBytes(ByteBuf buf) {
this.xCoord = buf.readInt();
this.yCoord = buf.readInt();
this.zCoord = buf.readInt();
byte[] compressedJson = null;
int readableBytes = buf.readInt();
if (readableBytes > 0) {
compressedJson = buf.readBytes(readableBytes).array();
}
if (compressedJson != null) {
try {
this.playerKnowledge = SerializationHelper.GSON.fromJson(CompressionHelper.decompress(compressedJson), PlayerKnowledge.class);
}
catch (JsonSyntaxException e) {
this.playerKnowledge = new PlayerKnowledge();
}
}
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(xCoord);
buf.writeInt(yCoord);
buf.writeInt(zCoord);
byte[] compressedJson = null;
if (playerKnowledge != null) {
compressedJson = CompressionHelper.compress(SerializationHelper.GSON.toJson(playerKnowledge));
}
if (compressedJson != null) {
buf.writeInt(compressedJson.length);
buf.writeBytes(compressedJson);
}
else {
buf.writeInt(0);
}
}
@Override
public IMessage onMessage(MessagePlayerKnowledge message, MessageContext ctx) {
if (message.yCoord != Integer.MIN_VALUE) {
if (FMLClientHandler.instance().getClient().currentScreen instanceof GuiContainer) {
GuiContainer guiContainer = (GuiContainer) FMLClientHandler.instance().getClient().currentScreen;
if (guiContainer.inventorySlots instanceof ContainerTransmutationTablet) {
if (FMLClientHandler.instance().getWorldClient().getTileEntity(message.xCoord, message.yCoord, message.zCoord) instanceof TileEntityTransmutationTablet) {
((ContainerTransmutationTablet) guiContainer.inventorySlots).handlePlayerKnowledgeUpdate(message.playerKnowledge);
}
}
}
}
return null;
}
}

View file

@ -1,135 +0,0 @@
package com.pahimar.ee3.network.message;
import com.pahimar.ee3.inventory.ContainerTransmutationTablet;
import com.pahimar.ee3.knowledge.TransmutationKnowledge;
import com.pahimar.ee3.tileentity.TileEntityTransmutationTablet;
import com.pahimar.ee3.util.CompressionHelper;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import java.util.Collection;
public class MessageTransmutationKnowledgeUpdate implements IMessage, IMessageHandler<MessageTransmutationKnowledgeUpdate, IMessage>
{
public int xCoord, yCoord, zCoord;
public TransmutationKnowledge transmutationKnowledge;
public MessageTransmutationKnowledgeUpdate()
{
}
public MessageTransmutationKnowledgeUpdate(TileEntityTransmutationTablet tileEntityTransmutationTablet, Collection<ItemStack> knownTransmutationsCollection)
{
if (tileEntityTransmutationTablet != null)
{
this.xCoord = tileEntityTransmutationTablet.xCoord;
this.yCoord = tileEntityTransmutationTablet.yCoord;
this.zCoord = tileEntityTransmutationTablet.zCoord;
}
else
{
this.xCoord = 0;
this.yCoord = Integer.MIN_VALUE;
this.zCoord = 0;
}
if (knownTransmutationsCollection != null)
{
this.transmutationKnowledge = new TransmutationKnowledge(knownTransmutationsCollection);
}
else
{
this.transmutationKnowledge = new TransmutationKnowledge();
}
}
public MessageTransmutationKnowledgeUpdate(int xCoord, int yCoord, int zCoord, Collection<ItemStack> knownTransmutationsCollection)
{
this.xCoord = xCoord;
this.yCoord = yCoord;
this.zCoord = zCoord;
if (knownTransmutationsCollection != null)
{
this.transmutationKnowledge = new TransmutationKnowledge(knownTransmutationsCollection);
}
else
{
this.transmutationKnowledge = new TransmutationKnowledge();
}
}
@Override
public void fromBytes(ByteBuf buf)
{
this.xCoord = buf.readInt();
this.yCoord = buf.readInt();
this.zCoord = buf.readInt();
byte[] compressedString = null;
int readableBytes = buf.readInt();
if (readableBytes > 0)
{
compressedString = buf.readBytes(readableBytes).array();
}
if (compressedString != null)
{
String uncompressedString = CompressionHelper.decompress(compressedString);
this.transmutationKnowledge = TransmutationKnowledge.createFromJson(uncompressedString);
}
}
@Override
public void toBytes(ByteBuf buf)
{
buf.writeInt(xCoord);
buf.writeInt(yCoord);
buf.writeInt(zCoord);
byte[] compressedString = null;
if (transmutationKnowledge != null) {
compressedString = CompressionHelper.compress(transmutationKnowledge.toJson());
}
if (compressedString != null)
{
buf.writeInt(compressedString.length);
buf.writeBytes(compressedString);
}
else
{
buf.writeInt(0);
}
}
@Override
public IMessage onMessage(MessageTransmutationKnowledgeUpdate message, MessageContext ctx)
{
if (message.yCoord != Integer.MIN_VALUE)
{
if (FMLClientHandler.instance().getClient().currentScreen instanceof GuiContainer)
{
GuiContainer guiContainer = (GuiContainer) FMLClientHandler.instance().getClient().currentScreen;
if (guiContainer.inventorySlots instanceof ContainerTransmutationTablet)
{
if (FMLClientHandler.instance().getWorldClient().getTileEntity(message.xCoord, message.yCoord, message.zCoord) instanceof TileEntityTransmutationTablet)
{
((ContainerTransmutationTablet) guiContainer.inventorySlots).handleTransmutationKnowledgeUpdate(message.transmutationKnowledge);
}
}
}
}
return null;
}
}

View file

@ -184,7 +184,7 @@ public class TileEntityResearchStation extends TileEntityEE implements IInventor
@Override
public void updateEntity()
{
if (!this.worldObj.isRemote)
if (!this.worldObj.isRemote && inventory[ALCHENOMICON_SLOT_INVENTORY_INDEX] != null && inventory[ITEM_SLOT_INVENTORY_INDEX] != null)
{
// Continue "cooking" the same item, if we can
if (this.canLearnItemStack())

View file

@ -11,7 +11,6 @@ import com.pahimar.ee3.exchange.EnergyValueStackMapping;
import com.pahimar.ee3.exchange.OreStack;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.knowledge.PlayerKnowledge;
import com.pahimar.ee3.knowledge.TransmutationKnowledge;
import com.pahimar.ee3.reference.Reference;
import com.pahimar.ee3.util.serialize.*;
import cpw.mods.fml.common.FMLCommonHandler;
@ -54,16 +53,6 @@ public class SerializationHelper {
return instanceDataDirectory;
}
/**
* Returns a File reference to the mod specific directory in the playerdata directory
*
* @return
*/
public static File getInstancePlayerDataDirectory()
{
return instancePlayerDataDirectory;
}
/**
* TODO Move this to {@link com.pahimar.ee3.reference.Files}
*
@ -78,67 +67,6 @@ public class SerializationHelper {
instancePlayerDataDirectory.mkdirs();
}
public static TransmutationKnowledge readTransmutationKnowledgeFromFile(File directory, String fileName)
{
if (!directory.exists())
{
directory.mkdirs();
}
return TransmutationKnowledge.readFromFile(new File(directory, fileName));
}
public static void writeTransmutationKnowledgeToFile(File directory, String fileName, TransmutationKnowledge transmutationKnowledge)
{
writeTransmutationKnowledgeToFile(directory, fileName, transmutationKnowledge, false);
}
public static void writeTransmutationKnowledgeToFile(File directory, String fileName, TransmutationKnowledge transmutationKnowledge, boolean verboseLogging)
{
if (directory != null && fileName != null)
{
if (!directory.exists())
{
directory.mkdirs();
}
if (transmutationKnowledge == null)
{
transmutationKnowledge = new TransmutationKnowledge();
}
try
{
File file1 = new File(directory, fileName + ".tmp");
File file2 = new File(directory, fileName);
TransmutationKnowledge.writeToFile(file1, transmutationKnowledge);
if (file2.exists())
{
file2.delete();
}
file1.renameTo(file2);
if (verboseLogging)
{
LogHelper.info("Successfully saved TransmutationKnowledge to file: {}", file2.getAbsolutePath());
}
}
catch (Exception exception)
{
exception.printStackTrace();
LogHelper.error("Failed to save TransmutationKnowledge to file: {}{}", directory.getAbsolutePath(), fileName);
}
}
}
public static Map<WrappedStack, EnergyValue> readEnergyValueStackMapFromJsonFile(String fileName)
{
File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues");
return readEnergyValueStackMapFromJsonFile(new File(energyValuesDataDirectory, fileName));
}
public static Map<WrappedStack, EnergyValue> readEnergyValueStackMapFromJsonFile(File jsonFile)
{
Map<WrappedStack, EnergyValue> energyValueStackMap = new TreeMap<WrappedStack, EnergyValue>();
@ -170,12 +98,6 @@ public class SerializationHelper {
return energyValueStackMap;
}
public static void writeEnergyValueStackMapToJsonFile(String fileName, Map<WrappedStack, EnergyValue> energyValueMap)
{
File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues");
writeEnergyValueStackMapToJsonFile(new File(energyValuesDataDirectory, fileName), energyValueMap);
}
public static void writeEnergyValueStackMapToJsonFile(File jsonFile, Map<WrappedStack, EnergyValue> energyValueMap)
{
JsonWriter jsonWriter;

View file

@ -3,16 +3,48 @@ package com.pahimar.ee3.util.serialize;
import com.google.gson.*;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.knowledge.PlayerKnowledge;
import com.pahimar.ee3.reference.Comparators;
import net.minecraft.item.ItemStack;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class PlayerKnowledgeSerializer implements JsonSerializer<PlayerKnowledge>, JsonDeserializer<PlayerKnowledge> {
@Override
public PlayerKnowledge deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
// FIXME Priority Number 1
return null;
Set<ItemStack> knownItemStacks = new TreeSet<>(Comparators.ID_COMPARATOR);
if (json.isJsonArray()) {
JsonArray jsonArray = json.getAsJsonArray();
Iterator<JsonElement> jsonArrayIterator = jsonArray.iterator();
while (jsonArrayIterator.hasNext()) {
JsonElement jsonElement = jsonArrayIterator.next();
if (jsonElement.isJsonObject()) {
WrappedStack wrappedStack = null;
try {
wrappedStack = context.deserialize(jsonElement, WrappedStack.class);
}
catch (JsonParseException e){
}
if (wrappedStack != null && wrappedStack.getWrappedObject() instanceof ItemStack) {
knownItemStacks.add((ItemStack) wrappedStack.getWrappedObject());
}
}
}
}
return new PlayerKnowledge(knownItemStacks);
}
@Override