Skip to content

Commit

Permalink
custom item system revamped, update 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ItziSpyder committed Jul 7, 2024
1 parent fe325f1 commit 46e6673
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

# PDK
pdk_version= 1.3.6
pdk_version= 1.4.0

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public static NamespacedKey registerItem(String namespace, Supplier<CustomItem>
return key;
}

public static ItemStack createItemContext(NamespacedKey key) {
public static ItemStack createItemContext(String namespace) {
NamespacedKey key = new NamespacedKey(Global.instance.getPlugin(), "items/" + namespace);
Supplier<CustomItem> registry = itemRegistry.get(key);
if (registry == null)
throw new IllegalArgumentException("context not found in item registry");
Expand Down Expand Up @@ -94,6 +95,34 @@ public static <C extends CustomItem> C getItemContext(ItemStack item, Class<C> t
return null;
}

public static <C extends CustomItem> C getOrDefItemContext(ItemStack item, Class<C> type, C fallback) {
C context = getItemContext(item, type);
if (context != null)
return context;

NamespacedKey key = getKeyOf(fallback);
if (key == null)
return fallback;

ItemMeta meta = item.getItemMeta();
PersistentDataContainer data = meta.getPersistentDataContainer();

data.set(key, PersistentDataType.STRING, gson.toJson(fallback));
fallback.updateMeta(meta);
item.setItemMeta(meta);
return fallback;
}

public static NamespacedKey getKeyOf(CustomItem context) {
if (context == null)
return null;

for (Map.Entry<NamespacedKey, Supplier<CustomItem>> entry : itemRegistry.entrySet())
if (entry.getValue().get().getClass() == context.getClass())
return entry.getKey();
return null;
}

public static CustomItem getItemContext(ItemStack item) {
return getItemContext(item, CustomItem.class);
}
Expand Down

0 comments on commit 46e6673

Please sign in to comment.