mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-31 12:56:28 +00:00
Improve cartography table
This commit is contained in:
@@ -98,6 +98,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
private final BukkitCraftEngine plugin;
|
||||
private final RecipeEventListener recipeEventListener;
|
||||
private final CrafterEventListener crafterEventListener;
|
||||
private final PaperRecipeEventListener paperRecipeEventListener;
|
||||
private final Map<Key, List<Recipe<ItemStack>>> recipes;
|
||||
private final VanillaRecipeReader recipeReader;
|
||||
private final List<NamespacedKey> injectedDataPackRecipes;
|
||||
@@ -123,6 +124,11 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
} else {
|
||||
this.crafterEventListener = null;
|
||||
}
|
||||
if (VersionHelper.isPaper()) {
|
||||
this.paperRecipeEventListener = new PaperRecipeEventListener();
|
||||
} else {
|
||||
this.paperRecipeEventListener = null;
|
||||
}
|
||||
if (VersionHelper.isVersionNewerThan1_21_2()) {
|
||||
this.recipeReader = new VanillaRecipeReader1_21_2();
|
||||
} else if (VersionHelper.isVersionNewerThan1_20_5()) {
|
||||
@@ -154,6 +160,9 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
if (this.crafterEventListener != null) {
|
||||
Bukkit.getPluginManager().registerEvents(this.crafterEventListener, this.plugin.bootstrap());
|
||||
}
|
||||
if (this.paperRecipeEventListener != null) {
|
||||
Bukkit.getPluginManager().registerEvents(this.paperRecipeEventListener, this.plugin.bootstrap());
|
||||
}
|
||||
if (VersionHelper.isVersionNewerThan1_21_2()) {
|
||||
try {
|
||||
this.stolenFeatureFlagSet = Reflections.field$RecipeManager$featureflagset.get(minecraftRecipeManager);
|
||||
@@ -170,6 +179,9 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
if (this.crafterEventListener != null) {
|
||||
HandlerList.unregisterAll(this.crafterEventListener);
|
||||
}
|
||||
if (this.paperRecipeEventListener != null) {
|
||||
HandlerList.unregisterAll(this.paperRecipeEventListener);
|
||||
}
|
||||
this.recipes.clear();
|
||||
this.dataPackRecipes.clear();
|
||||
this.customRecipes.clear();
|
||||
|
||||
@@ -50,7 +50,7 @@ public class CrafterEventListener implements Listener {
|
||||
|
||||
// if the recipe is a vanilla one, custom items should never be ingredients
|
||||
if (this.recipeManager.isDataPackRecipe(recipeId) && !isCustom) {
|
||||
if (hasCustomItem(ingredients)) {
|
||||
if (ItemUtils.hasCustomItem(ingredients)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
return;
|
||||
@@ -100,15 +100,4 @@ public class CrafterEventListener implements Listener {
|
||||
// clear result if not met
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
private boolean hasCustomItem(ItemStack[] stack) {
|
||||
for (ItemStack itemStack : stack) {
|
||||
if (!ItemUtils.isEmpty(itemStack)) {
|
||||
if (this.itemManager.wrap(itemStack).customId().isPresent()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.momirealms.craftengine.bukkit.item.recipe;
|
||||
|
||||
import com.destroystokyo.paper.event.inventory.PrepareResultEvent;
|
||||
import net.momirealms.craftengine.bukkit.util.ItemUtils;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.inventory.CartographyInventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class PaperRecipeEventListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPrepareResult(PrepareResultEvent event) {
|
||||
if (event.getInventory() instanceof CartographyInventory cartographyInventory) {
|
||||
if (ItemUtils.hasCustomItem(cartographyInventory.getStorageContents())) {
|
||||
event.setResult(new ItemStack(Material.AIR));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,12 @@ import net.momirealms.craftengine.core.item.recipe.input.CraftingInput;
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
import net.momirealms.craftengine.core.registry.Holder;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.PrepareItemCraftEvent;
|
||||
import org.bukkit.inventory.*;
|
||||
|
||||
@@ -35,6 +38,19 @@ public class RecipeEventListener implements Listener {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onClickCartographyTable(InventoryClickEvent event) {
|
||||
if (VersionHelper.isPaper()) return;
|
||||
if (!(event.getClickedInventory() instanceof CartographyInventory cartographyInventory)) {
|
||||
return;
|
||||
}
|
||||
plugin.scheduler().sync().runDelayed(() -> {
|
||||
if (ItemUtils.hasCustomItem(cartographyInventory.getContents())) {
|
||||
cartographyInventory.setResult(new ItemStack(Material.AIR));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onCrafting(PrepareItemCraftEvent event) {
|
||||
org.bukkit.inventory.Recipe recipe = event.getRecipe();
|
||||
@@ -56,7 +72,7 @@ public class RecipeEventListener implements Listener {
|
||||
|
||||
// if the recipe is a vanilla one but not injected, custom items should never be ingredients
|
||||
if (this.recipeManager.isDataPackRecipe(recipeId) && !isCustom) {
|
||||
if (hasCustomItem(ingredients)) {
|
||||
if (ItemUtils.hasCustomItem(ingredients)) {
|
||||
inventory.setResult(null);
|
||||
}
|
||||
return;
|
||||
@@ -126,14 +142,5 @@ public class RecipeEventListener implements Listener {
|
||||
inventory.setResult(null);
|
||||
}
|
||||
|
||||
private boolean hasCustomItem(ItemStack[] stack) {
|
||||
for (ItemStack itemStack : stack) {
|
||||
if (!ItemUtils.isEmpty(itemStack)) {
|
||||
if (this.itemManager.wrap(itemStack).customId().isPresent()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.momirealms.craftengine.bukkit.util;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -28,4 +29,15 @@ public class ItemUtils {
|
||||
CraftEngine.instance().logger().warn("Failed to set item", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean hasCustomItem(ItemStack[] stack) {
|
||||
for (ItemStack itemStack : stack) {
|
||||
if (!ItemUtils.isEmpty(itemStack)) {
|
||||
if (BukkitItemManager.instance().wrap(itemStack).customId().isPresent()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,14 @@ public class VersionHelper {
|
||||
private static float version;
|
||||
private static boolean mojmap;
|
||||
private static boolean folia;
|
||||
private static boolean paper;
|
||||
|
||||
public static void init(String serverVersion) {
|
||||
String[] split = serverVersion.split("\\.");
|
||||
version = Float.parseFloat(split[1] + "." + (split.length == 3 ? split[2] : "0"));
|
||||
checkMojMap();
|
||||
checkFolia();
|
||||
checkPaper();
|
||||
}
|
||||
|
||||
public static float version() {
|
||||
@@ -33,6 +35,14 @@ public class VersionHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkPaper() {
|
||||
try {
|
||||
Class.forName("io.papermc.paper.adventure.PaperAdventure");
|
||||
paper = true;
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isFolia() {
|
||||
return folia;
|
||||
}
|
||||
@@ -80,4 +90,8 @@ public class VersionHelper {
|
||||
public static boolean isVersionNewerThan1_20_3() {
|
||||
return version >= 20.29f;
|
||||
}
|
||||
|
||||
public static boolean isPaper() {
|
||||
return paper;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user