added item contexts, a way of providing required data to custom items

This commit is contained in:
XTerPL 2025-01-18 22:00:28 +01:00
parent 2429f0a5c9
commit 47a24cd04c
16 changed files with 75 additions and 44 deletions

View file

@ -16,6 +16,7 @@
package de.blazemcworld.blazinggames.builderwand;
import de.blazemcworld.blazinggames.BlazingGames;
import de.blazemcworld.blazinggames.items.ContextlessItem;
import de.blazemcworld.blazinggames.items.CustomItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
@ -36,7 +37,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class BuilderWand extends CustomItem {
public class BuilderWand extends ContextlessItem {
private static final NamespacedKey modeKey = BlazingGames.get().key("builder_mode");
@Override

View file

@ -16,6 +16,7 @@
package de.blazemcworld.blazinggames.commands;
import de.blazemcworld.blazinggames.BlazingGames;
import de.blazemcworld.blazinggames.items.ContextlessItem;
import de.blazemcworld.blazinggames.items.CustomItem;
import de.blazemcworld.blazinggames.items.CustomItems;
import net.kyori.adventure.text.Component;
@ -53,10 +54,10 @@ public class CustomGiveCommand implements CommandExecutor, TabCompleter {
return true;
}
CustomItem itemType = CustomItems.getByKey(BlazingGames.get().key(strings[0]));
CustomItem<?> itemType = CustomItems.getByKey(BlazingGames.get().key(strings[0]));
int count = 1;
if(itemType == null)
if(!(itemType instanceof ContextlessItem itemType2))
{
commandSender.sendMessage(Component.text("Unknown custom item: " + strings[0] + "!").color(NamedTextColor.RED));
return true;
@ -66,7 +67,7 @@ public class CustomGiveCommand implements CommandExecutor, TabCompleter {
count = Integer.parseInt(strings[1]);
}
ItemStack item = itemType.create();
ItemStack item = itemType2.create();
item.setAmount(count);
p.getInventory().addItem(item);
@ -80,7 +81,12 @@ public class CustomGiveCommand implements CommandExecutor, TabCompleter {
List<String> tabs = new ArrayList<>();
if(strings.length == 1) {
CustomItems.list().forEach(itemType -> tabs.add(itemType.getKey().getKey()));
CustomItems.list().forEach(itemType -> {
if(itemType instanceof ContextlessItem)
{
tabs.add(itemType.getKey().getKey());
}
});
}
return tabs;

View file

@ -18,6 +18,7 @@ package de.blazemcworld.blazinggames.crates;
import java.util.List;
import java.util.Map;
import de.blazemcworld.blazinggames.items.ContextlessItem;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
@ -26,13 +27,12 @@ import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import de.blazemcworld.blazinggames.BlazingGames;
import de.blazemcworld.blazinggames.items.CustomItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.NotNull;
public class SkeletonKey extends CustomItem {
public class SkeletonKey extends ContextlessItem {
@Override
public @NotNull NamespacedKey getKey() {
return BlazingGames.get().key("skeleton_key");

View file

@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.blazemcworld.blazinggames.items.ContextlessItem;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
@ -27,13 +28,12 @@ import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import de.blazemcworld.blazinggames.BlazingGames;
import de.blazemcworld.blazinggames.items.CustomItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.NotNull;
public class ToGoBoxItem extends CustomItem {
public class ToGoBoxItem extends ContextlessItem {
@Override
public @NotNull NamespacedKey getKey() {
return BlazingGames.get().key("to_go_box");

View file

@ -15,7 +15,7 @@
*/
package de.blazemcworld.blazinggames.enchantments.sys;
import de.blazemcworld.blazinggames.items.CustomItem;
import de.blazemcworld.blazinggames.items.ContextlessItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.NamespacedKey;
@ -25,7 +25,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
public class EnchantmentTome extends CustomItem {
public class EnchantmentTome extends ContextlessItem {
private final NamespacedKey tomeKey;
private final String tomeName;
private final EnchantmentWrapper wrapper;

View file

@ -20,6 +20,7 @@ import de.blazemcworld.blazinggames.computing.ComputerRegistry;
import de.blazemcworld.blazinggames.enchantments.sys.CustomEnchantment;
import de.blazemcworld.blazinggames.enchantments.sys.CustomEnchantments;
import de.blazemcworld.blazinggames.enchantments.sys.EnchantmentHelper;
import de.blazemcworld.blazinggames.items.ContextlessItem;
import de.blazemcworld.blazinggames.items.CustomItem;
import de.blazemcworld.blazinggames.items.CustomItems;
import de.blazemcworld.blazinggames.utils.InventoryUtils;
@ -110,11 +111,19 @@ public class EntityDamagedByEventListener implements Listener {
}
});
CustomItem slab = CustomItems.getByKey(BlazingGames.get().key(slabType + "_slab"));
if (slab == null) return;
if (p.getGameMode() != GameMode.CREATIVE) {
InventoryUtils.collectableDrop(p, blockLocation, slab.create());
CustomItem<?> slab = CustomItems.getByKey(BlazingGames.get().key(slabType + "_slab"));
if(slab instanceof ContextlessItem slab2)
{
if (p.getGameMode() != GameMode.CREATIVE) {
InventoryUtils.collectableDrop(p, blockLocation, slab2.create());
}
}
else
{
return;
}
p.getWorld().playSound(blockLocation, breakSound, 1, 1);
if (p.getWorld().getNearbyEntitiesByType(Shulker.class, blockLocation, 0.5).isEmpty())

View file

@ -0,0 +1,19 @@
package de.blazemcworld.blazinggames.items;
import de.blazemcworld.blazinggames.items.contexts.EmptyItemContext;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public abstract class ContextlessItem extends CustomItem<EmptyItemContext> {
public final @NotNull ItemStack create() {
return create(EmptyItemContext.instance);
}
protected final @NotNull ItemStack modifyMaterial(ItemStack stack, EmptyItemContext context) {
return modifyMaterial(stack);
}
protected @NotNull ItemStack modifyMaterial(ItemStack stack) {
return stack;
}
}

View file

@ -16,6 +16,7 @@
package de.blazemcworld.blazinggames.items;
import de.blazemcworld.blazinggames.BlazingGames;
import de.blazemcworld.blazinggames.items.contexts.ItemContext;
import de.blazemcworld.blazinggames.utils.NamespacedKeyDataType;
import net.kyori.adventure.text.Component;
import org.bukkit.Keyed;
@ -29,11 +30,11 @@ import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
public abstract class CustomItem implements RecipeProvider, Keyed, ItemPredicate {
public abstract class CustomItem<T extends ItemContext> implements RecipeProvider, Keyed, ItemPredicate {
private static final NamespacedKey key = BlazingGames.get().key("custom_item");
// returns null if not a custom item
public static @Nullable CustomItem getCustomItem(ItemStack stack) {
public static @Nullable CustomItem<?> getCustomItem(ItemStack stack) {
if(stack == null || !stack.hasItemMeta()) {
return null;
}
@ -63,7 +64,7 @@ public abstract class CustomItem implements RecipeProvider, Keyed, ItemPredicate
public abstract @NotNull NamespacedKey getKey();
public final @NotNull ItemStack create() {
public final @NotNull ItemStack create(T context) {
ItemStack result = new ItemStack(baseMaterial());
ItemMeta meta = result.getItemMeta();
@ -82,12 +83,12 @@ public abstract class CustomItem implements RecipeProvider, Keyed, ItemPredicate
result.setItemMeta(meta);
return modifyMaterial(result);
return modifyMaterial(result, context);
}
@Override
public final boolean matchItem(ItemStack stack) {
CustomItem other = getCustomItem(stack);
CustomItem<?> other = getCustomItem(stack);
if(other == null) return false;
@ -96,28 +97,13 @@ public abstract class CustomItem implements RecipeProvider, Keyed, ItemPredicate
@Override
public final Component getDescription() {
ItemStack item = create();
Component name = Component.translatable(item.translationKey());
ItemMeta reqMeta = item.getItemMeta();
if(reqMeta != null) {
if(reqMeta.hasItemName()) {
name = reqMeta.itemName();
}
if(reqMeta.hasDisplayName()) {
name = reqMeta.displayName();
}
}
return name;
return itemName();
}
// DO NOT CALL THIS METHOD, instead call create() on the item's instance
// also there's no need to set the "custom_item" item tag because
// the create() method does it anyway
protected @NotNull ItemStack modifyMaterial(ItemStack stack) {
protected @NotNull ItemStack modifyMaterial(ItemStack stack, T context) {
return stack;
}

View file

@ -59,7 +59,7 @@ public class CustomSlabs {
}
}
public static class CustomSlab extends CustomItem {
public static class CustomSlab extends ContextlessItem {
public final Material material;
public final String name;
public final String camelName;

View file

@ -17,7 +17,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class NetherStarChunk extends CustomItem {
public class NetherStarChunk extends ContextlessItem {
@Override
public @NotNull NamespacedKey getKey() {
return BlazingGames.get().key("nether_star_chunk");

View file

@ -25,7 +25,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Map;
public class PortableCraftingTable extends CustomItem {
public class PortableCraftingTable extends ContextlessItem {
@Override
public @NotNull NamespacedKey getKey() {
return BlazingGames.get().key("portable_crafting_table");

View file

@ -31,7 +31,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class TeleportAnchor extends CustomItem {
public class TeleportAnchor extends ContextlessItem {
@Override
public @NotNull NamespacedKey getKey() {
return BlazingGames.get().key("teleport_anchor");

View file

@ -25,7 +25,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Map;
public class TomeAltar extends CustomItem {
public class TomeAltar extends ContextlessItem {
@Override
public @NotNull NamespacedKey getKey() {
return BlazingGames.get().key("tome_altar");

View file

@ -0,0 +1,5 @@
package de.blazemcworld.blazinggames.items.contexts;
public class EmptyItemContext implements ItemContext {
public static EmptyItemContext instance = new EmptyItemContext();
}

View file

@ -0,0 +1,5 @@
package de.blazemcworld.blazinggames.items.contexts;
public interface ItemContext {
}

View file

@ -16,7 +16,7 @@
package de.blazemcworld.blazinggames.multiblocks;
import de.blazemcworld.blazinggames.BlazingGames;
import de.blazemcworld.blazinggames.items.CustomItem;
import de.blazemcworld.blazinggames.items.ContextlessItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.*;
@ -28,7 +28,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Blueprint extends CustomItem {
public class Blueprint extends ContextlessItem {
@Override
public @NotNull NamespacedKey getKey() {
return BlazingGames.get().key("blueprint");