diff --git a/common/src/main/java/org/kakara/core/common/annotations/ModelDataFile.java b/common/src/main/java/org/kakara/core/common/annotations/ModelDataFile.java new file mode 100644 index 0000000..38cd0bc --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/annotations/ModelDataFile.java @@ -0,0 +1,6 @@ +package org.kakara.core.common.annotations; + +public @interface ModelDataFile { + String value(); +} + diff --git a/common/src/main/java/org/kakara/core/common/game/CustomStackable.java b/common/src/main/java/org/kakara/core/common/game/CustomStackable.java index 24eb10a..50c8e8f 100644 --- a/common/src/main/java/org/kakara/core/common/game/CustomStackable.java +++ b/common/src/main/java/org/kakara/core/common/game/CustomStackable.java @@ -10,4 +10,11 @@ public interface CustomStackable { * @return the custom item stack */ ItemStack createItemStack(); + + /** + * Create a custom ServerItemStack with your item. + * + * @return the custom item stack + */ + ItemStack createServerItemStack(); } diff --git a/common/src/main/java/org/kakara/core/common/game/Item.java b/common/src/main/java/org/kakara/core/common/game/Item.java index dd79010..d46f4c3 100644 --- a/common/src/main/java/org/kakara/core/common/game/Item.java +++ b/common/src/main/java/org/kakara/core/common/game/Item.java @@ -4,8 +4,8 @@ import org.kakara.core.common.ControllerKey; import org.kakara.core.common.GameObject; import org.kakara.core.common.Identifiable; +import org.kakara.core.common.game.md.ModelData; import org.kakara.core.common.mod.Mod; - /** * This represents an Item in a game. *

@@ -23,11 +23,8 @@ public interface Item extends Identifiable, GameObject { String getModel(); - /** - * The controller key for the item - * - * @return the ControllerKey - */ + ModelData getModelData(); + ControllerKey getControllerKey(); Mod getMod(); diff --git a/common/src/main/java/org/kakara/core/common/game/ItemStack.java b/common/src/main/java/org/kakara/core/common/game/ItemStack.java index 1b23f72..68c06b4 100644 --- a/common/src/main/java/org/kakara/core/common/game/ItemStack.java +++ b/common/src/main/java/org/kakara/core/common/game/ItemStack.java @@ -2,6 +2,7 @@ import org.kakara.core.common.Serverable; +import org.kakara.core.common.game.meta.ItemMeta; import java.util.List; @@ -48,4 +49,6 @@ public interface ItemStack extends Serverable { * @return if they are equal */ boolean equalsIgnoreCount(ItemStack itemStack); + + ItemMeta getItemMeta(); } diff --git a/common/src/main/java/org/kakara/core/common/game/md/ModelData.java b/common/src/main/java/org/kakara/core/common/game/md/ModelData.java new file mode 100644 index 0000000..1a0b27c --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/ModelData.java @@ -0,0 +1,9 @@ +package org.kakara.core.common.game.md; + +public interface ModelData { + ModelDataObject getModelDataObject(int id); + + String getDefaultTexture(); + + String getDefaultModel(); +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/ModelDataLoader.java b/common/src/main/java/org/kakara/core/common/game/md/ModelDataLoader.java new file mode 100644 index 0000000..7b526a2 --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/ModelDataLoader.java @@ -0,0 +1,21 @@ +package org.kakara.core.common.game.md; + +import com.google.gson.JsonObject; +import org.kakara.core.common.Utils; +import org.kakara.core.common.game.md.properties.json.JsonModelDataLoader; +import org.kakara.core.common.resources.Resource; + +import java.io.FileNotFoundException; +import java.io.FileReader; + +public class ModelDataLoader { + public static ModelData loadData(Resource resource) { + try { + JsonObject jsonObject = Utils.getGson().fromJson(new FileReader(resource.getFile()), JsonObject.class); + return JsonModelDataLoader.loadModelData(jsonObject); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/ModelDataObject.java b/common/src/main/java/org/kakara/core/common/game/md/ModelDataObject.java new file mode 100644 index 0000000..38fd3a1 --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/ModelDataObject.java @@ -0,0 +1,28 @@ +package org.kakara.core.common.game.md; + +import org.kakara.core.common.game.md.properties.json.BasicTextureProperty; +import org.kakara.core.common.game.md.properties.json.ModelProperty; +import org.kakara.core.common.resources.Texture; + +//TODO think of better name. +public class ModelDataObject { + private final ModelDataProperties properties; + private final int id; + + public ModelDataObject(ModelDataProperties properties, int id) { + this.properties = properties; + this.id = id; + } + + public Texture getTexture() { + return ((BasicTextureProperty) properties.getProperty("TEXTURE")).getTexture(); + } + + public String getModel() { + return ((ModelProperty) properties.getProperty("MODEL")).getModel(); + } + + public int getId() { + return id; + } +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/ModelDataProperties.java b/common/src/main/java/org/kakara/core/common/game/md/ModelDataProperties.java new file mode 100644 index 0000000..138c325 --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/ModelDataProperties.java @@ -0,0 +1,7 @@ +package org.kakara.core.common.game.md; + +import org.kakara.core.common.game.md.properties.ModelDataProperty; + +public interface ModelDataProperties { + ModelDataProperty getProperty(String name); +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/properties/ModelDataProperty.java b/common/src/main/java/org/kakara/core/common/game/md/properties/ModelDataProperty.java new file mode 100644 index 0000000..bdb7b4b --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/properties/ModelDataProperty.java @@ -0,0 +1,8 @@ +package org.kakara.core.common.game.md.properties; + +import org.kakara.core.common.game.md.ModelData; +import org.kakara.core.common.game.md.ModelDataProperties; + +public interface ModelDataProperty { + String propertyName(); +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/properties/json/BasicTextureProperty.java b/common/src/main/java/org/kakara/core/common/game/md/properties/json/BasicTextureProperty.java new file mode 100644 index 0000000..89a5a02 --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/properties/json/BasicTextureProperty.java @@ -0,0 +1,20 @@ +package org.kakara.core.common.game.md.properties.json; + +import com.google.gson.JsonObject; +import org.kakara.core.common.resources.Texture; + +public class BasicTextureProperty extends JsonProperty { + + public BasicTextureProperty(JsonObject jsonObject) { + super(jsonObject); + } + + @Override + public String propertyName() { + return "TEXTURE"; + } + + public Texture getTexture() { + return null; + } +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonModelData.java b/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonModelData.java new file mode 100644 index 0000000..1fe282e --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonModelData.java @@ -0,0 +1,33 @@ +package org.kakara.core.common.game.md.properties.json; + +import org.kakara.core.common.game.md.ModelData; +import org.kakara.core.common.game.md.ModelDataObject; + +import java.util.List; + +public class JsonModelData implements ModelData { + private final List modelDataObjects; + private final String defaultTexture; + private final String defaultModel; + + public JsonModelData(List modelDataObjects, String defaultTexture, String defaultModel) { + this.modelDataObjects = modelDataObjects; + this.defaultTexture = defaultTexture; + this.defaultModel = defaultModel; + } + + @Override + public ModelDataObject getModelDataObject(int id) { + return modelDataObjects.get(id); + } + + @Override + public String getDefaultTexture() { + return defaultTexture; + } + + @Override + public String getDefaultModel() { + return defaultModel; + } +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonModelDataLoader.java b/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonModelDataLoader.java new file mode 100644 index 0000000..b8b1831 --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonModelDataLoader.java @@ -0,0 +1,39 @@ +package org.kakara.core.common.game.md.properties.json; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import org.kakara.core.common.game.md.ModelData; +import org.kakara.core.common.game.md.ModelDataObject; +import org.kakara.core.common.game.md.properties.ModelDataProperty; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class JsonModelDataLoader { + public static ModelData loadModelData(JsonObject object) { + + String defaultModel = object.get("default-model").getAsString(); + String defaultTexture = object.get("default-texture").getAsString(); + + List objectList = new ArrayList<>(); + if (object.has("models")) { + int i = 1; + for (JsonElement models : object.get("models").getAsJsonArray()) { + List properties = new ArrayList<>(); + for (Map.Entry stringJsonElementEntry : models.getAsJsonObject().entrySet()) { + String key = stringJsonElementEntry.getKey(); + + if (key.equalsIgnoreCase("TEXTURE")) { + properties.add(new BasicTextureProperty(stringJsonElementEntry.getValue().getAsJsonObject())); + } else if (key.equalsIgnoreCase("MODEL")) { + properties.add(new ModelProperty(stringJsonElementEntry.getValue().getAsJsonObject())); + } + } + i++; + objectList.add(new ModelDataObject(new JsonModelDataProperties(properties), i)); + } + } + return new JsonModelData(objectList, defaultTexture, defaultModel); + } +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonModelDataProperties.java b/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonModelDataProperties.java new file mode 100644 index 0000000..396c7e0 --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonModelDataProperties.java @@ -0,0 +1,19 @@ +package org.kakara.core.common.game.md.properties.json; + +import org.kakara.core.common.game.md.ModelDataProperties; +import org.kakara.core.common.game.md.properties.ModelDataProperty; + +import java.util.List; + +public class JsonModelDataProperties implements ModelDataProperties { + private final List propertyList; + + public JsonModelDataProperties(List propertyList) { + this.propertyList = propertyList; + } + + @Override + public ModelDataProperty getProperty(String name) { + return propertyList.stream().filter(modelDataProperty -> modelDataProperty.propertyName().equalsIgnoreCase(name)).findFirst().orElse(null); + } +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonProperty.java b/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonProperty.java new file mode 100644 index 0000000..508a3b5 --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/properties/json/JsonProperty.java @@ -0,0 +1,14 @@ +package org.kakara.core.common.game.md.properties.json; + +import com.google.gson.JsonObject; +import org.kakara.core.common.game.md.properties.ModelDataProperty; + +public abstract class JsonProperty implements ModelDataProperty { + private JsonObject jsonObject; + + public JsonProperty(JsonObject jsonObject) { + this.jsonObject = jsonObject; + } + + +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/properties/json/ModelProperty.java b/common/src/main/java/org/kakara/core/common/game/md/properties/json/ModelProperty.java new file mode 100644 index 0000000..1b063ee --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/properties/json/ModelProperty.java @@ -0,0 +1,18 @@ +package org.kakara.core.common.game.md.properties.json; + +import com.google.gson.JsonObject; + +public class ModelProperty extends JsonProperty { + public ModelProperty(JsonObject jsonObject) { + super(jsonObject); + } + + @Override + public String propertyName() { + return "MODEL"; + } + + public String getModel() { + return ""; + } +} diff --git a/common/src/main/java/org/kakara/core/common/game/md/properties/json/TextureColorOffsetProperty.java b/common/src/main/java/org/kakara/core/common/game/md/properties/json/TextureColorOffsetProperty.java new file mode 100644 index 0000000..612bba4 --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/md/properties/json/TextureColorOffsetProperty.java @@ -0,0 +1,14 @@ +package org.kakara.core.common.game.md.properties.json; + +import com.google.gson.JsonObject; + +public class TextureColorOffsetProperty extends JsonProperty { + public TextureColorOffsetProperty(JsonObject jsonObject) { + super(jsonObject); + } + + @Override + public String propertyName() { + return "COLOR_OFFSET"; + } +} diff --git a/common/src/main/java/org/kakara/core/common/game/meta/ItemMeta.java b/common/src/main/java/org/kakara/core/common/game/meta/ItemMeta.java new file mode 100644 index 0000000..902419b --- /dev/null +++ b/common/src/main/java/org/kakara/core/common/game/meta/ItemMeta.java @@ -0,0 +1,5 @@ +package org.kakara.core.common.game.meta; + +public interface ItemMeta { + int getModelData(); +} diff --git a/common/src/main/java/org/kakara/core/common/mod/game/ModItem.java b/common/src/main/java/org/kakara/core/common/mod/game/ModItem.java index c8b7c62..5886e04 100644 --- a/common/src/main/java/org/kakara/core/common/mod/game/ModItem.java +++ b/common/src/main/java/org/kakara/core/common/mod/game/ModItem.java @@ -2,13 +2,14 @@ import org.kakara.core.common.ControllerKey; -import org.kakara.core.common.annotations.Key; -import org.kakara.core.common.annotations.Model; -import org.kakara.core.common.annotations.Name; -import org.kakara.core.common.annotations.Texture; +import org.kakara.core.common.Kakara; +import org.kakara.core.common.annotations.*; import org.kakara.core.common.game.Item; import org.kakara.core.common.game.SimpleGameObject; +import org.kakara.core.common.game.md.ModelData; +import org.kakara.core.common.game.md.ModelDataLoader; import org.kakara.core.common.mod.Mod; +import org.kakara.core.common.resources.ResourceType; import java.util.Objects; @@ -18,6 +19,7 @@ public abstract class ModItem extends SimpleGameObject implements Item { protected final String name; protected final int id; protected String texture; + private ModelData modelData; public ModItem(GameMod mod) { super(); @@ -28,6 +30,18 @@ public ModItem(GameMod mod) { this.nameKey = new ControllerKey(mod, keyA == null ? getClass().getSimpleName() : keyA.value()); this.id = nameKey.hashCode(); texture = getClass().getAnnotation(Texture.class).value(); + if (getClass().isAnnotationPresent(ModelDataFile.class)) { + modelData = ModelDataLoader.loadData(Kakara.getGameInstance().getResourceManager().getResource(getClass().getAnnotation(ModelDataFile.class).value(), ResourceType.OTHER, mod)); + } + } + + protected void setModelData(ModelData modelData) { + this.modelData = modelData; + } + + @Override + public ModelData getModelData() { + return modelData; } @Override diff --git a/server/src/main/java/org/kakara/core/server/game/ServerItemStack.java b/server/src/main/java/org/kakara/core/server/game/ServerItemStack.java index 473f4eb..49fd1dd 100644 --- a/server/src/main/java/org/kakara/core/server/game/ServerItemStack.java +++ b/server/src/main/java/org/kakara/core/server/game/ServerItemStack.java @@ -1,10 +1,9 @@ package org.kakara.core.server.game; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.kakara.core.common.Serverable; import org.kakara.core.common.exceptions.NoServerVersionAvailableException; import org.kakara.core.common.game.ItemStack; +import org.kakara.core.common.game.meta.ItemMeta; import java.util.List; @@ -15,27 +14,27 @@ public interface ServerItemStack extends ItemStack { * @param count */ void setCount(int count); - /** * sets the name * * @param name the new name */ - void setName(@NotNull String name); + void setName(String name); /** * Sets the lore * * @param lore the item lore. Can be null */ - void setLore(@Nullable List lore); - + void setLore(List lore); /** * Clones the ItemStack exactly how it is now. * @return the cloned ItemStack */ ItemStack clone(); + void setItemMeta(ItemMeta itemMeta); + @Override default boolean isServerVersionAvailable() { return true; diff --git a/server/src/main/java/org/kakara/core/server/game/meta/ServerItemMeta.java b/server/src/main/java/org/kakara/core/server/game/meta/ServerItemMeta.java new file mode 100644 index 0000000..626ae5f --- /dev/null +++ b/server/src/main/java/org/kakara/core/server/game/meta/ServerItemMeta.java @@ -0,0 +1,8 @@ +package org.kakara.core.server.game.meta; + +import org.kakara.core.common.game.meta.ItemMeta; + +public interface ServerItemMeta extends ItemMeta { + + void setModelData(int modelData); +}