9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-25 09:59:20 +00:00
This commit is contained in:
Xiao-MoMi
2022-10-28 16:28:56 +08:00
parent a75c2b119c
commit b79fc94beb
24 changed files with 194 additions and 67 deletions

View File

@@ -53,6 +53,7 @@ public class CropHarvestEvent extends PlayerEvent implements Cancellable {
this.cancelled = cancel;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -31,6 +31,7 @@ public class CrowAttackEvent extends Event {
this.location = location;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -34,6 +34,7 @@ public class CustomWorldEvent extends WorldEvent {
this.state = worldState;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -49,6 +49,7 @@ public class FertilizerUseEvent extends PlayerEvent implements Cancellable {
this.cancelled = cancel;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -28,6 +28,7 @@ public class PreActionEvent extends PlayerEvent implements Cancellable {
cancelled = cancel;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -52,6 +52,7 @@ public class SeedPlantEvent extends Event implements Cancellable {
this.cancelled = cancel;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -46,6 +46,7 @@ public class SprinklerFillEvent extends PlayerEvent implements Cancellable {
this.cancelled = cancel;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -47,6 +47,7 @@ public class SprinklerPlaceEvent extends PlayerEvent implements Cancellable {
this.cancelled = cancel;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -51,6 +51,7 @@ public class SurveyorUseEvent extends PlayerEvent implements Cancellable {
this.cancelled = cancel;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -46,6 +46,7 @@ public class WaterEvent extends PlayerEvent implements Cancellable {
this.cancelled = cancel;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -46,6 +46,7 @@ public class WateringCanFillEvent extends PlayerEvent implements Cancellable {
this.cancelled = cancel;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

View File

@@ -40,7 +40,7 @@ public class CropUtils {
* whether planting succeeds
* @param location location
* @param crop crop
* @return
* @return success or not
*/
public static boolean plantCrop(Location location, String crop) {
return CustomCrops.plugin.getCropManager().getHandler().plantSeed(location, crop, null, null);

View File

@@ -17,10 +17,7 @@
package net.momirealms.customcrops.commands;
import net.momirealms.customcrops.commands.subcmd.GrowCommand;
import net.momirealms.customcrops.commands.subcmd.ReloadCommand;
import net.momirealms.customcrops.commands.subcmd.SetSeasonCommand;
import net.momirealms.customcrops.commands.subcmd.SimulateCommand;
import net.momirealms.customcrops.commands.subcmd.*;
import net.momirealms.customcrops.config.MessageConfig;
import net.momirealms.customcrops.utils.AdventureUtil;
import org.bukkit.command.Command;
@@ -61,6 +58,8 @@ public class PluginCommand implements TabExecutor {
regSubCommand(SetSeasonCommand.INSTANCE);
regSubCommand(SimulateCommand.INSTANCE);
regSubCommand(GrowCommand.INSTANCE);
regSubCommand(SaveCacheCommand.INSTANCE);
regSubCommand(BackUpCommand.INSTANCE);
}
public void regSubCommand(SubCommand executor) {

View File

@@ -0,0 +1,54 @@
package net.momirealms.customcrops.commands.subcmd;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.commands.AbstractSubCommand;
import net.momirealms.customcrops.commands.SubCommand;
import net.momirealms.customcrops.config.MessageConfig;
import net.momirealms.customcrops.managers.CustomWorld;
import net.momirealms.customcrops.utils.AdventureUtil;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import java.util.List;
public class BackUpCommand extends AbstractSubCommand {
public static final SubCommand INSTANCE = new BackUpCommand();
public BackUpCommand() {
super("backup", null);
}
@Override
public boolean onCommand(CommandSender sender, List<String> args) {
if (args.size() < 1) {
AdventureUtil.sendMessage(sender, MessageConfig.prefix + MessageConfig.lackArgs);
return true;
}
String worldName = args.get(0);
World world = Bukkit.getWorld(worldName);
if (world == null) {
AdventureUtil.sendMessage(sender, MessageConfig.prefix + MessageConfig.worldNotExists);
return true;
}
CustomWorld customWorld = CustomCrops.plugin.getCropManager().getCustomWorld(world);
if (customWorld == null) {
AdventureUtil.sendMessage(sender, MessageConfig.prefix + "CustomCrops is not enabled in that world");
return true;
}
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.plugin, () -> {
customWorld.backUp();
AdventureUtil.sendMessage(sender, MessageConfig.prefix + "Done");
});
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, List<String> args) {
if (args.size() == 1) {
return getWorlds(args);
}
return super.onTabComplete(sender, args);
}
}

View File

@@ -0,0 +1,54 @@
package net.momirealms.customcrops.commands.subcmd;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.commands.AbstractSubCommand;
import net.momirealms.customcrops.commands.SubCommand;
import net.momirealms.customcrops.config.MessageConfig;
import net.momirealms.customcrops.managers.CustomWorld;
import net.momirealms.customcrops.utils.AdventureUtil;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import java.util.List;
public class SaveCacheCommand extends AbstractSubCommand {
public static final SubCommand INSTANCE = new SaveCacheCommand();
public SaveCacheCommand() {
super("savecache", null);
}
@Override
public boolean onCommand(CommandSender sender, List<String> args) {
if (args.size() < 1) {
AdventureUtil.sendMessage(sender, MessageConfig.prefix + MessageConfig.lackArgs);
return true;
}
String worldName = args.get(0);
World world = Bukkit.getWorld(worldName);
if (world == null) {
AdventureUtil.sendMessage(sender, MessageConfig.prefix + MessageConfig.worldNotExists);
return true;
}
CustomWorld customWorld = CustomCrops.plugin.getCropManager().getCustomWorld(world);
if (customWorld == null) {
AdventureUtil.sendMessage(sender, MessageConfig.prefix + "CustomCrops is not enabled in that world");
return true;
}
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.plugin, () -> {
customWorld.unloadData();
AdventureUtil.sendMessage(sender, MessageConfig.prefix + "Done");
});
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, List<String> args) {
if (args.size() == 1) {
return getWorlds(args);
}
return super.onTabComplete(sender, args);
}
}

View File

@@ -122,6 +122,7 @@ public class MainConfig {
public static double bonusPerLevel;
public static HashMap<Material, String> vanilla2Crops;
public static boolean enableEvents;
public static int saveInterval;
public static void load() {
ConfigUtil.update("config.yml");
@@ -275,6 +276,8 @@ public class MainConfig {
vanilla2Crops = null;
}
saveInterval = config.getInt("other-settings.data-save-interval", 3);
antiGriefs = new ArrayList<>();
if (config.getBoolean("integration.Residence",false)){
if (Bukkit.getPluginManager().getPlugin("Residence") == null) Log.warn("Failed to initialize Residence!");

View File

@@ -216,9 +216,6 @@ public class ItemsAdderFrameHandler extends ItemsAdderHandler {
if (!canProceedAction(player, location)) return;
//fix buggy chorus duplication
chorusFix(event.getBlock());
if (namespacedId.equals(BasicItemConfig.dryPot)
|| namespacedId.equals(BasicItemConfig.wetPot)) {

View File

@@ -233,9 +233,4 @@ public abstract class ItemsAdderHandler extends HandlerP {
public void onBreakFurniture(FurnitureBreakEvent event) {
//null
}
public void chorusFix(Block block) {
if (block.getType() != Material.CHORUS_PLANT) return;
CustomBlock.remove(block.getLocation());
}
}

View File

@@ -178,7 +178,6 @@ public class ItemsAdderWireHandler extends ItemsAdderHandler {
}
}
public void onInteractBlock(CustomBlockInteractEvent event) {
// A broken API Event
@@ -277,9 +276,6 @@ public class ItemsAdderWireHandler extends ItemsAdderHandler {
String namespacedId = event.getNamespacedID();
Location location = event.getBlock().getLocation();
//fix buggy chorus duplication
super.chorusFix(event.getBlock());
//break crop
if (namespacedId.contains("_stage_")) {

View File

@@ -384,7 +384,7 @@ public class CropManager extends Function {
for (OtherLoot otherLoot : otherLoots) {
if (Math.random() < otherLoot.getChance()) {
int random = ThreadLocalRandom.current().nextInt(otherLoot.getMin(), otherLoot.getMax() + 1);
if (MainConfig.enableSkillBonus) {
if (MainConfig.enableSkillBonus && MainConfig.skillXP != null) {
double bonus_chance = MainConfig.skillXP.getLevel(player) * MainConfig.bonusPerLevel;
random *= (bonus_chance + 1);
}
@@ -463,6 +463,12 @@ public class CropManager extends Function {
return false;
}
public void saveData(World world) {
CustomWorld customWorld = getCustomWorld(world);
if (customWorld == null) return;
customWorld.tryToSaveData();
}
public ArmorStandUtil getArmorStandUtil() {
return armorStandUtil;
}

View File

@@ -51,6 +51,7 @@ public class CustomWorld {
private final ConcurrentHashMap<SimpleLocation, Sprinkler> sprinklerCache;
private final ConcurrentHashMap<SimpleLocation, Fertilizer> fertilizerCache;
private final ConcurrentHashMap<String, HashSet<SimpleLocation>> scarecrowCache;
private final ConcurrentHashMap<SimpleLocation, String> cropData;
private final Set<SimpleLocation> watered;
private HashSet<SimpleLocation> tempWatered;
private final HashSet<SimpleLocation> playerWatered;
@@ -58,19 +59,21 @@ public class CustomWorld {
private final BukkitScheduler bukkitScheduler;
private final HashSet<SimpleLocation> plantedToday;
private final CropModeInterface cropMode;
private YamlConfiguration cropData;
private int timer;
public CustomWorld(World world, CropManager cropManager) {
this.world = world;
this.fertilizerCache = new ConcurrentHashMap<>(2048);
this.sprinklerCache = new ConcurrentHashMap<>(1024);
this.scarecrowCache = new ConcurrentHashMap<>(256);
this.sprinklerCache = new ConcurrentHashMap<>(512);
this.scarecrowCache = new ConcurrentHashMap<>(128);
this.cropData = new ConcurrentHashMap<>(2048);
this.cropManager = cropManager;
this.bukkitScheduler = Bukkit.getScheduler();
this.watered = Collections.synchronizedSet(new HashSet<>());
this.playerWatered = new HashSet<>();
this.tempWatered = new HashSet<>();
this.plantedToday = new HashSet<>();
this.timer = 0;
this.cropMode = cropManager.getCropMode();
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.plugin, () -> {
loadData();
@@ -84,12 +87,12 @@ public class CustomWorld {
public void unload(boolean sync) {
if (sync) {
unloadData();
backUp(world.getName());
backUp();
}
else {
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.plugin, () -> {
unloadData();
backUp(world.getName());
backUp();
Bukkit.getScheduler().runTask(CustomCrops.plugin, () -> {
CustomWorldEvent customWorldEvent = new CustomWorldEvent(world, WorldState.UNLOAD);
Bukkit.getPluginManager().callEvent(customWorldEvent);
@@ -98,7 +101,7 @@ public class CustomWorld {
}
}
private void loadData() {
public void loadData() {
loadSeason();
loadCropCache();
loadSprinklerCache();
@@ -107,7 +110,7 @@ public class CustomWorld {
loadScarecrow();
}
private void unloadData() {
public void unloadData() {
unloadSeason();
unloadCrop();
unloadSprinkler();
@@ -116,16 +119,24 @@ public class CustomWorld {
unloadScarecrow();
}
public void backUp(String worldName) {
public void tryToSaveData() {
timer++;
if (timer >= MainConfig.saveInterval) {
timer = 0;
unloadData();
}
}
public void backUp() {
if (!MainConfig.autoBackUp) return;
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
try {
File file = new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), MainConfig.worldFolder + worldName + File.separator + "customcrops_data");
File file = new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), MainConfig.worldFolder + world.getName() + File.separator + "customcrops_data");
File[] files = file.listFiles();
if (files == null) return;
for (File data : files) {
FileUtils.copyFileToDirectory(data, new File(CustomCrops.plugin.getDataFolder(), "backup" + File.separator + worldName + "_" + format.format(date)));
FileUtils.copyFileToDirectory(data, new File(CustomCrops.plugin.getDataFolder(), "backup" + File.separator + world.getName() + "_" + format.format(date)));
}
}
catch (IOException e){
@@ -316,12 +327,22 @@ public class CustomWorld {
}
public void loadCropCache() {
cropData = loadData("crops", world.getName());
YamlConfiguration data = loadData("crops", world.getName());
String worldName = world.getName();
for (Map.Entry<String, Object> entry : data.getValues(false).entrySet()) {
cropData.put(MiscUtils.getSimpleLocation(entry.getKey(), worldName), (String) entry.getValue());
}
}
public void unloadCrop() {
YamlConfiguration data = new YamlConfiguration();
for (Map.Entry<SimpleLocation, String> en : cropData.entrySet()) {
SimpleLocation location = en.getKey();
String loc = location.getX() + "," + location.getY() + "," + location.getZ();
data.set(loc, en.getValue());
}
try {
cropData.save(new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), MainConfig.worldFolder + world.getName() + File.separator + "customcrops_data" + File.separator + "crops.yml"));
data.save(new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), MainConfig.worldFolder + world.getName() + File.separator + "customcrops_data" + File.separator + "crops.yml"));
}
catch (IOException e) {
e.printStackTrace();
@@ -334,75 +355,61 @@ public class CustomWorld {
Random randomGenerator = new Random();
if (force) {
cropData.getKeys(false).forEach(key -> {
Location location = MiscUtils.getLocation(key, world);
growSingleWire(location, randomGenerator.nextInt(cropTime), key);
});
cropData.keySet().forEach(key -> growSingleWire(key, randomGenerator.nextInt(cropTime)));
}
else 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);
});
cropData.keySet().forEach(key -> growSingleWire(key, sprinklerTime + dryTime + randomGenerator.nextInt(cropTime)));
}
else {
int delay = (int)(24000 - world.getTime());
double chance = (double) (24000 - world.getTime()) / 24000;
cropData.getKeys(false).forEach(key -> {
cropData.keySet().forEach(key -> {
if (Math.random() < chance) {
Location location = MiscUtils.getLocation(key, world);
growSingleWire(location, randomGenerator.nextInt(delay), key);
growSingleWire(key, randomGenerator.nextInt(delay));
}
});
}
}
private void growSingleWire(Location location, long delay, String key) {
private void growSingleWire(SimpleLocation simpleLocation, long delay) {
bukkitScheduler.runTaskLaterAsynchronously(CustomCrops.plugin, () -> {
Location location = MiscUtils.getLocation(simpleLocation);
if (cropMode.growJudge(location)) {
cropData.set(key, null);
cropData.remove(simpleLocation);
}
}, delay);
}
public void growFrame(int cropTime, int sprinklerTime, int dryTime, boolean compensation, boolean force) {
Random randomGenerator = new Random();
if (force) {
cropData.getKeys(false).forEach(key -> {
Location location = MiscUtils.getLocation(key, world);
growSingleFrame(location, randomGenerator.nextInt(cropTime), key);
});
cropData.keySet().forEach(key -> growSingleFrame(key, randomGenerator.nextInt(cropTime)));
}
else 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);
});
cropData.keySet().forEach(key -> growSingleFrame(key, sprinklerTime + dryTime + randomGenerator.nextInt(cropTime)));
}
else {
int delay = (int)(24000 - world.getTime());
int delay = (int) (24000 - world.getTime());
double chance = (double) (24000 - world.getTime()) / 24000;
cropData.getKeys(false).forEach(key -> {
cropData.keySet().forEach(key -> {
if (Math.random() < chance) {
Location location = MiscUtils.getLocation(key, world);
growSingleFrame(location, randomGenerator.nextInt(delay), key);
growSingleFrame(key, randomGenerator.nextInt(delay));
}
});
}
}
private void growSingleFrame(Location location, long delay, String key) {
bukkitScheduler.runTaskLater(CustomCrops.plugin, () -> {
cropMode.loadChunk(location);
}, delay);
private void growSingleFrame(SimpleLocation simpleLocation, long delay) {
Location location = MiscUtils.getLocation(simpleLocation);
if (location == null) return;
bukkitScheduler.runTaskLater(CustomCrops.plugin, () -> cropMode.loadChunk(location), delay);
bukkitScheduler.runTaskLater(CustomCrops.plugin, () -> {
if (cropMode.growJudge(location)) {
cropData.set(key, null);
cropData.remove(simpleLocation);
}
}, delay + 5);
}
@@ -502,7 +509,7 @@ public class CustomWorld {
}
public void removeCrop(Location cropLoc) {
cropData.set(MiscUtils.getStringLocation(cropLoc), null);
cropData.remove(MiscUtils.getSimpleLocation(cropLoc));
}
@Nullable
@@ -512,18 +519,17 @@ public class CustomWorld {
public void addCrop(Location cropLoc, String crop) {
SimpleLocation simpleLocation = MiscUtils.getSimpleLocation(cropLoc);
String key = MiscUtils.getStringLocation(cropLoc);
cropData.set(key, crop);
cropData.put(simpleLocation, 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(cropLoc, new Random().nextInt(delay), key);
growSingleWire(simpleLocation, new Random().nextInt(delay));
}
else {
growSingleFrame(cropLoc, new Random().nextInt(delay), key);
growSingleFrame(simpleLocation, new Random().nextInt(delay));
}
}
}

View File

@@ -38,6 +38,9 @@ public class TimerTask extends BukkitRunnable {
if (time > 950 && time < 1051) {
cropManager.grow(world, MainConfig.timeToGrow, MainConfig.timeToWork, MainConfig.timeToDry, false, false);
}
if (time > 0 && time < 101) {
cropManager.saveData(world);
}
}
}
}