From b75158a23e450b1f0b3010fea7ecd257087a5b0f Mon Sep 17 00:00:00 2001 From: Intybyte Date: Wed, 25 Sep 2024 21:01:38 +0200 Subject: [PATCH] Add StackResolver for backwards compatibility --- .../slimefun4/api/MinecraftVersion.java | 6 ++++ .../utils/multiversion/StackResolver.java | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/utils/multiversion/StackResolver.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/MinecraftVersion.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/MinecraftVersion.java index 4fc0160ac6..7a408e83fc 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/MinecraftVersion.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/MinecraftVersion.java @@ -55,6 +55,12 @@ public enum MinecraftVersion { */ MINECRAFT_1_20_5(20, 5, "1.20.5+"), + /** + * This constant represents Minecraft (Java Edition) Version 1.21 + * ("Tricky Trials") + */ + MINECRAFT_1_21(21, 0, "1.21+"), + /** * This constant represents an exceptional state in which we were unable * to identify the Minecraft Version we are using diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/multiversion/StackResolver.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/multiversion/StackResolver.java new file mode 100644 index 0000000000..5938400a2b --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/multiversion/StackResolver.java @@ -0,0 +1,31 @@ +package io.github.thebusybiscuit.slimefun4.utils.multiversion; + +import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +/** + * This utility class provides backwards compatibility to versions of minecraft + * regarding the creation of ItemStacks. + * + * @author Vaan1310 + */ +public final class StackResolver { + + private StackResolver() {} + + public static ItemStack of(Material material) { + return of(material, 1); + } + + public static ItemStack of(Material material, int amount) { + var version = Slimefun.getMinecraftVersion(); + + if (version.isAtLeast(MinecraftVersion.MINECRAFT_1_21)) { + return ItemStack.of(material, amount); + } else { + return new ItemStack(material, amount); + } + } +}