From be10cd1874d5fce5cfbddd188bc998a3aa7a66f3 Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Sun, 10 Mar 2024 09:35:55 +0800 Subject: [PATCH] improve interactions --- build.gradle.kts | 2 +- .../mechanic/item/ItemManagerImpl.java | 38 +++++++++++-------- .../item/custom/AbstractCustomListener.java | 11 ++++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index e28846f..1f1c98c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { allprojects { project.group = "net.momirealms" - project.version = "3.4.0.1" + project.version = "3.4.0.2" apply() apply(plugin = "java") 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 f96e76f..22cf9f7 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 @@ -1206,14 +1206,17 @@ public class ItemManagerImpl implements ItemManager { Optional optionalSprinkler = plugin.getWorldManager().getSprinklerAt(simpleLocation); if (optionalSprinkler.isEmpty()) { plugin.debug("Found a sprinkler without data interacted by " + player.getName() + " at " + location); - plugin.getWorldManager().addSprinklerAt(new MemorySprinkler(simpleLocation, sprinkler.getKey(), 0), simpleLocation); - return FunctionResult.RETURN; - } - if (!optionalSprinkler.get().getKey().equals(sprinkler.getKey())) { - LogUtils.warn("Found a sprinkler having inconsistent data interacted by " + player.getName() + " at " + location + "."); - plugin.getWorldManager().addSprinklerAt(new MemorySprinkler(simpleLocation, sprinkler.getKey(), 0), simpleLocation); - return FunctionResult.RETURN; + WorldSprinkler newSprinkler = new MemorySprinkler(simpleLocation, sprinkler.getKey(), 0); + plugin.getWorldManager().addSprinklerAt(newSprinkler, simpleLocation); + optionalSprinkler = Optional.of(newSprinkler); + } else { + if (!optionalSprinkler.get().getKey().equals(sprinkler.getKey())) { + LogUtils.warn("Found a sprinkler having inconsistent data interacted by " + player.getName() + " at " + location + "."); + plugin.getWorldManager().addSprinklerAt(new MemorySprinkler(simpleLocation, sprinkler.getKey(), 0), simpleLocation); + return FunctionResult.RETURN; + } } + // fire the event SprinklerInteractEvent interactEvent = new SprinklerInteractEvent(player, itemInHand, location, optionalSprinkler.get()); if (EventUtils.fireAndCheckCancel(interactEvent)) { @@ -1703,7 +1706,9 @@ public class ItemManagerImpl implements ItemManager { Optional optionalCrop = plugin.getWorldManager().getCropAt(simpleLocation); if (optionalCrop.isEmpty()) { plugin.debug("Found a crop without data interacted by " + player.getName() + " at " + cropLocation); - plugin.getWorldManager().addCropAt(new MemoryCrop(simpleLocation, crop.getKey(), stage.getPoint()), simpleLocation); + WorldCrop newCrop = new MemoryCrop(simpleLocation, crop.getKey(), stage.getPoint()); + plugin.getWorldManager().addCropAt(newCrop, simpleLocation); + optionalCrop = Optional.of(newCrop); } else { if (!optionalCrop.get().getKey().equals(crop.getKey())) { LogUtils.warn("Found a crop having inconsistent data interacted by " + player.getName() + " at " + cropLocation + "."); @@ -1878,14 +1883,17 @@ public class ItemManagerImpl implements ItemManager { Optional optionalPot = plugin.getWorldManager().getPotAt(simpleLocation); if (optionalPot.isEmpty()) { plugin.debug("Found a pot without data interacted by " + player.getName() + " at " + location); - plugin.getWorldManager().addPotAt(new MemoryPot(simpleLocation, pot.getKey()), simpleLocation); - return FunctionResult.RETURN; - } - if (!optionalPot.get().getKey().equals(pot.getKey())) { - LogUtils.warn("Found a pot having inconsistent data interacted by " + player.getName() + " at " + location + "."); - plugin.getWorldManager().addPotAt(new MemoryPot(simpleLocation, pot.getKey()), simpleLocation); - return FunctionResult.RETURN; + WorldPot newPot = new MemoryPot(simpleLocation, pot.getKey()); + plugin.getWorldManager().addPotAt(newPot, simpleLocation); + optionalPot = Optional.of(newPot); + } else { + if (!optionalPot.get().getKey().equals(pot.getKey())) { + LogUtils.warn("Found a pot having inconsistent data interacted by " + player.getName() + " at " + location + "."); + plugin.getWorldManager().addPotAt(new MemoryPot(simpleLocation, pot.getKey()), simpleLocation); + return FunctionResult.RETURN; + } } + // fire the event PotInteractEvent interactEvent = new PotInteractEvent(player, itemInHand, location, optionalPot.get()); if (EventUtils.fireAndCheckCancel(interactEvent)) { diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/AbstractCustomListener.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/AbstractCustomListener.java index 5f1c49e..6ab7427 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/AbstractCustomListener.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/item/custom/AbstractCustomListener.java @@ -126,6 +126,17 @@ public abstract class AbstractCustomListener implements Listener { } } + @EventHandler (ignoreCancelled = true) + public void onBlockChange(BlockFadeEvent event) { + Block block = event.getBlock(); + if (block.getType() == Material.FARMLAND) { + SimpleLocation above = SimpleLocation.of(block.getLocation()).add(0,1,0); + if (CustomCropsPlugin.get().getWorldManager().getBlockAt(above).isPresent()) { + event.setCancelled(true); + } + } + } + @EventHandler (ignoreCancelled = true) public void onTrampling(EntityChangeBlockEvent event) { Block block = event.getBlock();