Skip to content

Commit

Permalink
Entity Conditions PT1 - Use ResourceLocations too
Browse files Browse the repository at this point in the history
  • Loading branch information
Dueris committed Jul 14, 2024
1 parent 524609e commit b8fd6bb
Show file tree
Hide file tree
Showing 76 changed files with 970 additions and 536 deletions.
27 changes: 9 additions & 18 deletions calio/src/main/java/me/dueris/calio/CraftCalio.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import me.dueris.calio.registry.RegistryKey;
import me.dueris.calio.registry.impl.CalioRegistry;
import net.minecraft.resources.ResourceLocation;
import org.bukkit.NamespacedKey;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
Expand All @@ -29,20 +28,12 @@

public class CraftCalio {
public static CraftCalio INSTANCE = new CraftCalio();
public final ConcurrentHashMap<NamespacedKey, Pair<FactoryData, Class<? extends FactoryHolder>>> types = new ConcurrentHashMap<>();
public final ConcurrentHashMap<ResourceLocation, Pair<FactoryData, Class<? extends FactoryHolder>>> types = new ConcurrentHashMap<>();
public final ArrayList<AccessorKey> keys = new ArrayList<>();
public final ArrayList<AssetIdentifier> assetKeys = new ArrayList<>();
private final List<File> datapackDirectoriesToParse = new ArrayList<>();
private boolean isDebugging;

public static NamespacedKey bukkitIdentifier(String namespace, String path) {
return NamespacedKey.fromString(namespace + ":" + path);
}

public static ResourceLocation nmsIdentifier(String namespace, String path) {
return ResourceLocation.fromNamespaceAndPath(namespace, path);
}

/**
* Add a datapack path to the list of directories to parse.
*
Expand Down Expand Up @@ -81,7 +72,7 @@ public void start(boolean debug) {
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
// Input stream made.
Class<? extends Registrable> toInvoke = assetKey.registryKey().type();
NamespacedKey namespacedKey = new NamespacedKey(namespace, key);
ResourceLocation namespacedKey = ResourceLocation.fromNamespaceAndPath(namespace, key);
switch (assetKey.assetType()) {
case JSON -> {
StringBuilder line = new StringBuilder();
Expand All @@ -97,10 +88,10 @@ public void start(boolean debug) {
getLogger().severe("An unhandled exception occurred when parsing a json file! Invalid syntax? The datapack will not be loaded.");
continue packLoop;
}
CalioRegistry.INSTANCE.retrieve(assetKey.registryKey()).register(toInvoke.getConstructor(NamespacedKey.class, JsonObject.class).newInstance(namespacedKey, assetParser));
CalioRegistry.INSTANCE.retrieve(assetKey.registryKey()).register(toInvoke.getConstructor(ResourceLocation.class, JsonObject.class).newInstance(namespacedKey, assetParser));
}
case IMAGE -> {
CalioRegistry.INSTANCE.retrieve(assetKey.registryKey()).register(toInvoke.getConstructor(NamespacedKey.class, BufferedImage.class).newInstance(namespacedKey, ImageIO.read(is)));
CalioRegistry.INSTANCE.retrieve(assetKey.registryKey()).register(toInvoke.getConstructor(ResourceLocation.class, BufferedImage.class).newInstance(namespacedKey, ImageIO.read(is)));
}
}
} catch (Throwable throwable) {
Expand All @@ -121,7 +112,7 @@ public void start(boolean debug) {
FileReader fileReader = FileReaderFactory.createFileReader(datapack.toPath());
if (fileReader == null) continue;
List<String> files = fileReader.listFiles();
HashMap<Pair<JsonObject, NamespacedKey>, Integer> newLoadingPrioritySortedMap = new HashMap<>();
HashMap<Pair<JsonObject, ResourceLocation>, Integer> newLoadingPrioritySortedMap = new HashMap<>();
fileLoop:
for (String file : files) {
file = file.replace("/", "\\");
Expand Down Expand Up @@ -151,18 +142,18 @@ public void start(boolean debug) {
getLogger().severe("An unhandled exception occurred when parsing a json file \"{}\"! Invalid syntax? The datapack will not be loaded.".replace("{}", namespace + ":" + key));
continue fileLoop;
}
NamespacedKey namespacedKey = new NamespacedKey(namespace, key);
ResourceLocation namespacedKey = ResourceLocation.fromNamespaceAndPath(namespace, key);
JsonObject remappedJsonObject = JsonObjectRemapper.remapJsonObject(powerParser, namespacedKey);
newLoadingPrioritySortedMap.put(new Pair<>(remappedJsonObject, namespacedKey), remappedJsonObject.has("loading_priority") ? remappedJsonObject.getAsJsonPrimitive("loading_priority").getAsInt() : 0);
}
}
}
}

List<Map.Entry<Pair<JsonObject, NamespacedKey>, Integer>> list = new ArrayList<>(newLoadingPrioritySortedMap.entrySet());
List<Map.Entry<Pair<JsonObject, ResourceLocation>, Integer>> list = new ArrayList<>(newLoadingPrioritySortedMap.entrySet());
Collections.sort(list, Map.Entry.comparingByValue());

for (Map.Entry<Pair<JsonObject, NamespacedKey>, Integer> entry : list) {
for (Map.Entry<Pair<JsonObject, ResourceLocation>, Integer> entry : list) {
CalioJsonParser.init(entry.getKey(), accessorKey);
}

Expand Down Expand Up @@ -222,7 +213,7 @@ public void register(Class<? extends FactoryHolder> holder) {
if (rC == null)
throw new IllegalArgumentException("FactoryHolder doesn't have registerComponents method in it or its superclasses!");
FactoryData data = (FactoryData) rC.invoke(null, new FactoryData());
NamespacedKey identifier = data.getIdentifier();
ResourceLocation identifier = data.getIdentifier();
if (identifier == null)
throw new IllegalArgumentException("Type identifier was not provided! FactoryHolder will not be loaded : " + holder.getSimpleName());
this.types.put(identifier, new Pair<>(data, holder));
Expand Down
5 changes: 5 additions & 0 deletions calio/src/main/java/me/dueris/calio/data/CalioDataTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -141,6 +142,10 @@ public static ParticleEffect particleEffect(JsonElement raw) {
return new ParticleEffect(particle, data, blockData);
}

public static PotionEffectType potionEffectType(JsonElement raw) {
return PotionEffectType.getByKey(NamespacedKey.fromString(raw.getAsString()));
}

private static int calculateParticleValue(float value) {
if (Math.round(value * 255) > 255) {
return 254;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import me.dueris.calio.data.factory.FactoryJsonObject;
import me.dueris.calio.data.types.OptionalInstance;
import me.dueris.calio.data.types.RequiredInstance;
import org.bukkit.NamespacedKey;
import net.minecraft.resources.ResourceLocation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
Expand All @@ -24,9 +24,9 @@
// Note for devs: Optionals ALWAYS return an Optional of a JsonElement if found
public class ConstructorCreator {

public static FactoryHolder invoke(Constructor<? extends FactoryHolder> constructor, FactoryData data, Pair<JsonObject, NamespacedKey> pair) throws InvocationTargetException, InstantiationException, IllegalAccessException {
public static FactoryHolder invoke(Constructor<? extends FactoryHolder> constructor, FactoryData data, Pair<JsonObject, ResourceLocation> pair) throws InvocationTargetException, InstantiationException, IllegalAccessException {
JsonObject getter = pair.getFirst();
NamespacedKey tag = pair.getSecond();
ResourceLocation tag = pair.getSecond();
List<Object> invoker = new ArrayList<>();
for (FactoryDataDefiner provider : data.getProviders()) {
if (getter.has(provider.getObjName())) {
Expand All @@ -43,7 +43,7 @@ public static FactoryHolder invoke(Constructor<? extends FactoryHolder> construc
if (provider.getDefaultValue() instanceof RequiredInstance) {
throw new IllegalArgumentException("Instance of \"{a}\" is required in registerable: {b}"
.replace("{a}", provider.getObjName())
.replace("{b}", tag.asString())
.replace("{b}", tag.toString())
);
} else if (provider.getDefaultValue() instanceof OptionalInstance) {
invoker.add(null);
Expand Down
10 changes: 5 additions & 5 deletions calio/src/main/java/me/dueris/calio/data/FactoryData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import com.google.common.base.Preconditions;
import me.dueris.calio.data.types.OptionalInstance;
import me.dueris.calio.data.types.RequiredInstance;
import org.bukkit.NamespacedKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ConcurrentLinkedQueue;

public class FactoryData {
private final ConcurrentLinkedQueue<FactoryDataDefiner> providers;
private NamespacedKey identifier;
private ResourceLocation identifier;

public FactoryData() {
this.providers = new ConcurrentLinkedQueue<>();
Expand Down Expand Up @@ -67,18 +67,18 @@ public FactoryData remove(String objName) {
return this;
}

public FactoryData ofNamespace(NamespacedKey identifier) {
public FactoryData ofNamespace(ResourceLocation identifier) {
this.identifier = identifier;
return this;
}

public NamespacedKey getIdentifier() {
public ResourceLocation getIdentifier() {
return identifier;
}

@Override
public String toString() {
return "FactoryData :: [%N%] : DataDefiners: [%%%]".replace("%%%", this.providers.toString()).replace("%N%", identifier.asString());
return "FactoryData :: [%N%] : DataDefiners: [%%%]".replace("%%%", this.providers.toString()).replace("%N%", identifier.toString());
}

}
5 changes: 3 additions & 2 deletions calio/src/main/java/me/dueris/calio/data/FactoryHolder.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package me.dueris.calio.data;

import me.dueris.calio.registry.Registrable;
import org.bukkit.NamespacedKey;
import net.minecraft.resources.ResourceLocation;


public interface FactoryHolder extends Registrable {
static FactoryData registerComponents(FactoryData data) {
return data;
}

FactoryHolder ofResourceLocation(NamespacedKey key);
FactoryHolder ofResourceLocation(ResourceLocation key);

default void bootstrap() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.gson.JsonObject;
import it.unimi.dsi.fastutil.Pair;
import me.dueris.calio.util.NamespaceUtils;
import org.bukkit.NamespacedKey;
import net.minecraft.resources.ResourceLocation;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -47,11 +47,11 @@ public static void addObjectMapping(String key, Pair<Object, Object> mapper) {
* @param obj the JSON object to be remapped
* @param currentNamespace the current namespace used for dynamic namespace remapping
*/
public static JsonObject remapJsonObject(JsonObject obj, NamespacedKey currentNamespace) {
public static JsonObject remapJsonObject(JsonObject obj, ResourceLocation currentNamespace) {
JsonObject objectReturnable = new JsonObject();
for (String key : obj.keySet()) {
if (objectReturnable.has(key))
throw new IllegalStateException("JsonFile has duplicate value: (key=\"{k}\", namespace=\"{n}\"".replace("{k}", key).replace("{n}", currentNamespace.asString()));
throw new IllegalStateException("JsonFile has duplicate value: (key=\"{k}\", namespace=\"{n}\"".replace("{k}", key).replace("{n}", currentNamespace.toString()));
JsonElement valueInst = obj.get(key);
// Object mappings
for (String keyName : objectMappings.keySet()) {
Expand All @@ -67,7 +67,7 @@ public static JsonObject remapJsonObject(JsonObject obj, NamespacedKey currentNa
if (valueInst.isJsonPrimitive() && valueInst.getAsJsonPrimitive().isString()) {
String g = valueInst.getAsJsonPrimitive().getAsString();
if (g.contains(":") && g.contains("*")) {
g = NamespaceUtils.getDynamicNamespace(currentNamespace.asString(), g).asString();
g = NamespaceUtils.getDynamicNamespace(currentNamespace.toString(), g).toString();
}

for (Pair<String, String> pair : typeMappings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ public String getString() {
public FactoryElement deepCopy() {
return new FactoryElement(this.handle);
}

public JsonElement handle() {
return handle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import net.minecraft.tags.TagKey;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;

import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -121,16 +121,16 @@ public List<FactoryElement> values() {
return this.handle.asMap().values().stream().map(FactoryElement::fromJson).collect(Collectors.toUnmodifiableList());
}

public NamespacedKey getNamespacedKey(String key) {
private NamespacedKey getBukkitNamespacedKey(String key) {
return NamespacedKey.fromString(this.getString(key));
}

public ResourceLocation getResourceLocation(String key) {
return CraftNamespacedKey.toMinecraft(this.getNamespacedKey(key));
return ResourceLocation.parse(this.getString(key));
}

public Material getMaterial(String key) {
NamespacedKey a = this.getNamespacedKey(key);
NamespacedKey a = this.getBukkitNamespacedKey(key);
return Material.matchMaterial(a.asString());
}

Expand All @@ -156,6 +156,10 @@ public ItemStack getItemStack(String key) {
return new ItemStack(Material.PLAYER_HEAD, 1);
}

public PotionEffectType getPotionEffectType(String key) {
return PotionEffectType.getByKey(getBukkitNamespacedKey(key));
}

public ItemStack asItemStack() {
String materialVal = "player_head";
int amt = 1;
Expand Down
18 changes: 9 additions & 9 deletions calio/src/main/java/me/dueris/calio/parse/CalioJsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
import me.dueris.calio.data.*;
import me.dueris.calio.data.annotations.RequiresPlugin;
import me.dueris.calio.registry.impl.CalioRegistry;
import org.bukkit.NamespacedKey;
import net.minecraft.resources.ResourceLocation;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;

public class CalioJsonParser {

public static FactoryHolder init(Pair<JsonObject, NamespacedKey> pair, AccessorKey accessorKey) {
NamespacedKey key = pair.getSecond();
public static FactoryHolder init(Pair<JsonObject, ResourceLocation> pair, AccessorKey accessorKey) {
ResourceLocation key = pair.getSecond();
if (accessorKey.getOfType() == null) return null;
try {
FactoryData data;
Class<? extends FactoryHolder> holder;
if (accessorKey.usesTypeDefiner()) {
String type = pair.getFirst().has("type") ? pair.getFirst().get("type").getAsString() : accessorKey.getDefaultType();
if (!CraftCalio.INSTANCE.types.containsKey(NamespacedKey.fromString(type))) {
if (!CraftCalio.INSTANCE.types.containsKey(ResourceLocation.parse(type))) {
CraftCalio.INSTANCE.getLogger().severe("Unknown type was provided! : {a} | {b}"
.replace("{a}", NamespacedKey.fromString(pair.getFirst().get("type").getAsString()).asString())
.replace("{b}", key.asString())
.replace("{a}", ResourceLocation.parse(pair.getFirst().get("type").getAsString()).toString())
.replace("{b}", key.toString())
);
return null;
} else {
data = CraftCalio.INSTANCE.types.get(NamespacedKey.fromString(type)).getFirst();
holder = CraftCalio.INSTANCE.types.get(NamespacedKey.fromString(type)).getSecond();
data = CraftCalio.INSTANCE.types.get(ResourceLocation.parse(type)).getFirst();
holder = CraftCalio.INSTANCE.types.get(ResourceLocation.parse(type)).getSecond();
}
} else {
// We gotta invoke the FactoryData manually
Expand Down Expand Up @@ -67,7 +67,7 @@ public static FactoryHolder init(Pair<JsonObject, NamespacedKey> pair, AccessorK
CraftCalio.INSTANCE.getLogger().severe(
"Registry: {a} | Associated Namespace: {b} | Type: {c} | Throwable: {d}"
.replace("{a}", accessorKey.getOfType().getSimpleName())
.replace("{b}", key.asString())
.replace("{b}", key.toString())
.replace("{c}", accessorKey.usesTypeDefiner() ? pair.getFirst().get("type").getAsString() : "No Type")
.replace("{d}", throwable.getMessage() == null ? "Null Message" : throwable.getMessage()) + stacktrace[0]
);
Expand Down
5 changes: 3 additions & 2 deletions calio/src/main/java/me/dueris/calio/registry/Registrable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.dueris.calio.registry;

import org.bukkit.NamespacedKey;

import net.minecraft.resources.ResourceLocation;

public interface Registrable {

Expand All @@ -9,5 +10,5 @@ public interface Registrable {
*
* @return the namespaced key
*/
NamespacedKey key();
ResourceLocation key();
}
Loading

0 comments on commit b8fd6bb

Please sign in to comment.