mirror of
https://github.com/Xiao-MoMi/Custom-Crops.git
synced 2025-12-29 11:59:15 +00:00
1.3.0 beta3
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
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 org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@@ -17,11 +22,22 @@ public class SprinklerManager {
|
||||
|
||||
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) {
|
||||
|
||||
FileConfiguration config = CustomCrops.instance.getConfig();
|
||||
File file = new File(CustomCrops.instance.getDataFolder(), "sprinkler-data.yml");
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
try {
|
||||
for (String world : config.getStringList("config.whitelist-worlds")) {
|
||||
SprinklerManager.instances = new ConcurrentHashMap<Location, String>();
|
||||
@@ -40,6 +56,7 @@ public class SprinklerManager {
|
||||
}
|
||||
saveData();
|
||||
}
|
||||
/*
|
||||
//根据世界名获取所有的洒水器
|
||||
public static List<Location> getSprinklers(World world){
|
||||
FileConfiguration config = CustomCrops.instance.getConfig();
|
||||
@@ -64,6 +81,7 @@ public class SprinklerManager {
|
||||
}
|
||||
return locations;
|
||||
}
|
||||
*/
|
||||
//保存数据
|
||||
public static void saveData(){
|
||||
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) {
|
||||
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");
|
||||
FileConfiguration data;
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
//map不能一边循环一边删除
|
||||
//创建一个新的HashSet,用作循环
|
||||
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());
|
||||
}
|
||||
}
|
||||
long finish1 = System.currentTimeMillis();
|
||||
MessageManager.consoleMessage("&#ccfbff-#ef96c5&[CustomCrops|性能监测] &f洒水器数据更新耗时&a" + String.valueOf(finish1-start1) + "&fms",Bukkit.getConsoleSender());
|
||||
/*
|
||||
阶段2:清理数据内无效的农作物
|
||||
*/
|
||||
Bukkit.getScheduler().callSyncMethod(CustomCrops.instance,()->{
|
||||
Set<Location> key = new HashSet(instances.keySet());
|
||||
try{
|
||||
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);
|
||||
}
|
||||
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);
|
||||
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;
|
||||
});
|
||||
}
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user