Skip to content

Commit

Permalink
Add Support for Custom Item definitions for JSON Advancements and the…
Browse files Browse the repository at this point in the history
… showtoast command

Custom Item Definitions define the Custom Model Data of an Item. The Custom Definition can then be used in JSON Advancements and the showtoast command
  • Loading branch information
ZockerAxel committed Feb 23, 2022
1 parent 8e5ad50 commit fb2ea25
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 5 deletions.
95 changes: 90 additions & 5 deletions src/eu/endercentral/crazy_advancements/CrazyAdvancementsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -46,6 +47,8 @@
import eu.endercentral.crazy_advancements.advancement.progress.GrantCriteriaResult;
import eu.endercentral.crazy_advancements.advancement.serialized.SerializedAdvancement;
import eu.endercentral.crazy_advancements.advancement.serialized.SerializedAdvancementDisplay;
import eu.endercentral.crazy_advancements.item.CustomItem;
import eu.endercentral.crazy_advancements.item.SerializedCustomItem;
import eu.endercentral.crazy_advancements.manager.AdvancementManager;
import eu.endercentral.crazy_advancements.packet.AdvancementsPacket;
import net.minecraft.advancements.Criterion;
Expand Down Expand Up @@ -90,16 +93,71 @@ public MinecraftKey a() {
private static AdvancementPacketReceiver packetReciever;
private static HashMap<String, NameKey> activeTabs = new HashMap<>();

private final List<CustomItem> customItems = new ArrayList<>();
private AdvancementManager fileAdvancementManager;

@Override
public void onLoad() {
instance = this;
loadCustomItems();
fileAdvancementManager = new AdvancementManager(new NameKey(API_NAMESPACE, "file"));
fileAdvancementManager.makeAccessible();
loadFileAdvancements();
}

private void loadCustomItems() {
File location = new File(getDataFolder().getAbsolutePath() + File.separator + "custom_items" + File.separator);

customItems.clear();

location.mkdirs();
File[] files = location.listFiles();
for(File file : files) {
if(file.isDirectory()) {
String namespace = file.getName();
customItems.addAll(loadCustomItemsFromNamespace(namespace, "", file));
}
}

System.out.println("Loaded " + customItems.size() + " Custom Items");
}

private List<CustomItem> loadCustomItemsFromNamespace(String namespace, String path, File location) {
File[] files = location.listFiles();

List<CustomItem> items = new ArrayList<>();

for(File file : files) {
if(file.isDirectory()) {
items.addAll(loadCustomItemsFromNamespace(namespace, path + file.getName() + "/", file));
} else if(file.isFile() && file.getName().endsWith(".json")) {
FileReader os = null;
try {
os = new FileReader(file);

JsonElement element = JsonParser.parseReader(os);
os.close();

SerializedCustomItem item = gson.fromJson(element, SerializedCustomItem.class);

String fileName = file.getName();
String key = fileName.substring(0, fileName.length() - 5);//Remove .json
items.add(item.deserialize(new NameKey(namespace, path + key)));
} catch (Exception e) {
if(os != null) {
try {
os.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
System.err.println("Unable to load Custom Item from File " + namespace + "/" + file.getName() + ": " + e.getLocalizedMessage());
}
}
}
return items;
}

private void loadFileAdvancements() {
File location = new File(getDataFolder().getAbsolutePath() + File.separator + "advancements" + File.separator);

Expand All @@ -110,7 +168,7 @@ private void loadFileAdvancements() {
for(File file : files) {
if(file.isDirectory()) {
String namespace = file.getName();
advancements.putAll(loadNamespace(namespace, "", file));
advancements.putAll(loadAdvancementsFromNamespace(namespace, "", file));
}
}

Expand Down Expand Up @@ -177,14 +235,14 @@ private void loadFileAdvancements() {
}
}

private HashMap<NameKey, SerializedAdvancement> loadNamespace(String namespace, String path, File location) {
private HashMap<NameKey, SerializedAdvancement> loadAdvancementsFromNamespace(String namespace, String path, File location) {
File[] files = location.listFiles();

HashMap<NameKey, SerializedAdvancement> advancements = new HashMap<NameKey, SerializedAdvancement>();

for(File file : files) {
if(file.isDirectory()) {
advancements.putAll(loadNamespace(namespace, path + file.getName() + "/", file));
advancements.putAll(loadAdvancementsFromNamespace(namespace, path + file.getName() + "/", file));
} else if(file.isFile() && file.getName().endsWith(".json")) {
FileReader os = null;
try {
Expand Down Expand Up @@ -577,6 +635,11 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String alia
tab.add(mat.name().toLowerCase());
}
}
for(CustomItem customItem : customItems) {
if(customItem.getName().toString().startsWith(args[1].toLowerCase())) {
tab.add(customItem.getName().toString());
}
}
} else if(args.length == 3) {
for(AdvancementFrame frame : AdvancementFrame.values()) {
if(frame.name().toLowerCase().startsWith(args[2].toLowerCase())) {
Expand Down Expand Up @@ -681,16 +744,38 @@ private Material getMaterial(String input) {
return Material.matchMaterial(input);
}

private CustomItem getCustomItem(String input) {
NameKey inputName = new NameKey(input);
for(CustomItem item : customItems) {
if(item.getName().isSimilar(inputName)) {
return item;
}
}
return null;
}

private ItemStack getItemStack(String input, CommandSender... commandSender) {
int colonIndex = input.indexOf(':');
String materialName = colonIndex == -1 ? input : input.substring(0, colonIndex);
String data = colonIndex == -1 ? "" : input.substring(colonIndex + 1);
Material material = getMaterial(materialName);

ItemStack stack;

if(material == null || !material.isItem()) {
return null;
CustomItem customItem = getCustomItem(input);
if(customItem == null) {
return null;
} else {
material = customItem.getType();
stack = new ItemStack(material);
ItemMeta meta = stack.getItemMeta();
meta.setCustomModelData(customItem.getCustomModelData());
stack.setItemMeta(meta);
}
} else {
stack = new ItemStack(material);
}
ItemStack stack = new ItemStack(material);

switch(material) {
case PLAYER_HEAD:
Expand Down
44 changes: 44 additions & 0 deletions src/eu/endercentral/crazy_advancements/item/CustomItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package eu.endercentral.crazy_advancements.item;

import org.bukkit.Material;

import eu.endercentral.crazy_advancements.NameKey;

public class CustomItem {

private final NameKey name;
private final Material type;
private final int customModelData;

public CustomItem(NameKey name, Material type, int customModelData) {
if(name == null) {
throw new RuntimeException("Custom Item Name may not be null");
}
if(type == null) {
throw new RuntimeException("Custom Item Type may not be null");
}
if(!type.isItem()) {
throw new RuntimeException("Can't create Custom Item from non-item Type '" + type.name().toLowerCase() + "'");
}
this.name = name;
this.type = type;
this.customModelData = customModelData;
}

public NameKey getName() {
return name;
}

public Material getType() {
return type;
}

public int getCustomModelData() {
return customModelData;
}

public SerializedCustomItem serialize() {
return new SerializedCustomItem(type.name().toLowerCase(), customModelData);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package eu.endercentral.crazy_advancements.item;

import org.bukkit.Material;

import eu.endercentral.crazy_advancements.NameKey;

public class SerializedCustomItem {

private final String item;
private final int customModelData;

public SerializedCustomItem(String item, int customModelData) {
this.item = item;
this.customModelData = customModelData;
}

public String getItem() {
return item;
}

public int getCustomModelData() {
return customModelData;
}

public CustomItem deserialize(NameKey name) {
Material type = Material.matchMaterial(getItem());
return new CustomItem(name, type, customModelData);
}

}

0 comments on commit fb2ea25

Please sign in to comment.