From 25e28963e6a9cfc647d9737fc95fc2d839bb0a02 Mon Sep 17 00:00:00 2001 From: Xiao-MoMi <70987828+Xiao-MoMi@users.noreply.github.com> Date: Wed, 18 Jan 2023 02:14:51 +0800 Subject: [PATCH] 2.2.1 --- build.gradle | 4 +- .../customcrops/api/event/WaterPotEvent.java | 15 ++++--- .../integrations/customplugin/HandlerP.java | 16 ++++++- .../itemsadder/ItemsAdderHandler.java | 14 +++--- .../customplugin/oraxen/OraxenHandler.java | 9 +++- .../integrations/protection/LandsHook.java | 28 +++++++++--- .../customcrops/managers/CustomWorld.java | 44 ++++++++++++------- 7 files changed, 93 insertions(+), 37 deletions(-) diff --git a/build.gradle b/build.gradle index 777d63d..343dfd5 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'net.momirealms' -version = '2.2.0' +version = '2.2.1' repositories { mavenCentral() @@ -58,7 +58,7 @@ dependencies { compileOnly 'dev.dejvokep:boosted-yaml:1.3' compileOnly 'com.github.Archy-X:AureliumSkills:Beta1.3.6' compileOnly 'commons-io:commons-io:2.11.0' - compileOnly 'com.github.angeschossen:LandsAPI:6.5.1' + compileOnly 'com.github.angeschossen:LandsAPI:6.26.18' compileOnly 'com.github.TechFortress:GriefPrevention:16.18' compileOnly 'io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT' compileOnly 'me.clip:placeholderapi:2.11.2' diff --git a/src/main/java/net/momirealms/customcrops/api/event/WaterPotEvent.java b/src/main/java/net/momirealms/customcrops/api/event/WaterPotEvent.java index 83dcc05..dde2833 100644 --- a/src/main/java/net/momirealms/customcrops/api/event/WaterPotEvent.java +++ b/src/main/java/net/momirealms/customcrops/api/event/WaterPotEvent.java @@ -17,26 +17,29 @@ package net.momirealms.customcrops.api.event; -import de.tr7zw.changeme.nbtapi.NBTItem; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerEvent; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; public class WaterPotEvent extends PlayerEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); private boolean cancelled; - private final NBTItem nbtItem; + private final ItemStack itemStack; private int currentWater; private final Location location; - public WaterPotEvent(@NotNull Player who, Location location, NBTItem nbtItem, int currentWater) { + /** + * ItemStack can be a water-bucket or a watering-can + */ + public WaterPotEvent(@NotNull Player who, Location location, ItemStack itemStack, int currentWater) { super(who); this.cancelled = false; - this.nbtItem = nbtItem; + this.itemStack = itemStack; this.currentWater = currentWater; this.location = location; } @@ -62,8 +65,8 @@ public class WaterPotEvent extends PlayerEvent implements Cancellable { return getHandlerList(); } - public NBTItem getNbtItem() { - return nbtItem; + public ItemStack getItemStack() { + return itemStack; } public int getCurrentWater() { diff --git a/src/main/java/net/momirealms/customcrops/integrations/customplugin/HandlerP.java b/src/main/java/net/momirealms/customcrops/integrations/customplugin/HandlerP.java index ba5434d..13b3484 100644 --- a/src/main/java/net/momirealms/customcrops/integrations/customplugin/HandlerP.java +++ b/src/main/java/net/momirealms/customcrops/integrations/customplugin/HandlerP.java @@ -17,6 +17,7 @@ package net.momirealms.customcrops.integrations.customplugin; +import com.willfp.eco.core.items.Items; import de.tr7zw.changeme.nbtapi.NBTCompound; import de.tr7zw.changeme.nbtapi.NBTItem; import net.kyori.adventure.text.minimessage.MiniMessage; @@ -314,7 +315,6 @@ public abstract class HandlerP extends Function { if (player.getGameMode() != GameMode.CREATIVE) item.setAmount(item.getAmount() - 1); customWorld.addSprinklerCache(sprinklerLoc, sprinkler); customInterface.placeFurniture(sprinklerLoc, config.getThreeD()); - return true; } return false; @@ -639,6 +639,20 @@ public abstract class HandlerP extends Function { return true; } + protected boolean useBucket(Location potLoc, Player player, ItemStack itemInHand) { + if (itemInHand.getType() == Material.WATER_BUCKET) { + WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, itemInHand, 0); + Bukkit.getPluginManager().callEvent(waterPotEvent); + if (waterPotEvent.isCancelled()) { + return false; + } + itemInHand.setType(Material.BUCKET); + waterPot(1,1, potLoc, 0); + return true; + } + return false; + } + protected boolean isInCoolDown(Player player, int delay) { long time = System.currentTimeMillis(); if (time - (coolDown.getOrDefault(player, time - delay)) < delay) return true; diff --git a/src/main/java/net/momirealms/customcrops/integrations/customplugin/itemsadder/ItemsAdderHandler.java b/src/main/java/net/momirealms/customcrops/integrations/customplugin/itemsadder/ItemsAdderHandler.java index c2d2422..abadcfe 100644 --- a/src/main/java/net/momirealms/customcrops/integrations/customplugin/itemsadder/ItemsAdderHandler.java +++ b/src/main/java/net/momirealms/customcrops/integrations/customplugin/itemsadder/ItemsAdderHandler.java @@ -125,8 +125,12 @@ public abstract class ItemsAdderHandler extends HandlerP { public boolean tryMisc(Player player, ItemStack itemInHand, Location potLoc) { if (!AntiGrief.testPlace(player, potLoc)) return true; if (itemInHand == null || itemInHand.getType() == Material.AIR) return true; - CustomStack customStack = CustomStack.byItemStack(itemInHand); + if (useBucket(potLoc, player, itemInHand)) { + return true; + } + + CustomStack customStack = CustomStack.byItemStack(itemInHand); if (customStack == null) return false; String itemID = customStack.getNamespacedID(); @@ -153,17 +157,17 @@ public abstract class ItemsAdderHandler extends HandlerP { int water = nbtItem.getInteger("WaterAmount"); if (water > 0) { - WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, nbtItem, --water); + WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, itemStack, --water); Bukkit.getPluginManager().callEvent(waterPotEvent); if (waterPotEvent.isCancelled()) { return true; } - + nbtItem = new NBTItem(waterPotEvent.getItemStack()); NBTCompound nbtCompound = nbtItem.getCompound("itemsadder"); - if (nbtCompound.hasKey("custom_durability")){ + if (nbtCompound.hasTag("custom_durability")){ int dur = nbtCompound.getInteger("custom_durability"); int max_dur = nbtCompound.getInteger("max_custom_durability"); - if (dur > 0){ + if (dur > 0) { nbtCompound.setInteger("custom_durability", dur - 1); nbtCompound.setDouble("fake_durability", (int) itemStack.getType().getMaxDurability() * (double) (dur/max_dur)); nbtItem.setInteger("Damage", (int) (itemStack.getType().getMaxDurability() * (1 - (double) dur/max_dur))); diff --git a/src/main/java/net/momirealms/customcrops/integrations/customplugin/oraxen/OraxenHandler.java b/src/main/java/net/momirealms/customcrops/integrations/customplugin/oraxen/OraxenHandler.java index 3ac36a5..6b7a1a6 100644 --- a/src/main/java/net/momirealms/customcrops/integrations/customplugin/oraxen/OraxenHandler.java +++ b/src/main/java/net/momirealms/customcrops/integrations/customplugin/oraxen/OraxenHandler.java @@ -88,6 +88,11 @@ public abstract class OraxenHandler extends HandlerP { public boolean tryMisc(Player player, ItemStack itemInHand, Location potLoc) { if (!AntiGrief.testPlace(player, potLoc)) return true; if (itemInHand == null || itemInHand.getType() == Material.AIR) return true; + + if (useBucket(potLoc, player, itemInHand)) { + return true; + } + String id = OraxenItems.getIdByItem(itemInHand); if (id == null) return false; @@ -100,6 +105,7 @@ public abstract class OraxenHandler extends HandlerP { if (useWateringCan(potLoc, id, player, itemInHand)) { return true; } + return false; //for future misc } @@ -146,12 +152,13 @@ public abstract class OraxenHandler extends HandlerP { int water = nbtItem.getInteger("WaterAmount"); if (water > 0) { - WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, nbtItem, --water); + WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, can, --water); Bukkit.getPluginManager().callEvent(waterPotEvent); if (waterPotEvent.isCancelled()) { return true; } + nbtItem = new NBTItem(waterPotEvent.getItemStack()); nbtItem.setInteger("WaterAmount", waterPotEvent.getCurrentWater()); if (SoundConfig.waterPot.isEnable()) { diff --git a/src/main/java/net/momirealms/customcrops/integrations/protection/LandsHook.java b/src/main/java/net/momirealms/customcrops/integrations/protection/LandsHook.java index 9241134..3403d08 100644 --- a/src/main/java/net/momirealms/customcrops/integrations/protection/LandsHook.java +++ b/src/main/java/net/momirealms/customcrops/integrations/protection/LandsHook.java @@ -17,26 +17,42 @@ package net.momirealms.customcrops.integrations.protection; +import me.angeschossen.lands.api.LandsIntegration; import me.angeschossen.lands.api.flags.Flags; +import me.angeschossen.lands.api.flags.enums.FlagTarget; +import me.angeschossen.lands.api.flags.enums.RoleFlagCategory; +import me.angeschossen.lands.api.flags.types.RoleFlag; import me.angeschossen.lands.api.land.Area; +import me.angeschossen.lands.api.land.LandWorld; import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.integrations.AntiGrief; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.entity.Player; public class LandsHook implements AntiGrief { + private final LandsIntegration api; + + public LandsHook() { + api = LandsIntegration.of(CustomCrops.plugin);; + } + @Override public boolean canBreak(Location location, Player player) { - Area area = new me.angeschossen.lands.api.integration.LandsIntegration(CustomCrops.plugin).getAreaByLoc(location); - if (area != null) return area.hasFlag(player, Flags.BLOCK_BREAK, false); - else return true; + LandWorld world = api.getWorld(location.getWorld()); + if (world != null) { + return world.hasRoleFlag(player.getUniqueId(), location, RoleFlag.of("BLOCK_BREAK")); + } + return true; } @Override public boolean canPlace(Location location, Player player) { - Area area = new me.angeschossen.lands.api.integration.LandsIntegration(CustomCrops.plugin).getAreaByLoc(location); - if (area != null) return area.hasFlag(player, Flags.BLOCK_PLACE, false); - else return true; + LandWorld world = api.getWorld(location.getWorld()); + if (world != null) { + return world.hasRoleFlag(player.getUniqueId(), location, RoleFlag.of("BLOCK_PLACE")); + } + return true; } } diff --git a/src/main/java/net/momirealms/customcrops/managers/CustomWorld.java b/src/main/java/net/momirealms/customcrops/managers/CustomWorld.java index d12701d..16c80cb 100644 --- a/src/main/java/net/momirealms/customcrops/managers/CustomWorld.java +++ b/src/main/java/net/momirealms/customcrops/managers/CustomWorld.java @@ -64,7 +64,6 @@ public class CustomWorld { private int timer; public CustomWorld(World world, CropManager cropManager) { - this.world = world; this.fertilizerCache = new ConcurrentHashMap<>(2048); this.sprinklerCache = new ConcurrentHashMap<>(512); @@ -370,10 +369,14 @@ public class CustomWorld { } else if (!compensation) { route(sprinklerTime); - potDryJudge(sprinklerTime + randomGenerator.nextInt(dryTime)); + for (Map.Entry entry : cropData.entrySet()) { growSingleWire(entry.getKey(), entry.getValue(), sprinklerTime + dryTime + randomGenerator.nextInt(cropTime)); } + + Bukkit.getScheduler().runTaskLaterAsynchronously(CustomCrops.plugin, () -> { + potDryJudge(dryTime); + }, sprinklerTime); } else { int delay = (int) (24000 - world.getTime()); @@ -405,10 +408,14 @@ public class CustomWorld { } else if (!compensation) { route(sprinklerTime); - potDryJudge(sprinklerTime + randomGenerator.nextInt(dryTime)); + for (Map.Entry entry : cropData.entrySet()) { growSingleFrame(entry.getKey(), entry.getValue(), sprinklerTime + dryTime + randomGenerator.nextInt(cropTime)); } + + Bukkit.getScheduler().runTaskLaterAsynchronously(CustomCrops.plugin, () -> { + potDryJudge(dryTime); + }, sprinklerTime); } else { int delay = (int) (24000 - world.getTime()); @@ -438,13 +445,18 @@ public class CustomWorld { private void route(int sprinklerTime) { - + //先将湿润的种植盆放入等待判断的种植盆列表中 tempWatered = new HashSet<>(watered); + //清空湿润的 watered.clear(); + //玩家浇水 watered.addAll(playerWatered); + //清除昨日玩家浇水 playerWatered.clear(); + //清除玩家昨日种植 plantedToday.clear();; + //洒水器工作会把种植盆放入watered中 Random randomGenerator = new Random(); for (Map.Entry sprinklerEntry : sprinklerCache.entrySet()) { bukkitScheduler.runTaskLaterAsynchronously(CustomCrops.plugin, () -> sprinklerWork(sprinklerEntry.getKey(), sprinklerEntry.getValue()), randomGenerator.nextInt(sprinklerTime)); @@ -489,25 +501,25 @@ public class CustomWorld { } } - private void potDryJudge(int time) { - new BukkitRunnable() { - @Override - public void run() { - tempWatered.removeAll(watered); - for (SimpleLocation simpleLocation : tempWatered) { + private void potDryJudge(int dry_time) { + //将待干的种植盆中今日被浇水的种植盆移除 + //剩余的种植盆即为需要变干的 + tempWatered.removeAll(watered); + for (SimpleLocation simpleLocation : tempWatered) { + new BukkitRunnable() { + @Override + public void run() { Location potLoc = MiscUtils.getLocation(simpleLocation); if (potLoc == null) return; - if (!isPotWet(potLoc)) { - cropManager.makePotDry(potLoc); - continue; - } Fertilizer fertilizer = getFertilizerCache(potLoc); if (!(fertilizer instanceof RetainingSoil retainingSoil && Math.random() < retainingSoil.getChance())) { cropManager.makePotDry(potLoc); } } - } - }.runTaskLaterAsynchronously(CustomCrops.plugin, time); + }.runTaskLaterAsynchronously(CustomCrops.plugin, new Random().nextInt(dry_time)); + } + //用完就抛弃 + tempWatered.clear(); } /**