Changed condition deserializing in order to allow arguments inside the .json and utility functions in Position

This commit is contained in:
jglrxavpok 2020-05-21 19:17:34 +02:00
parent 3d919db633
commit 556c6fd837
6 changed files with 76 additions and 27 deletions

View File

@ -1,8 +1,13 @@
package net.minestom.server.gamedata.conditions;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import net.minestom.server.data.Data;
import net.minestom.server.gamedata.Condition;
import java.lang.reflect.Type;
import java.util.Random;
/**
@ -19,4 +24,11 @@ public class SurvivesExplosionCondition implements Condition {
return true; // no explosion here
return rng.nextDouble() <= 1.0/data.<Double>get("explosionPower");
}
public static class Deserializer implements JsonDeserializer<SurvivesExplosionCondition> {
@Override
public SurvivesExplosionCondition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new SurvivesExplosionCondition();
}
}
}

View File

@ -0,0 +1,36 @@
package net.minestom.server.gamedata.loottables;
import com.google.gson.*;
import net.minestom.server.gamedata.Condition;
import net.minestom.server.utils.NamespaceID;
import java.lang.reflect.Type;
public abstract class ConditionContainer {
private ConditionContainer() {}
public abstract Condition create(LootTableManager lootTableManager);
static class Deserializer implements JsonDeserializer<ConditionContainer> {
private final LootTableManager lootTableManager;
Deserializer(LootTableManager lootTableManager) {
this.lootTableManager = lootTableManager;
}
@Override
public ConditionContainer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject();
String type = obj.get("condition").getAsString();
JsonDeserializer<? extends Condition> deserializer = lootTableManager.getConditionDeserializer(NamespaceID.from(type));
return new ConditionContainer() {
@Override
public Condition create(LootTableManager lootTableManager) {
return deserializer.deserialize(obj, typeOfT, context);
}
};
}
}
}

View File

@ -95,16 +95,6 @@ class LootTableContainer {
}
}
private class ConditionContainer {
private String condition;
private ConditionContainer() {}
public Condition create(LootTableManager lootTableManager) {
return lootTableManager.getCondition(NamespaceID.from(condition));
}
}
private class FunctionContainer {
private String function;
private ConditionContainer[] conditions;

View File

@ -2,6 +2,7 @@ package net.minestom.server.gamedata.loottables;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import net.minestom.server.gamedata.Condition;
import net.minestom.server.registry.ResourceGatherer;
import net.minestom.server.utils.NamespaceID;
@ -14,26 +15,27 @@ import java.io.*;
*/
public class LootTableManager {
private NamespaceIDHashMap<Condition> conditions = new NamespaceIDHashMap<>();
private NamespaceIDHashMap<JsonDeserializer<? extends Condition>> conditionDeserializers = new NamespaceIDHashMap<>();
private NamespaceIDHashMap<LootTableType> tableTypes = new NamespaceIDHashMap<>();
private NamespaceIDHashMap<LootTableEntryType> entryTypes = new NamespaceIDHashMap<>();
private NamespaceIDHashMap<LootTableFunction> functions = new NamespaceIDHashMap<>();
private NamespaceIDHashMap<LootTable> cache = new NamespaceIDHashMap<>();
private static Gson gson;
private Gson gson;
static {
public LootTableManager() {
gson = new GsonBuilder()
.registerTypeAdapter(RangeContainer.class, new RangeContainer.Deserializer())
.registerTypeAdapter(ConditionContainer.class, new ConditionContainer.Deserializer(this))
.create();
}
/**
* Registers a condition to the given namespaceID
* Registers a condition factory to the given namespaceID
* @param namespaceID
* @param condition
* @param factory
*/
public void registerCondition(NamespaceID namespaceID, Condition condition) {
conditions.put(namespaceID, condition);
public <T extends Condition> void registerConditionDeserializer(NamespaceID namespaceID, JsonDeserializer<T> factory) {
conditionDeserializers.put(namespaceID, factory);
}
/**
@ -88,15 +90,6 @@ public class LootTableManager {
return container.createTable(this);
}
/**
* Returns the registered condition corresponding to the given namespace ID. If none is registered, returns {@link Condition#ALWAYS_NO}.
* @param id
* @return
*/
public Condition getCondition(NamespaceID id) {
return conditions.getOrDefault(id, Condition.ALWAYS_NO);
}
/**
* Returns the registered table type corresponding to the given namespace ID. If none is registered, throws {@link IllegalArgumentException}
* @param id
@ -127,4 +120,8 @@ public class LootTableManager {
public LootTableFunction getFunction(NamespaceID id) {
return functions.getOrDefault(id, LootTableFunction.IDENTITY);
}
public JsonDeserializer<? extends Condition> getConditionDeserializer(NamespaceID id) {
return conditionDeserializers.getOrDefault(id, (json, typeOfT, context) -> Condition.ALWAYS_NO);
}
}

View File

@ -95,6 +95,20 @@ public class Position {
return this;
}
public void copy(Vector position) {
this.x = position.getX();
this.y = position.getY();
this.z = position.getZ();
}
public void copy(Position position) {
this.x = position.getX();
this.y = position.getY();
this.z = position.getZ();
this.yaw = position.getYaw();
this.pitch = position.getPitch();
}
public Position clone() {
return new Position(getX(), getY(), getZ(), getYaw(), getPitch());
}

View File

@ -28,7 +28,7 @@ public class TestLootTables {
RegistryMain.registerBlocks();
RegistryMain.registerItems();
tableManager = new LootTableManager();
tableManager.registerCondition(NamespaceID.from("minecraft:survives_explosion"), new SurvivesExplosionCondition());
tableManager.registerConditionDeserializer(NamespaceID.from("minecraft:survives_explosion"), new SurvivesExplosionCondition.Deserializer());
tableManager.registerTableType(NamespaceID.from("minecraft:block"), new BlockType());
tableManager.registerEntryType(NamespaceID.from("minecraft:item"), new ItemType());
}