diff --git a/src/main/java/net/momirealms/customcrops/managers/CropManager.java b/src/main/java/net/momirealms/customcrops/managers/CropManager.java index 7d33e4f..a41a2e0 100644 --- a/src/main/java/net/momirealms/customcrops/managers/CropManager.java +++ b/src/main/java/net/momirealms/customcrops/managers/CropManager.java @@ -198,7 +198,7 @@ public class CropManager extends Function { CustomWorld customWorld = new CustomWorld(world, this); customWorlds.put(world, customWorld); if (MainConfig.autoGrow && MainConfig.enableCompensation) { - if (world.getTime() < 24000 - MainConfig.timeToWork - MainConfig.timeToDry - 1200 && world.getTime() > 1200) { + if (world.getTime() > 1200) { Bukkit.getScheduler().runTaskLaterAsynchronously(CustomCrops.plugin, () -> grow(world, MainConfig.timeToGrow, MainConfig.timeToWork, MainConfig.timeToDry, true), 100); } } diff --git a/src/main/java/net/momirealms/customcrops/managers/CustomWorld.java b/src/main/java/net/momirealms/customcrops/managers/CustomWorld.java index 7e8a818..7b6f761 100644 --- a/src/main/java/net/momirealms/customcrops/managers/CustomWorld.java +++ b/src/main/java/net/momirealms/customcrops/managers/CustomWorld.java @@ -50,7 +50,7 @@ import java.util.concurrent.ConcurrentHashMap; public class CustomWorld { private final World world; - private final ConcurrentHashMap cropCache; + private final ConcurrentHashMap sprinklerCache; private final ConcurrentHashMap fertilizerCache; private final ConcurrentHashMap> scarecrowCache; @@ -58,17 +58,17 @@ public class CustomWorld { private HashSet tempWatered; private final HashSet playerWatered; private final CropManager cropManager; - private final HashSet tasksCache; private final BukkitScheduler bukkitScheduler; private final HashSet plantedToday; private final CropModeInterface cropMode; + private YamlConfiguration cropData; + public CustomWorld(World world, CropManager cropManager) { this.world = world; - this.cropCache = new ConcurrentHashMap<>(4096); + this.fertilizerCache = new ConcurrentHashMap<>(2048); this.sprinklerCache = new ConcurrentHashMap<>(1024); - this.tasksCache = new HashSet<>(4096); this.scarecrowCache = new ConcurrentHashMap<>(256); this.cropManager = cropManager; this.bukkitScheduler = Bukkit.getScheduler(); @@ -89,18 +89,10 @@ public class CustomWorld { public void unload(boolean disable) { if (disable) { unloadData(); - for (BukkitTask task : tasksCache) { - task.cancel(); - } - tasksCache.clear(); } else { Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.plugin, () -> { unloadData(); - for (BukkitTask task : tasksCache) { - task.cancel(); - } - tasksCache.clear(); Bukkit.getScheduler().runTask(CustomCrops.plugin, () -> { CustomWorldEvent customWorldEvent = new CustomWorldEvent(world, WorldState.UNLOAD); Bukkit.getPluginManager().callEvent(customWorldEvent); @@ -327,27 +319,12 @@ public class CustomWorld { } private void loadCropCache() { - YamlConfiguration data = loadData("crops", world.getName()); - for (String key : data.getKeys(false)) { - String[] loc = StringUtils.split(key, ","); - SimpleLocation location = new SimpleLocation(world.getName(), Integer.parseInt(loc[0]), Integer.parseInt(loc[1]), Integer.parseInt(loc[2])); - String crop = data.getString(key); - if (crop == null) return; - if (CropConfig.CROPS.containsKey(crop)) { - cropCache.put(location, crop); - } - } + cropData = loadData("crops", world.getName()); } private void unloadCrop() { - YamlConfiguration data = new YamlConfiguration(); - for (Map.Entry en : cropCache.entrySet()) { - SimpleLocation location = en.getKey(); - String loc = location.getX() + "," + location.getY() + "," + location.getZ(); - data.set(loc, en.getValue()); - } try { - data.save(new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), world.getName() + File.separator + "customcrops_data" + File.separator + "crops.yml")); + cropData.save(new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), world.getName() + File.separator + "customcrops_data" + File.separator + "crops.yml")); } catch (IOException e) { e.printStackTrace(); @@ -362,22 +339,29 @@ public class CustomWorld { if (!compensation) { route(sprinklerTime); potDryJudge(sprinklerTime + randomGenerator.nextInt(dryTime)); + cropData.getKeys(false).forEach(key -> { + Location location = MiscUtils.getLocation(key, world); + growSingleWire(location, sprinklerTime + dryTime + randomGenerator.nextInt(cropTime), key); + }); } - - for (SimpleLocation location : cropCache.keySet()) { - growSingleWire(location, sprinklerTime + dryTime + randomGenerator.nextInt(cropTime)); + else { + int delay = (int)(24000 - world.getTime()); + double chance = (double) (24000 - world.getTime()) / 24000; + cropData.getKeys(false).forEach(key -> { + if (Math.random() < chance) { + Location location = MiscUtils.getLocation(key, world); + growSingleWire(location, randomGenerator.nextInt(delay), key); + } + }); } } - private void growSingleWire(SimpleLocation location, long delay) { - BukkitTask task = bukkitScheduler.runTaskLaterAsynchronously(CustomCrops.plugin, () -> { - Location seedLoc = MiscUtils.getLocation(location); - if (seedLoc == null) return; - if (cropMode.growJudge(seedLoc)) { - cropCache.remove(location); + private void growSingleWire(Location location, long delay, String key) { + bukkitScheduler.runTaskLaterAsynchronously(CustomCrops.plugin, () -> { + if (cropMode.growJudge(location)) { + cropData.set(key, null); } }, delay); - tasksCache.add(task); } public void growFrame(int cropTime, int sprinklerTime, int dryTime, boolean compensation) { @@ -387,38 +371,36 @@ public class CustomWorld { if (!compensation) { route(sprinklerTime); potDryJudge(sprinklerTime + randomGenerator.nextInt(dryTime)); + cropData.getKeys(false).forEach(key -> { + Location location = MiscUtils.getLocation(key, world); + growSingleFrame(location, sprinklerTime + dryTime + randomGenerator.nextInt(cropTime), key); + }); } - - for (SimpleLocation location : cropCache.keySet()) { - long random = randomGenerator.nextInt(cropTime); - growSingleFrame(location, sprinklerTime + dryTime + random); + else { + int delay = (int)(24000 - world.getTime()); + double chance = (double) (24000 - world.getTime()) / 24000; + cropData.getKeys(false).forEach(key -> { + if (Math.random() < chance) { + Location location = MiscUtils.getLocation(key, world); + growSingleFrame(location, randomGenerator.nextInt(delay), key); + } + }); } } - private void growSingleFrame(SimpleLocation location, long delay) { - BukkitTask task1 = bukkitScheduler.runTaskLater(CustomCrops.plugin, () -> { - Location seedLoc = MiscUtils.getLocation(location); - if (seedLoc == null) return; - cropMode.loadChunk(seedLoc); + private void growSingleFrame(Location location, long delay, String key) { + bukkitScheduler.runTaskLater(CustomCrops.plugin, () -> { + cropMode.loadChunk(location); }, delay); - BukkitTask task2 = bukkitScheduler.runTaskLater(CustomCrops.plugin, () -> { - Location seedLoc = MiscUtils.getLocation(location); - if (seedLoc == null) return; - if (cropMode.growJudge(seedLoc)) { - cropCache.remove(location); + bukkitScheduler.runTaskLater(CustomCrops.plugin, () -> { + if (cropMode.growJudge(location)) { + cropData.set(key, null); } }, delay + 5); - tasksCache.add(task1); - tasksCache.add(task2); } private void route(int sprinklerTime) { - for (BukkitTask task : tasksCache) { - task.cancel(); - } - tasksCache.clear(); - tempWatered = new HashSet<>(watered); watered.clear(); watered.addAll(playerWatered); @@ -428,11 +410,10 @@ public class CustomWorld { Random randomGenerator = new Random(); for (Map.Entry sprinklerEntry : sprinklerCache.entrySet()) { - BukkitTask task = bukkitScheduler.runTaskLaterAsynchronously(CustomCrops.plugin, () -> { + bukkitScheduler.runTaskLaterAsynchronously(CustomCrops.plugin, () -> { sprinklerWork(sprinklerEntry.getKey(), sprinklerEntry.getValue()); }, randomGenerator.nextInt(sprinklerTime)); - tasksCache.add(task); } for (Map.Entry fertilizerEntry : fertilizerCache.entrySet()) { @@ -511,7 +492,7 @@ public class CustomWorld { } public void removeCrop(Location cropLoc) { - cropCache.remove(MiscUtils.getSimpleLocation(cropLoc)); + cropData.set(MiscUtils.getStringLocation(cropLoc), null); } @Nullable @@ -521,15 +502,18 @@ public class CustomWorld { public void addCrop(Location cropLoc, String crop) { SimpleLocation simpleLocation = MiscUtils.getSimpleLocation(cropLoc); - cropCache.put(simpleLocation, crop); - if (MainConfig.enableCompensation && !plantedToday.contains(simpleLocation) && world.getTime() < 23000) { - long random = new Random().nextInt(MainConfig.timeToGrow); + String key = MiscUtils.getStringLocation(cropLoc); + cropData.set(key, crop); + if (MainConfig.enableCompensation && !plantedToday.contains(simpleLocation) && world.getTime() > 1500) { + int delay = (int)(24000 - world.getTime()); + double chance = (double) (24000 - world.getTime()) / 24000; plantedToday.add(simpleLocation); + if (Math.random() > chance) return; if (MainConfig.cropMode) { - growSingleWire(simpleLocation, random); + growSingleWire(cropLoc, new Random().nextInt(delay), key); } else { - growSingleFrame(simpleLocation, random); + growSingleFrame(cropLoc, new Random().nextInt(delay), key); } } } diff --git a/src/main/java/net/momirealms/customcrops/utils/LimitationUtil.java b/src/main/java/net/momirealms/customcrops/utils/LimitationUtil.java index da9b7a3..982f981 100644 --- a/src/main/java/net/momirealms/customcrops/utils/LimitationUtil.java +++ b/src/main/java/net/momirealms/customcrops/utils/LimitationUtil.java @@ -27,7 +27,7 @@ public class LimitationUtil { int minHeight = location.getWorld().getMinHeight(); int maxHeight = location.getWorld().getMaxHeight(); Location chunkLocation = new Location(location.getWorld(), location.getChunk().getX() * 16, minHeight, location.getChunk().getZ() * 16); - int n = 0; + int n = 1; for (int i = 0; i < 16; ++i){ for (int j = 0; j < 16; ++j) { Location square = chunkLocation.clone().add(i, 0, j); @@ -46,7 +46,7 @@ public class LimitationUtil { int minHeight = location.getWorld().getMinHeight(); int maxHeight = location.getWorld().getMaxHeight(); Location chunkLocation = new Location(location.getWorld(), location.getChunk().getX() * 16, minHeight, location.getChunk().getZ() * 16); - int n = 0; + int n = 1; for (int i = 0; i < 16; ++i) { for (int j = 0; j < 16; ++j) { Location square; diff --git a/src/main/java/net/momirealms/customcrops/utils/MiscUtils.java b/src/main/java/net/momirealms/customcrops/utils/MiscUtils.java index e545e0f..1188e0d 100644 --- a/src/main/java/net/momirealms/customcrops/utils/MiscUtils.java +++ b/src/main/java/net/momirealms/customcrops/utils/MiscUtils.java @@ -41,4 +41,13 @@ public class MiscUtils { String[] loc = StringUtils.split(location, ","); return new SimpleLocation(world, Integer.parseInt(loc[0]), Integer.parseInt(loc[1]), Integer.parseInt(loc[2])); } + + public static Location getLocation(String location, World world) { + String[] loc = StringUtils.split(location, ","); + return new Location(world, Integer.parseInt(loc[0]), Integer.parseInt(loc[1]), Integer.parseInt(loc[2])); + } + + public static String getStringLocation(Location location) { + return location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ(); + } }