summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2014-10-24 23:28:04 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2014-10-24 23:28:04 +0100
commit638723eb96a513b3020d76603c8f5ac8f1324d13 (patch)
tree0c534d6bba01f487b9e53707d2ff0dfb7540eda7
parentd8514d79700c3d79465b46b6f855ff5cfd3fa221 (diff)
downloadnoctis-638723eb96a513b3020d76603c8f5ac8f1324d13.tar.bz2
Add support for the KnowledgeBase and rework everything in that light
-rw-r--r--src/main/java/net/flarn/noctis/Noctis.java6
-rw-r--r--src/main/java/net/flarn/noctis/handler/PlayerHandler.java32
-rw-r--r--src/main/java/net/flarn/noctis/init/Recipes.java15
-rw-r--r--src/main/java/net/flarn/noctis/item/ItemCatalyst.java45
-rw-r--r--src/main/java/net/flarn/noctis/knowledge/InternalKnowledgeTree.java14
-rw-r--r--src/main/java/net/flarn/noctis/knowledge/KnowledgeBase.java126
-rw-r--r--src/main/java/net/flarn/noctis/knowledge/KnowledgeItem.java62
-rw-r--r--src/main/java/net/flarn/noctis/network/KnowledgeBaseTransfer.java49
-rw-r--r--src/main/java/net/flarn/noctis/network/PacketRegistry.java15
-rw-r--r--src/main/java/net/flarn/noctis/recipes/CatalysedRecipe.java24
-rw-r--r--src/main/java/net/flarn/noctis/refs/Names.java4
-rw-r--r--src/main/java/net/flarn/noctis/refs/Tags.java9
-rw-r--r--src/main/java/net/flarn/noctis/utils/KnowledgeHelper.java32
-rw-r--r--src/main/java/net/flarn/noctis/utils/PlayerHelper.java19
14 files changed, 437 insertions, 15 deletions
diff --git a/src/main/java/net/flarn/noctis/Noctis.java b/src/main/java/net/flarn/noctis/Noctis.java
index 54a53b1..6866ed7 100644
--- a/src/main/java/net/flarn/noctis/Noctis.java
+++ b/src/main/java/net/flarn/noctis/Noctis.java
@@ -7,8 +7,11 @@ import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import net.flarn.noctis.handler.ConfigHandler;
+import net.flarn.noctis.handler.PlayerHandler;
import net.flarn.noctis.init.ModItems;
import net.flarn.noctis.init.Recipes;
+import net.flarn.noctis.knowledge.InternalKnowledgeTree;
+import net.flarn.noctis.network.PacketRegistry;
import net.flarn.noctis.proxy.IProxy;
import net.flarn.noctis.refs.Refs;
@@ -26,7 +29,10 @@ public class Noctis {
// Network, mod config, items, blocks
ConfigHandler.init(event.getSuggestedConfigurationFile());
FMLCommonHandler.instance().bus().register(new ConfigHandler());
+ FMLCommonHandler.instance().bus().register(new PlayerHandler());
ModItems.init();
+ PacketRegistry.registerPackets();
+ InternalKnowledgeTree.init();
}
@Mod.EventHandler
diff --git a/src/main/java/net/flarn/noctis/handler/PlayerHandler.java b/src/main/java/net/flarn/noctis/handler/PlayerHandler.java
new file mode 100644
index 0000000..556d6be
--- /dev/null
+++ b/src/main/java/net/flarn/noctis/handler/PlayerHandler.java
@@ -0,0 +1,32 @@
+package net.flarn.noctis.handler;
+
+
+import cpw.mods.fml.common.eventhandler.SubscribeEvent;
+import cpw.mods.fml.common.gameevent.PlayerEvent;
+import cpw.mods.fml.relauncher.Side;
+import cpw.mods.fml.relauncher.SideOnly;
+import net.flarn.noctis.knowledge.InternalKnowledgeTree;
+import net.flarn.noctis.knowledge.KnowledgeBase;
+import net.flarn.noctis.knowledge.KnowledgeItem;
+import net.flarn.noctis.network.KnowledgeBaseTransfer;
+import net.flarn.noctis.network.PacketRegistry;
+import net.flarn.noctis.utils.KnowledgeHelper;
+import net.flarn.noctis.utils.LogHelper;
+import net.minecraft.entity.player.EntityPlayerMP;
+import sun.rmi.log.LogHandler;
+
+import java.util.UUID;
+
+public class PlayerHandler {
+
+ @SubscribeEvent
+ public void onPlayerLogsIn(PlayerEvent.PlayerLoggedInEvent playerLoggedInEvent) {
+
+ KnowledgeBase kb = KnowledgeHelper.loadKnowledgeBase(playerLoggedInEvent.player);
+ if (InternalKnowledgeTree.alwaysResetTree) {
+ kb.reset();
+ }
+ KnowledgeItem.expandKnowledgeBase(kb);
+ KnowledgeHelper.sendKnowledgeBase(playerLoggedInEvent.player, kb);
+ }
+}
diff --git a/src/main/java/net/flarn/noctis/init/Recipes.java b/src/main/java/net/flarn/noctis/init/Recipes.java
index acdfd76..51ed35a 100644
--- a/src/main/java/net/flarn/noctis/init/Recipes.java
+++ b/src/main/java/net/flarn/noctis/init/Recipes.java
@@ -3,6 +3,7 @@ package net.flarn.noctis.init;
import cpw.mods.fml.common.registry.GameRegistry;
import net.flarn.noctis.handler.ConfigHandler;
import net.flarn.noctis.item.ItemCatalyst;
+import net.flarn.noctis.knowledge.KnowledgeItem;
import net.flarn.noctis.recipes.CatalysedRecipe;
import net.flarn.noctis.refs.Numbers;
import net.flarn.noctis.utils.CatalystType;
@@ -32,12 +33,22 @@ public class Recipes {
}
public static void initCatalysedRecipes() {
+ /* Swiftness potion I == 8194, health potion I == 8197, these might change */
+ CatalysedRecipe.newRecipe(CatalystType.LIGHT, new ItemStack(Items.potionitem, 1, 8194),
+ new ItemStack[]{new ItemStack(Items.potionitem, 1, 0), new ItemStack(Items.sugar, 1, 0)}, 10, Times.NIGHT, SkyAccess.SKY,
+ KnowledgeItem.findKnowledgeItem("noctis.potion.speed"), KnowledgeItem.findKnowledgeItem("noctis.potion.speed"));
+ CatalysedRecipe.newRecipe(CatalystType.LIGHT, new ItemStack(Items.potionitem, 1, 8197),
+ new ItemStack[]{new ItemStack(Items.potionitem, 1, 0), new ItemStack(Items.speckled_melon, 1, 0)}, 10, Times.NIGHT, SkyAccess.SKY,
+ KnowledgeItem.findKnowledgeItem("noctis.potion.health"), KnowledgeItem.findKnowledgeItem("noctis.potion.health"));
+
+ /*
if (ConfigHandler.cheapRecipes) {
CatalysedRecipe.newRecipe(CatalystType.LIGHT, new ItemStack(ModItems.glassSword),
- new ItemStack[]{new ItemStack(Blocks.glass, 2, 0), new ItemStack(Items.stick)}, 10, Times.NIGHT, SkyAccess.SKY);
+ new ItemStack[]{new ItemStack(Blocks.glass, 2, 0), new ItemStack(Items.stick)}, 10, Times.NIGHT, SkyAccess.SKY,
+ );
} else {
CatalysedRecipe.newRecipe(CatalystType.LIGHT, new ItemStack(ModItems.glassSword),
new ItemStack[]{new ItemStack(Blocks.glass, 2, 0), new ItemStack(Items.diamond_sword)}, 10, Times.NIGHT, SkyAccess.SKY);
- }
+ }*/
}
}
diff --git a/src/main/java/net/flarn/noctis/item/ItemCatalyst.java b/src/main/java/net/flarn/noctis/item/ItemCatalyst.java
index 6cfb43d..e823a97 100644
--- a/src/main/java/net/flarn/noctis/item/ItemCatalyst.java
+++ b/src/main/java/net/flarn/noctis/item/ItemCatalyst.java
@@ -1,8 +1,14 @@
package net.flarn.noctis.item;
+import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.flarn.noctis.init.ModItems;
+import net.flarn.noctis.knowledge.KnowledgeBase;
+import net.flarn.noctis.knowledge.KnowledgeItem;
+import net.flarn.noctis.knowledge.KnowledgeVisibility;
+import net.flarn.noctis.network.KnowledgeBaseTransfer;
+import net.flarn.noctis.network.PacketRegistry;
import net.flarn.noctis.recipes.CatalysedRecipe;
import net.flarn.noctis.refs.*;
import net.flarn.noctis.utils.*;
@@ -10,6 +16,7 @@ import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
@@ -110,7 +117,8 @@ public class ItemCatalyst extends ItemNoctisBase {
AxisAlignedBB axisalignedbb = AxisAlignedBB.getBoundingBox(entityItem.posX - 0.5, entityItem.posY, entityItem.posZ - 0.5,
entityItem.posX + 0.5, entityItem.posY + 1.0, entityItem.posZ + 0.5);
List entities = entityItem.worldObj.getEntitiesWithinAABB(EntityItem.class, axisalignedbb);
- List recipes = CatalysedRecipe.getPossibleRecipes(entities, getCatalystType(s), nowTime, getEnergy(s), skyAccess);
+ KnowledgeBase knowledgeBase = KnowledgeHelper.loadKnowledgeBase(entityItem.worldObj, NBTHelper.getUUID(entityItem.getEntityItem(), Tags.General.OWNER));
+ List recipes = CatalysedRecipe.getPossibleRecipes(entities, getCatalystType(s), nowTime, getEnergy(s), skyAccess, knowledgeBase);
if (!recipes.isEmpty()) {
/* Something could be crafted, so craft the first thing returned */
if (!entityItem.worldObj.isRemote) {
@@ -119,6 +127,18 @@ public class ItemCatalyst extends ItemNoctisBase {
EntityItem newItem = new EntityItem(entityItem.worldObj, entityItem.posX, entityItem.posY, entityItem.posZ,
recipe.output.copy());
entityItem.worldObj.spawnEntityInWorld(newItem);
+ boolean changedsomething = knowledgeBase.setKnowledgeVisibility(recipe.requiredKnowledge.name, KnowledgeVisibility.KNOWN);
+ if (recipe.triggerKnowledge != null) {
+ changedsomething = changedsomething || recipe.triggerKnowledge.triggerKnowledge(knowledgeBase);
+ }
+ if (changedsomething) {
+ KnowledgeItem.expandKnowledgeBase(knowledgeBase);
+ EntityPlayer player = PlayerHelper.getPlayerEntity(NBTHelper.getUUID(entityItem.getEntityItem(), Tags.General.OWNER));
+ if (player != null) {
+ KnowledgeHelper.sendKnowledgeBase(player, knowledgeBase);
+ }
+
+ }
}
}
@@ -139,6 +159,16 @@ public class ItemCatalyst extends ItemNoctisBase {
if (!NBTHelper.hasEntry(s, Tags.General.OWNER)) {
NBTHelper.setUUID(s, Tags.General.OWNER, p.getUniqueID());
LogHelper.warn("Set ownership of " + s + " to " + p.getUniqueID());
+ if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) {
+ /* We bound a catalyst, best let the knowledgebase know */
+ KnowledgeBase knowledgeBase = KnowledgeHelper.loadKnowledgeBase(p);
+ KnowledgeItem knowledgeItem = KnowledgeItem.findKnowledgeItem(Names.Items.CATALYST_BOUND_KNOWLEDGE[getCatalystType(s).getIndex()]);
+ if (knowledgeItem.triggerKnowledge(knowledgeBase)) {
+ KnowledgeItem.expandKnowledgeBase(knowledgeBase);
+ KnowledgeHelper.sendKnowledgeBase(p, knowledgeBase);
+ }
+ }
+
} else {
LogHelper.warn("Owner already set to " + NBTHelper.getUUID(s, Tags.General.OWNER));
LogHelper.warn("Player ID is " + p.getUniqueID());
@@ -160,6 +190,19 @@ public class ItemCatalyst extends ItemNoctisBase {
}
@Override
+ public void onCreated(ItemStack stack, World world, EntityPlayer player) {
+ if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) {
+ /* A catalyst was created, so we should update this player's knowledgebase */
+ KnowledgeBase kb = KnowledgeHelper.loadKnowledgeBase(world, player.getPersistentID());
+ KnowledgeItem triggered = KnowledgeItem.findKnowledgeItem(Names.Items.CATALYST_KNOWLEDGE[getCatalystType(stack).getIndex()]);
+ if (triggered.triggerKnowledge(kb)) {
+ KnowledgeItem.expandKnowledgeBase(kb);
+ KnowledgeHelper.sendKnowledgeBase(player, kb);
+ }
+ }
+ }
+
+ @Override
@SideOnly(Side.CLIENT)
public boolean hasEffect(ItemStack s, int pass) {
return NBTHelper.hasEntry(s, Tags.General.OWNER);
diff --git a/src/main/java/net/flarn/noctis/knowledge/InternalKnowledgeTree.java b/src/main/java/net/flarn/noctis/knowledge/InternalKnowledgeTree.java
index 737c544..ef378eb 100644
--- a/src/main/java/net/flarn/noctis/knowledge/InternalKnowledgeTree.java
+++ b/src/main/java/net/flarn/noctis/knowledge/InternalKnowledgeTree.java
@@ -2,10 +2,12 @@ package net.flarn.noctis.knowledge;
public class InternalKnowledgeTree {
- public static final KnowledgeItem ROOT_KNOWLEDGE = KnowledgeItem.addKnowledgeItem("root", true, KnowledgeVisibility.KNOWN);
- public static final KnowledgeItem LIGHT_CATALYST = KnowledgeItem.addKnowledgeItem("catalyst.light", true, KnowledgeVisibility.KNOWN, ROOT_KNOWLEDGE);
- public static final KnowledgeItem BOUND_CATALYST = KnowledgeItem.addKnowledgeItem("catalyst.bound", true, KnowledgeVisibility.THEORY, LIGHT_CATALYST);
- public static final KnowledgeItem POTION_SPEED = KnowledgeItem.addKnowledgeItem("potion.speed", true, KnowledgeVisibility.THEORY, BOUND_CATALYST);
- public static final KnowledgeItem POTION_HEALTH = KnowledgeItem.addKnowledgeItem("potion.health", true, KnowledgeVisibility.HIDDEN, BOUND_CATALYST);
-
+ public static final boolean alwaysResetTree = true;
+ public static void init() {
+ KnowledgeItem ROOT_KNOWLEDGE = KnowledgeItem.addKnowledgeItem("noctis.root", true, KnowledgeVisibility.KNOWN);
+ KnowledgeItem LIGHT_CATALYST = KnowledgeItem.addKnowledgeItem("noctis.catalyst.light", true, KnowledgeVisibility.THEORY, ROOT_KNOWLEDGE);
+ KnowledgeItem LIGHT_BOUND_CATALYST = KnowledgeItem.addKnowledgeItem("noctis.catalyst.light.bound", true, KnowledgeVisibility.THEORY, LIGHT_CATALYST);
+ KnowledgeItem POTION_SPEED = KnowledgeItem.addKnowledgeItem("noctis.potion.speed", true, KnowledgeVisibility.THEORY, LIGHT_BOUND_CATALYST);
+ KnowledgeItem POTION_HEALTH = KnowledgeItem.addKnowledgeItem("noctis.potion.health", true, KnowledgeVisibility.HIDDEN, LIGHT_BOUND_CATALYST);
+ }
}
diff --git a/src/main/java/net/flarn/noctis/knowledge/KnowledgeBase.java b/src/main/java/net/flarn/noctis/knowledge/KnowledgeBase.java
new file mode 100644
index 0000000..a595def
--- /dev/null
+++ b/src/main/java/net/flarn/noctis/knowledge/KnowledgeBase.java
@@ -0,0 +1,126 @@
+package net.flarn.noctis.knowledge;
+
+
+import cpw.mods.fml.common.FMLCommonHandler;
+import cpw.mods.fml.server.FMLServerHandler;
+import net.flarn.noctis.refs.Tags;
+import net.flarn.noctis.utils.LogHelper;
+import net.minecraft.nbt.NBTTagCompound;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.world.World;
+import net.minecraft.world.WorldSavedData;
+
+import java.util.*;
+
+import static java.util.Collections.sort;
+
+public class KnowledgeBase extends WorldSavedData {
+ public class KnowledgeEvent implements Comparable<KnowledgeEvent> {
+ public final String name;
+ public final KnowledgeVisibility vis;
+ public final long time;
+ public KnowledgeEvent(String _name, KnowledgeVisibility _vis, long _time) {
+ name = _name;
+ vis = _vis;
+ time = _time;
+ }
+
+ @Override
+ public int compareTo(KnowledgeEvent knowledgeEvent) {
+ int ret = Long.compare(time, knowledgeEvent.time);
+ if (ret == 0) {
+ ret = Long.compare(name.length(), knowledgeEvent.name.length());
+ }
+ return ret;
+ }
+ }
+ private Map<String, KnowledgeVisibility> visiblityMap;
+ private Map<String, Long> theoryMap;
+ private Map<String, Long> knownMap;
+ private List<KnowledgeEvent> eventSequence;
+
+ public KnowledgeBase(String tagName) {
+ super(tagName);
+ LogHelper.warn("Instantiating knowledge base: " + tagName);
+ reset();
+ }
+
+ public KnowledgeVisibility getKnowledgeVisibility(String knowledgeName) {
+ if (visiblityMap.containsKey(knowledgeName)) {
+ return visiblityMap.get(knowledgeName);
+ }
+ return KnowledgeVisibility.UNKNOWN;
+ }
+
+ private long getTime() {
+ MinecraftServer s = MinecraftServer.getServer();
+ if (s != null) {
+ World w = s.getEntityWorld();
+ if (w != null) {
+ return w.getTotalWorldTime();
+ }
+ }
+ return 0;
+ }
+
+ public boolean setKnowledgeVisibility(String name, KnowledgeVisibility visOnGet) {
+ if (visiblityMap.containsKey(name) && visiblityMap.get(name) == visOnGet) { return false; }
+ visiblityMap.put(name, visOnGet);
+ if (visOnGet == KnowledgeVisibility.THEORY) {
+ theoryMap.put(name, getTime());
+ eventSequence.add(new KnowledgeEvent(name, visOnGet, getTime()));
+ }
+ if (visOnGet == KnowledgeVisibility.KNOWN) {
+ knownMap.put(name, getTime());
+ eventSequence.add(new KnowledgeEvent(name, visOnGet, getTime()));
+ }
+ markDirty();
+ return true;
+ }
+
+ public void writeToNBT(NBTTagCompound tagCompound) {
+ for (String key : visiblityMap.keySet()) {
+ NBTTagCompound comp = new NBTTagCompound();
+ comp.setInteger(Tags.KnowledgeBase.VISIBILITY, visiblityMap.get(key).toInteger());
+ if (theoryMap.containsKey(key)) {
+ comp.setLong(Tags.KnowledgeBase.THEORYTIME, theoryMap.get(key));
+ }
+ if (knownMap.containsKey(key)) {
+ comp.setLong(Tags.KnowledgeBase.KNOWNTIME, knownMap.get(key));
+ }
+ tagCompound.setTag(key, comp);
+ }
+ }
+
+ @Override
+ public void readFromNBT(NBTTagCompound nbtTagCompound) {
+ for (Object _key : nbtTagCompound.func_150296_c()) {
+ String key = (String)_key;
+ if (key != null) {
+ NBTTagCompound comp = nbtTagCompound.getCompoundTag(key);
+ visiblityMap.put(key, KnowledgeVisibility.fromInt(comp.getInteger(Tags.KnowledgeBase.VISIBILITY)));
+ if (comp.hasKey(Tags.KnowledgeBase.THEORYTIME)) {
+ long time = comp.getLong(Tags.KnowledgeBase.THEORYTIME);
+ theoryMap.put(key, time);
+ eventSequence.add(new KnowledgeEvent(key, KnowledgeVisibility.THEORY, time));
+ }
+ if (comp.hasKey(Tags.KnowledgeBase.KNOWNTIME)) {
+ long time = comp.getLong(Tags.KnowledgeBase.KNOWNTIME);
+ knownMap.put(key, time);
+ eventSequence.add(new KnowledgeEvent(key, KnowledgeVisibility.KNOWN, time));
+ }
+ }
+ }
+ Collections.sort(eventSequence);
+ }
+
+ public void reset() {
+ visiblityMap = new HashMap<String, KnowledgeVisibility>();
+ theoryMap = new HashMap<String, Long>();
+ knownMap = new HashMap<String, Long>();
+ eventSequence = new LinkedList<KnowledgeEvent>();
+ }
+
+ public List<KnowledgeEvent> getEventSequence() { return eventSequence; }
+}
+
diff --git a/src/main/java/net/flarn/noctis/knowledge/KnowledgeItem.java b/src/main/java/net/flarn/noctis/knowledge/KnowledgeItem.java
index edf5e3a..ceb0ca7 100644
--- a/src/main/java/net/flarn/noctis/knowledge/KnowledgeItem.java
+++ b/src/main/java/net/flarn/noctis/knowledge/KnowledgeItem.java
@@ -1,7 +1,13 @@
package net.flarn.noctis.knowledge;
+import cpw.mods.fml.relauncher.Side;
+import cpw.mods.fml.relauncher.SideOnly;
+import net.flarn.noctis.network.KnowledgeBaseTransfer;
+import net.flarn.noctis.network.PacketRegistry;
import net.flarn.noctis.utils.LogHelper;
+import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.entity.player.EntityPlayerMP;
import java.util.HashMap;
import java.util.HashSet;
@@ -11,7 +17,7 @@ public class KnowledgeItem {
private static HashMap<String, KnowledgeItem> allKnowledgeItems = new HashMap<String, KnowledgeItem>();
private KnowledgeItem prerequisites[];
private Set<KnowledgeItem> dependents;
- private String name;
+ public final String name;
boolean autoGet;
KnowledgeVisibility visOnGet;
@@ -47,6 +53,7 @@ public class KnowledgeItem {
if (ki.findLoops(new HashSet<KnowledgeItem>())) {
LogHelper.fatal("Loop detected adding " + ki);
}
+ LogHelper.warn("Knowledge item " + ki.name + " remembered");
allKnowledgeItems.put(_name, ki);
return ki;
}
@@ -55,4 +62,57 @@ public class KnowledgeItem {
return allKnowledgeItems.get(_name);
}
+ public static boolean expandKnowledgeBase(KnowledgeBase b) {
+ boolean didExpand = false;
+ boolean thisExpanded = false;
+ LogHelper.warn("Attempting to expand knowledgebase");
+ do {
+ thisExpanded = false;
+ for (KnowledgeItem ki : allKnowledgeItems.values()) {
+ LogHelper.warn("Considering " + ki.name);
+ if (ki.autoGet == false) { continue; }
+ if (b.getKnowledgeVisibility(ki.name) != KnowledgeVisibility.UNKNOWN) {
+ continue;
+ }
+ boolean prereqsmet = true;
+ for (KnowledgeItem prereq : ki.prerequisites) {
+ if (b.getKnowledgeVisibility(prereq.name) != KnowledgeVisibility.KNOWN) {
+ prereqsmet = false;
+ break;
+ }
+ }
+ LogHelper.warn("Prerequisites met? " + prereqsmet);
+ if (prereqsmet) {
+ thisExpanded = thisExpanded || b.setKnowledgeVisibility(ki.name, ki.visOnGet);
+ didExpand = didExpand || thisExpanded;
+ }
+ }
+ } while (thisExpanded);
+ return didExpand;
+ }
+
+ public boolean triggerKnowledge(KnowledgeBase b) {
+ boolean prereqsmet = true;
+ switch (b.getKnowledgeVisibility(name)) {
+ case UNKNOWN:
+ break;
+ case HIDDEN:
+ case THEORY:
+ b.setKnowledgeVisibility(name, KnowledgeVisibility.KNOWN);
+ return true;
+ case KNOWN:
+ return false;
+ }
+ for (KnowledgeItem prereq : prerequisites) {
+ if (b.getKnowledgeVisibility(prereq.name) != KnowledgeVisibility.KNOWN) {
+ prereqsmet = false;
+ break;
+ }
+ }
+ if (prereqsmet) {
+ return b.setKnowledgeVisibility(name, visOnGet);
+ }
+ return false;
+ }
+
}
diff --git a/src/main/java/net/flarn/noctis/network/KnowledgeBaseTransfer.java b/src/main/java/net/flarn/noctis/network/KnowledgeBaseTransfer.java
new file mode 100644
index 0000000..6efde68
--- /dev/null
+++ b/src/main/java/net/flarn/noctis/network/KnowledgeBaseTransfer.java
@@ -0,0 +1,49 @@
+package net.flarn.noctis.network;
+
+import cpw.mods.fml.client.FMLClientHandler;
+import cpw.mods.fml.common.network.ByteBufUtils;
+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.flarn.noctis.knowledge.KnowledgeBase;
+import net.flarn.noctis.refs.Tags;
+import net.flarn.noctis.utils.LogHelper;
+import net.flarn.noctis.utils.PlayerHelper;
+import net.minecraft.nbt.NBTTagCompound;
+import net.minecraftforge.common.util.Constants;
+
+public class KnowledgeBaseTransfer implements IMessage {
+
+ private NBTTagCompound kbTag;
+
+ public KnowledgeBaseTransfer() {
+ kbTag = new NBTTagCompound();
+ }
+
+ public KnowledgeBaseTransfer(KnowledgeBase kb) {
+ kbTag = new NBTTagCompound();
+ kb.writeToNBT(kbTag);
+ }
+
+ @Override
+ public void toBytes(ByteBuf buffer) {
+ ByteBufUtils.writeTag(buffer, kbTag);
+ }
+
+ @Override
+ public void fromBytes(ByteBuf buffer) {
+ kbTag = ByteBufUtils.readTag(buffer);
+ }
+
+ public static class Handler implements IMessageHandler<KnowledgeBaseTransfer, IMessage> {
+ @Override
+ public IMessage onMessage(KnowledgeBaseTransfer message, MessageContext ctx) {
+ /* This packet only ever goes SERVER -> CLIENT */
+ LogHelper.warn("Processing incoming knowledgebase: " + message.kbTag.toString());
+ NBTTagCompound noctisData = PlayerHelper.getNBTData(FMLClientHandler.instance().getClient().thePlayer);
+ noctisData.setTag(Tags.Player.KNOWLEDGE_BASE, message.kbTag);
+ return null;
+ }
+ }
+}
diff --git a/src/main/java/net/flarn/noctis/network/PacketRegistry.java b/src/main/java/net/flarn/noctis/network/PacketRegistry.java
new file mode 100644
index 0000000..c39d14e
--- /dev/null
+++ b/src/main/java/net/flarn/noctis/network/PacketRegistry.java
@@ -0,0 +1,15 @@
+package net.flarn.noctis.network;
+
+import cpw.mods.fml.common.network.NetworkRegistry;
+import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
+import cpw.mods.fml.relauncher.Side;
+import net.flarn.noctis.refs.Names;
+import net.flarn.noctis.refs.Refs;
+
+public class PacketRegistry {
+ public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(Refs.MOD_ID);
+ public static void registerPackets() {
+ int id = 0;
+ INSTANCE.registerMessage(KnowledgeBaseTransfer.Handler.class, KnowledgeBaseTransfer.class, id++, Side.CLIENT);
+ }
+}
diff --git a/src/main/java/net/flarn/noctis/recipes/CatalysedRecipe.java b/src/main/java/net/flarn/noctis/recipes/CatalysedRecipe.java
index a80de5b..d27a5c9 100644
--- a/src/main/java/net/flarn/noctis/recipes/CatalysedRecipe.java
+++ b/src/main/java/net/flarn/noctis/recipes/CatalysedRecipe.java
@@ -1,5 +1,8 @@
package net.flarn.noctis.recipes;
+import net.flarn.noctis.knowledge.KnowledgeBase;
+import net.flarn.noctis.knowledge.KnowledgeItem;
+import net.flarn.noctis.knowledge.KnowledgeVisibility;
import net.flarn.noctis.utils.*;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
@@ -14,6 +17,8 @@ public class CatalysedRecipe {
public final int energy;
public final Times timeNeeded;
public final SkyAccess skyAccess;
+ public final KnowledgeItem requiredKnowledge;
+ public final KnowledgeItem triggerKnowledge;
private static List<CatalysedRecipe> recipes = new LinkedList();
@@ -22,13 +27,17 @@ public class CatalysedRecipe {
ItemStack[] _ingredients,
int _energy,
Times _timeNeeded,
- SkyAccess _skyAccess) {
+ SkyAccess _skyAccess,
+ KnowledgeItem _requiredKnowledge,
+ KnowledgeItem _triggerKnowledge) {
catalyst = _catalyst;
ingredients = _ingredients;
output = _output;
energy = _energy;
timeNeeded = _timeNeeded;
skyAccess = _skyAccess;
+ requiredKnowledge = _requiredKnowledge;
+ triggerKnowledge = _triggerKnowledge;
}
public static void newRecipe(CatalystType _catalyst,
@@ -36,8 +45,11 @@ public class CatalysedRecipe {
ItemStack[] _ingredients,
int _energy,
Times _timeNeeded,
- SkyAccess _skyAccess) {
- recipes.add(new CatalysedRecipe(_catalyst, _output, _ingredients, _energy, _timeNeeded, _skyAccess));
+ SkyAccess _skyAccess,
+ KnowledgeItem _requiredKnowledge,
+ KnowledgeItem _triggerKnowledge) {
+ recipes.add(new CatalysedRecipe(_catalyst, _output, _ingredients, _energy, _timeNeeded, _skyAccess,
+ _requiredKnowledge, _triggerKnowledge));
}
public boolean areIngredientsPresent(List entities) {
@@ -82,14 +94,16 @@ public class CatalysedRecipe {
public static List<CatalysedRecipe> getPossibleRecipes(List entities, CatalystType catalyst,
Times now, int energyavailable,
- SkyAccess skyAccessNow) {
+ SkyAccess skyAccessNow,
+ KnowledgeBase knowledgeBase) {
List<CatalysedRecipe> ret = new LinkedList();
for (CatalysedRecipe r : recipes) {
if (r.energy <= energyavailable &&
now.meetsNeed(r.timeNeeded) &&
catalyst.meetsNeeds(r.catalyst) &&
skyAccessNow.meetsNeed(r.skyAccess) &&
- r.areIngredientsPresent(entities)) {
+ r.areIngredientsPresent(entities) &&
+ knowledgeBase.getKnowledgeVisibility(r.requiredKnowledge.name) != KnowledgeVisibility.UNKNOWN) {
ret.add(r);
}
}
diff --git a/src/main/java/net/flarn/noctis/refs/Names.java b/src/main/java/net/flarn/noctis/refs/Names.java
index ff3fe17..29c7790 100644
--- a/src/main/java/net/flarn/noctis/refs/Names.java
+++ b/src/main/java/net/flarn/noctis/refs/Names.java
@@ -2,9 +2,13 @@ package net.flarn.noctis.refs;
public class Names {
+ public static final String KNOWLEDGE_BASE_NAME = "noctis.knowledgebase.";
+
public static final class Items {
public static final String GLASS_SWORD = "glassSword";
public static final String CATALYST = "catalyst";
public static final String CATALYSTS[] = new String[] { "lightCatalyst", "darkCatalyst", "mixedCatalyst" };
+ public static final String CATALYST_KNOWLEDGE[] = new String[] { "noctis.catalyst.light", "noctis.catalyst.dark", "noctis.catalyst.mixed" };
+ public static final String CATALYST_BOUND_KNOWLEDGE[] = new String[] { "noctis.catalyst.light.bound", "noctis.catalyst.dark.bound", "noctis.catalyst.mixed.bound" };
}
}
diff --git a/src/main/java/net/flarn/noctis/refs/Tags.java b/src/main/java/net/flarn/noctis/refs/Tags.java
index d523f64..b5433c8 100644
--- a/src/main/java/net/flarn/noctis/refs/Tags.java
+++ b/src/main/java/net/flarn/noctis/refs/Tags.java
@@ -6,4 +6,13 @@ public class Tags {
public static final String OWNER = "owner";
public static final String TYPE = "type";
}
+ public static final class Player {
+ public static final String KNOWLEDGE_BASE = "knowledgebase";
+ }
+
+ public static final class KnowledgeBase {
+ public static final String VISIBILITY = "visibility";
+ public static final String THEORYTIME = "theorytime";
+ public static final String KNOWNTIME = "knowntime";
+ }
}
diff --git a/src/main/java/net/flarn/noctis/utils/KnowledgeHelper.java b/src/main/java/net/flarn/noctis/utils/KnowledgeHelper.java
new file mode 100644
index 0000000..e7861d5
--- /dev/null
+++ b/src/main/java/net/flarn/noctis/utils/KnowledgeHelper.java
@@ -0,0 +1,32 @@
+package net.flarn.noctis.utils;
+
+import net.flarn.noctis.knowledge.KnowledgeBase;
+import net.flarn.noctis.network.KnowledgeBaseTransfer;
+import net.flarn.noctis.network.PacketRegistry;
+import net.flarn.noctis.refs.Names;
+import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.entity.player.EntityPlayerMP;
+import net.minecraft.world.World;
+
+import java.util.UUID;
+
+public class KnowledgeHelper {
+ public static KnowledgeBase loadKnowledgeBase(World world, UUID owner) {
+ KnowledgeBase ret = (KnowledgeBase)world.loadItemData(KnowledgeBase.class, Names.KNOWLEDGE_BASE_NAME + owner.toString());
+ if (ret == null) {
+ ret = new KnowledgeBase(Names.KNOWLEDGE_BASE_NAME + owner.toString());
+ world.setItemData(Names.KNOWLEDGE_BASE_NAME + owner.toString(), ret);
+ }
+ return ret;
+ }
+
+ public static KnowledgeBase loadKnowledgeBase(EntityPlayer player) {
+ return loadKnowledgeBase(player.worldObj, player.getPersistentID());
+ }
+
+ public static void sendKnowledgeBase(EntityPlayer player, KnowledgeBase kb) {
+ KnowledgeBaseTransfer xfer = new KnowledgeBaseTransfer(kb);
+ LogHelper.warn("Attempting to send KnowledgeBase to " + player);
+ PacketRegistry.INSTANCE.sendTo(xfer, (EntityPlayerMP) player);
+ }
+}
diff --git a/src/main/java/net/flarn/noctis/utils/PlayerHelper.java b/src/main/java/net/flarn/noctis/utils/PlayerHelper.java
index fde7638..0615b76 100644
--- a/src/main/java/net/flarn/noctis/utils/PlayerHelper.java
+++ b/src/main/java/net/flarn/noctis/utils/PlayerHelper.java
@@ -2,6 +2,9 @@ package net.flarn.noctis.utils;
import com.mojang.authlib.GameProfile;
+import net.flarn.noctis.refs.Refs;
+import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import java.util.UUID;
@@ -14,4 +17,20 @@ public class PlayerHelper {
return profile.getName();
}
+ public static NBTTagCompound getNBTData(EntityPlayer player) {
+ if (!player.getEntityData().hasKey(player.PERSISTED_NBT_TAG)) {
+ player.getEntityData().setTag(player.PERSISTED_NBT_TAG, new NBTTagCompound());
+ }
+ NBTTagCompound compound = player.getEntityData().getCompoundTag(player.PERSISTED_NBT_TAG);
+ if (!compound.hasKey(Refs.MOD_ID)) {
+ compound.setTag(Refs.MOD_ID, new NBTTagCompound());
+ }
+ return compound.getCompoundTag(Refs.MOD_ID);
+ }
+
+ public static EntityPlayer getPlayerEntity(UUID uuid) {
+ MinecraftServer server = MinecraftServer.getServer();
+ GameProfile profile = server.func_152358_ax().func_152652_a(uuid);
+ return server.getEntityWorld().getPlayerEntityByName(profile.getName());
+ }
}