More work on the new player knowledge system

This commit is contained in:
Pahimar 2016-05-19 22:17:31 -04:00
parent a7e85aba7b
commit 591a9ab2eb
3 changed files with 61 additions and 5 deletions

View file

@ -11,9 +11,6 @@ import java.util.TreeSet;
public class PlayerKnowledge {
private static final int VERSION = 1;
private int version;
private Set<ItemStack> knownItemStacks;
public PlayerKnowledge() {
@ -22,7 +19,6 @@ public class PlayerKnowledge {
public PlayerKnowledge(Collection<ItemStack> itemStacks) {
version = VERSION;
knownItemStacks = new TreeSet<>(Comparators.ID_COMPARATOR);
if (itemStacks != null) {
@ -36,7 +32,7 @@ public class PlayerKnowledge {
return knownItemStacks.contains(ItemHelper.clone(itemStack, 1));
}
public Set<ItemStack> get() {
public Set<ItemStack> getKnownItemStacks() {
return knownItemStacks;
}
@ -48,6 +44,13 @@ public class PlayerKnowledge {
}
}
public void learn(Collection<ItemStack> itemStacks) {
for (ItemStack itemStack : itemStacks) {
learn(itemStack);
}
}
public void forget(ItemStack itemStack) {
if (itemStack != null) {
@ -56,7 +59,28 @@ public class PlayerKnowledge {
}
}
public void forget(Collection<ItemStack> itemStacks) {
for (ItemStack itemStack : itemStacks) {
forget(itemStack);
}
}
public void forgetAll() {
knownItemStacks.clear();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (ItemStack itemStack : knownItemStacks) {
stringBuilder.append(String.format("%s, ", ItemHelper.toString(itemStack)));
}
stringBuilder.append("]");
return stringBuilder.toString();
}
}

View file

@ -10,6 +10,7 @@ import com.pahimar.ee3.api.exchange.EnergyValue;
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.*;
@ -35,6 +36,7 @@ public class SerializationHelper {
.registerTypeAdapter(OreStack.class, new OreStackSerializer())
.registerTypeAdapter(FluidStack.class, new FluidStackSerializer())
.registerTypeAdapter(WrappedStack.class, new WrappedStackSerializer())
.registerTypeAdapter(PlayerKnowledge.class, new PlayerKnowledgeSerializer())
.registerTypeAdapter(ENERGY_VALUE_MAP_TYPE, new EnergyValueMapSerializer())
.create();

View file

@ -0,0 +1,30 @@
package com.pahimar.ee3.util.serialize;
import com.google.gson.*;
import com.pahimar.ee3.knowledge.PlayerKnowledge;
import net.minecraft.item.ItemStack;
import java.lang.reflect.Type;
public class PlayerKnowledgeSerializer implements JsonSerializer<PlayerKnowledge>, JsonDeserializer<PlayerKnowledge> {
@Override
public PlayerKnowledge deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
// TODO Pick up here
return null;
}
@Override
public JsonElement serialize(PlayerKnowledge src, Type typeOfSrc, JsonSerializationContext context) {
JsonArray jsonArray = new JsonArray();
if (src != null) {
for (ItemStack itemStack : src.getKnownItemStacks()) {
jsonArray.add(context.serialize(itemStack));
}
}
return jsonArray;
}
}