Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advance model data #50

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.kakara.core.common.annotations;

public @interface ModelDataFile {
String value();
}

Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
9 changes: 3 additions & 6 deletions common/src/main/java/org/kakara/core/common/game/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import org.kakara.core.common.Serverable;
import org.kakara.core.common.game.meta.ItemMeta;

import java.util.List;

Expand Down Expand Up @@ -48,4 +49,6 @@ public interface ItemStack extends Serverable {
* @return if they are equal
*/
boolean equalsIgnoreCount(ItemStack itemStack);

ItemMeta getItemMeta();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.kakara.core.common.game.md;

public interface ModelData {
ModelDataObject getModelDataObject(int id);

String getDefaultTexture();

String getDefaultModel();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<ModelDataObject> modelDataObjects;
private final String defaultTexture;
private final String defaultModel;

public JsonModelData(List<ModelDataObject> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<ModelDataObject> objectList = new ArrayList<>();
if (object.has("models")) {
int i = 1;
for (JsonElement models : object.get("models").getAsJsonArray()) {
List<ModelDataProperty> properties = new ArrayList<>();
for (Map.Entry<String, JsonElement> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ModelDataProperty> propertyList;

public JsonModelDataProperties(List<ModelDataProperty> propertyList) {
this.propertyList = propertyList;
}

@Override
public ModelDataProperty getProperty(String name) {
return propertyList.stream().filter(modelDataProperty -> modelDataProperty.propertyName().equalsIgnoreCase(name)).findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}


}
Original file line number Diff line number Diff line change
@@ -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 "";
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.kakara.core.common.game.meta;

public interface ItemMeta {
int getModelData();
}
22 changes: 18 additions & 4 deletions common/src/main/java/org/kakara/core/common/mod/game/ModItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<String> lore);

void setLore(List<String> 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;
Expand Down
Loading