mirror of
https://github.com/Xiao-MoMi/Custom-Crops.git
synced 2025-12-22 08:29:35 +00:00
1.4.0 beta
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
package net.momirealms.customcrops;
|
||||
|
||||
import com.comphenix.protocol.ProtocolManager;
|
||||
import net.momirealms.customcrops.crops.CropTimer;
|
||||
import net.momirealms.customcrops.datamanager.BackUp;
|
||||
import net.momirealms.customcrops.datamanager.CropManager;
|
||||
import net.momirealms.customcrops.datamanager.SprinklerManager;
|
||||
import net.momirealms.customcrops.commands.CommandHandler;
|
||||
import net.momirealms.customcrops.commands.CommandTabComplete;
|
||||
import net.momirealms.customcrops.datamanager.*;
|
||||
import net.momirealms.customcrops.listener.BreakCrops;
|
||||
import net.momirealms.customcrops.timer.CropTimer;
|
||||
import net.momirealms.customcrops.listener.BreakCustomBlock;
|
||||
import net.momirealms.customcrops.listener.BreakFurniture;
|
||||
import net.momirealms.customcrops.listener.RightClickBlock;
|
||||
import net.momirealms.customcrops.listener.RightClickCustomBlock;
|
||||
import net.momirealms.customcrops.utils.Placeholders;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@@ -21,7 +21,6 @@ import java.util.Objects;
|
||||
public final class CustomCrops extends JavaPlugin {
|
||||
|
||||
public static JavaPlugin instance;
|
||||
public static ProtocolManager manager;
|
||||
public static CropTimer timer;
|
||||
public static CropManager cropManager;
|
||||
public static SprinklerManager sprinklerManager;
|
||||
@@ -40,7 +39,7 @@ public final class CustomCrops extends JavaPlugin {
|
||||
Bukkit.getPluginManager().registerEvents(new RightClickCustomBlock(),this);
|
||||
Bukkit.getPluginManager().registerEvents(new BreakCustomBlock(),this);
|
||||
Bukkit.getPluginManager().registerEvents(new RightClickBlock(),this);
|
||||
Bukkit.getPluginManager().registerEvents(new BreakFurniture(),this);
|
||||
Bukkit.getPluginManager().registerEvents(new BreakCrops(),this);
|
||||
|
||||
//开始计时任务
|
||||
CustomCrops.timer = new CropTimer();
|
||||
@@ -66,21 +65,17 @@ public final class CustomCrops extends JavaPlugin {
|
||||
}
|
||||
|
||||
//载入data数据
|
||||
FileConfiguration crop_data;
|
||||
FileConfiguration sprinkler_data;
|
||||
crop_data = YamlConfiguration.loadConfiguration(crop_file);
|
||||
sprinkler_data = YamlConfiguration.loadConfiguration(sprinkler_file);
|
||||
CustomCrops.cropManager = new CropManager(crop_data);
|
||||
CustomCrops.sprinklerManager = new SprinklerManager(sprinkler_data);
|
||||
CropManager.loadData();
|
||||
SprinklerManager.loadData();
|
||||
|
||||
//检测papi依赖
|
||||
if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null){
|
||||
new Placeholders(this).register();
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops] 检测到PlaceHolderAPI 已启用季节变量!",Bukkit.getConsoleSender());
|
||||
}
|
||||
//启动成功
|
||||
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops] 自定义农作物插件已启用!作者:小默米 QQ:3266959688",Bukkit.getConsoleSender());
|
||||
//this.getLogger().info("自定义农作物插件已启用!作者:小默米 QQ:3266959688");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package net.momirealms.customcrops;
|
||||
package net.momirealms.customcrops.commands;
|
||||
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import net.momirealms.customcrops.CustomCrops;
|
||||
import net.momirealms.customcrops.datamanager.MessageManager;
|
||||
import net.momirealms.customcrops.datamanager.BackUp;
|
||||
import net.momirealms.customcrops.datamanager.CropManager;
|
||||
import net.momirealms.customcrops.datamanager.NextSeason;
|
||||
@@ -117,11 +120,6 @@ public class CommandHandler implements CommandExecutor {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if(args[0].equalsIgnoreCase("test")){
|
||||
CropManager.testData_2();
|
||||
SprinklerManager.testData_3();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.momirealms.customcrops;
|
||||
package net.momirealms.customcrops.commands;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -1,11 +1,25 @@
|
||||
package net.momirealms.customcrops;
|
||||
package net.momirealms.customcrops.datamanager;
|
||||
|
||||
import net.momirealms.customcrops.CustomCrops;
|
||||
import net.momirealms.customcrops.utils.Crop;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ConfigManager {
|
||||
|
||||
public static HashMap<String, Crop> CONFIG;
|
||||
static {
|
||||
CONFIG = new HashMap<>();
|
||||
}
|
||||
|
||||
public static class Config{
|
||||
|
||||
public static boolean res;
|
||||
@@ -14,7 +28,6 @@ public class ConfigManager {
|
||||
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;
|
||||
@@ -56,8 +69,6 @@ public class ConfigManager {
|
||||
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;
|
||||
@@ -69,6 +80,7 @@ public class ConfigManager {
|
||||
|
||||
CustomCrops.instance.reloadConfig();
|
||||
FileConfiguration configuration = CustomCrops.instance.getConfig();
|
||||
cropLoad();
|
||||
|
||||
//处理配置
|
||||
Config.res = configuration.getBoolean("integration.residence");
|
||||
@@ -77,12 +89,9 @@ public class ConfigManager {
|
||||
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");
|
||||
@@ -130,5 +139,54 @@ public class ConfigManager {
|
||||
Config.autumn = configuration.getString("messages.autumn");
|
||||
Config.winter = configuration.getString("messages.winter");
|
||||
}
|
||||
|
||||
/*
|
||||
根据文件名获取配置文件
|
||||
*/
|
||||
public static YamlConfiguration getConfig(String configName) {
|
||||
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), configName);
|
||||
//文件不存在则生成默认配置
|
||||
if (!file.exists()) {
|
||||
CustomCrops.instance.saveResource(configName, false);
|
||||
}
|
||||
return YamlConfiguration.loadConfiguration(file);
|
||||
}
|
||||
|
||||
/*
|
||||
加载农作物数据
|
||||
*/
|
||||
public static void cropLoad(){
|
||||
try {
|
||||
CONFIG.clear();
|
||||
YamlConfiguration cropConfig = getConfig("crops.yml");
|
||||
Set<String> keys = cropConfig.getConfigurationSection("crops").getKeys(false);
|
||||
keys.forEach(key -> {
|
||||
double chance = cropConfig.getDouble("crops."+key+".grow-chance");
|
||||
Crop crop = new Crop(key, chance);
|
||||
if(cropConfig.getConfigurationSection("crops."+key).contains("return")){
|
||||
crop.setWillReturn(true);
|
||||
crop.setReturnStage(cropConfig.getString("crops."+key+".return"));
|
||||
}else {
|
||||
crop.setWillReturn(false);
|
||||
}
|
||||
if(cropConfig.getConfigurationSection("crops."+key).contains("season")){
|
||||
crop.setSeasons(StringUtils.split( cropConfig.getString("crops."+key+".season"), ","));
|
||||
}
|
||||
if(cropConfig.getConfigurationSection("crops."+key).contains("gigantic")){
|
||||
crop.setWillGiant(true);
|
||||
crop.setGiant(cropConfig.getString("crops."+key+".gigantic"));
|
||||
crop.setGiantChance(cropConfig.getDouble("crops."+key+".gigantic-chance"));
|
||||
}else {
|
||||
crop.setWillGiant(false);
|
||||
}
|
||||
CONFIG.put(key, crop);
|
||||
});
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
CustomCrops.instance.getLogger().warning("crops.yml加载失败!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,11 @@
|
||||
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.MessageManager;
|
||||
import net.momirealms.customcrops.utils.Crop;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@@ -21,90 +19,43 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class CropManager {
|
||||
|
||||
public static ConcurrentHashMap<Location, String> instances;
|
||||
public static ConcurrentHashMap<Location, String> CROPS;
|
||||
/*
|
||||
开服的时候将文件的数据读入
|
||||
*/
|
||||
public static void loadData() {
|
||||
|
||||
//4W性能测试
|
||||
public static void testData_1(){
|
||||
for(int i = -100; i < 100; i++){
|
||||
for(int j = -100; j < 100; j++){
|
||||
Location tempLoc = new Location(Bukkit.getWorld("world"),i,100,j);
|
||||
String name = "spring";
|
||||
instances.put(tempLoc, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
//20W性能测试
|
||||
public static void testData_2(){
|
||||
for(int i = -100000; i < 100000; i++){
|
||||
Location tempLoc = new Location(Bukkit.getWorld("world"),i,100,i);
|
||||
String name = "spring";
|
||||
instances.put(tempLoc, name);
|
||||
}
|
||||
}
|
||||
|
||||
//开服的时候将文件的数据读入
|
||||
public CropManager(FileConfiguration data) {
|
||||
FileConfiguration config = CustomCrops.instance.getConfig();
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml");
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
try {
|
||||
for (String world : config.getStringList("config.whitelist-worlds")) {
|
||||
CropManager.instances = new ConcurrentHashMap<Location, String>();
|
||||
if(data.getConfigurationSection(world) != null){
|
||||
for (String coordinate : data.getConfigurationSection(world).getKeys(false)) {
|
||||
Location tempLocation = new Location(Bukkit.getWorld(world), (double)Integer.parseInt(coordinate.split(",")[0]), (double)Integer.parseInt(coordinate.split(",")[1]), (double)Integer.parseInt(coordinate.split(",")[2]));
|
||||
String season = data.getString(world + "." + coordinate);
|
||||
CropManager.instances.put(tempLocation, season);
|
||||
}
|
||||
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
CROPS = new ConcurrentHashMap<>();
|
||||
|
||||
for (String world : ConfigManager.Config.worlds) {
|
||||
//如果数据文件中有相应世界才进行读取
|
||||
if(data.contains(world)){
|
||||
for (String coordinate : data.getConfigurationSection(world).getKeys(false)) {
|
||||
Location tempLoc = new Location(Bukkit.getWorld(world), Integer.parseInt(coordinate.split(",")[0]), Integer.parseInt(coordinate.split(",")[1]), Integer.parseInt(coordinate.split(",")[2]));
|
||||
String cropName = data.getString(world + "." + coordinate);
|
||||
CROPS.put(tempLoc, cropName);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
CropManager.instances = new ConcurrentHashMap<Location, String>();
|
||||
e.printStackTrace();
|
||||
}
|
||||
saveData();
|
||||
}
|
||||
|
||||
/*
|
||||
//根据世界名获取所有的农作物
|
||||
public static List<Location> getCrops(World world){
|
||||
FileConfiguration config = CustomCrops.instance.getConfig();
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml");
|
||||
FileConfiguration data;
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
List<Location> locations = new ArrayList<Location>();
|
||||
if (config.getStringList("config.whitelist-worlds").contains(world.getName())){
|
||||
if(data.contains(world.getName())){
|
||||
data.getConfigurationSection(world.getName()).getKeys(false).forEach(key ->{
|
||||
String[] string_list = key.split(",");
|
||||
if (config.getBoolean("config.only-grow-in-loaded-chunks")){
|
||||
if (world.isChunkLoaded(Integer.parseInt(string_list[0])/16, Integer.parseInt(string_list[2])/16)){
|
||||
locations.add(new Location(world, Double.parseDouble(string_list[0]),Double.parseDouble(string_list[1]),Double.parseDouble(string_list[2])));
|
||||
}
|
||||
}else {
|
||||
locations.add(new Location(world, Double.parseDouble(string_list[0]),Double.parseDouble(string_list[1]),Double.parseDouble(string_list[2])));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return locations;
|
||||
}
|
||||
保存数据
|
||||
*/
|
||||
//保存数据
|
||||
public static void saveData(){
|
||||
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml");
|
||||
FileConfiguration data;
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
if (CropManager.instances != null) {
|
||||
//性能更高
|
||||
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());
|
||||
}
|
||||
}
|
||||
else {
|
||||
CropManager.instances = new ConcurrentHashMap<Location, String>();
|
||||
Bukkit.getConsoleSender().sendMessage("错误:空数据");
|
||||
|
||||
//性能更高
|
||||
Set<Map.Entry<Location, String>> en = CROPS.entrySet();
|
||||
for(Map.Entry<Location, String> entry : en){
|
||||
Location loc = entry.getKey();
|
||||
data.set(loc.getWorld().getName()+"."+ loc.getBlockX() + "," + loc.getBlockY()+ ","+loc.getBlockZ(), entry.getValue());
|
||||
}
|
||||
try {
|
||||
data.save(file);
|
||||
@@ -114,209 +65,198 @@ public class CropManager {
|
||||
CustomCrops.instance.getLogger().warning("农作物数据保存出错");
|
||||
}
|
||||
}
|
||||
public static void putInstance(Location location, String season) {
|
||||
CropManager.instances.put(location, season);
|
||||
|
||||
/*
|
||||
添加农作物实例
|
||||
*/
|
||||
public static void putInstance(Location location, String crop) {
|
||||
CROPS.put(location, crop);
|
||||
}
|
||||
//清理无效的农作物并生长
|
||||
|
||||
/*
|
||||
生长部分
|
||||
*/
|
||||
public static void CropGrow() {
|
||||
/*
|
||||
阶段1:更新数据
|
||||
*/
|
||||
*/
|
||||
long start1 = System.currentTimeMillis();
|
||||
FileConfiguration config = CustomCrops.instance.getConfig();
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), "crop-data.yml");
|
||||
FileConfiguration data;
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
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());
|
||||
}
|
||||
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
|
||||
BukkitScheduler bukkitScheduler = Bukkit.getScheduler();
|
||||
|
||||
Set<Map.Entry<Location, String>> en = CROPS.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());
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops] &7农作物数据更新耗时&a" + (finish1 - start1) + "&fms",Bukkit.getConsoleSender());
|
||||
|
||||
/*
|
||||
阶段2:清理数据内无效的农作物
|
||||
*/
|
||||
阶段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(tempLoc.getBlock().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])));
|
||||
|
||||
String[] coordinate = StringUtils.split(key,",");
|
||||
//先判断区块是否加载,未加载则不进行下一步计算
|
||||
if (world.isChunkLoaded(Integer.parseInt(coordinate[0])/16, Integer.parseInt(coordinate[2])/16)){
|
||||
|
||||
Location sLoc = new Location(world,Double.parseDouble(coordinate[0]),Double.parseDouble(coordinate[1]),Double.parseDouble(coordinate[2]));
|
||||
CustomBlock seedBlock = CustomBlock.byAlreadyPlaced(sLoc.getBlock());
|
||||
|
||||
if(seedBlock == null){
|
||||
CROPS.remove(sLoc);
|
||||
data.set(worldName+"."+coordinate[0]+","+coordinate[1]+","+coordinate[2], null);
|
||||
}else{
|
||||
String namespacedID = seedBlock.getNamespacedID();
|
||||
/*
|
||||
对之前旧版本的一些兼容
|
||||
以及一些意料之外的情况,防止报错
|
||||
*/
|
||||
if(namespacedID.equalsIgnoreCase(ConfigManager.Config.dead)){
|
||||
CROPS.remove(sLoc);
|
||||
data.set(worldName+"."+coordinate[0]+","+coordinate[1]+","+coordinate[2], null);
|
||||
return;
|
||||
}
|
||||
if(namespacedID.contains("_stage_")){
|
||||
|
||||
Location potLoc = sLoc.clone().subtract(0,1,0);
|
||||
Block potBlock = potLoc.getBlock();
|
||||
CustomBlock pot = CustomBlock.byAlreadyPlaced(potBlock);
|
||||
|
||||
if (pot != null){
|
||||
String potName = pot.getNamespacedID();
|
||||
/*
|
||||
是湿润的种植盆吗
|
||||
*/
|
||||
if (potName.equalsIgnoreCase(ConfigManager.Config.watered_pot)){
|
||||
|
||||
String[] split = StringUtils.split(namespacedID,":");
|
||||
String[] cropNameList = StringUtils.split(split[1],"_");
|
||||
Crop crop = ConfigManager.CONFIG.get(cropNameList[0]);
|
||||
|
||||
//季节判断
|
||||
Label_out:
|
||||
if(ConfigManager.Config.season){
|
||||
if(ConfigManager.Config.greenhouse){
|
||||
for(int i = 1; i <= ConfigManager.Config.range; i++){
|
||||
CustomBlock cb = CustomBlock.byAlreadyPlaced(sLoc.clone().add(0,i,0).getBlock());
|
||||
if (cb != null){
|
||||
if(cb.getNamespacedID().equalsIgnoreCase(ConfigManager.Config.glass)){
|
||||
break Label_out;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean ws = true;
|
||||
for(String season : crop.getSeasons()){
|
||||
if (Objects.equals(season, ConfigManager.Config.current)) {
|
||||
ws = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(ws){
|
||||
CROPS.remove(sLoc);
|
||||
data.set(worldName+"."+coordinate[0]+","+coordinate[1]+","+coordinate[2], null);
|
||||
bukkitScheduler.callSyncMethod(CustomCrops.instance, () -> {
|
||||
CustomBlock.remove(sLoc);
|
||||
CustomBlock.place(ConfigManager.Config.dead, sLoc);
|
||||
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(potLoc);
|
||||
CustomBlock.place(ConfigManager.Config.pot, potLoc);
|
||||
if(Math.random()< crop.getChance()){
|
||||
CustomBlock.remove(sLoc);
|
||||
CustomBlock.place(split[0] + ":" + cropNameList[0] + "_stage_" + nextStage, sLoc);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
//巨大化判断
|
||||
else if(crop.getWillGiant()){
|
||||
bukkitScheduler.callSyncMethod(CustomCrops.instance, () ->{
|
||||
CustomBlock.remove(potLoc);
|
||||
CustomBlock.place(ConfigManager.Config.pot, potLoc);
|
||||
if(crop.getGiantChance() > Math.random()){
|
||||
CustomBlock.remove(sLoc);
|
||||
CustomBlock.place(crop.getGiant(), sLoc);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
/*
|
||||
是干燥的种植盆吗
|
||||
*/
|
||||
else if(potName.equalsIgnoreCase(ConfigManager.Config.pot)){
|
||||
if(ConfigManager.Config.season) {
|
||||
if(ConfigManager.Config.greenhouse){
|
||||
for(int i = 1; i <= ConfigManager.Config.range; i++){
|
||||
CustomBlock cb = CustomBlock.byAlreadyPlaced(sLoc.clone().add(0,i,0).getBlock());
|
||||
if (cb != null){
|
||||
if(cb.getNamespacedID().equalsIgnoreCase(ConfigManager.Config.glass)){
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean ws = true;
|
||||
Crop crop = ConfigManager.CONFIG.get(StringUtils.split(StringUtils.split(namespacedID,":")[1],"_")[0]);
|
||||
for (String season : crop.getSeasons()) {
|
||||
if (Objects.equals(season, ConfigManager.Config.current)) {
|
||||
ws = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ws) {
|
||||
CROPS.remove(sLoc);
|
||||
data.set(worldName+"."+coordinate[0]+","+coordinate[1]+","+coordinate[2], null);
|
||||
bukkitScheduler.callSyncMethod(CustomCrops.instance, () -> {
|
||||
CustomBlock.remove(sLoc);
|
||||
CustomBlock.place(ConfigManager.Config.dead, sLoc);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
CROPS.remove(sLoc);
|
||||
data.set(worldName+"."+coordinate[0]+","+coordinate[1]+","+coordinate[2], null);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
long finish2 = System.currentTimeMillis();
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f农作物缓存清理耗时&a" + String.valueOf(finish2-start2) + "&fms",Bukkit.getConsoleSender());
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops] &7农作物生长耗时&a" + (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 = seedLocation.getBlock();
|
||||
Block potBlock = potLocation.getBlock();
|
||||
|
||||
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(tempLocation.getBlock()) != null){
|
||||
if(CustomBlock.byAlreadyPlaced(tempLocation.getBlock()).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(tempLocation.getBlock()) != null){
|
||||
if(CustomBlock.byAlreadyPlaced(tempLocation.getBlock()).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)){
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
try{
|
||||
data.save(file);
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
CustomCrops.instance.getLogger().warning("农作物缓存清理保存出错!");
|
||||
}
|
||||
long finish3 = System.currentTimeMillis();
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops] &7农作物数据保存耗时&a" + (finish3 - start3) + "&fms",Bukkit.getConsoleSender());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package net.momirealms.customcrops;
|
||||
package net.momirealms.customcrops.datamanager;
|
||||
|
||||
import net.momirealms.customcrops.Libs.minedown.MineDown;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class MessageManager {
|
||||
public static void consoleMessage(String s, CommandSender sender) {
|
||||
sender.spigot().sendMessage(MineDown.parse(s));
|
||||
}
|
||||
public static void consoleMessage(String s, CommandSender sender) { sender.spigot().sendMessage(MineDown.parse(s)); }
|
||||
public static void playerMessage(String s, Player player){
|
||||
player.spigot().sendMessage(MineDown.parse(s));
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package net.momirealms.customcrops.datamanager;
|
||||
|
||||
import net.momirealms.customcrops.ConfigManager;
|
||||
import net.momirealms.customcrops.CustomCrops;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
@@ -8,7 +7,6 @@ import java.util.Objects;
|
||||
|
||||
public class NextSeason {
|
||||
public static void changeSeason(){
|
||||
|
||||
FileConfiguration config = CustomCrops.instance.getConfig();
|
||||
String currentSeason = ConfigManager.Config.current;
|
||||
String nextSeason = switch (Objects.requireNonNull(currentSeason)) {
|
||||
@@ -26,4 +24,4 @@ public class NextSeason {
|
||||
CustomCrops.instance.getLogger().warning("季节配置文件出错!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
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.IAFurniture;
|
||||
import net.momirealms.customcrops.MessageManager;
|
||||
import net.momirealms.customcrops.utils.IAFurniture;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
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 org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -19,83 +18,38 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class SprinklerManager {
|
||||
|
||||
public static ConcurrentHashMap<Location, String> instances;
|
||||
public static ConcurrentHashMap<Location, String> SPRINKLERS;
|
||||
/*
|
||||
开服的时候将文件的数据读入
|
||||
*/
|
||||
public static void loadData(){
|
||||
|
||||
//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);
|
||||
}
|
||||
}
|
||||
SPRINKLERS = new ConcurrentHashMap<>();
|
||||
|
||||
//开服的时候将文件的数据读入
|
||||
public SprinklerManager(FileConfiguration data) {
|
||||
|
||||
FileConfiguration config = CustomCrops.instance.getConfig();
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml");
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
try {
|
||||
for (String world : config.getStringList("config.whitelist-worlds")) {
|
||||
SprinklerManager.instances = new ConcurrentHashMap<Location, String>();
|
||||
if(data.getConfigurationSection(world) != null){
|
||||
for (String coordinate : data.getConfigurationSection(world).getKeys(false)) {
|
||||
Location tempLocation = new Location(Bukkit.getWorld(world), Integer.parseInt(coordinate.split(",")[0]), Integer.parseInt(coordinate.split(",")[1]), Integer.parseInt(coordinate.split(",")[2]));
|
||||
String type = data.getString(world + "." + coordinate);
|
||||
SprinklerManager.instances.put(tempLocation, type);
|
||||
}
|
||||
for (String world : ConfigManager.Config.worlds) {
|
||||
if(data.getConfigurationSection(world) != null){
|
||||
for (String coordinate : data.getConfigurationSection(world).getKeys(false)) {
|
||||
Location tempLocation = new Location(Bukkit.getWorld(world), Integer.parseInt(coordinate.split(",")[0]), Integer.parseInt(coordinate.split(",")[1]), Integer.parseInt(coordinate.split(",")[2]));
|
||||
String type = data.getString(world + "." + coordinate);
|
||||
SPRINKLERS.put(tempLocation, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
SprinklerManager.instances = new ConcurrentHashMap<Location, String>();
|
||||
e.printStackTrace();
|
||||
}
|
||||
saveData();
|
||||
}
|
||||
/*
|
||||
//根据世界名获取所有的洒水器
|
||||
public static List<Location> getSprinklers(World world){
|
||||
FileConfiguration config = CustomCrops.instance.getConfig();
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml");
|
||||
FileConfiguration data;
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
List<Location> locations = new ArrayList<Location>();
|
||||
if (config.getStringList("config.whitelist-worlds").contains(world.getName())){
|
||||
if(data.contains(world.getName())){
|
||||
data.getConfigurationSection(world.getName()).getKeys(false).forEach(key ->{
|
||||
String[] string_list = key.split(",");
|
||||
//只返回被加载的区块中的洒水器坐标
|
||||
if (config.getBoolean("config.only-grow-in-loaded-chunks")){
|
||||
if (world.isChunkLoaded(Integer.parseInt(string_list[0])/16, Integer.parseInt(string_list[2])/16)){
|
||||
locations.add(new Location(world, Double.parseDouble(string_list[0]),Double.parseDouble(string_list[1]),Double.parseDouble(string_list[2])));
|
||||
}
|
||||
}else {
|
||||
locations.add(new Location(world, Double.parseDouble(string_list[0]),Double.parseDouble(string_list[1]),Double.parseDouble(string_list[2])));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return locations;
|
||||
}
|
||||
*/
|
||||
//保存数据
|
||||
保存数据
|
||||
*/
|
||||
public static void saveData(){
|
||||
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml");
|
||||
FileConfiguration data;
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
if (SprinklerManager.instances != null) {
|
||||
//性能更高
|
||||
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());
|
||||
}
|
||||
}
|
||||
else {
|
||||
SprinklerManager.instances = new ConcurrentHashMap<Location, String>();
|
||||
Bukkit.getConsoleSender().sendMessage("错误:空数据");
|
||||
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
Set<Map.Entry<Location, String>> en = SPRINKLERS.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());
|
||||
}
|
||||
try {
|
||||
data.save(file);
|
||||
@@ -106,8 +60,9 @@ public class SprinklerManager {
|
||||
}
|
||||
}
|
||||
public static void putInstance(Location location, String type) {
|
||||
SprinklerManager.instances.put(location, type);
|
||||
SPRINKLERS.put(location, type);
|
||||
}
|
||||
|
||||
public static void SprinklerWork() {
|
||||
/*
|
||||
阶段1:更新数据
|
||||
@@ -116,100 +71,71 @@ public class SprinklerManager {
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml");
|
||||
FileConfiguration data;
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
if (SprinklerManager.instances != null) {
|
||||
//性能更高
|
||||
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());
|
||||
}
|
||||
BukkitScheduler bukkitScheduler = Bukkit.getScheduler();
|
||||
|
||||
Set<Map.Entry<Location, String>> en = SPRINKLERS.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());
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops] &7洒水器数据更新耗时&a" + String.valueOf(finish1-start1) + "&fms",Bukkit.getConsoleSender());
|
||||
/*
|
||||
阶段2:清理数据内无效的农作物
|
||||
*/
|
||||
Bukkit.getScheduler().callSyncMethod(CustomCrops.instance,()->{
|
||||
阶段2:清理数据内无效的洒水器并工作
|
||||
*/
|
||||
bukkitScheduler.callSyncMethod(CustomCrops.instance,()->{
|
||||
long start2 = System.currentTimeMillis();
|
||||
//map不能一边循环一边删除
|
||||
//创建一个新的HashSet,用作循环
|
||||
//检测碰撞体积需要同步
|
||||
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])+0.5,Double.parseDouble(string_list[1])+0.5,Double.parseDouble(string_list[2])+0.5);
|
||||
String[] coordinate = StringUtils.split(key,",");
|
||||
if (world.isChunkLoaded(Integer.parseInt(coordinate[0])/16, Integer.parseInt(coordinate[2])/16)){
|
||||
Location tempLoc = new Location(world,Double.parseDouble(coordinate[0])+0.5,Double.parseDouble(coordinate[1])+0.5,Double.parseDouble(coordinate[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);
|
||||
SPRINKLERS.remove(tempLoc);
|
||||
data.set(worldName+"."+coordinate[0]+","+coordinate[1]+","+coordinate[2], null);
|
||||
}else {
|
||||
locations.add(new Location(world, Double.parseDouble(string_list[0]),Double.parseDouble(string_list[1]),Double.parseDouble(string_list[2])));
|
||||
String type = data.getString(worldName + "." + coordinate[0] + "," + coordinate[1] + "," + coordinate[2]);
|
||||
if(type.equals("s1")){
|
||||
for(int i = -1; i <= 1;i++){
|
||||
for (int j = -1; j <= 1; j++){
|
||||
waterPot(tempLoc.clone().add(i,-1,j));
|
||||
}
|
||||
}
|
||||
}else{
|
||||
for(int i = -2; i <= 2;i++){
|
||||
for (int j = -2; j <= 2; j++){
|
||||
waterPot(tempLoc.clone().add(i,-1,j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
/*
|
||||
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])));
|
||||
}
|
||||
}
|
||||
*/
|
||||
long finish2 = System.currentTimeMillis();
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f洒水器缓存清理耗时&a" + String.valueOf(finish2-start2) + "&fms",Bukkit.getConsoleSender());
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops] &7洒水器工作耗时&a" + String.valueOf(finish2-start2) + "&fms",Bukkit.getConsoleSender());
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.instance,()->{
|
||||
bukkitScheduler.runTaskAsynchronously(CustomCrops.instance,()->{
|
||||
/*
|
||||
阶段3:保存数据
|
||||
*/
|
||||
long start3 = System.currentTimeMillis();
|
||||
try{
|
||||
data.save(file);
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
CustomCrops.instance.getLogger().warning("洒水器缓存清理保存出错!");
|
||||
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());
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops] &7洒水器数据保存耗时&a" + String.valueOf(finish3-start3) + "&fms",Bukkit.getConsoleSender());
|
||||
});
|
||||
return null;
|
||||
});
|
||||
}
|
||||
private static void waterPot(Location tempLoc, World world) {
|
||||
private static void waterPot(Location tempLoc) {
|
||||
if(CustomBlock.byAlreadyPlaced(tempLoc.getBlock()) != null){
|
||||
if(CustomBlock.byAlreadyPlaced(tempLoc.getBlock()).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.pot)){
|
||||
Bukkit.getScheduler().callSyncMethod(CustomCrops.instance,()->{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.momirealms.customcrops.integrations;
|
||||
|
||||
import net.momirealms.customcrops.ConfigManager;
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
||||
@@ -12,19 +12,15 @@ import org.bukkit.entity.Player;
|
||||
|
||||
public class WorldGuardIntegrations {
|
||||
public static boolean checkWGBuild(Location loc,Player player){
|
||||
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
|
||||
RegionQuery query = container.createQuery();
|
||||
|
||||
return !query.testState(BukkitAdapter.adapt(loc), localPlayer, Flags.BUILD);
|
||||
}
|
||||
public static boolean checkWGHarvest(Location loc,Player player){
|
||||
|
||||
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
|
||||
RegionQuery query = container.createQuery();
|
||||
|
||||
return !query.testState(BukkitAdapter.adapt(loc), localPlayer, Flags.BLOCK_BREAK);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.momirealms.customcrops.datamanager;
|
||||
package net.momirealms.customcrops.limits;
|
||||
|
||||
import dev.lone.itemsadder.api.CustomBlock;
|
||||
import net.momirealms.customcrops.ConfigManager;
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
|
||||
@@ -28,7 +28,7 @@ public class MaxCropsPerChunk {
|
||||
square.add(0.0, 1.0, 0.0);
|
||||
Block b = square.getBlock();
|
||||
if(CustomBlock.byAlreadyPlaced(b)!= null){
|
||||
if (CustomBlock.byAlreadyPlaced(b).getNamespacedID().contains("stage")) {
|
||||
if (CustomBlock.byAlreadyPlaced(b).getNamespacedID().contains("_stage_")) {
|
||||
if (n++ > maxAmount) {
|
||||
break Label_out;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.momirealms.customcrops.datamanager;
|
||||
package net.momirealms.customcrops.limits;
|
||||
|
||||
import net.momirealms.customcrops.ConfigManager;
|
||||
import net.momirealms.customcrops.IAFurniture;
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import net.momirealms.customcrops.utils.IAFurniture;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package net.momirealms.customcrops.listener;
|
||||
|
||||
import dev.lone.itemsadder.api.CustomStack;
|
||||
import net.momirealms.customcrops.ConfigManager;
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntitySpawnEvent;
|
||||
|
||||
public class BreakFurniture implements Listener {
|
||||
public class BreakCrops implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void breakFurniture(EntitySpawnEvent event){
|
||||
public void breakCrops(EntitySpawnEvent event){
|
||||
Entity entity = event.getEntity();
|
||||
if(!(entity instanceof Item)) return;
|
||||
if(CustomStack.byItemStack(((Item) entity).getItemStack()) != null){
|
||||
@@ -27,4 +27,4 @@ public class BreakFurniture implements Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package net.momirealms.customcrops.listener;
|
||||
|
||||
import dev.lone.itemsadder.api.CustomBlock;
|
||||
import dev.lone.itemsadder.api.Events.CustomBlockBreakEvent;
|
||||
import net.momirealms.customcrops.ConfigManager;
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
@@ -17,24 +17,29 @@ public class BreakCustomBlock implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void breakCustomBlock(CustomBlockBreakEvent event){
|
||||
Player player =event.getPlayer();
|
||||
Location location = event.getBlock().getLocation();
|
||||
|
||||
String namespacedId = event.getNamespacedID();
|
||||
|
||||
if(namespacedId.contains("stage")){
|
||||
//用于防止玩家使用精准采集获取生长阶段物品
|
||||
if(namespacedId.contains("_stage_")){
|
||||
Player player =event.getPlayer();
|
||||
if(player.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH) || player.getInventory().getItemInMainHand().getType() == Material.SHEARS){
|
||||
event.setCancelled(true);
|
||||
Location location = event.getBlock().getLocation();
|
||||
CustomBlock.place(namespacedId, location);
|
||||
CustomBlock.byAlreadyPlaced(location.getBlock()).getLoot().forEach(itemStack -> {
|
||||
location.getWorld().dropItem(location.clone().add(0.5,0.2,0.5), itemStack);
|
||||
CustomBlock.remove(location);
|
||||
});
|
||||
}
|
||||
}else if(namespacedId.equalsIgnoreCase(ConfigManager.Config.watered_pot) || namespacedId.equalsIgnoreCase(ConfigManager.Config.pot)){
|
||||
}
|
||||
//玩家破坏种植盆也会使得农作物掉落
|
||||
else if(namespacedId.equalsIgnoreCase(ConfigManager.Config.watered_pot) || namespacedId.equalsIgnoreCase(ConfigManager.Config.pot)){
|
||||
Location location = event.getBlock().getLocation();
|
||||
World world = location.getWorld();
|
||||
Block blockUp = location.add(0,1,0).getBlock();
|
||||
if(CustomBlock.byAlreadyPlaced(blockUp) != null){
|
||||
if(CustomBlock.byAlreadyPlaced(blockUp).getNamespacedID().contains("stage")){
|
||||
if(CustomBlock.byAlreadyPlaced(blockUp).getNamespacedID().contains("_stage_")){
|
||||
for (ItemStack itemStack : CustomBlock.byAlreadyPlaced(blockUp).getLoot()) {
|
||||
world.dropItem(location.clone().add(0.5, 0.2, 0.5), itemStack);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package net.momirealms.customcrops.listener;
|
||||
|
||||
import dev.lone.itemsadder.api.CustomStack;
|
||||
import net.momirealms.customcrops.ConfigManager;
|
||||
import net.momirealms.customcrops.datamanager.MaxSprinklersPerChunk;
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import net.momirealms.customcrops.limits.MaxSprinklersPerChunk;
|
||||
import net.momirealms.customcrops.datamanager.SprinklerManager;
|
||||
import net.momirealms.customcrops.IAFurniture;
|
||||
import net.momirealms.customcrops.utils.IAFurniture;
|
||||
import net.momirealms.customcrops.integrations.IntegrationCheck;
|
||||
import net.momirealms.customcrops.MessageManager;
|
||||
import net.momirealms.customcrops.datamanager.MessageManager;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
@@ -49,13 +49,13 @@ public class RightClickBlock implements Listener {
|
||||
if(hasWater){
|
||||
|
||||
if(namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_1) ||
|
||||
namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_2) ||
|
||||
namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_3)) {
|
||||
if(customStack.getMaxDurability() == customStack.getDurability()){
|
||||
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.can_full,player);
|
||||
}else {
|
||||
customStack.setDurability(customStack.getDurability() + 1);
|
||||
player.getWorld().playSound(player.getLocation(),Sound.ITEM_BUCKET_FILL,1,1);
|
||||
namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_2) ||
|
||||
namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_3)) {
|
||||
if(customStack.getMaxDurability() == customStack.getDurability()){
|
||||
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.can_full, player);
|
||||
}else {
|
||||
customStack.setDurability(customStack.getDurability() + 1);
|
||||
player.getWorld().playSound(player.getLocation(),Sound.ITEM_BUCKET_FILL,1,1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,17 @@ package net.momirealms.customcrops.listener;
|
||||
import dev.lone.itemsadder.api.CustomBlock;
|
||||
import dev.lone.itemsadder.api.CustomStack;
|
||||
import dev.lone.itemsadder.api.Events.CustomBlockInteractEvent;
|
||||
import net.momirealms.customcrops.ConfigManager;
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import net.momirealms.customcrops.CustomCrops;
|
||||
import net.momirealms.customcrops.datamanager.CropManager;
|
||||
import net.momirealms.customcrops.datamanager.MaxCropsPerChunk;
|
||||
import net.momirealms.customcrops.limits.MaxCropsPerChunk;
|
||||
import net.momirealms.customcrops.integrations.IntegrationCheck;
|
||||
import net.momirealms.customcrops.MessageManager;
|
||||
import net.momirealms.customcrops.datamanager.MessageManager;
|
||||
import net.momirealms.customcrops.utils.Crop;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -25,36 +25,32 @@ import java.util.Objects;
|
||||
|
||||
public class RightClickCustomBlock implements Listener {
|
||||
|
||||
BukkitScheduler bukkitScheduler = Bukkit.getScheduler();
|
||||
|
||||
@EventHandler
|
||||
public void rightClickCustomCrop(CustomBlockInteractEvent event){
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
|
||||
return;
|
||||
}
|
||||
//获取配置文件
|
||||
Player player = event.getPlayer();
|
||||
public void rightClickCustomBlock(CustomBlockInteractEvent event){
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
|
||||
|
||||
Block clickedBlock = event.getBlockClicked();
|
||||
Location clickedBlockLocation = clickedBlock.getLocation();
|
||||
/*
|
||||
手里无物品,则进行收获判断
|
||||
手里有物品,则进行浇水,骨粉,种植判断
|
||||
*/
|
||||
//手无物品部分
|
||||
//空手部分
|
||||
if (event.getItem() == null) {
|
||||
CustomBlock clickedCustomBlock = CustomBlock.byAlreadyPlaced(clickedBlock);
|
||||
if(clickedCustomBlock == null) return;
|
||||
Player player = event.getPlayer();
|
||||
//兼容性检测
|
||||
if(!IntegrationCheck.HarvestCheck(clickedBlockLocation, player)){
|
||||
//获取点击方块的命名空间与ID
|
||||
String namespacedID = clickedCustomBlock.getNamespacedID();
|
||||
String namespacedID = event.getNamespacedID();
|
||||
//如果ID内有stage则进行下一步
|
||||
if (namespacedID.contains("stage")){
|
||||
if (namespacedID.contains("_stage_")){
|
||||
//是否为枯萎植物
|
||||
if(namespacedID.equalsIgnoreCase(ConfigManager.Config.dead)) return;
|
||||
//String namespace = clickedCustomBlock.getNamespacedID().split(":")[0];
|
||||
String[] split = StringUtils.split(namespacedID,":");
|
||||
//String[] cropNameList = clickedCustomBlock.getNamespacedID().split(":")[1].split("_");
|
||||
String[] cropNameList = StringUtils.split(split[1],"_");
|
||||
|
||||
int nextStage = Integer.parseInt(cropNameList[2]) + 1;
|
||||
//农作物是否存在下一阶段
|
||||
if (CustomBlock.getInstance(split[0] + ":" + cropNameList[0] + "_stage_" + nextStage) == null) {
|
||||
@@ -63,33 +59,27 @@ public class RightClickCustomBlock implements Listener {
|
||||
clickedCustomBlock.getLoot().forEach(itemStack -> clickedBlockLocation.getWorld().dropItem(clickedBlockLocation.clone().add(0.5,0.2,0.5),itemStack));
|
||||
CustomBlock.remove(clickedBlockLocation);
|
||||
//如果配置文件中有return项目则放置方块
|
||||
FileConfiguration config = CustomCrops.instance.getConfig();
|
||||
|
||||
if(config.getConfigurationSection("crops." + cropNameList[0]).getKeys(false).contains("return")){
|
||||
CustomBlock.place(config.getString("crops." + cropNameList[0] + ".return"), clickedBlockLocation);
|
||||
Crop crop = ConfigManager.CONFIG.get(cropNameList[0]);
|
||||
if(crop.getWillReturn()){
|
||||
CustomBlock.place(crop.getReturnStage(), clickedBlockLocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
//非空手状态
|
||||
//右键的不是自定义方块返回
|
||||
CustomBlock clickedCustomBlock = CustomBlock.byAlreadyPlaced(clickedBlock);
|
||||
if(clickedCustomBlock == null) return;
|
||||
|
||||
//获取右键物品的namespaceID
|
||||
String namespacedId = clickedCustomBlock.getNamespacedID();
|
||||
|
||||
String namespacedId = event.getNamespacedID();
|
||||
/*
|
||||
右键的是特殊作物吗
|
||||
*/
|
||||
if (namespacedId.contains("stage")) {
|
||||
//下方方块不是自定义方块则返回
|
||||
World world = player.getWorld();
|
||||
Player player = event.getPlayer();
|
||||
Block blockUnder = clickedBlockLocation.clone().subtract(0,1,0).getBlock();
|
||||
if (CustomBlock.byAlreadyPlaced(blockUnder) == null) return;
|
||||
CustomBlock cb = CustomBlock.byAlreadyPlaced(blockUnder);
|
||||
if (cb == null) return;
|
||||
//检测右键的方块下方是否为干燥的种植盆方块
|
||||
if (CustomBlock.byAlreadyPlaced(blockUnder).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.pot)) {
|
||||
if (cb.getNamespacedID().equalsIgnoreCase(ConfigManager.Config.pot)) {
|
||||
//获取手中的物品
|
||||
ItemStack mainHandItem = player.getInventory().getItemInMainHand();
|
||||
Location locUnder = clickedBlockLocation.clone().subtract(0,1,0);
|
||||
@@ -107,18 +97,14 @@ public class RightClickCustomBlock implements Listener {
|
||||
}
|
||||
}
|
||||
//检测右键的方块下方是否为湿润的种植盆方块
|
||||
else if(CustomBlock.byAlreadyPlaced(blockUnder).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.watered_pot)){
|
||||
else if(cb.getNamespacedID().equalsIgnoreCase(ConfigManager.Config.watered_pot)){
|
||||
//获取手中的物品
|
||||
ItemStack mainHandItem = player.getInventory().getItemInMainHand();
|
||||
//如果是骨粉
|
||||
if (mainHandItem.getType() == Material.BONE_MEAL){
|
||||
//植物是否具有stage属性
|
||||
if (clickedCustomBlock.getNamespacedID().contains("stage")){
|
||||
//获取点击方块的命名空间与ID
|
||||
String namespacedID = clickedCustomBlock.getNamespacedID();
|
||||
//String namespace = clickedCustomBlock.getNamespacedID().split(":")[0];
|
||||
String[] split = StringUtils.split(namespacedID,":");
|
||||
//String[] cropNameList = clickedCustomBlock.getNamespacedID().split(":")[1].split("_");
|
||||
if (namespacedId.contains("_stage_")){
|
||||
String[] split = StringUtils.split(namespacedId,":");
|
||||
String[] cropNameList = StringUtils.split(split[1],"_");
|
||||
//下一生长阶段
|
||||
int nextStage = Integer.parseInt(cropNameList[2]) + 1;
|
||||
@@ -127,6 +113,7 @@ public class RightClickCustomBlock implements Listener {
|
||||
if(player.getGameMode() != GameMode.CREATIVE){
|
||||
mainHandItem.setAmount(mainHandItem.getAmount() - 1);
|
||||
}
|
||||
World world = player.getWorld();
|
||||
//骨粉的成功率
|
||||
if (Math.random() < ConfigManager.Config.bone_chance){
|
||||
CustomBlock.remove(clickedBlockLocation);
|
||||
@@ -147,130 +134,125 @@ public class RightClickCustomBlock implements Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
右键的是种植盆吗
|
||||
*/
|
||||
else if (event.getBlockFace() == BlockFace.UP){
|
||||
//获取手中的物品
|
||||
ItemStack mainHandItem = player.getInventory().getItemInMainHand();
|
||||
|
||||
ItemStack item = event.getItem();
|
||||
Player player = event.getPlayer();
|
||||
//检测右键的方块是否为干燥的种植盆方块
|
||||
if (namespacedId.equalsIgnoreCase(ConfigManager.Config.pot)){
|
||||
//如果手中的是水桶,那么转干为湿
|
||||
|
||||
if (mainHandItem.getType() == Material.WATER_BUCKET){
|
||||
if (item.getType() == Material.WATER_BUCKET){
|
||||
//扣除水桶
|
||||
if(player.getGameMode() != GameMode.CREATIVE){
|
||||
mainHandItem.setAmount(mainHandItem.getAmount() - 1);
|
||||
item.setAmount(item.getAmount() - 1);
|
||||
player.getInventory().addItem(new ItemStack(Material.BUCKET));
|
||||
}
|
||||
CustomBlock.remove(clickedBlockLocation);
|
||||
CustomBlock.place(ConfigManager.Config.watered_pot,clickedBlockLocation);
|
||||
} else if (mainHandItem.getType() == Material.WOODEN_SWORD){
|
||||
waterPot(mainHandItem, player,clickedBlockLocation);
|
||||
} else {
|
||||
tryPlantSeed(clickedBlockLocation, mainHandItem, player);
|
||||
return;
|
||||
}
|
||||
CustomStack customStack = CustomStack.byItemStack(item);
|
||||
if (customStack != null){
|
||||
String namespacedID = customStack.getNamespacedID();
|
||||
if (namespacedID.endsWith("_seeds")){
|
||||
if(tryPlantSeed(clickedBlockLocation, namespacedID, player)){
|
||||
if(player.getGameMode() != GameMode.CREATIVE){
|
||||
item.setAmount(item.getAmount() -1);
|
||||
}
|
||||
}
|
||||
}else {
|
||||
waterPot(item, player, clickedBlockLocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
//检测右键的方块是否为湿润的种植盆方块
|
||||
else if(namespacedId.equalsIgnoreCase(ConfigManager.Config.watered_pot)){
|
||||
tryPlantSeed(clickedBlockLocation, mainHandItem, player);
|
||||
CustomStack customStack = CustomStack.byItemStack(item);
|
||||
if (customStack != null){
|
||||
String namespacedID = customStack.getNamespacedID();
|
||||
if (namespacedID.endsWith("_seeds")){
|
||||
if(tryPlantSeed(clickedBlockLocation, namespacedID, player)){
|
||||
if(player.getGameMode() != GameMode.CREATIVE){
|
||||
item.setAmount(item.getAmount() -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//尝试种植植物
|
||||
private void tryPlantSeed(Location clickedBlockLocation, ItemStack mainHandItem, Player player) {
|
||||
//是否为IA物品
|
||||
if(CustomStack.byItemStack(mainHandItem) == null) return;
|
||||
//获取命名空间名与ID
|
||||
String namespaced_id = CustomStack.byItemStack(mainHandItem).getNamespacedID();
|
||||
//是否为种子
|
||||
if (namespaced_id.endsWith("_seeds")){
|
||||
//获取农作物名
|
||||
String cropName = StringUtils.split(namespaced_id.replace("_seeds",""),":")[1];
|
||||
//String[] crop = CustomStack.byItemStack(mainHandItem).getNamespacedID().toLowerCase().replace("_seeds","").split(":");
|
||||
|
||||
//检测上方为空气
|
||||
if(clickedBlockLocation.clone().add(0, 1, 0).getBlock().getType() != Material.AIR){
|
||||
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() < ConfigManager.Config.minh || clickedBlockLocation.getY() > ConfigManager.Config.maxh){
|
||||
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.bad_place,player);
|
||||
return;
|
||||
}
|
||||
Location locUp = clickedBlockLocation.clone().add(0,1,0);
|
||||
//是否启用了季节
|
||||
Label_out:
|
||||
if(ConfigManager.Config.season){
|
||||
if(ConfigManager.Config.greenhouse){
|
||||
int range = ConfigManager.Config.range;
|
||||
World world = player.getWorld();
|
||||
for(int i = 1; i <= range; i++){
|
||||
Location tempLocation = locUp.clone().add(0,i,0);
|
||||
if (CustomBlock.byAlreadyPlaced(tempLocation.getBlock()) != null){
|
||||
if(CustomBlock.byAlreadyPlaced(tempLocation.getBlock()).getNamespacedID().equalsIgnoreCase(ConfigManager.Config.glass)){
|
||||
break Label_out;
|
||||
}
|
||||
private boolean tryPlantSeed(Location clickedBlockLocation, String namespacedID, Player player) {
|
||||
//获取农作物名
|
||||
String key = StringUtils.split(namespacedID.replace("_seeds",""),":")[1];
|
||||
//检测上方为空气
|
||||
if(clickedBlockLocation.clone().add(0, 1, 0).getBlock().getType() != Material.AIR){
|
||||
return false;
|
||||
}
|
||||
//该种子是否存在于配置文件中
|
||||
if(!ConfigManager.CONFIG.containsKey(key)){
|
||||
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.no_such_seed,player);
|
||||
return false;
|
||||
}
|
||||
//是否超高超低
|
||||
if (clickedBlockLocation.getY() < ConfigManager.Config.minh || clickedBlockLocation.getY() > ConfigManager.Config.maxh){
|
||||
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.bad_place,player);
|
||||
return false;
|
||||
}
|
||||
Location locUp = clickedBlockLocation.clone().add(0,1,0);
|
||||
//是否启用了季节
|
||||
Label_out:
|
||||
if(ConfigManager.Config.season){
|
||||
if(ConfigManager.Config.greenhouse){
|
||||
int range = ConfigManager.Config.range;
|
||||
for(int i = 1; i <= range; i++){
|
||||
CustomBlock cb = CustomBlock.byAlreadyPlaced(locUp.clone().add(0,i,0).getBlock());
|
||||
if (cb != null){
|
||||
if(cb.getNamespacedID().equalsIgnoreCase(ConfigManager.Config.glass)){
|
||||
break Label_out;
|
||||
}
|
||||
}
|
||||
}
|
||||
//获取种子适宜生长的季节
|
||||
String[] seasons = config.getString("crops."+cropName+".season").split(",");
|
||||
boolean wrongSeason = true;
|
||||
for(String season : seasons){
|
||||
if (Objects.equals(season, ConfigManager.Config.current)) {
|
||||
wrongSeason = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(wrongSeason){
|
||||
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.wrong_season,player);
|
||||
return;
|
||||
}
|
||||
//获取种子适宜生长的季节
|
||||
boolean ws = true;
|
||||
Crop crop = ConfigManager.CONFIG.get(key);
|
||||
for(String season : crop.getSeasons()){
|
||||
if (Objects.equals(season, ConfigManager.Config.current)) {
|
||||
ws = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//是否到达区块上限
|
||||
if(MaxCropsPerChunk.maxCropsPerChunk(clickedBlockLocation)){
|
||||
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.limit_crop.replace("{Max}", String.valueOf(ConfigManager.Config.max_crop)),player);
|
||||
return;
|
||||
if(ws){
|
||||
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.wrong_season,player);
|
||||
return false;
|
||||
}
|
||||
//添加到缓存中
|
||||
if(ConfigManager.Config.season){
|
||||
CropManager.putInstance(locUp, config.getString("crops."+cropName+".season"));
|
||||
}else{
|
||||
CropManager.putInstance(locUp, "all");
|
||||
}
|
||||
//减少种子数量
|
||||
if(player.getGameMode() != GameMode.CREATIVE){
|
||||
mainHandItem.setAmount(mainHandItem.getAmount() -1);
|
||||
}
|
||||
//放置自定义农作物
|
||||
CustomBlock.place(namespaced_id.replace("_seeds","_stage_1"),locUp);
|
||||
}
|
||||
//是否到达区块上限
|
||||
if(MaxCropsPerChunk.maxCropsPerChunk(clickedBlockLocation)){
|
||||
MessageManager.playerMessage(ConfigManager.Config.prefix + ConfigManager.Config.limit_crop.replace("{Max}", String.valueOf(ConfigManager.Config.max_crop)),player);
|
||||
return false;
|
||||
}
|
||||
//添加到缓存中
|
||||
CropManager.putInstance(locUp, key);
|
||||
//放置自定义农作物
|
||||
CustomBlock.place(namespacedID.replace("_seeds","_stage_1"),locUp);
|
||||
return true;
|
||||
}
|
||||
private void waterPot(ItemStack itemStack, Player player, Location location){
|
||||
//是否为IA物品
|
||||
if(CustomStack.byItemStack(itemStack) == null) return;
|
||||
|
||||
BukkitScheduler bukkitScheduler = Bukkit.getScheduler();
|
||||
|
||||
bukkitScheduler.runTaskAsynchronously(CustomCrops.instance,()-> {
|
||||
//获取IA物品
|
||||
CustomStack customStack = CustomStack.byItemStack(itemStack);
|
||||
String namespacedId = customStack.getNamespacedID();
|
||||
World world = player.getWorld();
|
||||
|
||||
int x;
|
||||
int z;
|
||||
|
||||
if (namespacedId.equalsIgnoreCase(ConfigManager.Config.watering_can_1)) {
|
||||
x = 0;
|
||||
z = 0;
|
||||
@@ -281,19 +263,14 @@ public class RightClickCustomBlock implements Listener {
|
||||
x = 4;
|
||||
z = 4;
|
||||
} else return;
|
||||
|
||||
//判断耐久度
|
||||
if(customStack.getDurability() > 0){
|
||||
CustomStack.byItemStack(itemStack).setDurability(CustomStack.byItemStack(itemStack).getDurability() - 1);
|
||||
}else return;
|
||||
//播放洒水音效
|
||||
|
||||
world.playSound(player.getLocation(),Sound.BLOCK_WATER_AMBIENT,1,1);
|
||||
//获取玩家朝向
|
||||
float yaw = player.getLocation().getYaw();
|
||||
|
||||
String wateredPot = ConfigManager.Config.watered_pot;
|
||||
String pot = ConfigManager.Config.pot;
|
||||
|
||||
//根据朝向确定浇水方向
|
||||
if (yaw <= 45 && yaw >= -135) {
|
||||
if (yaw > -45) {
|
||||
@@ -304,12 +281,13 @@ public class RightClickCustomBlock implements Listener {
|
||||
for (int i = 0; i <= x; i++) {
|
||||
for (int j = 0; j <= z; j++) {
|
||||
Location tempLoc = location.clone().add(i, 0, j);
|
||||
if(CustomBlock.byAlreadyPlaced(tempLoc.getBlock()) != null){
|
||||
if(CustomBlock.byAlreadyPlaced(tempLoc.getBlock()).getNamespacedID().equalsIgnoreCase(pot)){
|
||||
CustomBlock cb = CustomBlock.byAlreadyPlaced(tempLoc.getBlock());
|
||||
if(cb != null){
|
||||
if(cb.getNamespacedID().equalsIgnoreCase(ConfigManager.Config.pot)){
|
||||
//同步替换方块
|
||||
bukkitScheduler.callSyncMethod(CustomCrops.instance,()->{
|
||||
CustomBlock.remove(tempLoc);
|
||||
CustomBlock.place(wateredPot,tempLoc);
|
||||
CustomBlock.place(ConfigManager.Config.watered_pot,tempLoc);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
@@ -325,12 +303,13 @@ public class RightClickCustomBlock implements Listener {
|
||||
for (int i = 0; i <= x; i++) {
|
||||
for (int j = 0; j <= z; j++) {
|
||||
Location tempLoc = location.clone().subtract(i, 0, j);
|
||||
if(CustomBlock.byAlreadyPlaced(tempLoc.getBlock()) != null){
|
||||
if(CustomBlock.byAlreadyPlaced(tempLoc.getBlock()).getNamespacedID().equalsIgnoreCase(pot)){
|
||||
CustomBlock cb = CustomBlock.byAlreadyPlaced(tempLoc.getBlock());
|
||||
if(cb != null){
|
||||
if(cb.getNamespacedID().equalsIgnoreCase(ConfigManager.Config.pot)){
|
||||
//同步替换方块
|
||||
bukkitScheduler.callSyncMethod(CustomCrops.instance,()->{
|
||||
CustomBlock.remove(tempLoc);
|
||||
CustomBlock.place(wateredPot,tempLoc);
|
||||
CustomBlock.place(ConfigManager.Config.watered_pot,tempLoc);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.momirealms.customcrops.crops;
|
||||
package net.momirealms.customcrops.timer;
|
||||
|
||||
import net.momirealms.customcrops.CustomCrops;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.momirealms.customcrops.crops;
|
||||
package net.momirealms.customcrops.timer;
|
||||
|
||||
import net.momirealms.customcrops.ConfigManager;
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import net.momirealms.customcrops.CustomCrops;
|
||||
import net.momirealms.customcrops.datamanager.CropManager;
|
||||
import net.momirealms.customcrops.datamanager.SprinklerManager;
|
||||
45
src/main/java/net/momirealms/customcrops/utils/Crop.java
Normal file
45
src/main/java/net/momirealms/customcrops/utils/Crop.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package net.momirealms.customcrops.utils;
|
||||
|
||||
public class Crop {
|
||||
|
||||
private final String key;
|
||||
private final double chance;
|
||||
private double giantChance;
|
||||
private String[] seasons;
|
||||
private boolean willReturn;
|
||||
private String returnStage;
|
||||
private boolean willGiant;
|
||||
private String giant;
|
||||
|
||||
public Crop(String key, double chance){
|
||||
this.key = key;
|
||||
this.chance = chance;
|
||||
}
|
||||
|
||||
public String getReturnStage(){
|
||||
return this.returnStage;
|
||||
}
|
||||
public String getGiant(){
|
||||
return this.giant;
|
||||
}
|
||||
public boolean getWillReturn(){
|
||||
return this.willReturn;
|
||||
}
|
||||
public boolean getWillGiant(){
|
||||
return this.willGiant;
|
||||
}
|
||||
public String[] getSeasons() {return this.seasons;}
|
||||
public double getChance() {return this.chance;}
|
||||
public double getGiantChance() { return this.giantChance; }
|
||||
|
||||
public void setWillReturn(boolean b){
|
||||
this.willReturn = b;
|
||||
}
|
||||
public void setReturnStage(String stage){
|
||||
this.returnStage = stage;
|
||||
}
|
||||
public void setSeasons(String[] seasons) { this.seasons = seasons; }
|
||||
public void setWillGiant(boolean b) { this.willGiant = b; }
|
||||
public void setGiant(String giant) {this.giant = giant; }
|
||||
public void setGiantChance(double giantChance) { this.giantChance = giantChance; }
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.momirealms.customcrops;
|
||||
package net.momirealms.customcrops.utils;
|
||||
|
||||
import dev.lone.itemsadder.api.CustomFurniture;
|
||||
import net.momirealms.customcrops.CustomCrops;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.momirealms.customcrops;
|
||||
package net.momirealms.customcrops.utils;
|
||||
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import net.momirealms.customcrops.datamanager.ConfigManager;
|
||||
import net.momirealms.customcrops.CustomCrops;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
Reference in New Issue
Block a user