From c82244188e14e7f25d5692378eb2f2e9ee4712c6 Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Fri, 29 Mar 2024 13:09:20 +0800 Subject: [PATCH] fix item detection --- .../api/mechanic/requirement/State.java | 3 ++- .../customcrops/api/util/LocationUtils.java | 19 +++++++++++++++++++ build.gradle.kts | 2 +- .../compatibility/item/MMOItemsItemImpl.java | 2 +- .../mechanic/item/CustomProvider.java | 7 ++++--- .../mechanic/item/ItemManagerImpl.java | 13 +++++++------ .../custom/crucible/CrucibleProvider.java | 6 +----- .../custom/itemsadder/ItemsAdderProvider.java | 5 ++--- .../item/custom/oraxen/OraxenListener.java | 5 +++-- .../item/custom/oraxen/OraxenProvider.java | 8 +------- .../customcrops/mechanic/world/CWorld.java | 15 +++++++++++---- plugin/src/main/resources/plugin.yml | 2 +- 12 files changed, 53 insertions(+), 34 deletions(-) diff --git a/api/src/main/java/net/momirealms/customcrops/api/mechanic/requirement/State.java b/api/src/main/java/net/momirealms/customcrops/api/mechanic/requirement/State.java index 73d9836..630a49a 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/mechanic/requirement/State.java +++ b/api/src/main/java/net/momirealms/customcrops/api/mechanic/requirement/State.java @@ -17,6 +17,7 @@ package net.momirealms.customcrops.api.mechanic.requirement; +import net.momirealms.customcrops.api.util.LocationUtils; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -35,7 +36,7 @@ public class State { public State(Player player, ItemStack itemInHand, @NotNull Location location) { this.player = player; this.itemInHand = itemInHand; - this.location = location.toBlockLocation(); + this.location = LocationUtils.toBlockLocation(location); this.args = new HashMap<>(); if (player != null) { setArg("{player}", player.getName()); diff --git a/api/src/main/java/net/momirealms/customcrops/api/util/LocationUtils.java b/api/src/main/java/net/momirealms/customcrops/api/util/LocationUtils.java index e865ce0..41c2879 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/util/LocationUtils.java +++ b/api/src/main/java/net/momirealms/customcrops/api/util/LocationUtils.java @@ -19,6 +19,7 @@ package net.momirealms.customcrops.api.util; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.jetbrains.annotations.NotNull; public class LocationUtils { @@ -41,4 +42,22 @@ public class LocationUtils { public static Location getAnyLocationInstance() { return new Location(Bukkit.getWorlds().get(0), 0, 64, 0); } + + @NotNull + public static Location toBlockLocation(Location location) { + Location blockLoc = location.clone(); + blockLoc.setX(location.getBlockX()); + blockLoc.setY(location.getBlockY()); + blockLoc.setZ(location.getBlockZ()); + return blockLoc; + } + + @NotNull + public static Location toCenterLocation(Location location) { + Location centerLoc = location.clone(); + centerLoc.setX(location.getBlockX() + 0.5); + centerLoc.setY(location.getBlockY() + 0.5); + centerLoc.setZ(location.getBlockZ() + 0.5); + return centerLoc; + } } diff --git a/build.gradle.kts b/build.gradle.kts index 047e8fa..62352d7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { allprojects { project.group = "net.momirealms" - project.version = "3.4.3.3" + project.version = "3.4.3.4" apply() apply(plugin = "java") diff --git a/plugin/src/main/java/net/momirealms/customcrops/compatibility/item/MMOItemsItemImpl.java b/plugin/src/main/java/net/momirealms/customcrops/compatibility/item/MMOItemsItemImpl.java index 6d8ad25..4d9578f 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/compatibility/item/MMOItemsItemImpl.java +++ b/plugin/src/main/java/net/momirealms/customcrops/compatibility/item/MMOItemsItemImpl.java @@ -46,6 +46,6 @@ public class MMOItemsItemImpl implements ItemLibrary { public String getItemID(ItemStack itemStack) { NBTItem nbtItem = new NBTItem(itemStack); if (!nbtItem.hasTag("MMOITEMS_ITEM_ID")) return null; - return nbtItem.getString("MMOITEMS_ITEM_ID"); + return nbtItem.getString("MMOITEMS_ITEM_TYPE") + ":" + nbtItem.getString("MMOITEMS_ITEM_ID"); } } diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/CustomProvider.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/CustomProvider.java index c23dd29..1f6b772 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/CustomProvider.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/CustomProvider.java @@ -19,6 +19,7 @@ package net.momirealms.customcrops.mechanic.item; import net.momirealms.customcrops.api.manager.VersionManager; import net.momirealms.customcrops.api.mechanic.misc.CRotation; +import net.momirealms.customcrops.api.util.LocationUtils; import net.momirealms.customcrops.utils.ConfigUtils; import net.momirealms.customcrops.utils.DisplayEntityUtils; import net.momirealms.customcrops.utils.RotationUtils; @@ -65,7 +66,7 @@ public interface CustomProvider { Block block = location.getBlock(); if (block.getType() != Material.AIR) return false; - Location center = location.toCenterLocation(); + Location center = LocationUtils.toCenterLocation(location); Collection entities = center.getWorld().getNearbyEntities(center, 0.5,0.51,0.5); entities.removeIf(entity -> entity instanceof Player); return entities.size() == 0; @@ -73,7 +74,7 @@ public interface CustomProvider { default CRotation removeAnythingAt(Location location) { if (!removeBlock(location)) { - Collection entities = location.getWorld().getNearbyEntities(location.toCenterLocation(), 0.5,0.51,0.5); + Collection entities = location.getWorld().getNearbyEntities(LocationUtils.toCenterLocation(location), 0.5,0.51,0.5); entities.removeIf(entity -> { EntityType type = entity.getType(); return type != EntityType.ITEM_FRAME @@ -102,7 +103,7 @@ public interface CustomProvider { if (block.getType() != Material.AIR) { return getBlockID(block); } else { - Collection entities = location.getWorld().getNearbyEntities(location.toCenterLocation(), 0.5,0.5,0.5); + Collection entities = location.getWorld().getNearbyEntities(location.toCenterLocation(), 0.5,0.51,0.5); for (Entity entity : entities) { if (isFurniture(entity)) { return getEntityID(entity); diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/ItemManagerImpl.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/ItemManagerImpl.java index 5f7f147..2498be4 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/ItemManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/ItemManagerImpl.java @@ -38,6 +38,7 @@ import net.momirealms.customcrops.api.mechanic.requirement.State; import net.momirealms.customcrops.api.mechanic.world.CustomCropsBlock; import net.momirealms.customcrops.api.mechanic.world.SimpleLocation; import net.momirealms.customcrops.api.mechanic.world.level.*; +import net.momirealms.customcrops.api.util.LocationUtils; import net.momirealms.customcrops.api.util.LogUtils; import net.momirealms.customcrops.mechanic.item.custom.AbstractCustomListener; import net.momirealms.customcrops.mechanic.item.custom.crucible.CrucibleListener; @@ -124,13 +125,13 @@ public class ItemManagerImpl implements ItemManager { this.item2FertilizerMap = new HashMap<>(); this.stage2CropStageMap = new HashMap<>(); this.deadCrops = new HashSet<>(); - if (Bukkit.getPluginManager().isPluginEnabled("Oraxen")) { + if (Bukkit.getPluginManager().getPlugin("Oraxen") != null) { listener = new OraxenListener(this); customProvider = new OraxenProvider(); - } else if (Bukkit.getPluginManager().isPluginEnabled("ItemsAdder")) { + } else if (Bukkit.getPluginManager().getPlugin("ItemsAdder") != null) { listener = new ItemsAdderListener(this); customProvider = new ItemsAdderProvider(); - } else if (Bukkit.getPluginManager().isPluginEnabled("MythicCrucible")) { + } else if (Bukkit.getPluginManager().getPlugin("MythicCrucible") != null) { listener = new CrucibleListener(this); customProvider = new CrucibleProvider(); } else { @@ -221,7 +222,7 @@ public class ItemManagerImpl implements ItemManager { for (ItemLibrary library : itemDetectionArray) { id = library.getItemID(itemStack); if (id != null) - return id; + return library.identification() + ":" + id; } } return itemStack.getType().name(); @@ -1821,7 +1822,7 @@ public class ItemManagerImpl implements ItemManager { } Player player = interactWrapper.getPlayer(); - Location cropLocation = interactWrapper.getLocation().toBlockLocation(); + Location cropLocation = LocationUtils.toBlockLocation(interactWrapper.getLocation()); ItemStack itemInHand = interactWrapper.getItemInHand(); State cropState = new State(player, itemInHand, cropLocation); @@ -1928,7 +1929,7 @@ public class ItemManagerImpl implements ItemManager { return FunctionResult.PASS; } Player player = breakWrapper.getPlayer(); - Location cropLocation = breakWrapper.getLocation().toBlockLocation(); + Location cropLocation = LocationUtils.toBlockLocation(breakWrapper.getLocation()); State state = new State(player, breakWrapper.getItemInHand(), cropLocation); // check crop break requirements if (!RequirementManager.isRequirementMet(state, crop.getBreakRequirements())) { diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/crucible/CrucibleProvider.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/crucible/CrucibleProvider.java index 705e499..aa9a73f 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/crucible/CrucibleProvider.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/crucible/CrucibleProvider.java @@ -76,8 +76,6 @@ public class CrucibleProvider implements CustomProvider { @Override public Entity placeFurniture(Location location, String id) { - Location center = location.toCenterLocation(); - center.setY(center.getBlockY()); Optional optionalCI = itemManager.getItem(id); if (optionalCI.isPresent()) { return optionalCI.get().getFurnitureData().placeFrame(location.getBlock(), BlockFace.UP, 0f, null); @@ -101,9 +99,7 @@ public class CrucibleProvider implements CustomProvider { @Override public String getItemID(ItemStack itemStack) { - Optional optionalCI = itemManager.getItem(itemStack); - if (optionalCI.isEmpty()) return itemStack.getType().name(); - else return optionalCI.get().getInternalName(); + return itemManager.getItem(itemStack).map(CrucibleItem::getInternalName).orElse(null); } @Override diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/itemsadder/ItemsAdderProvider.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/itemsadder/ItemsAdderProvider.java index d008c99..7604f3d 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/itemsadder/ItemsAdderProvider.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/itemsadder/ItemsAdderProvider.java @@ -20,6 +20,7 @@ package net.momirealms.customcrops.mechanic.item.custom.itemsadder; import dev.lone.itemsadder.api.CustomBlock; import dev.lone.itemsadder.api.CustomFurniture; import dev.lone.itemsadder.api.CustomStack; +import net.momirealms.customcrops.api.util.LocationUtils; import net.momirealms.customcrops.api.util.LogUtils; import net.momirealms.customcrops.mechanic.item.CustomProvider; import org.bukkit.Location; @@ -52,8 +53,6 @@ public class ItemsAdderProvider implements CustomProvider { @Override public Entity placeFurniture(Location location, String id) { try { - Location center = location.toCenterLocation(); - center.setY(center.getBlockY()); CustomFurniture furniture = CustomFurniture.spawnPreciseNonSolid(id, location); if (furniture == null) return null; return furniture.getEntity(); @@ -82,7 +81,7 @@ public class ItemsAdderProvider implements CustomProvider { public String getItemID(ItemStack itemInHand) { CustomStack customStack = CustomStack.byItemStack(itemInHand); if (customStack == null) { - return itemInHand.getType().name(); + return null; } return customStack.getNamespacedID(); } diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/oraxen/OraxenListener.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/oraxen/OraxenListener.java index 8991f0a..41bc62e 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/oraxen/OraxenListener.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/oraxen/OraxenListener.java @@ -24,6 +24,7 @@ import io.th0rgal.oraxen.api.events.noteblock.OraxenNoteBlockBreakEvent; import io.th0rgal.oraxen.api.events.noteblock.OraxenNoteBlockPlaceEvent; import io.th0rgal.oraxen.api.events.stringblock.OraxenStringBlockBreakEvent; import io.th0rgal.oraxen.api.events.stringblock.OraxenStringBlockPlaceEvent; +import net.momirealms.customcrops.api.util.LocationUtils; import net.momirealms.customcrops.mechanic.item.ItemManagerImpl; import net.momirealms.customcrops.mechanic.item.custom.AbstractCustomListener; import org.bukkit.event.EventHandler; @@ -88,7 +89,7 @@ public class OraxenListener extends AbstractCustomListener { public void onBreakFurniture(OraxenFurnitureBreakEvent event) { super.onBreakFurniture( event.getPlayer(), - event.getBaseEntity().getLocation().toBlockLocation(), + LocationUtils.toBlockLocation(event.getBaseEntity().getLocation()), event.getMechanic().getItemID(), event ); @@ -98,7 +99,7 @@ public class OraxenListener extends AbstractCustomListener { public void onInteractFurniture(OraxenFurnitureInteractEvent event) { super.onInteractFurniture( event.getPlayer(), - event.getBaseEntity().getLocation().toBlockLocation(), + LocationUtils.toBlockLocation(event.getBaseEntity().getLocation()), event.getMechanic().getItemID(), event.getBaseEntity(), event diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/oraxen/OraxenProvider.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/oraxen/OraxenProvider.java index 1b2d5bd..754cd9b 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/oraxen/OraxenProvider.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/oraxen/OraxenProvider.java @@ -54,8 +54,6 @@ public class OraxenProvider implements CustomProvider { @Override public Entity placeFurniture(Location location, String id) { - Location center = location.toCenterLocation(); - center.setY(center.getBlockY()); Entity entity = OraxenFurniture.place(id, location, Rotation.NONE, BlockFace.UP); if (entity == null) { LogUtils.warn("Furniture(" + id +") doesn't exist in Oraxen configs. Please double check if that furniture exists."); @@ -79,11 +77,7 @@ public class OraxenProvider implements CustomProvider { @Override public String getItemID(ItemStack itemStack) { - String id = OraxenItems.getIdByItem(itemStack); - if (id == null) { - return itemStack.getType().name(); - } - return id; + return OraxenItems.getIdByItem(itemStack); } @Override diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CWorld.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CWorld.java index 6ce3304..052024d 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CWorld.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CWorld.java @@ -37,6 +37,7 @@ import net.momirealms.customcrops.api.scheduler.CancellableTask; import net.momirealms.customcrops.api.scheduler.Scheduler; import net.momirealms.customcrops.api.util.LogUtils; import net.momirealms.customcrops.utils.EventUtils; +import org.bukkit.Bukkit; import org.bukkit.World; import org.jetbrains.annotations.Nullable; @@ -51,7 +52,7 @@ import java.util.concurrent.TimeUnit; public class CWorld implements CustomCropsWorld { private final WorldManager worldManager; - private final WeakReference world; + private WeakReference world; private final ConcurrentHashMap loadedChunks; private final ConcurrentHashMap lazyChunks; private final ConcurrentHashMap loadedRegions; @@ -124,7 +125,7 @@ public class CWorld implements CustomCropsWorld { if (VersionManager.folia()) { Scheduler scheduler = CustomCropsPlugin.get().getScheduler(); for (CChunk chunk : loadedChunks.values()) { - scheduler.runTaskSync(chunk::secondTimer,world.get(), chunk.getChunkPos().x(), chunk.getChunkPos().z()); + scheduler.runTaskSync(chunk::secondTimer, getWorld(), chunk.getChunkPos().x(), chunk.getChunkPos().z()); } } else { for (CChunk chunk : loadedChunks.values()) { @@ -151,7 +152,7 @@ public class CWorld implements CustomCropsWorld { } private void updateSeasonAndDate() { - World bukkitWorld = world.get(); + World bukkitWorld = getWorld(); if (bukkitWorld == null) { LogUtils.severe(String.format("World %s unloaded unexpectedly. Stop ticking task...", worldName)); this.cancelTick(); @@ -201,7 +202,13 @@ public class CWorld implements CustomCropsWorld { @Nullable @Override public World getWorld() { - return world.get(); + return Optional.ofNullable(world.get()).orElseGet(() -> { + World bukkitWorld = Bukkit.getWorld(worldName); + if (bukkitWorld != null) { + this.world = new WeakReference<>(bukkitWorld); + } + return bukkitWorld; + }); } @Override diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index 1bf4774..24cfed3 100644 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -34,7 +34,7 @@ softdepend: - GriefDefender - GriefPrevention - BentoBox - - IridiumSkyBlock + - IridiumSkyblock - KingdomsX - Landlord - Lands