9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-22 16:39:36 +00:00

1.3.0 beta3

This commit is contained in:
Xiao-MoMi
2022-05-27 04:19:21 +08:00
parent 68f5e83f51
commit a82840c36f
21 changed files with 617 additions and 558 deletions

View File

@@ -12,7 +12,6 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Objects;
public class CommandHandler implements CommandExecutor { public class CommandHandler implements CommandExecutor {
@@ -25,83 +24,103 @@ public class CommandHandler implements CommandExecutor {
return false; return false;
} }
FileConfiguration config = CustomCrops.instance.getConfig();
//重载插件 //重载插件
if(args[0].equalsIgnoreCase("reload")){ if(args[0].equalsIgnoreCase("reload")){
CustomCrops.loadConfig();
ConfigManager.Config.ReloadConfig();
if(sender instanceof Player){ if(sender instanceof Player){
MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.reload"), (Player) sender); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.reload, (Player) sender);
}else { }else {
MessageManager.consoleMessage(config.getString("messages.prefix") + config.getString("messages.reload"), Bukkit.getConsoleSender()); MessageManager.consoleMessage(ConfigManager.Config.prefix + ConfigManager.Config.reload, Bukkit.getConsoleSender());
} }
return true;
} }
//设置季节 //设置季节
if(args[0].equalsIgnoreCase("setseason")){ if(args[0].equalsIgnoreCase("setseason")){
if(config.getBoolean("enable-season")){ if(ConfigManager.Config.season){
FileConfiguration config = CustomCrops.instance.getConfig();
config.set("current-season", args[1]); config.set("current-season", args[1]);
if(sender instanceof Player){
MessageManager.playerMessage(config.getString("messages.prefix") + Objects.requireNonNull(config.getString("messages.season-set")).replace("{Season}",args[1])
.replace("spring", Objects.requireNonNull(config.getString("messages.spring")))
.replace("summer", Objects.requireNonNull(config.getString("messages.summer")))
.replace("autumn", Objects.requireNonNull(config.getString("messages.autumn")))
.replace("winter", Objects.requireNonNull(config.getString("messages.winter"))), (Player) sender);
}else {
MessageManager.consoleMessage(config.getString("messages.prefix") + Objects.requireNonNull(config.getString("messages.season-set")).replace("{Season}",args[1])
.replace("spring", Objects.requireNonNull(config.getString("messages.spring")))
.replace("summer", Objects.requireNonNull(config.getString("messages.summer")))
.replace("autumn", Objects.requireNonNull(config.getString("messages.autumn")))
.replace("winter", Objects.requireNonNull(config.getString("messages.winter"))), Bukkit.getConsoleSender());
}
CustomCrops.instance.saveConfig(); CustomCrops.instance.saveConfig();
ConfigManager.Config.current = args[1];
if(sender instanceof Player){
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.season_set.replace("{Season}",args[1])
.replace("spring", ConfigManager.Config.spring)
.replace("summer", ConfigManager.Config.summer)
.replace("autumn", ConfigManager.Config.autumn)
.replace("winter", ConfigManager.Config.winter), (Player) sender);
}else {
MessageManager.consoleMessage(config.getString("messages.prefix") + ConfigManager.Config.season_set.replace("{Season}",args[1])
.replace("spring", ConfigManager.Config.spring)
.replace("summer", ConfigManager.Config.summer)
.replace("autumn", ConfigManager.Config.autumn)
.replace("winter", ConfigManager.Config.winter), Bukkit.getConsoleSender());
}
}else{ }else{
if(sender instanceof Player){ if(sender instanceof Player){
MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.season-disabled"), (Player) sender); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.season_disabled, (Player) sender);
}else { }else {
MessageManager.consoleMessage(config.getString("messages.prefix") + config.getString("messages.season-disabled"), Bukkit.getConsoleSender()); MessageManager.consoleMessage(ConfigManager.Config.prefix + ConfigManager.Config.season_disabled, Bukkit.getConsoleSender());
} }
} }
return true;
} }
//强制保存 //强制保存
if(args[0].equalsIgnoreCase("forcesave")){ if(args[0].equalsIgnoreCase("forcesave")){
CropManager.saveData(); CropManager.saveData();
SprinklerManager.saveData(); SprinklerManager.saveData();
if(sender instanceof Player){ if(sender instanceof Player){
MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.force-save"), (Player) sender); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.force_save, (Player) sender);
}else { }else {
MessageManager.consoleMessage(config.getString("messages.prefix") + config.getString("messages.force-save"), Bukkit.getConsoleSender()); MessageManager.consoleMessage(ConfigManager.Config.prefix + ConfigManager.Config.force_save, Bukkit.getConsoleSender());
} }
return true;
} }
//清除缓存 //强制生长
if(args[0].equalsIgnoreCase("cleancache")){ if(args[0].equalsIgnoreCase("forcegrow")){
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.instance,()->{ Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.instance, CropManager::CropGrow);
CropManager.cleanLoadedCache();
SprinklerManager.cleanCache();
});
if(sender instanceof Player){ if(sender instanceof Player){
MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.clean-cache"), (Player) sender); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.force_grow, (Player) sender);
}else { }else {
MessageManager.consoleMessage(config.getString("messages.prefix") + config.getString("messages.clean-cache"), Bukkit.getConsoleSender()); MessageManager.consoleMessage(ConfigManager.Config.prefix + ConfigManager.Config.force_grow, Bukkit.getConsoleSender());
} }
return true;
}
//强制洒水
if(args[0].equalsIgnoreCase("forcewater")){
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.instance, SprinklerManager::SprinklerWork);
if(sender instanceof Player){
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.force_water, (Player) sender);
}else {
MessageManager.consoleMessage(ConfigManager.Config.prefix + ConfigManager.Config.force_water, Bukkit.getConsoleSender());
}
return true;
} }
if(args[0].equalsIgnoreCase("backup")){ if(args[0].equalsIgnoreCase("backup")){
BackUp.backUpData(); BackUp.backUpData();
if(sender instanceof Player){ if(sender instanceof Player){
MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.backup"), (Player) sender); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.backup, (Player) sender);
}else { }else {
MessageManager.consoleMessage(config.getString("messages.prefix") + config.getString("messages.backup"), Bukkit.getConsoleSender()); MessageManager.consoleMessage(ConfigManager.Config.prefix + ConfigManager.Config.backup, Bukkit.getConsoleSender());
} }
return true;
} }
if(args[0].equalsIgnoreCase("nextseason")){ if(args[0].equalsIgnoreCase("nextseason")){
NextSeason.changeSeason(); NextSeason.changeSeason();
if(sender instanceof Player){ if(sender instanceof Player){
MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.nextseason"), (Player) sender); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.nextSeason, (Player) sender);
}else { }else {
MessageManager.consoleMessage(config.getString("messages.prefix") + config.getString("messages.nextseason"), Bukkit.getConsoleSender()); MessageManager.consoleMessage(ConfigManager.Config.prefix + ConfigManager.Config.nextSeason, Bukkit.getConsoleSender());
} }
return true;
} }
if(args[0].equalsIgnoreCase("test")){ if(args[0].equalsIgnoreCase("test")){
CropManager.testData_2(); CropManager.testData_2();
SprinklerManager.testData_3();
return true;
} }
return false; return false;
} }

View File

@@ -14,7 +14,7 @@ public class CommandTabComplete implements TabCompleter {
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public @Nullable List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { public @Nullable List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (args.length == 1) { if (args.length == 1) {
return Arrays.asList("backup" , "cleancache", "forcesave", "reload", "setseason" , "nextseason"); return Arrays.asList("backup" , "forcegrow", "forcesave", "forcewater", "reload", "setseason" , "nextseason");
} }
if(args[0].equalsIgnoreCase("setseason")){ if(args[0].equalsIgnoreCase("setseason")){
return Arrays.asList("spring","summer","autumn","winter"); return Arrays.asList("spring","summer","autumn","winter");

View File

@@ -0,0 +1,134 @@
package net.momirealms.customcrops;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.List;
public class ConfigManager {
public static class Config{
public static boolean res;
public static boolean wg;
public static boolean king;
public static boolean season;
public static boolean need_water;
public static boolean greenhouse;
public static boolean big;
public static boolean limit;
public static List<String> worlds;
public static List<Long> cropGrowTimeList;
public static List<Long> sprinklerWorkTimeList;
public static String current;
public static String prefix;
public static String bad_place;
public static String reload;
public static String force_save;
public static String nextSeason;
public static String no_such_seed;
public static String wrong_season;
public static String season_set;
public static String season_disabled;
public static String force_grow;
public static String force_water;
public static String limit_crop;
public static String limit_sprinkler;
public static String backup;
public static String spring;
public static String summer;
public static String autumn;
public static String winter;
public static String can_full;
public static String pot;
public static String watered_pot;
public static String watering_can_1;
public static String watering_can_2;
public static String watering_can_3;
public static String glass;
public static String sprinkler_1;
public static String sprinkler_2;
public static String sprinkler_1i;
public static String sprinkler_2i;
public static String dead;
public static String success;
public static String failure;
public static double bone_chance;
public static double grow_chance;
public static double big_chance;
public static int range;
public static int maxh;
public static int minh;
public static int max_crop;
public static int max_sprinkler;
public static void ReloadConfig(){
CustomCrops.instance.reloadConfig();
FileConfiguration configuration = CustomCrops.instance.getConfig();
//处理配置
Config.res = configuration.getBoolean("integration.residence");
Config.king = configuration.getBoolean("integration.kingdomsX");
Config.wg = configuration.getBoolean("integration.worldguard");
Config.season = configuration.getBoolean("enable-season");
Config.need_water = configuration.getBoolean("config.bone-meal-consume-water");
Config.greenhouse = configuration.getBoolean("config.enable-greenhouse");
Config.big = configuration.getBoolean("config.gigantic.enable");
Config.limit = configuration.getBoolean("config.enable-limit");
Config.bone_chance = configuration.getDouble("config.bone-meal-chance");
Config.grow_chance = configuration.getDouble("config.grow-success-chance");
Config.big_chance = configuration.getDouble("config.gigantic.chance");
Config.range = configuration.getInt("config.greenhouse-range");
Config.maxh = configuration.getInt("config.height.max");
Config.minh = configuration.getInt("config.height.min");
Config.max_crop = configuration.getInt("config.max-crops");
Config.max_sprinkler = configuration.getInt("config.max-sprinklers");
Config.current = configuration.getString("current-season");
Config.pot = configuration.getString("config.pot");
Config.watered_pot = configuration.getString("config.watered-pot");
Config.watering_can_1 = configuration.getString("config.watering-can-1");
Config.watering_can_2 = configuration.getString("config.watering-can-2");
Config.watering_can_3 = configuration.getString("config.watering-can-3");
Config.glass = configuration.getString("config.greenhouse-glass");
Config.sprinkler_1 = configuration.getString("config.sprinkler-1");
Config.sprinkler_2 = configuration.getString("config.sprinkler-2");
Config.sprinkler_1i = configuration.getString("config.sprinkler-1-item");
Config.sprinkler_2i = configuration.getString("config.sprinkler-2-item");
Config.dead = configuration.getString("config.dead-crop");
Config.success = configuration.getString("config.particle.success");
Config.failure = configuration.getString("config.particle.failure");
Config.worlds = configuration.getStringList("config.whitelist-worlds");
Config.cropGrowTimeList = configuration.getLongList("config.grow-time");
Config.sprinklerWorkTimeList = configuration.getLongList("config.sprinkler-time");
//处理消息
Config.prefix = configuration.getString("messages.prefix");
Config.bad_place = configuration.getString("messages.not-a-good-place");
Config.reload = configuration.getString("messages.reload");
Config.force_save = configuration.getString("messages.force-save");
Config.nextSeason = configuration.getString("messages.nextseason");
Config.no_such_seed = configuration.getString("messages.no-such-seed");
Config.wrong_season = configuration.getString("messages.wrong-season");
Config.season_set = configuration.getString("messages.season-set");
Config.season_disabled = configuration.getString("messages.season-disabled");
Config.force_grow = configuration.getString("messages.force-grow");
Config.force_water = configuration.getString("messages.force-water");
Config.limit_crop = configuration.getString("messages.reach-limit-crop");
Config.limit_sprinkler = configuration.getString("messages.reach-limit-sprinkler");
Config.can_full = configuration.getString("messages.can-full");
Config.backup = configuration.getString("messages.backup");
Config.spring = configuration.getString("messages.spring");
Config.summer = configuration.getString("messages.summer");
Config.autumn = configuration.getString("messages.autumn");
Config.winter = configuration.getString("messages.winter");
}
}
}

View File

@@ -1,135 +0,0 @@
package net.momirealms.customcrops.Crops;
import dev.lone.itemsadder.api.CustomBlock;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.DataManager.CropManager;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitScheduler;
import java.io.File;
import java.util.Objects;
public class CropGrow {
public static void cropGrow(){
FileConfiguration config = CustomCrops.instance.getConfig();
File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml");
FileConfiguration data;
data = YamlConfiguration.loadConfiguration(file);
boolean enable_season = config.getBoolean("enable-season");
boolean enable_greenhouse = config.getBoolean("config.enable-greenhouse");
int range = config.getInt("config.greenhouse-range");
double growChance = config.getDouble("config.grow-success-chance");
String glass = config.getString("config.greenhouse-glass");
String wateredPot = config.getString("config.watered-pot");
String pot = config.getString("config.pot");
String deadCrop = config.getString("config.dead-crop");
String current = config.getString("current-season");
BukkitScheduler bukkitScheduler = Bukkit.getScheduler();
config.getStringList("config.whitelist-worlds").forEach(worldName -> {
World world = Bukkit.getWorld(worldName);
CropManager.getCrops(Bukkit.getWorld(worldName)).forEach(seedLocation -> {
Location potLocation = seedLocation.clone().subtract(0,1,0);
Block seedBlock = world.getBlockAt(seedLocation);
Block potBlock = world.getBlockAt(potLocation);
String[] seasons = StringUtils.split(data.getString(worldName + "." + seedLocation.getBlockX() + "," + seedLocation.getBlockY() + "," + seedLocation.getBlockZ()),",");
if (CustomBlock.byAlreadyPlaced(potBlock) != null && CustomBlock.byAlreadyPlaced(seedBlock) != null){
String seedNamespace = CustomBlock.byAlreadyPlaced(seedBlock).getNamespacedID();
if (CustomBlock.byAlreadyPlaced(potBlock).getNamespacedID().equalsIgnoreCase(wateredPot) && seedNamespace.contains("stage")){
if (seedNamespace.equalsIgnoreCase(deadCrop)){
return;
}
String[] split = StringUtils.split(seedNamespace,":");
String[] cropNameList = StringUtils.split(split[1],"_");
Label_out:
if(enable_season){
if(enable_greenhouse){
for(int i = 1; i <= range; i++){
Location tempLocation = seedLocation.clone().add(0,i,0);
if (CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)) != null){
if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)).getNamespacedID().equalsIgnoreCase(glass)){
break Label_out;
}
}
}
}
boolean wrongSeason = true;
for(String season : seasons){
if (Objects.equals(season, current)) {
wrongSeason = false;
break;
}
}
if(wrongSeason){
bukkitScheduler.callSyncMethod(CustomCrops.instance, () -> {
CustomBlock.remove(seedLocation);
CustomBlock.place(deadCrop,seedLocation);
return null;
});
return;
}
}
int nextStage = Integer.parseInt(cropNameList[2]) + 1;
if (CustomBlock.getInstance( split[0] +":"+cropNameList[0] + "_stage_" + nextStage) != null) {
bukkitScheduler.callSyncMethod(CustomCrops.instance, () ->{
CustomBlock.remove(potLocation);
CustomBlock.place(pot,potLocation);
if(Math.random()< growChance){
CustomBlock.remove(seedLocation);
CustomBlock.place(split[0] + ":" + cropNameList[0] + "_stage_" + nextStage,seedLocation);
}
return null;
});
}
}else if(CustomBlock.byAlreadyPlaced(potBlock).getNamespacedID().equalsIgnoreCase(pot) && seedNamespace.contains("stage")){
if (seedNamespace.equalsIgnoreCase(deadCrop)){
return;
}
if(enable_season) {
if(enable_greenhouse){
for(int i = 1; i <= range; i++){
Location tempLocation = seedLocation.clone().add(0,i,0);
if (CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)) != null){
if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)).getNamespacedID().equalsIgnoreCase(glass)){
return;
}
}
}
}
boolean wrongSeason = true;
for (String season : seasons) {
if (Objects.equals(season, current)) {
wrongSeason = false;
break;
}
}
if (wrongSeason) {
bukkitScheduler.callSyncMethod(CustomCrops.instance, () -> {
CustomBlock.remove(seedLocation);
CustomBlock.place(deadCrop, seedLocation);
return null;
});
}
}
}
}
});
});
}
}

View File

@@ -1,63 +0,0 @@
package net.momirealms.customcrops.Crops;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import dev.lone.itemsadder.api.CustomBlock;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.DataManager.SprinklerManager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.Objects;
public class SprinklerWork {
public static void sprinklerWork(){
File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml");
FileConfiguration data;
data = YamlConfiguration.loadConfiguration(file);
FileConfiguration config = CustomCrops.instance.getConfig();
config.getStringList("config.whitelist-worlds").forEach(worldName -> {
World world = Bukkit.getWorld(worldName);
SprinklerManager.getSprinklers(Bukkit.getWorld(worldName)).forEach(location -> {
String type = Objects.requireNonNull(data.getString(worldName + "." + location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ()));
if(type.equals("s1")){
for(int i = -1; i <= 1;i++){
for (int j = -1; j <= 1; j++){
Location tempLoc = location.clone().add(i,-1,j);
waterPot(tempLoc, world, config);
}
}
}else if(type.equals("s2")){
for(int i = -2; i <= 2;i++){
for (int j = -2; j <= 2; j++){
Location tempLoc = location.clone().add(i,-1,j);
waterPot(tempLoc, world, config);
}
}
}
});
});
}
private static void waterPot(Location tempLoc, World world, FileConfiguration config) {
if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLoc)) != null){
if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLoc)).getNamespacedID().equalsIgnoreCase(config.getString("config.pot"))){
PacketContainer fakeWater = new PacketContainer(PacketType.Play.Server.ENTITY_HEAD_ROTATION);
Bukkit.getScheduler().callSyncMethod(CustomCrops.instance,()->{
CustomBlock.remove(tempLoc);
CustomBlock.place(config.getString("config.watered-pot"), tempLoc);
return null;
});
}
}
}
}

View File

@@ -1,66 +1,32 @@
package net.momirealms.customcrops.Crops; package net.momirealms.customcrops.Crops;
import net.momirealms.customcrops.ConfigManager;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.DataManager.CropManager; import net.momirealms.customcrops.DataManager.CropManager;
import net.momirealms.customcrops.DataManager.SprinklerManager; import net.momirealms.customcrops.DataManager.SprinklerManager;
import net.momirealms.customcrops.MessageManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class TimeCheck extends BukkitRunnable { public class TimeCheck extends BukkitRunnable {
FileConfiguration config = CustomCrops.instance.getConfig();
List<Long> cropGrowTimeList = config.getLongList("config.grow-time");
List<Long> sprinklerWorkTimeList = config.getLongList("config.sprinkler-time");
BukkitScheduler bukkitScheduler = Bukkit.getScheduler(); BukkitScheduler bukkitScheduler = Bukkit.getScheduler();
@Override @Override
public void run() { public void run() {
long time = Objects.requireNonNull(Bukkit.getWorld("world")).getTime(); long time = Objects.requireNonNull(Bukkit.getWorld("world")).getTime();
cropGrowTimeList.forEach(cropGrowTime -> { ConfigManager.Config.cropGrowTimeList.forEach(cropGrowTime -> {
if(time == cropGrowTime){ if(time == cropGrowTime){
bukkitScheduler.runTaskAsynchronously(CustomCrops.instance,()->{ bukkitScheduler.runTaskAsynchronously(CustomCrops.instance, CropManager::CropGrow);
long start1 = System.currentTimeMillis();
CropManager.cleanLoadedCache();
long finish1 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f农作物缓存清理耗时&a" + String.valueOf(finish1-start1) + "&fms异步",Bukkit.getConsoleSender());
long start2 = System.currentTimeMillis();
CropManager.saveData();
long finish2 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f农作物数据保存耗时&a" + String.valueOf(finish2-start2) + "&fms异步",Bukkit.getConsoleSender());
long start3 = System.currentTimeMillis();
CropGrow.cropGrow();
long finish3 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f农作物生长耗时&a" + String.valueOf(finish3-start3) + "&fms部分异步",Bukkit.getConsoleSender());
});
} }
}); });
ConfigManager.Config.sprinklerWorkTimeList.forEach(sprinklerTime -> {
sprinklerWorkTimeList.forEach(sprinklerTime -> {
if(time == sprinklerTime){ if(time == sprinklerTime){
bukkitScheduler.runTaskAsynchronously(CustomCrops.instance,()->{ bukkitScheduler.runTaskAsynchronously(CustomCrops.instance, SprinklerManager::SprinklerWork);
long start1 = System.currentTimeMillis();
SprinklerManager.cleanCache();
long finish1 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f洒水器缓存清理耗时&a" + String.valueOf(finish1-start1) + "&fms部分异步",Bukkit.getConsoleSender());
long start2 = System.currentTimeMillis();
SprinklerManager.saveData();
long finish2 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f洒水器数据保存耗时&a" + String.valueOf(finish2-start2) + "&fms异步",Bukkit.getConsoleSender());
long start3 = System.currentTimeMillis();
SprinklerWork.sprinklerWork();
long finish3 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f洒水器工作耗时&a" + String.valueOf(finish3-start3) + "&fms部分异步",Bukkit.getConsoleSender());
});
} }
}); });
} }
} }

View File

@@ -1,12 +1,6 @@
package net.momirealms.customcrops; package net.momirealms.customcrops;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import net.momirealms.customcrops.Crops.CropTimer; import net.momirealms.customcrops.Crops.CropTimer;
import net.momirealms.customcrops.DataManager.BackUp; import net.momirealms.customcrops.DataManager.BackUp;
import net.momirealms.customcrops.DataManager.CropManager; import net.momirealms.customcrops.DataManager.CropManager;
@@ -22,7 +16,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public final class CustomCrops extends JavaPlugin { public final class CustomCrops extends JavaPlugin {
@@ -38,6 +31,9 @@ public final class CustomCrops extends JavaPlugin {
instance = this; instance = this;
saveDefaultConfig(); saveDefaultConfig();
//加载配置文件
ConfigManager.Config.ReloadConfig();
//指令注册 //指令注册
Objects.requireNonNull(Bukkit.getPluginCommand("customcrops")).setExecutor(new CommandHandler()); Objects.requireNonNull(Bukkit.getPluginCommand("customcrops")).setExecutor(new CommandHandler());
Objects.requireNonNull(Bukkit.getPluginCommand("customcrops")).setTabCompleter(new CommandTabComplete()); Objects.requireNonNull(Bukkit.getPluginCommand("customcrops")).setTabCompleter(new CommandTabComplete());
@@ -46,24 +42,8 @@ public final class CustomCrops extends JavaPlugin {
Bukkit.getPluginManager().registerEvents(new RightClickBlock(),this); Bukkit.getPluginManager().registerEvents(new RightClickBlock(),this);
Bukkit.getPluginManager().registerEvents(new BreakFurniture(),this); Bukkit.getPluginManager().registerEvents(new BreakFurniture(),this);
//修改末影箱标题
manager = ProtocolLibrary.getProtocolManager();
manager.addPacketListener(new PacketAdapter(this, PacketType.Play.Server.OPEN_WINDOW) {
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
List<WrappedChatComponent> components = packet.getChatComponents().getValues();
for (WrappedChatComponent component : components) {
if(component.toString().contains("Ender Chest")){
//component.setJson("{\"text\":\"收纳袋\"}");
component.setJson("{\"translate\":\"container.enderchest\"}");
packet.getChatComponents().write(components.indexOf(component), component);
}
}
}
});
//开始计时任务 //开始计时任务
startTimer(); CustomCrops.timer = new CropTimer();
//新建data文件 //新建data文件
File crop_file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml"); File crop_file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml");
@@ -73,6 +53,7 @@ public final class CustomCrops extends JavaPlugin {
crop_file.createNewFile(); crop_file.createNewFile();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
MessageManager.consoleMessage("&c[CustomCrops] 农作物数据文件生成失败!",Bukkit.getConsoleSender());
} }
} }
if(!sprinkler_file.exists()){ if(!sprinkler_file.exists()){
@@ -80,6 +61,7 @@ public final class CustomCrops extends JavaPlugin {
sprinkler_file.createNewFile(); sprinkler_file.createNewFile();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
MessageManager.consoleMessage("&c[CustomCrops] 洒水器数据文件生成失败!",Bukkit.getConsoleSender());
} }
} }
@@ -103,7 +85,7 @@ public final class CustomCrops extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
//关闭异步定时任务 //关闭定时任务
if (CustomCrops.timer != null) { if (CustomCrops.timer != null) {
CropTimer.stopTimer(CustomCrops.timer.getTaskID()); CropTimer.stopTimer(CustomCrops.timer.getTaskID());
} }
@@ -112,20 +94,8 @@ public final class CustomCrops extends JavaPlugin {
CropManager.saveData(); CropManager.saveData();
SprinklerManager.saveData(); SprinklerManager.saveData();
//备份 //备份
BackUp.backUpData(); BackUp.backUpData();
MessageManager.consoleMessage(("&#ccfbff-#ef96c5&[CustomCrops] 自定义农作物插件已卸载!作者:小默米 QQ:3266959688"),Bukkit.getConsoleSender()); MessageManager.consoleMessage(("&#ccfbff-#ef96c5&[CustomCrops] 自定义农作物插件已卸载!作者:小默米 QQ:3266959688"),Bukkit.getConsoleSender());
} }
//定时任务
public static void startTimer() {
CustomCrops.timer = new CropTimer();
}
//重载插件
public static void loadConfig(){
instance.reloadConfig();
}
} }

View File

@@ -1,13 +1,18 @@
package net.momirealms.customcrops.DataManager; package net.momirealms.customcrops.DataManager;
import dev.lone.itemsadder.api.CustomBlock;
import net.momirealms.customcrops.ConfigManager;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.MessageManager;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitScheduler;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -60,6 +65,7 @@ public class CropManager {
} }
saveData(); saveData();
} }
/*
//根据世界名获取所有的农作物 //根据世界名获取所有的农作物
public static List<Location> getCrops(World world){ public static List<Location> getCrops(World world){
FileConfiguration config = CustomCrops.instance.getConfig(); FileConfiguration config = CustomCrops.instance.getConfig();
@@ -83,6 +89,7 @@ public class CropManager {
} }
return locations; return locations;
} }
*/
//保存数据 //保存数据
public static void saveData(){ public static void saveData(){
File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml"); File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml");
@@ -110,17 +117,186 @@ public class CropManager {
public static void putInstance(Location location, String season) { public static void putInstance(Location location, String season) {
CropManager.instances.put(location, season); CropManager.instances.put(location, season);
} }
public ConcurrentHashMap<Location, String> getMap() { //清理无效的农作物并生长
return CropManager.instances; public static void CropGrow() {
} /*
阶段1更新数据
//清理无效的农作物 */
public static void cleanLoadedCache() { long start1 = System.currentTimeMillis();
FileConfiguration config = CustomCrops.instance.getConfig(); FileConfiguration config = CustomCrops.instance.getConfig();
File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml"); File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml");
FileConfiguration data; FileConfiguration data;
data = YamlConfiguration.loadConfiguration(file); data = YamlConfiguration.loadConfiguration(file);
config.getStringList("config.whitelist-worlds").forEach(worldName ->{ if (CropManager.instances != null) {
//性能更高
Set<Map.Entry<Location, String>> en = instances.entrySet();
for(Map.Entry<Location, String> entry : en){
Location key = entry.getKey();
data.set(key.getWorld().getName() + "." + key.getBlockX() + "," + key.getBlockY()+ ","+ key.getBlockZ(), entry.getValue());
}
}
long finish1 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f农作物数据更新耗时&a" + String.valueOf(finish1-start1) + "&fms",Bukkit.getConsoleSender());
/*
阶段2清理数据内无效的农作物
*/
long start2 = System.currentTimeMillis();
List<Location> locations = new ArrayList<Location>();
ConfigManager.Config.worlds.forEach(worldName ->{
if(data.contains(worldName)){
World world = Bukkit.getWorld(worldName);
data.getConfigurationSection(worldName).getKeys(false).forEach(key ->{
String[] string_list = StringUtils.split(key,",");
if (world.isChunkLoaded(Integer.parseInt(string_list[0])/16, Integer.parseInt(string_list[2])/16)){
Location tempLoc = new Location(world,Double.parseDouble(string_list[0]),Double.parseDouble(string_list[1]),Double.parseDouble(string_list[2]));
if(world.getBlockAt(tempLoc).getType() != Material.TRIPWIRE){
CropManager.instances.remove(tempLoc);
data.set(worldName+"."+string_list[0]+","+string_list[1]+","+string_list[2], null);
}else {
locations.add(new Location(world, Double.parseDouble(string_list[0]),Double.parseDouble(string_list[1]),Double.parseDouble(string_list[2])));
}
}
});
}
});
long finish2 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f农作物缓存清理耗时&a" + String.valueOf(finish2-start2) + "&fms",Bukkit.getConsoleSender());
/*
阶段3保存文件
*/
try{
data.save(file);
}catch (IOException e){
e.printStackTrace();
CustomCrops.instance.getLogger().warning("农作物缓存清理保存出错!");
}
/*
阶段4农作物生长判断
*/
long start3 = System.currentTimeMillis();
BukkitScheduler bukkitScheduler = Bukkit.getScheduler();
ConfigManager.Config.worlds.forEach(worldName -> {
World world = Bukkit.getWorld(worldName);
locations.forEach(seedLocation -> {
Location potLocation = seedLocation.clone().subtract(0,1,0);
Block seedBlock = world.getBlockAt(seedLocation);
Block potBlock = world.getBlockAt(potLocation);
String[] seasons = StringUtils.split(data.getString(worldName + "." + seedLocation.getBlockX() + "," + seedLocation.getBlockY() + "," + seedLocation.getBlockZ()),",");
if (CustomBlock.byAlreadyPlaced(potBlock) != null && CustomBlock.byAlreadyPlaced(seedBlock) != null){
String seedNamespace = CustomBlock.byAlreadyPlaced(seedBlock).getNamespacedID();
if (CustomBlock.byAlreadyPlaced(potBlock).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.watered_pot) && seedNamespace.contains("stage")){
if (seedNamespace.equalsIgnoreCase(ConfigManager.Config.dead)){
return;
}
String[] split = StringUtils.split(seedNamespace,":");
String[] cropNameList = StringUtils.split(split[1],"_");
Label_out:
if(ConfigManager.Config.season){
if(ConfigManager.Config.greenhouse){
for(int i = 1; i <= ConfigManager.Config.range; i++){
Location tempLocation = seedLocation.clone().add(0,i,0);
if (CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)) != null){
if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.glass)){
break Label_out;
}
}
}
}
boolean wrongSeason = true;
for(String season : seasons){
if (Objects.equals(season, ConfigManager.Config.current)) {
wrongSeason = false;
break;
}
}
if(wrongSeason){
bukkitScheduler.callSyncMethod(CustomCrops.instance, () -> {
CustomBlock.remove(seedLocation);
CustomBlock.place(ConfigManager.Config.dead, seedLocation);
return null;
});
return;
}
}
int nextStage = Integer.parseInt(cropNameList[2]) + 1;
if (CustomBlock.getInstance( split[0] +":"+cropNameList[0] + "_stage_" + nextStage) != null) {
bukkitScheduler.callSyncMethod(CustomCrops.instance, () ->{
CustomBlock.remove(potLocation);
CustomBlock.place(ConfigManager.Config.pot, potLocation);
if(Math.random()< ConfigManager.Config.grow_chance){
CustomBlock.remove(seedLocation);
CustomBlock.place(split[0] + ":" + cropNameList[0] + "_stage_" + nextStage,seedLocation);
}
return null;
});
}else if(ConfigManager.Config.big){
//农作物巨大化
if(config.getConfigurationSection("crops." + cropNameList[0]).getKeys(false).contains("gigantic")){
bukkitScheduler.callSyncMethod(CustomCrops.instance, () ->{
CustomBlock.remove(potLocation);
CustomBlock.place(ConfigManager.Config.pot, potLocation);
if(ConfigManager.Config.big_chance > Math.random()){
CustomBlock.remove(seedLocation);
CustomBlock.place(config.getString("crops." + cropNameList[0] + ".gigantic"),seedLocation);
}
return null;
});
}
}
}else if(CustomBlock.byAlreadyPlaced(potBlock).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.pot) && seedNamespace.contains("stage")){
if (seedNamespace.equalsIgnoreCase(ConfigManager.Config.dead)){
return;
}
if(ConfigManager.Config.season) {
if(ConfigManager.Config.greenhouse){
for(int i = 1; i <= ConfigManager.Config.range; i++){
Location tempLocation = seedLocation.clone().add(0,i,0);
if (CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)) != null){
if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.glass)){
return;
}
}
}
}
boolean wrongSeason = true;
for (String season : seasons) {
if (Objects.equals(season, ConfigManager.Config.current)) {
wrongSeason = false;
break;
}
}
if (wrongSeason) {
bukkitScheduler.callSyncMethod(CustomCrops.instance, () -> {
CustomBlock.remove(seedLocation);
CustomBlock.place(ConfigManager.Config.dead, seedLocation);
return null;
});
}
}
}
}
});
});
long finish3 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f农作物生长判断耗时&a" + String.valueOf(finish3-start3) + "&fms",Bukkit.getConsoleSender());
}
/*
//清理无效的农作物
public static void cleanLoadedCache() {
File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml");
FileConfiguration data;
data = YamlConfiguration.loadConfiguration(file);
ConfigManager.Config.worlds.forEach(worldName ->{
if(data.contains(worldName)){ if(data.contains(worldName)){
World world = Bukkit.getWorld(worldName); World world = Bukkit.getWorld(worldName);
data.getConfigurationSection(worldName).getKeys(false).forEach(key ->{ data.getConfigurationSection(worldName).getKeys(false).forEach(key ->{
@@ -142,4 +318,5 @@ public class CropManager {
CustomCrops.instance.getLogger().warning("农作物缓存清理保存出错!"); CustomCrops.instance.getLogger().warning("农作物缓存清理保存出错!");
} }
} }
*/
} }

View File

@@ -1,23 +1,20 @@
package net.momirealms.customcrops.DataManager; package net.momirealms.customcrops.DataManager;
import dev.lone.itemsadder.api.CustomBlock; import dev.lone.itemsadder.api.CustomBlock;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.ConfigManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
public class MaxCropsPerChunk { public class MaxCropsPerChunk {
static FileConfiguration config = CustomCrops.instance.getConfig();
public static boolean maxCropsPerChunk(Location location){ public static boolean maxCropsPerChunk(Location location){
if(!config.getBoolean("config.enable-limit")){ if(!ConfigManager.Config.limit){
return false; return false;
} }
int maxY = config.getInt("config.height.max"); int maxY = ConfigManager.Config.maxh;
int minY = config.getInt("config.height.min"); int minY = ConfigManager.Config.minh;
int maxAmount = config.getInt("config.max-crops"); int maxAmount = ConfigManager.Config.max_crop;
int n = 1; int n = 1;

View File

@@ -1,23 +1,20 @@
package net.momirealms.customcrops.DataManager; package net.momirealms.customcrops.DataManager;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.ConfigManager;
import net.momirealms.customcrops.IAFurniture; import net.momirealms.customcrops.IAFurniture;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
public class MaxSprinklersPerChunk { public class MaxSprinklersPerChunk {
static FileConfiguration config = CustomCrops.instance.getConfig();
public static boolean maxSprinklersPerChunk(Location location){ public static boolean maxSprinklersPerChunk(Location location){
if(!config.getBoolean("config.enable-limit")){ if(!ConfigManager.Config.limit){
return false; return false;
} }
int maxY = config.getInt("config.height.max"); int maxY = ConfigManager.Config.maxh;
int minY = config.getInt("config.height.min"); int minY = ConfigManager.Config.minh;
int maxAmount = config.getInt("config.max-sprinklers"); int maxAmount = ConfigManager.Config.max_sprinkler;
int n = 1; int n = 1;

View File

@@ -1,5 +1,6 @@
package net.momirealms.customcrops.DataManager; package net.momirealms.customcrops.DataManager;
import net.momirealms.customcrops.ConfigManager;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.CustomCrops;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@@ -7,8 +8,9 @@ import java.util.Objects;
public class NextSeason { public class NextSeason {
public static void changeSeason(){ public static void changeSeason(){
FileConfiguration config = CustomCrops.instance.getConfig(); FileConfiguration config = CustomCrops.instance.getConfig();
String currentSeason = config.getString("current-season"); String currentSeason = ConfigManager.Config.current;
String nextSeason = switch (Objects.requireNonNull(currentSeason)) { String nextSeason = switch (Objects.requireNonNull(currentSeason)) {
case "spring" -> "summer"; case "spring" -> "summer";
case "summer" -> "autumn"; case "summer" -> "autumn";
@@ -18,6 +20,7 @@ public class NextSeason {
}; };
if(nextSeason != null){ if(nextSeason != null){
config.set("current-season", nextSeason); config.set("current-season", nextSeason);
ConfigManager.Config.current = nextSeason;
CustomCrops.instance.saveConfig(); CustomCrops.instance.saveConfig();
}else { }else {
CustomCrops.instance.getLogger().warning("季节配置文件出错!"); CustomCrops.instance.getLogger().warning("季节配置文件出错!");

View File

@@ -1,9 +1,14 @@
package net.momirealms.customcrops.DataManager; package net.momirealms.customcrops.DataManager;
import dev.lone.itemsadder.api.CustomBlock;
import net.momirealms.customcrops.ConfigManager;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.IAFurniture; import net.momirealms.customcrops.IAFurniture;
import net.momirealms.customcrops.MessageManager;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@@ -17,11 +22,22 @@ public class SprinklerManager {
public static ConcurrentHashMap<Location, String> instances; public static ConcurrentHashMap<Location, String> instances;
//10W性能测试
public static void testData_3(){
for(int i = -50000; i < 50000; i++){
Location tempLoc = new Location(Bukkit.getWorld("world"),i,100,i);
String name = "s1";
instances.put(tempLoc, name);
}
}
//开服的时候将文件的数据读入 //开服的时候将文件的数据读入
public SprinklerManager(FileConfiguration data) { public SprinklerManager(FileConfiguration data) {
FileConfiguration config = CustomCrops.instance.getConfig(); FileConfiguration config = CustomCrops.instance.getConfig();
File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml"); File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml");
data = YamlConfiguration.loadConfiguration(file); data = YamlConfiguration.loadConfiguration(file);
try { try {
for (String world : config.getStringList("config.whitelist-worlds")) { for (String world : config.getStringList("config.whitelist-worlds")) {
SprinklerManager.instances = new ConcurrentHashMap<Location, String>(); SprinklerManager.instances = new ConcurrentHashMap<Location, String>();
@@ -40,6 +56,7 @@ public class SprinklerManager {
} }
saveData(); saveData();
} }
/*
//根据世界名获取所有的洒水器 //根据世界名获取所有的洒水器
public static List<Location> getSprinklers(World world){ public static List<Location> getSprinklers(World world){
FileConfiguration config = CustomCrops.instance.getConfig(); FileConfiguration config = CustomCrops.instance.getConfig();
@@ -64,6 +81,7 @@ public class SprinklerManager {
} }
return locations; return locations;
} }
*/
//保存数据 //保存数据
public static void saveData(){ public static void saveData(){
File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml"); File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml");
@@ -91,31 +109,116 @@ public class SprinklerManager {
public static void putInstance(Location location, String type) { public static void putInstance(Location location, String type) {
SprinklerManager.instances.put(location, type); SprinklerManager.instances.put(location, type);
} }
public static void cleanCache() { public static void SprinklerWork() {
/*
阶段1更新数据
*/
long start1 = System.currentTimeMillis();
File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml"); File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml");
FileConfiguration data; FileConfiguration data;
data = YamlConfiguration.loadConfiguration(file); data = YamlConfiguration.loadConfiguration(file);
//map不能一边循环一边删除 if (SprinklerManager.instances != null) {
//创建一个新的HashSet,用作循环 //性能更高
Set<Map.Entry<Location, String>> en = instances.entrySet();
for(Map.Entry<Location, String> entry : en){
data.set(entry.getKey().getWorld().getName() + "." + entry.getKey().getBlockX() + "," + entry.getKey().getBlockY()+ ","+entry.getKey().getBlockZ(), entry.getValue());
}
}
long finish1 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f洒水器数据更新耗时&a" + String.valueOf(finish1-start1) + "&fms",Bukkit.getConsoleSender());
/*
阶段2清理数据内无效的农作物
*/
Bukkit.getScheduler().callSyncMethod(CustomCrops.instance,()->{ Bukkit.getScheduler().callSyncMethod(CustomCrops.instance,()->{
Set<Location> key = new HashSet(instances.keySet()); long start2 = System.currentTimeMillis();
try{ //map不能一边循环一边删除
for(Location u_key : key) { //创建一个新的HashSet,用作循环
if (!IAFurniture.getFromLocation(u_key.clone().add(0.5,0.5,0.5), u_key.getWorld())){ //检测碰撞体积需要同步
SprinklerManager.instances.remove(u_key); List<Location> locations = new ArrayList<Location>();
data.set(u_key.getWorld().getName()+"."+u_key.getBlockX()+","+u_key.getBlockY()+","+u_key.getBlockZ(), null);
} ConfigManager.Config.worlds.forEach(worldName ->{
if(data.contains(worldName)){
World world = Bukkit.getWorld(worldName);
data.getConfigurationSection(worldName).getKeys(false).forEach(key ->{
String[] string_list = StringUtils.split(key,",");
if (world.isChunkLoaded(Integer.parseInt(string_list[0])/16, Integer.parseInt(string_list[2])/16)){
Location tempLoc = new Location(world,Double.parseDouble(string_list[0])+0.5,Double.parseDouble(string_list[1])+0.5,Double.parseDouble(string_list[2])+0.5);
if(!IAFurniture.getFromLocation(tempLoc, world)){
SprinklerManager.instances.remove(tempLoc);
data.set(worldName+"."+string_list[0]+","+string_list[1]+","+string_list[2], null);
}else {
locations.add(new Location(world, Double.parseDouble(string_list[0]),Double.parseDouble(string_list[1]),Double.parseDouble(string_list[2])));
}
}
});
}
});
/*
Set<Location> key = new HashSet(instances.keySet());
for(Location u_key : key) {
if (!IAFurniture.getFromLocation(u_key.clone().add(0.5,0.5,0.5), u_key.getWorld())){
SprinklerManager.instances.remove(u_key);
data.set(u_key.getWorld().getName()+"."+u_key.getBlockX()+","+u_key.getBlockY()+","+u_key.getBlockZ(), null);
}else {
locations.add(new Location(world, Double.parseDouble(string_list[0]),Double.parseDouble(string_list[1]),Double.parseDouble(string_list[2])));
} }
}catch (ConcurrentModificationException e){
e.printStackTrace();
}
try{
data.save(file);
}catch (IOException e){
e.printStackTrace();
CustomCrops.instance.getLogger().warning("洒水器缓存清理保存出错!");
} }
*/
long finish2 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f洒水器缓存清理耗时&a" + String.valueOf(finish2-start2) + "&fms",Bukkit.getConsoleSender());
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.instance,()->{
/*
阶段3保存数据
*/
try{
data.save(file);
}catch (IOException e){
e.printStackTrace();
CustomCrops.instance.getLogger().warning("洒水器缓存清理保存出错!");
}
/*
阶段4洒水器工作
*/
long start3 = System.currentTimeMillis();
ConfigManager.Config.worlds.forEach(worldName -> {
World world = Bukkit.getWorld(worldName);
locations.forEach(location -> {
String type = data.getString(worldName + "." + location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ());
if(type.equals("s1")){
for(int i = -1; i <= 1;i++){
for (int j = -1; j <= 1; j++){
Location tempLoc = location.clone().add(i,-1,j);
waterPot(tempLoc, world);
}
}
}else{
for(int i = -2; i <= 2;i++){
for (int j = -2; j <= 2; j++){
Location tempLoc = location.clone().add(i,-1,j);
waterPot(tempLoc, world);
}
}
}
});
});
long finish3 = System.currentTimeMillis();
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f洒水器工作耗时&a" + String.valueOf(finish3-start3) + "&fms",Bukkit.getConsoleSender());
});
return null; return null;
}); });
} }
private static void waterPot(Location tempLoc, World world) {
if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLoc)) != null){
if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLoc)).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.pot)){
Bukkit.getScheduler().callSyncMethod(CustomCrops.instance,()->{
CustomBlock.remove(tempLoc);
CustomBlock.place((ConfigManager.Config.watered_pot), tempLoc);
return null;
});
}
}
}
} }

View File

@@ -1,44 +1,41 @@
package net.momirealms.customcrops.Integrations; package net.momirealms.customcrops.Integrations;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.ConfigManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class IntegrationCheck { public class IntegrationCheck {
static FileConfiguration config = CustomCrops.instance.getConfig();
//收获权限检测 //收获权限检测
public static boolean HarvestCheck(Location location, Player player){ public static boolean HarvestCheck(Location location, Player player){
if(config.getBoolean("config.integration.kingdomsX")){ if(ConfigManager.Config.king){
if(KingdomsXIntegrations.checkKDBuild(location,player)){ if(KingdomsXIntegrations.checkKDBuild(location,player)){
return true; return true;
} }
} }
if(config.getBoolean("config.integration.residence")){ if(ConfigManager.Config.res){
if(ResidenceIntegrations.checkResHarvest(location,player)){ if(ResidenceIntegrations.checkResHarvest(location,player)){
return true; return true;
} }
} }
if(config.getBoolean("config.integration.worldguard")){ if(ConfigManager.Config.wg){
return WorldGuardIntegrations.checkWGHarvest(location, player); return WorldGuardIntegrations.checkWGHarvest(location, player);
} }
return false; return false;
} }
//种植等权限检测 //种植等权限检测
public static boolean PlaceCheck(Location location, Player player){ public static boolean PlaceCheck(Location location, Player player){
if(config.getBoolean("config.integration.kingdomsX")){ if(ConfigManager.Config.king){
if(KingdomsXIntegrations.checkKDBuild(location,player)){ if(KingdomsXIntegrations.checkKDBuild(location,player)){
return true; return true;
} }
} }
if(config.getBoolean("config.integration.residence")){ if(ConfigManager.Config.res){
if(ResidenceIntegrations.checkResBuild(location,player)){ if(ResidenceIntegrations.checkResBuild(location,player)){
return true; return true;
} }
} }
if(config.getBoolean("config.integration.worldguard")){ if(ConfigManager.Config.wg){
return WorldGuardIntegrations.checkWGBuild(location, player); return WorldGuardIntegrations.checkWGBuild(location, player);
} }
return false; return false;

View File

@@ -1,4 +0,0 @@
package net.momirealms.customcrops.Integrations;
public class PlotSquaredIntegrations {
}

View File

@@ -2,21 +2,13 @@ package net.momirealms.customcrops;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class Placeholders extends PlaceholderExpansion{ public class Placeholders extends PlaceholderExpansion{
private CustomCrops plugin;
public Placeholders(CustomCrops customCrops) { public Placeholders(CustomCrops customCrops) {
this.plugin = plugin;
} }
@Override @Override
public @NotNull String getIdentifier() { public @NotNull String getIdentifier() {
return "customcrops"; return "customcrops";
@@ -35,14 +27,12 @@ public class Placeholders extends PlaceholderExpansion{
@Override @Override
public String onRequest(OfflinePlayer player, String params) { public String onRequest(OfflinePlayer player, String params) {
FileConfiguration config = CustomCrops.instance.getConfig();
if(params.equalsIgnoreCase("season")){ if(params.equalsIgnoreCase("season")){
return Objects.requireNonNull(config.getString("current-season")) return ConfigManager.Config.current
.replace("spring", Objects.requireNonNull(config.getString("messages.spring"))) .replace("spring", ConfigManager.Config.spring)
.replace("summer", Objects.requireNonNull(config.getString("messages.summer"))) .replace("summer", ConfigManager.Config.summer)
.replace("autumn", Objects.requireNonNull(config.getString("messages.autumn"))) .replace("autumn", ConfigManager.Config.autumn)
.replace("winter", Objects.requireNonNull(config.getString("messages.winter"))); .replace("winter", ConfigManager.Config.winter);
} }
return null; return null;
} }

View File

@@ -2,21 +2,19 @@ package net.momirealms.customcrops.listener;
import dev.lone.itemsadder.api.CustomBlock; import dev.lone.itemsadder.api.CustomBlock;
import dev.lone.itemsadder.api.Events.CustomBlockBreakEvent; import dev.lone.itemsadder.api.Events.CustomBlockBreakEvent;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.ConfigManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
public class BreakCustomBlock implements Listener { public class BreakCustomBlock implements Listener {
FileConfiguration config = CustomCrops.instance.getConfig();
@EventHandler @EventHandler
public void breakCustomBlock(CustomBlockBreakEvent event){ public void breakCustomBlock(CustomBlockBreakEvent event){
Player player =event.getPlayer(); Player player =event.getPlayer();
@@ -32,14 +30,14 @@ public class BreakCustomBlock implements Listener {
CustomBlock.remove(location); CustomBlock.remove(location);
}); });
} }
}else if(namespacedId.equalsIgnoreCase(config.getString("config.watered-pot")) || namespacedId.equalsIgnoreCase(config.getString("config.pot"))){ }else if(namespacedId.equalsIgnoreCase(ConfigManager.Config.watered_pot) || namespacedId.equalsIgnoreCase(ConfigManager.Config.pot)){
World world = location.getWorld(); World world = location.getWorld();
Block blockUp = world.getBlockAt(location.add(0,1,0)); Block blockUp = world.getBlockAt(location.add(0,1,0));
if(CustomBlock.byAlreadyPlaced(blockUp) != null){ if(CustomBlock.byAlreadyPlaced(blockUp) != null){
if(CustomBlock.byAlreadyPlaced(blockUp).getNamespacedID().contains("stage")){ if(CustomBlock.byAlreadyPlaced(blockUp).getNamespacedID().contains("stage")){
CustomBlock.byAlreadyPlaced(blockUp).getLoot().forEach(itemStack -> { for (ItemStack itemStack : CustomBlock.byAlreadyPlaced(blockUp).getLoot()) {
world.dropItem(location.clone().add(0.5,0.2,0.5), itemStack); world.dropItem(location.clone().add(0.5, 0.2, 0.5), itemStack);
}); }
CustomBlock.remove(location); CustomBlock.remove(location);
} }
} }

View File

@@ -1,8 +1,7 @@
package net.momirealms.customcrops.listener; package net.momirealms.customcrops.listener;
import dev.lone.itemsadder.api.CustomStack; import dev.lone.itemsadder.api.CustomStack;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.ConfigManager;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@@ -11,18 +10,16 @@ import org.bukkit.event.entity.EntitySpawnEvent;
public class BreakFurniture implements Listener { public class BreakFurniture implements Listener {
FileConfiguration config = CustomCrops.instance.getConfig();
@EventHandler @EventHandler
public void breakFurniture(EntitySpawnEvent event){ public void breakFurniture(EntitySpawnEvent event){
Entity entity = event.getEntity(); Entity entity = event.getEntity();
if(!(entity instanceof Item)) return; if(!(entity instanceof Item)) return;
if(CustomStack.byItemStack(((Item) entity).getItemStack()) != null){ if(CustomStack.byItemStack(((Item) entity).getItemStack()) != null){
String namespacedId = CustomStack.byItemStack(((Item) entity).getItemStack()).getNamespacedID(); String namespacedId = CustomStack.byItemStack(((Item) entity).getItemStack()).getNamespacedID();
if(namespacedId.equalsIgnoreCase(config.getString("config.sprinkler-1"))){ if(namespacedId.equalsIgnoreCase(ConfigManager.Config.sprinkler_1)){
entity.remove(); entity.remove();
entity.getWorld().dropItem(entity.getLocation() ,CustomStack.getInstance(namespacedId + "_item").getItemStack()); entity.getWorld().dropItem(entity.getLocation() ,CustomStack.getInstance(namespacedId + "_item").getItemStack());
}else if(namespacedId.equalsIgnoreCase(config.getString("config.sprinkler-2"))){ }else if(namespacedId.equalsIgnoreCase(ConfigManager.Config.sprinkler_2)){
entity.remove(); entity.remove();
entity.getWorld().dropItem(entity.getLocation() ,CustomStack.getInstance(namespacedId + "_item").getItemStack()); entity.getWorld().dropItem(entity.getLocation() ,CustomStack.getInstance(namespacedId + "_item").getItemStack());
}else if(namespacedId.contains("_stage_")){ }else if(namespacedId.contains("_stage_")){

View File

@@ -1,7 +1,7 @@
package net.momirealms.customcrops.listener; package net.momirealms.customcrops.listener;
import dev.lone.itemsadder.api.CustomStack; import dev.lone.itemsadder.api.CustomStack;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.ConfigManager;
import net.momirealms.customcrops.DataManager.MaxSprinklersPerChunk; import net.momirealms.customcrops.DataManager.MaxSprinklersPerChunk;
import net.momirealms.customcrops.DataManager.SprinklerManager; import net.momirealms.customcrops.DataManager.SprinklerManager;
import net.momirealms.customcrops.IAFurniture; import net.momirealms.customcrops.IAFurniture;
@@ -13,7 +13,6 @@ import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@@ -48,12 +47,12 @@ public class RightClickBlock implements Listener {
} }
} }
if(hasWater){ if(hasWater){
FileConfiguration config = CustomCrops.instance.getConfig();
if(namespacedId.equalsIgnoreCase(config.getString("config.watering-can-1")) || if(namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_1) ||
namespacedId.equalsIgnoreCase(config.getString("config.watering-can-2")) || namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_2) ||
namespacedId.equalsIgnoreCase(config.getString("config.watering-can-3"))) { namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_3)) {
if(customStack.getMaxDurability() == customStack.getDurability()){ if(customStack.getMaxDurability() == customStack.getDurability()){
MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.can-full"),player); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.can_full,player);
}else { }else {
customStack.setDurability(customStack.getDurability() + 1); customStack.setDurability(customStack.getDurability() + 1);
player.getWorld().playSound(player.getLocation(),Sound.ITEM_BUCKET_FILL,1,1); player.getWorld().playSound(player.getLocation(),Sound.ITEM_BUCKET_FILL,1,1);
@@ -63,16 +62,16 @@ public class RightClickBlock implements Listener {
return; return;
} }
if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getBlockFace() == BlockFace.UP) { if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getBlockFace() == BlockFace.UP) {
FileConfiguration config = CustomCrops.instance.getConfig();
if(namespacedId.equalsIgnoreCase(config.getString("config.sprinkler-1-item")) || namespacedId.equalsIgnoreCase(config.getString("config.sprinkler-2-item"))){ if(namespacedId.equalsIgnoreCase(ConfigManager.Config.sprinkler_1i) || namespacedId.equalsIgnoreCase(ConfigManager.Config.sprinkler_2i)){
Location location = event.getClickedBlock().getLocation(); Location location = event.getClickedBlock().getLocation();
//兼容性检测 //兼容性检测
if(IntegrationCheck.PlaceCheck(location,player)){ if(IntegrationCheck.PlaceCheck(location,player)){
return; return;
} }
//高度限制 //高度限制
if(event.getClickedBlock().getY() > config.getInt("config.height.max") || event.getClickedBlock().getY() < config.getInt("config.height.min")){ if(event.getClickedBlock().getY() > ConfigManager.Config.maxh || event.getClickedBlock().getY() < ConfigManager.Config.minh){
MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.not-a-good-place"),player); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.bad_place,player);
return; return;
} }
//此位置是否已有洒水器 //此位置是否已有洒水器
@@ -81,13 +80,13 @@ public class RightClickBlock implements Listener {
} }
//区块上限 //区块上限
if(MaxSprinklersPerChunk.maxSprinklersPerChunk(location)){ if(MaxSprinklersPerChunk.maxSprinklersPerChunk(location)){
MessageManager.playerMessage(config.getString("messages.prefix")+config.getString("messages.reach-limit-sprinkler").replace("{Max}", config.getString("config.max-sprinklers")),player); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.limit_sprinkler.replace("{Max}", String.valueOf(ConfigManager.Config.max_sprinkler)),player);
return; return;
} }
if(player.getGameMode() != GameMode.CREATIVE){ if(player.getGameMode() != GameMode.CREATIVE){
itemStack.setAmount(itemStack.getAmount() -1); itemStack.setAmount(itemStack.getAmount() -1);
} }
if(namespacedId.equalsIgnoreCase(config.getString("config.sprinkler-1-item"))){ if(namespacedId.equalsIgnoreCase(ConfigManager.Config.sprinkler_1i)){
SprinklerManager.putInstance(location.clone().add(0,1,0),"s1"); SprinklerManager.putInstance(location.clone().add(0,1,0),"s1");
}else { }else {
SprinklerManager.putInstance(location.clone().add(0,1,0),"s2"); SprinklerManager.putInstance(location.clone().add(0,1,0),"s2");

View File

@@ -3,6 +3,7 @@ package net.momirealms.customcrops.listener;
import dev.lone.itemsadder.api.CustomBlock; import dev.lone.itemsadder.api.CustomBlock;
import dev.lone.itemsadder.api.CustomStack; import dev.lone.itemsadder.api.CustomStack;
import dev.lone.itemsadder.api.Events.CustomBlockInteractEvent; import dev.lone.itemsadder.api.Events.CustomBlockInteractEvent;
import net.momirealms.customcrops.ConfigManager;
import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.DataManager.CropManager; import net.momirealms.customcrops.DataManager.CropManager;
import net.momirealms.customcrops.DataManager.MaxCropsPerChunk; import net.momirealms.customcrops.DataManager.MaxCropsPerChunk;
@@ -30,7 +31,6 @@ public class RightClickCustomBlock implements Listener {
return; return;
} }
//获取配置文件 //获取配置文件
FileConfiguration config = CustomCrops.instance.getConfig();
Player player = event.getPlayer(); Player player = event.getPlayer();
Block clickedBlock = event.getBlockClicked(); Block clickedBlock = event.getBlockClicked();
Location clickedBlockLocation = clickedBlock.getLocation(); Location clickedBlockLocation = clickedBlock.getLocation();
@@ -49,7 +49,7 @@ public class RightClickCustomBlock implements Listener {
//如果ID内有stage则进行下一步 //如果ID内有stage则进行下一步
if (namespacedID.contains("stage")){ if (namespacedID.contains("stage")){
//是否为枯萎植物 //是否为枯萎植物
if(namespacedID.equalsIgnoreCase(config.getString("config.dead-crop"))) return; if(namespacedID.equalsIgnoreCase(ConfigManager.Config.dead)) return;
//String namespace = clickedCustomBlock.getNamespacedID().split(":")[0]; //String namespace = clickedCustomBlock.getNamespacedID().split(":")[0];
String[] split = StringUtils.split(namespacedID,":"); String[] split = StringUtils.split(namespacedID,":");
//String[] cropNameList = clickedCustomBlock.getNamespacedID().split(":")[1].split("_"); //String[] cropNameList = clickedCustomBlock.getNamespacedID().split(":")[1].split("_");
@@ -60,11 +60,11 @@ public class RightClickCustomBlock implements Listener {
if (CustomBlock.getInstance(split[0] + ":" + cropNameList[0] + "_stage_" + nextStage) == null) { if (CustomBlock.getInstance(split[0] + ":" + cropNameList[0] + "_stage_" + nextStage) == null) {
//如果不存在下一阶段说明已是最终阶段,可以收获 //如果不存在下一阶段说明已是最终阶段,可以收获
//遍历掉落物并删除方块 //遍历掉落物并删除方块
clickedCustomBlock.getLoot().forEach(itemStack -> { clickedCustomBlock.getLoot().forEach(itemStack -> clickedBlockLocation.getWorld().dropItem(clickedBlockLocation.clone().add(0.5,0.2,0.5),itemStack));
clickedBlockLocation.getWorld().dropItem(clickedBlockLocation.clone().add(0.5,0.2,0.5),itemStack);
});
CustomBlock.remove(clickedBlockLocation); CustomBlock.remove(clickedBlockLocation);
//如果配置文件中有return项目则放置方块 //如果配置文件中有return项目则放置方块
FileConfiguration config = CustomCrops.instance.getConfig();
if(config.getConfigurationSection("crops." + cropNameList[0]).getKeys(false).contains("return")){ if(config.getConfigurationSection("crops." + cropNameList[0]).getKeys(false).contains("return")){
CustomBlock.place(config.getString("crops." + cropNameList[0] + ".return"), clickedBlockLocation); CustomBlock.place(config.getString("crops." + cropNameList[0] + ".return"), clickedBlockLocation);
} }
@@ -89,7 +89,7 @@ public class RightClickCustomBlock implements Listener {
Block blockUnder = world.getBlockAt(clickedBlockLocation.clone().subtract(0,1,0)); Block blockUnder = world.getBlockAt(clickedBlockLocation.clone().subtract(0,1,0));
if (CustomBlock.byAlreadyPlaced(blockUnder) == null) return; if (CustomBlock.byAlreadyPlaced(blockUnder) == null) return;
//检测右键的方块下方是否为干燥的种植盆方块 //检测右键的方块下方是否为干燥的种植盆方块
if (CustomBlock.byAlreadyPlaced(blockUnder).getNamespacedID().equalsIgnoreCase(config.getString("config.pot"))) { if (CustomBlock.byAlreadyPlaced(blockUnder).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.pot)) {
//获取手中的物品 //获取手中的物品
ItemStack mainHandItem = player.getInventory().getItemInMainHand(); ItemStack mainHandItem = player.getInventory().getItemInMainHand();
Location locUnder = clickedBlockLocation.clone().subtract(0,1,0); Location locUnder = clickedBlockLocation.clone().subtract(0,1,0);
@@ -101,13 +101,13 @@ public class RightClickCustomBlock implements Listener {
player.getInventory().addItem(new ItemStack(Material.BUCKET)); player.getInventory().addItem(new ItemStack(Material.BUCKET));
} }
CustomBlock.remove(locUnder); CustomBlock.remove(locUnder);
CustomBlock.place(config.getString("config.watered-pot"), locUnder); CustomBlock.place(ConfigManager.Config.watered_pot, locUnder);
} else if (mainHandItem.getType() == Material.WOODEN_SWORD) { } else if (mainHandItem.getType() == Material.WOODEN_SWORD) {
waterPot(mainHandItem, player, locUnder, config); waterPot(mainHandItem, player, locUnder);
} }
} }
//检测右键的方块下方是否为湿润的种植盆方块 //检测右键的方块下方是否为湿润的种植盆方块
else if(CustomBlock.byAlreadyPlaced(blockUnder).getNamespacedID().equalsIgnoreCase(config.getString("config.watered-pot"))){ else if(CustomBlock.byAlreadyPlaced(blockUnder).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.watered_pot)){
//获取手中的物品 //获取手中的物品
ItemStack mainHandItem = player.getInventory().getItemInMainHand(); ItemStack mainHandItem = player.getInventory().getItemInMainHand();
//如果是骨粉 //如果是骨粉
@@ -128,18 +128,18 @@ public class RightClickCustomBlock implements Listener {
mainHandItem.setAmount(mainHandItem.getAmount() - 1); mainHandItem.setAmount(mainHandItem.getAmount() - 1);
} }
//骨粉的成功率 //骨粉的成功率
if (Math.random() < config.getDouble("config.bone-meal-chance")){ if (Math.random() < ConfigManager.Config.bone_chance){
CustomBlock.remove(clickedBlockLocation); CustomBlock.remove(clickedBlockLocation);
CustomBlock.place(split[0] + ":" + cropNameList[0] + "_stage_" + nextStage,clickedBlockLocation); CustomBlock.place(split[0] + ":" + cropNameList[0] + "_stage_" + nextStage,clickedBlockLocation);
Particle particleSuccess = Particle.valueOf(config.getString("config.particle.success")); Particle particleSuccess = Particle.valueOf(ConfigManager.Config.success);
world.spawnParticle(particleSuccess, clickedBlockLocation.clone().add(0.5, 0.1,0.5), 1 ,0,0,0,0); world.spawnParticle(particleSuccess, clickedBlockLocation.clone().add(0.5, 0.1,0.5), 1 ,0,0,0,0);
//使用骨粉是否消耗水分 //使用骨粉是否消耗水分
if(config.getBoolean("config.bone-meal-consume-water")){ if(ConfigManager.Config.need_water){
CustomBlock.remove(clickedBlockLocation.clone().subtract(0,1,0)); CustomBlock.remove(clickedBlockLocation.clone().subtract(0,1,0));
CustomBlock.place(config.getString("config.pot"), clickedBlockLocation.clone().subtract(0,1,0)); CustomBlock.place(ConfigManager.Config.pot, clickedBlockLocation.clone().subtract(0,1,0));
} }
}else { }else {
Particle particleFailure = Particle.valueOf(config.getString("config.particle.failure")); Particle particleFailure = Particle.valueOf(ConfigManager.Config.failure);
world.spawnParticle(particleFailure, clickedBlockLocation.clone().add(0.5, 0.1,0.5), 1 ,0,0,0,0); world.spawnParticle(particleFailure, clickedBlockLocation.clone().add(0.5, 0.1,0.5), 1 ,0,0,0,0);
} }
} }
@@ -154,10 +154,9 @@ public class RightClickCustomBlock implements Listener {
else if (event.getBlockFace() == BlockFace.UP){ else if (event.getBlockFace() == BlockFace.UP){
//获取手中的物品 //获取手中的物品
ItemStack mainHandItem = player.getInventory().getItemInMainHand(); ItemStack mainHandItem = player.getInventory().getItemInMainHand();
World world = player.getWorld();
//检测右键的方块是否为干燥的种植盆方块 //检测右键的方块是否为干燥的种植盆方块
if (namespacedId.equalsIgnoreCase(config.getString("config.pot"))){ if (namespacedId.equalsIgnoreCase(ConfigManager.Config.pot)){
//如果手中的是水桶,那么转干为湿 //如果手中的是水桶,那么转干为湿
if (mainHandItem.getType() == Material.WATER_BUCKET){ if (mainHandItem.getType() == Material.WATER_BUCKET){
@@ -167,22 +166,22 @@ public class RightClickCustomBlock implements Listener {
player.getInventory().addItem(new ItemStack(Material.BUCKET)); player.getInventory().addItem(new ItemStack(Material.BUCKET));
} }
CustomBlock.remove(clickedBlockLocation); CustomBlock.remove(clickedBlockLocation);
CustomBlock.place(config.getString("config.watered-pot"),clickedBlockLocation); CustomBlock.place(ConfigManager.Config.watered_pot,clickedBlockLocation);
} else if (mainHandItem.getType() == Material.WOODEN_SWORD){ } else if (mainHandItem.getType() == Material.WOODEN_SWORD){
waterPot(mainHandItem, player,clickedBlockLocation, config); waterPot(mainHandItem, player,clickedBlockLocation);
} else { } else {
tryPlantSeed(clickedBlockLocation, mainHandItem, player, config); tryPlantSeed(clickedBlockLocation, mainHandItem, player);
} }
} }
//检测右键的方块是否为湿润的种植盆方块 //检测右键的方块是否为湿润的种植盆方块
else if(namespacedId.equalsIgnoreCase(config.getString("config.watered-pot"))){ else if(namespacedId.equalsIgnoreCase(ConfigManager.Config.watered_pot)){
tryPlantSeed(clickedBlockLocation, mainHandItem, player, config); tryPlantSeed(clickedBlockLocation, mainHandItem, player);
} }
} }
} }
} }
//尝试种植植物 //尝试种植植物
private void tryPlantSeed(Location clickedBlockLocation, ItemStack mainHandItem, Player player, FileConfiguration config) { private void tryPlantSeed(Location clickedBlockLocation, ItemStack mainHandItem, Player player) {
//是否为IA物品 //是否为IA物品
if(CustomStack.byItemStack(mainHandItem) == null) return; if(CustomStack.byItemStack(mainHandItem) == null) return;
//获取命名空间名与ID //获取命名空间名与ID
@@ -192,30 +191,33 @@ public class RightClickCustomBlock implements Listener {
//获取农作物名 //获取农作物名
String cropName = StringUtils.split(namespaced_id.replace("_seeds",""),":")[1]; String cropName = StringUtils.split(namespaced_id.replace("_seeds",""),":")[1];
//String[] crop = CustomStack.byItemStack(mainHandItem).getNamespacedID().toLowerCase().replace("_seeds","").split(":"); //String[] crop = CustomStack.byItemStack(mainHandItem).getNamespacedID().toLowerCase().replace("_seeds","").split(":");
//该种子是否存在于配置文件中
if(!config.getConfigurationSection("crops").getKeys(false).contains(cropName)){
MessageManager.playerMessage(config.getString("messages.prefix")+config.getString("messages.no-such-seed"),player);
return;
}
//检测上方为空气 //检测上方为空气
if(clickedBlockLocation.getWorld().getBlockAt(clickedBlockLocation.clone().add(0, 1, 0)).getType() != Material.AIR){ if(clickedBlockLocation.getWorld().getBlockAt(clickedBlockLocation.clone().add(0, 1, 0)).getType() != Material.AIR){
return; return;
} }
FileConfiguration config = CustomCrops.instance.getConfig();
//该种子是否存在于配置文件中
if(!config.getConfigurationSection("crops").getKeys(false).contains(cropName)){
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.no_such_seed,player);
return;
}
//是否超高超低 //是否超高超低
if (clickedBlockLocation.getY() < config.getInt("config.height.min") || clickedBlockLocation.getY() > config.getInt("config.height.max")){ if (clickedBlockLocation.getY() < ConfigManager.Config.minh || clickedBlockLocation.getY() > ConfigManager.Config.maxh){
MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.not-a-good-place"),player); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.bad_place,player);
return; return;
} }
Location locUp = clickedBlockLocation.clone().add(0,1,0); Location locUp = clickedBlockLocation.clone().add(0,1,0);
//是否启用了季节 //是否启用了季节
Label_out: Label_out:
if(config.getBoolean("enable-season")){ if(ConfigManager.Config.season){
if(config.getBoolean("config.enable-greenhouse")){ if(ConfigManager.Config.greenhouse){
int range = ConfigManager.Config.range;
World world = player.getWorld(); World world = player.getWorld();
for(int i = 1; i <= config.getInt("config.greenhouse-range"); i++){ for(int i = 1; i <= range; i++){
Location tempLocation = locUp.clone().add(0,i,0); Location tempLocation = locUp.clone().add(0,i,0);
if (CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)) != null){ if (CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)) != null){
if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)).getNamespacedID().equalsIgnoreCase(config.getString("config.greenhouse-glass"))){ if(CustomBlock.byAlreadyPlaced(world.getBlockAt(tempLocation)).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.glass)){
break Label_out; break Label_out;
} }
} }
@@ -225,22 +227,23 @@ public class RightClickCustomBlock implements Listener {
String[] seasons = config.getString("crops."+cropName+".season").split(","); String[] seasons = config.getString("crops."+cropName+".season").split(",");
boolean wrongSeason = true; boolean wrongSeason = true;
for(String season : seasons){ for(String season : seasons){
if(Objects.equals(season, config.getString("current-season"))){ if (Objects.equals(season, ConfigManager.Config.current)) {
wrongSeason = false; wrongSeason = false;
break;
} }
} }
if(wrongSeason){ if(wrongSeason){
MessageManager.playerMessage(config.getString("messages.prefix")+config.getString("messages.wrong-season"),player); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.wrong_season,player);
return; return;
} }
} }
//是否到达区块上限 //是否到达区块上限
if(MaxCropsPerChunk.maxCropsPerChunk(clickedBlockLocation)){ if(MaxCropsPerChunk.maxCropsPerChunk(clickedBlockLocation)){
MessageManager.playerMessage(config.getString("messages.prefix")+config.getString("messages.reach-limit-crop").replace("{Max}", config.getString("config.max-crops")),player); MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.limit_crop.replace("{Max}", String.valueOf(ConfigManager.Config.max_crop)),player);
return; return;
} }
//添加到缓存中 //添加到缓存中
if(config.getBoolean("enable-season")){ if(ConfigManager.Config.season){
CropManager.putInstance(locUp, config.getString("crops."+cropName+".season")); CropManager.putInstance(locUp, config.getString("crops."+cropName+".season"));
}else{ }else{
CropManager.putInstance(locUp, "all"); CropManager.putInstance(locUp, "all");
@@ -253,7 +256,7 @@ public class RightClickCustomBlock implements Listener {
CustomBlock.place(namespaced_id.replace("_seeds","_stage_1"),locUp); CustomBlock.place(namespaced_id.replace("_seeds","_stage_1"),locUp);
} }
} }
private void waterPot(ItemStack itemStack, Player player, Location location, FileConfiguration config){ private void waterPot(ItemStack itemStack, Player player, Location location){
//是否为IA物品 //是否为IA物品
if(CustomStack.byItemStack(itemStack) == null) return; if(CustomStack.byItemStack(itemStack) == null) return;
@@ -268,13 +271,13 @@ public class RightClickCustomBlock implements Listener {
int x; int x;
int z; int z;
if (namespacedId.equalsIgnoreCase(config.getString("config.watering-can-1"))) { if (namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_1)) {
x = 0; x = 0;
z = 0; z = 0;
} else if (namespacedId.equalsIgnoreCase(config.getString("config.watering-can-2"))) { } else if (namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_2)){
x = 2; x = 2;
z = 2; z = 2;
} else if (namespacedId.equalsIgnoreCase(config.getString("config.watering-can-3"))) { } else if (namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_3)){
x = 4; x = 4;
z = 4; z = 4;
} else return; } else return;
@@ -289,8 +292,8 @@ public class RightClickCustomBlock implements Listener {
float yaw = player.getLocation().getYaw(); float yaw = player.getLocation().getYaw();
//提前获取ID与命名空间以免for循环循环get //提前获取ID与命名空间以免for循环循环get
String wateredPot = config.getString("config.watered-pot"); String wateredPot = ConfigManager.Config.watered_pot;
String pot = config.getString("config.pot"); String pot = ConfigManager.Config.pot;
//根据朝向确定浇水方向 //根据朝向确定浇水方向
if (yaw <= 45 && yaw >= -135) { if (yaw <= 45 && yaw >= -135) {

View File

@@ -1,99 +0,0 @@
enable-season: true
# Current season
# Use command "/customcrops setseason" to change season
# You can experience season feature by using "CMI schedule" or other Season plugin
current-season: spring
config:
# The pot that crops can be planted in
# format: namespace:item_id
pot: customcrops:pot
# Crops can only grow on watered pot
watered-pot: customcrops:watered_pot
# Watering Can
# 1x1 range
watering-can-1: customcrops:watering_can_1
# 1x3 range
watering-can-2: customcrops:watering_can_2
# 1x5 range
watering-can-3: customcrops:watering_can_3
# enable greenhouse feature
# crops can be planted under greenhouse glass
enable-greenhouse: true
# effective range
greenhouse-range: 7
greenhouse-glass: customcrops:greenhouse_glass
# sprinkler furniture in ItemsAdder
# 3x3 range
sprinkler-1: customcrops:sprinkler_1
# sprinkler item in ItemsAdder
sprinkler-1-item: customcrops:sprinkler_1_item
# 5x5 range
sprinkler-2: customcrops:sprinkler_2
sprinkler-2-item: customcrops:sprinkler_2_item
# dead crops
dead-crop: customcrops:crop_stage_death
# The chance that crops grow every morning
grow-success-chance: 0.8
# The chance that using bone meal to skip one stage
bone-meal-chance: 0.5
# Will using bone meal consume water
bone-meal-consume-water: true
# Particle Effects
particle:
# Use bone meal
success: HEART
failure: VILLAGER_ANGRY
# The height where crops can grow
height:
min: 50
max: 100
# should we limit the max number of crops and spinklers in chunks?
# Maybe too many crops would cause lagggggggg
enable-limit: true
max-crops: 32
max-sprinklers: 4
# whitelist worlds where crops would grow
whitelist-worlds:
- world
# should we only alow crops in loaded chunks to grow?
# This is good for server performance
only-grow-in-loaded-chunks: true
integration:
residence: false
worldguard: false
kingdomsX: false
messages:
prefix: '<gradient:#ccfbff:#ef96c5>[CustomCrops] </gradient>'
not-a-good-place: 'That place is too high/low. Just try another place!'
reload: 'Reloaded'
force-save: 'Successfully save cache data!'
no-such-seed: 'This seed is not configured in config.yml'
wrong-season: 'You can not plant this crop in this season'
season-set: 'Successfully change season to {Season}'
season-disabled: 'Season is not enabled in this server'
clean-cache: 'Successfully clean removed crops and sprinkler'
reach-limit-crop: 'Too many crops. The limit is {Max} per chunk!'
reach-limit-sprinkler: 'Too many sprinklers. The limit is {Max} per chunk!'
backup: 'Back up Successfully'
spring: 'spring'
summer: 'summer'
autumn: 'autumn'
winter: 'winter'
can-full: 'The watering can is already full!'
# Crops
crops:
tomato:
# Please name the crops with "_seeds" and "_stage_X"
# Otherwise they would not grow
# You can refer to the example configuration :)
season: spring,autumn
#return: customcrops:tomato_stage_3

View File

@@ -30,11 +30,9 @@ config:
- 200 - 200
# - 12200 # - 12200
# 生长生效的世界 # 生长生效的世界
whitelist-worlds: whitelist-worlds:
- world - world
# 只在被加载的区块中生长和枯萎,对性能友好
only-grow-in-loaded-chunks: true
# 农作物每次成长一个阶段的概率0-1 # 农作物每次成长一个阶段的概率0-1
grow-success-chance: 0.8 grow-success-chance: 0.8
@@ -103,6 +101,15 @@ config:
max-crops: 32 max-crops: 32
max-sprinklers: 4 max-sprinklers: 4
# ######################
# 巨大植物 #
# ######################
gigantic:
# 是否启用巨大植物
enable: true
# 农作物巨大化的概率
chance: 0.01
# ###################### # ######################
# 插件兼容 # # 插件兼容 #
@@ -132,7 +139,8 @@ messages:
season-set: '&f成功设置季节为{Season}' season-set: '&f成功设置季节为{Season}'
nextseason: '&f已切换到下一季节' nextseason: '&f已切换到下一季节'
season-disabled: '&f季节系统未启用.' season-disabled: '&f季节系统未启用.'
clean-cache: '&f无效农作物和洒水器数据清除完毕!' force-grow: '&f已强制生长!'
force-water: '&f已强制洒水!'
reach-limit-crop: '&f农作物已到达最大区块上限{Max}!' reach-limit-crop: '&f农作物已到达最大区块上限{Max}!'
reach-limit-sprinkler: '&f洒水器已到达最大区块上限{Max}!' reach-limit-sprinkler: '&f洒水器已到达最大区块上限{Max}!'
backup: '&f备份完成' backup: '&f备份完成'
@@ -151,8 +159,10 @@ crops:
# 在IA配置文件中命名农作物时请以_seeds和_stage_X结尾否则会无法生长和种植 # 在IA配置文件中命名农作物时请以_seeds和_stage_X结尾否则会无法生长和种植
# 适宜的生长季节,若未启用季节系统可以无视此项目 # 适宜的生长季节,若未启用季节系统可以无视此项目
season: spring,summer season: spring,summer
# 空手收获后返回第几个生长状态 # 空手收获后返回第几个生长状态
# return: customcrops:tomato_stage_3 # return: customcrops:tomato_stage_3
# 巨大化植物
# gigantic: customcrops:gigantic_tomato
# ###################### # ######################