9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-22 16:39:36 +00:00
This commit is contained in:
Xiao-MoMi
2022-07-21 13:47:14 +08:00
parent 99f941eed0
commit 1955677b06
5 changed files with 56 additions and 32 deletions

View File

@@ -73,6 +73,7 @@ public class ConfigReader {
public static boolean asyncCheck; public static boolean asyncCheck;
public static boolean enableLimit; public static boolean enableLimit;
public static boolean hasParticle; public static boolean hasParticle;
public static boolean rightClickHarvest;
public static int cropLimit; public static int cropLimit;
public static int sprinklerLimit; public static int sprinklerLimit;
public static int yMin; public static int yMin;
@@ -114,6 +115,7 @@ public class ConfigReader {
onlyLoadedGrow = !config.getBoolean("config.only-grow-in-loaded-chunks",true); onlyLoadedGrow = !config.getBoolean("config.only-grow-in-loaded-chunks",true);
allWorld = config.getBoolean("config.all-world-grow",false); allWorld = config.getBoolean("config.all-world-grow",false);
hasParticle = config.getBoolean("config.water-particles", true); hasParticle = config.getBoolean("config.water-particles", true);
rightClickHarvest = config.getBoolean("config.right-click-harvest", true);
//数量与高度限制 //数量与高度限制
enableLimit = config.getBoolean("config.limit.enable",true); enableLimit = config.getBoolean("config.limit.enable",true);

View File

@@ -30,7 +30,7 @@ public class Residence implements Integration {
ClaimedResidence res = com.bekvon.bukkit.residence.Residence.getInstance().getResidenceManager().getByLoc(location); ClaimedResidence res = com.bekvon.bukkit.residence.Residence.getInstance().getResidenceManager().getByLoc(location);
if(res!=null){ if(res!=null){
ResidencePermissions perms = res.getPermissions(); ResidencePermissions perms = res.getPermissions();
return perms.playerHas(player, Flags.build, true); return perms.playerHas(player, Flags.destroy, true);
} }
return true; return true;
} }
@@ -40,7 +40,7 @@ public class Residence implements Integration {
ClaimedResidence res = com.bekvon.bukkit.residence.Residence.getInstance().getResidenceManager().getByLoc(location); ClaimedResidence res = com.bekvon.bukkit.residence.Residence.getInstance().getResidenceManager().getByLoc(location);
if(res!=null){ if(res!=null){
ResidencePermissions perms = res.getPermissions(); ResidencePermissions perms = res.getPermissions();
return perms.playerHas(player, Flags.destroy, true); return perms.playerHas(player, Flags.build, true);
} }
return true; return true;
} }

View File

@@ -21,6 +21,7 @@ import dev.lone.itemsadder.api.CustomBlock;
import dev.lone.itemsadder.api.CustomStack; import dev.lone.itemsadder.api.CustomStack;
import dev.lone.itemsadder.api.Events.CustomBlockBreakEvent; import dev.lone.itemsadder.api.Events.CustomBlockBreakEvent;
import net.momirealms.customcrops.ConfigReader; import net.momirealms.customcrops.ConfigReader;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.datamanager.PotManager; import net.momirealms.customcrops.datamanager.PotManager;
import net.momirealms.customcrops.fertilizer.Fertilizer; import net.momirealms.customcrops.fertilizer.Fertilizer;
import net.momirealms.customcrops.fertilizer.QualityCrop; import net.momirealms.customcrops.fertilizer.QualityCrop;
@@ -28,6 +29,7 @@ import net.momirealms.customcrops.integrations.Integration;
import net.momirealms.customcrops.utils.CropInstance; import net.momirealms.customcrops.utils.CropInstance;
import net.momirealms.customcrops.utils.SimpleLocation; import net.momirealms.customcrops.utils.SimpleLocation;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
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;
@@ -46,7 +48,7 @@ public class BreakBlock implements Listener {
public void onBreak(CustomBlockBreakEvent event){ public void onBreak(CustomBlockBreakEvent event){
String namespacedId = event.getNamespacedID(); String namespacedId = event.getNamespacedID();
if(namespacedId.contains("_stage_")){ if(namespacedId.contains("_stage_")){
Player player =event.getPlayer(); Player player = event.getPlayer();
Location location = event.getBlock().getLocation(); Location location = event.getBlock().getLocation();
for (Integration integration : ConfigReader.Config.integration){ for (Integration integration : ConfigReader.Config.integration){
if(!integration.canBreak(location, player)) return; if(!integration.canBreak(location, player)) return;
@@ -64,35 +66,51 @@ public class BreakBlock implements Listener {
String[] cropNameList = StringUtils.split(StringUtils.split(namespacedId, ":")[1], "_"); String[] cropNameList = StringUtils.split(StringUtils.split(namespacedId, ":")[1], "_");
int nextStage = Integer.parseInt(cropNameList[2]) + 1; int nextStage = Integer.parseInt(cropNameList[2]) + 1;
if (CustomBlock.getInstance(StringUtils.chop(namespacedId) + nextStage) == null) { if (CustomBlock.getInstance(StringUtils.chop(namespacedId) + nextStage) == null) {
CropInstance cropInstance = ConfigReader.CROPS.get(cropNameList[0]);
ThreadLocalRandom current = ThreadLocalRandom.current(); Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.instance, ()-> {
int random = current.nextInt(cropInstance.getMin(), cropInstance.getMax() + 1); if (location.getBlock().getType() != Material.AIR) return;
Location itemLoc = location.clone().add(0.5,0.2,0.5); CropInstance cropInstance = ConfigReader.CROPS.get(cropNameList[0]);
World world = location.getWorld(); ThreadLocalRandom current = ThreadLocalRandom.current();
Fertilizer fertilizer = PotManager.Cache.get(SimpleLocation.fromLocation(location.clone().subtract(0,1,0))); int random = current.nextInt(cropInstance.getMin(), cropInstance.getMax() + 1);
if (fertilizer != null){ Location itemLoc = location.clone().add(0.5,0.2,0.5);
if (fertilizer instanceof QualityCrop qualityCrop){ World world = location.getWorld();
int[] weights = qualityCrop.getChance(); Fertilizer fertilizer = PotManager.Cache.get(SimpleLocation.fromLocation(location.clone().subtract(0,1,0)));
double weightTotal = weights[0] + weights[1] + weights[2]; if (fertilizer != null){
double rank_1 = weights[0]/(weightTotal); if (fertilizer instanceof QualityCrop qualityCrop){
double rank_2 = 1 - weights[1]/(weightTotal); int[] weights = qualityCrop.getChance();
for (int i = 0; i < random; i++){ double weightTotal = weights[0] + weights[1] + weights[2];
double ran = Math.random(); double rank_1 = weights[0]/(weightTotal);
if (ran < rank_1){ double rank_2 = 1 - weights[1]/(weightTotal);
world.dropItem(itemLoc, CustomStack.getInstance(cropInstance.getQuality_1()).getItemStack()); Bukkit.getScheduler().callSyncMethod(CustomCrops.instance, ()-> {
}else if(ran > rank_2){ for (int i = 0; i < random; i++){
world.dropItem(itemLoc, CustomStack.getInstance(cropInstance.getQuality_2()).getItemStack()); double ran = Math.random();
}else { if (ran < rank_1){
world.dropItem(itemLoc, CustomStack.getInstance(cropInstance.getQuality_3()).getItemStack()); world.dropItem(itemLoc, CustomStack.getInstance(cropInstance.getQuality_1()).getItemStack());
} }else if(ran > rank_2){
world.dropItem(itemLoc, CustomStack.getInstance(cropInstance.getQuality_2()).getItemStack());
}else {
world.dropItem(itemLoc, CustomStack.getInstance(cropInstance.getQuality_3()).getItemStack());
}
}
return null;
});
}else {
Bukkit.getScheduler().callSyncMethod(CustomCrops.instance, ()-> {
normalDrop(cropInstance, random, itemLoc, world);
return null;
});
} }
}else {
normalDrop(cropInstance, random, itemLoc, world);
} }
} else {
else { Bukkit.getScheduler().callSyncMethod(CustomCrops.instance, ()-> {
normalDrop(cropInstance, random, itemLoc, world); normalDrop(cropInstance, random, itemLoc, world);
} return null;
});
}
});
} }
} }
else if(namespacedId.equalsIgnoreCase(ConfigReader.Basic.watered_pot) || namespacedId.equalsIgnoreCase(ConfigReader.Basic.pot)){ else if(namespacedId.equalsIgnoreCase(ConfigReader.Basic.watered_pot) || namespacedId.equalsIgnoreCase(ConfigReader.Basic.pot)){

View File

@@ -318,7 +318,7 @@ public class RightClick implements Listener {
} }
} }
} }
else if (action == Action.RIGHT_CLICK_BLOCK) { else if (ConfigReader.Config.rightClickHarvest && action == Action.RIGHT_CLICK_BLOCK) {
Block block = event.getClickedBlock(); Block block = event.getClickedBlock();
Location location = block.getLocation(); Location location = block.getLocation();
CustomBlock customBlock = CustomBlock.byAlreadyPlaced(block); CustomBlock customBlock = CustomBlock.byAlreadyPlaced(block);

View File

@@ -73,4 +73,8 @@ config:
#Should all the worlds' crops grow? #Should all the worlds' crops grow?
#This is useful for per player per world server. #This is useful for per player per world server.
#In this mode, whitelist world can only have one as the standard of time & season judgment. #In this mode, whitelist world can only have one as the standard of time & season judgment.
all-world-grow: false all-world-grow: false
#can player harvest crops with right click?
#if set "false" crops can't be harvested repeatedly
right-click-harvest: true