9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-23 08:59:28 +00:00
This commit is contained in:
Xiao-MoMi
2023-03-14 21:10:31 +08:00
parent f154e9fa05
commit 84ed09d35a
14 changed files with 93 additions and 127 deletions

View File

@@ -4,7 +4,7 @@ plugins {
} }
group = 'net.momirealms' group = 'net.momirealms'
version = '2.2.6-hotfix' version = '2.2.7.1'
repositories { repositories {
mavenCentral() mavenCentral()

View File

@@ -81,14 +81,9 @@ public final class CustomCrops extends JavaPlugin {
public void onEnable() { public void onEnable() {
adventure = BukkitAudiences.create(plugin); adventure = BukkitAudiences.create(plugin);
protocolManager = ProtocolLibrary.getProtocolManager(); protocolManager = ProtocolLibrary.getProtocolManager();
this.versionHelper = new VersionHelper(this);
AdventureUtil.consoleMessage("[CustomCrops] Running on <white>" + Bukkit.getVersion()); AdventureUtil.consoleMessage("[CustomCrops] Running on <white>" + Bukkit.getVersion());
MinecraftVersion.disableBStats();
MinecraftVersion.disablePackageWarning();
MinecraftVersion.disableUpdateCheck();
MinecraftVersion.getVersion();
VersionChecker.hideOk = true; VersionChecker.hideOk = true;
if (Bukkit.getPluginManager().getPlugin("ItemsAdder") != null) { if (Bukkit.getPluginManager().getPlugin("ItemsAdder") != null) {
MainConfig.customPlugin = "itemsadder"; MainConfig.customPlugin = "itemsadder";
AdventureUtil.consoleMessage("[CustomCrops] Custom Item Plugin Platform: <#BA55D3><u>ItemsAdder"); AdventureUtil.consoleMessage("[CustomCrops] Custom Item Plugin Platform: <#BA55D3><u>ItemsAdder");
@@ -110,7 +105,6 @@ public final class CustomCrops extends JavaPlugin {
Objects.requireNonNull(Bukkit.getPluginCommand("customcrops")).setTabCompleter(pluginCommand); Objects.requireNonNull(Bukkit.getPluginCommand("customcrops")).setTabCompleter(pluginCommand);
this.cropManager = new CropManager(); this.cropManager = new CropManager();
this.versionHelper = new VersionHelper();
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
this.placeholderManager = new PlaceholderManager(); this.placeholderManager = new PlaceholderManager();

View File

@@ -63,7 +63,6 @@ public class SeasonUtils {
case AUTUMN -> MessageConfig.autumn; case AUTUMN -> MessageConfig.autumn;
case WINTER -> MessageConfig.winter; case WINTER -> MessageConfig.winter;
case UNKNOWN -> "Error"; case UNKNOWN -> "Error";
default -> throw new IllegalStateException("Unexpected value: " + season);
}; };
} }
} }

View File

@@ -42,34 +42,4 @@ public class WorldUtils {
public static void unloadCropWorld(World world, boolean sync) { public static void unloadCropWorld(World world, boolean sync) {
CustomCrops.plugin.getCropManager().onWorldUnload(world, sync); CustomCrops.plugin.getCropManager().onWorldUnload(world, sync);
} }
/**
* Add a world to the world list
* @param world world
*/
public static void addWorldToWorldList(World world) {
MainConfig.worldList.add(world);
MainConfig.worlds = MainConfig.worldList.toArray(new World[0]);
}
/**
* Remove a world from world list
* @param world world
* @return success or not
*/
public static boolean removeWorldFromWorldList(World world) {
boolean success = MainConfig.worldList.remove(world);
if (success) {
MainConfig.worlds = MainConfig.worldList.toArray(new World[0]);
}
return success;
}
/**
* get the world list
* @return world list
*/
public static List<World> getWorldList() {
return MainConfig.worldList;
}
} }

View File

@@ -87,11 +87,11 @@ public abstract class AbstractSubCommand implements SubCommand {
} }
public List<String> getWorlds(List<String> args) { public List<String> getWorlds(List<String> args) {
List<World> worlds = MainConfig.getWorldsList(); List<String> worlds = MainConfig.getWorldNameList();
List<String> worldNames = new ArrayList<>(); List<String> worldNames = new ArrayList<>();
for (World world : worlds) { for (String world : worlds) {
if (world.getName().startsWith(args.get(0))) { if (world.startsWith(args.get(0))) {
worldNames.add(world.getName()); worldNames.add(world);
} }
} }
return worldNames; return worldNames;

View File

@@ -38,9 +38,6 @@ import java.util.List;
public class MainConfig { public class MainConfig {
public static World[] worlds;
public static String[] worldNames;
public static List<World> worldList;
public static List<String> worldNameList; public static List<String> worldNameList;
public static boolean whiteOrBlack; public static boolean whiteOrBlack;
public static boolean dropLootsInAllWorlds; public static boolean dropLootsInAllWorlds;
@@ -138,21 +135,7 @@ public class MainConfig {
whiteOrBlack = config.getString("worlds.mode","whitelist").equals("whitelist"); whiteOrBlack = config.getString("worlds.mode","whitelist").equals("whitelist");
worldNameList = config.getStringList("worlds.list"); worldNameList = config.getStringList("worlds.list");
worlds = new World[worldNameList.size()];
for (int i = 0; i < worldNameList.size(); i++) {
if (Bukkit.getWorld(worldNameList.get(i)) != null) {
worlds[i] = Bukkit.getWorld(worldNameList.get(i));
}
}
worldList = new ArrayList<>();
for (World world : worlds) {
if (world == null) continue;
worldList.add(world);
}
worlds = worldList.toArray(new World[0]);
worldNames = worldNameList.toArray(new String[0]);
worldFolder = StringUtils.replace(config.getString("worlds.worlds-folder",""), "\\", File.separator); worldFolder = StringUtils.replace(config.getString("worlds.worlds-folder",""), "\\", File.separator);
cropMode = config.getString("mechanics.crops-mode", "tripwire").equals("tripwire"); cropMode = config.getString("mechanics.crops-mode", "tripwire").equals("tripwire");
@@ -404,28 +387,6 @@ public class MainConfig {
} }
} }
public static World[] getWorldsArray() {
if (MainConfig.whiteOrBlack) {
return worlds;
}
else {
List<World> worldList = new ArrayList<>(Bukkit.getWorlds());
worldList.removeAll(MainConfig.worldList);
return worldList.toArray(new World[0]);
}
}
public static List<World> getWorldsList() {
if (MainConfig.whiteOrBlack) {
return worldList;
}
else {
List<World> worldList = new ArrayList<>(Bukkit.getWorlds());
worldList.removeAll(MainConfig.worldList);
return worldList;
}
}
public static List<String> getWorldNameList() { public static List<String> getWorldNameList() {
if (whiteOrBlack) { if (whiteOrBlack) {
return worldNameList; return worldNameList;

View File

@@ -1,15 +1,27 @@
package net.momirealms.customcrops.helper; package net.momirealms.customcrops.helper;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
import de.tr7zw.changeme.nbtapi.utils.VersionChecker;
import net.momirealms.customcrops.CustomCrops;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.lang.reflect.Field;
public class VersionHelper { public class VersionHelper {
private boolean isNewerThan1_19_R2; private boolean isNewerThan1_19_R2;
private String version; private String version;
private final CustomCrops plugin;
public VersionHelper(CustomCrops plugin) {
this.plugin = plugin;
isVersionNewerThan1_19_R2();
disableUseLessInfo();
}
public boolean isVersionNewerThan1_19_R2() { public boolean isVersionNewerThan1_19_R2() {
if (version == null) { if (version == null) {
version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; version = plugin.getServer().getClass().getPackage().getName().split("\\.")[3];
String[] split = version.split("_"); String[] split = version.split("_");
int main_ver = Integer.parseInt(split[1]); int main_ver = Integer.parseInt(split[1]);
if (main_ver >= 20) isNewerThan1_19_R2 = true; if (main_ver >= 20) isNewerThan1_19_R2 = true;
@@ -18,4 +30,37 @@ public class VersionHelper {
} }
return isNewerThan1_19_R2; return isNewerThan1_19_R2;
} }
private void disableUseLessInfo() {
MinecraftVersion.disableBStats();
MinecraftVersion.disableUpdateCheck();
VersionChecker.hideOk = true;
try {
Field field = MinecraftVersion.class.getDeclaredField("version");
field.setAccessible(true);
MinecraftVersion minecraftVersion;
try {
minecraftVersion = MinecraftVersion.valueOf(version.replace("v", "MC"));
} catch (IllegalArgumentException ex) {
minecraftVersion = MinecraftVersion.UNKNOWN;
}
field.set(MinecraftVersion.class, minecraftVersion);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
boolean hasGsonSupport;
try {
Class.forName("com.google.gson.Gson");
hasGsonSupport = true;
} catch (Exception ex) {
hasGsonSupport = false;
}
try {
Field field= MinecraftVersion.class.getDeclaredField("hasGsonSupport");
field.setAccessible(true);
field.set(Boolean.class, hasGsonSupport);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
} }

View File

@@ -95,7 +95,7 @@ public class OraxenHook implements CustomInterface {
@Nullable @Nullable
public ItemFrame placeFurniture(Location location, String id) { public ItemFrame placeFurniture(Location location, String id) {
FurnitureMechanic mechanic = (FurnitureMechanic) FurnitureFactory.getInstance().getMechanic(id); FurnitureMechanic mechanic = (FurnitureMechanic) FurnitureFactory.getInstance().getMechanic(id);
return mechanic.place(Rotation.NONE, 0, BlockFace.UP, location, null); return mechanic.place(Rotation.NONE, 0, BlockFace.UP, location);
} }
@Override @Override

View File

@@ -56,38 +56,38 @@ public class SeasonPapi extends PlaceholderExpansion {
if (!SeasonConfig.enable) return MessageConfig.seasonDisabled; if (!SeasonConfig.enable) return MessageConfig.seasonDisabled;
switch (params) { switch (params) {
case "current" -> { case "current" -> {
if (!MainConfig.getWorldsList().contains(player.getWorld())) return MessageConfig.noSeason; if (!MainConfig.getWorldNameList().contains(player.getWorld().getName())) return MessageConfig.noSeason;
return getSeasonText(player.getWorld()); return getSeasonText(player.getWorld());
} }
case "days_left" -> { case "days_left" -> {
if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled; if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled;
if (!MainConfig.getWorldsList().contains(player.getWorld())) return MessageConfig.noSeason; if (!MainConfig.getWorldNameList().contains(player.getWorld().getName())) return MessageConfig.noSeason;
return String.valueOf(SeasonConfig.duration - ((int) ((player.getWorld().getFullTime() / 24000L) % (SeasonConfig.duration * 4)) % SeasonConfig.duration)); return String.valueOf(SeasonConfig.duration - ((int) ((player.getWorld().getFullTime() / 24000L) % (SeasonConfig.duration * 4)) % SeasonConfig.duration));
} }
case "days_gone" -> { case "days_gone" -> {
if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled; if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled;
if (!MainConfig.getWorldsList().contains(player.getWorld())) return MessageConfig.noSeason; if (!MainConfig.getWorldNameList().contains(player.getWorld().getName())) return MessageConfig.noSeason;
return String.valueOf((int) ((player.getWorld().getFullTime() / 24000L) % (SeasonConfig.duration * 4)) % SeasonConfig.duration + 1); return String.valueOf((int) ((player.getWorld().getFullTime() / 24000L) % (SeasonConfig.duration * 4)) % SeasonConfig.duration + 1);
} }
default -> { default -> {
if (params.startsWith("current_")) { if (params.startsWith("current_")) {
World world = Bukkit.getWorld(params.substring(8)); World world = Bukkit.getWorld(params.substring(8));
if (world == null) return MessageConfig.noSeason; if (world == null) return MessageConfig.noSeason;
if (!MainConfig.getWorldsList().contains(world)) return MessageConfig.noSeason; if (!MainConfig.getWorldNameList().contains(world.getName())) return MessageConfig.noSeason;
return getSeasonText(world); return getSeasonText(world);
} }
if (params.startsWith("days_left_")) { if (params.startsWith("days_left_")) {
if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled; if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled;
World world = Bukkit.getWorld(params.substring(10)); World world = Bukkit.getWorld(params.substring(10));
if (world == null) return MessageConfig.noSeason; if (world == null) return MessageConfig.noSeason;
if (!MainConfig.getWorldsList().contains(world)) return MessageConfig.noSeason; if (!MainConfig.getWorldNameList().contains(world.getName())) return MessageConfig.noSeason;
return String.valueOf(SeasonConfig.duration - ((int) ((world.getFullTime() / 24000L) % (SeasonConfig.duration * 4)) % SeasonConfig.duration)); return String.valueOf(SeasonConfig.duration - ((int) ((world.getFullTime() / 24000L) % (SeasonConfig.duration * 4)) % SeasonConfig.duration));
} }
if (params.startsWith("days_gone_")) { if (params.startsWith("days_gone_")) {
if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled; if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled;
World world = Bukkit.getWorld(params.substring(10)); World world = Bukkit.getWorld(params.substring(10));
if (world == null) return MessageConfig.noSeason; if (world == null) return MessageConfig.noSeason;
if (!MainConfig.getWorldsList().contains(world)) return MessageConfig.noSeason; if (!MainConfig.getWorldNameList().contains(world.getName())) return MessageConfig.noSeason;
return String.valueOf((int) ((world.getFullTime() / 24000L) % (SeasonConfig.duration * 4)) % SeasonConfig.duration + 1); return String.valueOf((int) ((world.getFullTime() / 24000L) % (SeasonConfig.duration * 4)) % SeasonConfig.duration + 1);
} }
} }

View File

@@ -58,31 +58,16 @@ public class WorldGuardHook implements CCAntiGrief {
@Override @Override
public boolean canPlace(Location location, Player player) { public boolean canPlace(Location location, Player player) {
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
World world = BukkitAdapter.adapt(location.getWorld());
WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform(); WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform();
if (hasRegion(world, BukkitAdapter.asBlockVector(location))){
RegionQuery query = platform.getRegionContainer().createQuery(); RegionQuery query = platform.getRegionContainer().createQuery();
return query.testBuild(BukkitAdapter.adapt(location), localPlayer, PLACE_FLAG); return query.testBuild(BukkitAdapter.adapt(location), localPlayer, PLACE_FLAG);
} }
else return true;
}
@Override @Override
public boolean canBreak(Location location, Player player) { public boolean canBreak(Location location, Player player) {
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
World world = BukkitAdapter.adapt(location.getWorld());
WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform(); WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform();
if (hasRegion(world, BukkitAdapter.asBlockVector(location))){
RegionQuery query = platform.getRegionContainer().createQuery(); RegionQuery query = platform.getRegionContainer().createQuery();
return query.testBuild(BukkitAdapter.adapt(location), localPlayer, HARVEST_FLAG); return query.testBuild(BukkitAdapter.adapt(location), localPlayer, HARVEST_FLAG);
} }
else return true;
}
private boolean hasRegion(World world, BlockVector3 vector){
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionManager regionManager = container.get(world);
if (regionManager == null) return true;
return regionManager.getApplicableRegions(vector).size() > 0;
}
} }

View File

@@ -158,12 +158,15 @@ public class InternalSeason extends Function implements SeasonInterface {
@Override @Override
public void run() { public void run() {
if (!SeasonConfig.auto) return; if (!SeasonConfig.auto) return;
for (World world : MainConfig.getWorldsArray()) { for (String world_name : MainConfig.getWorldNameList()) {
World world = Bukkit.getWorld(world_name);
if (world != null) {
if (world.getTime() < 100) { if (world.getTime() < 100) {
setSeason(countSeason(world), world); setSeason(countSeason(world), world);
} }
} }
} }
}
}.runTaskTimerAsynchronously(CustomCrops.plugin, 0, 100); }.runTaskTimerAsynchronously(CustomCrops.plugin, 0, 100);
} }

View File

@@ -23,6 +23,7 @@ import net.momirealms.customcrops.api.event.CropHarvestEvent;
import net.momirealms.customcrops.api.event.CrowAttackEvent; import net.momirealms.customcrops.api.event.CrowAttackEvent;
import net.momirealms.customcrops.api.utils.CCSeason; import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.config.*; import net.momirealms.customcrops.config.*;
import net.momirealms.customcrops.helper.Log;
import net.momirealms.customcrops.integrations.customplugin.CustomInterface; import net.momirealms.customcrops.integrations.customplugin.CustomInterface;
import net.momirealms.customcrops.integrations.customplugin.HandlerP; import net.momirealms.customcrops.integrations.customplugin.HandlerP;
import net.momirealms.customcrops.integrations.customplugin.itemsadder.ItemsAdderFrameHandler; import net.momirealms.customcrops.integrations.customplugin.itemsadder.ItemsAdderFrameHandler;
@@ -47,6 +48,7 @@ import net.momirealms.customcrops.utils.ArmorStandUtil;
import net.momirealms.customcrops.utils.FurnitureUtil; import net.momirealms.customcrops.utils.FurnitureUtil;
import net.momirealms.customcrops.utils.MiscUtils; import net.momirealms.customcrops.utils.MiscUtils;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.ItemFrame; import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -54,6 +56,7 @@ import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@@ -492,13 +495,15 @@ public class CropManager extends Function {
return true; return true;
} }
if (MainConfig.enableCrow && crowJudge(location)) return true; if (MainConfig.enableCrow && crowJudge(location)) {
return true;
}
String potID = customInterface.getBlockID(potLoc); String potID = customInterface.getBlockID(potLoc);
if (potID == null) return true; if (potID == null) return true;
boolean certainGrow = potID.equals(BasicItemConfig.wetPot); boolean certainGrow = potID.equals(BasicItemConfig.wetPot);
if (certainGrow && !hasWater(potLoc)) { if (certainGrow && !hasWater(potLoc.getBlock())) {
if (!(fertilizer instanceof RetainingSoil retainingSoil && Math.random() < retainingSoil.getChance())) { if (!(fertilizer instanceof RetainingSoil retainingSoil && Math.random() < retainingSoil.getChance())) {
dry(potLoc); dry(potLoc);
certainGrow = false; certainGrow = false;
@@ -526,11 +531,10 @@ public class CropManager extends Function {
return false; return false;
} }
private boolean hasWater(Location potLoc) { private boolean hasWater(Block block) {
World world = potLoc.getWorld(); return Optional.ofNullable(customWorlds.get(block.getWorld()))
CustomWorld customWorld = customWorlds.get(world); .map(customWorld -> customWorld.isPotWet(block.getLocation()))
if (customWorld == null) return false; .orElse(false);
return customWorld.isPotWet(potLoc);
} }
public boolean itemFrameGrowJudge(Location location, GrowingCrop growingCrop) { public boolean itemFrameGrowJudge(Location location, GrowingCrop growingCrop) {
@@ -577,7 +581,7 @@ public class CropManager extends Function {
if (potID == null) return true; if (potID == null) return true;
boolean certainGrow = potID.equals(BasicItemConfig.wetPot); boolean certainGrow = potID.equals(BasicItemConfig.wetPot);
if (certainGrow && !hasWater(potLoc)) { if (certainGrow && !hasWater(potLoc.getBlock())) {
if (!(fertilizer instanceof RetainingSoil retainingSoil && Math.random() < retainingSoil.getChance())) { if (!(fertilizer instanceof RetainingSoil retainingSoil && Math.random() < retainingSoil.getChance())) {
dry(potLoc); dry(potLoc);
certainGrow = false; certainGrow = false;

View File

@@ -19,6 +19,7 @@ package net.momirealms.customcrops.managers.timer;
import net.momirealms.customcrops.config.MainConfig; import net.momirealms.customcrops.config.MainConfig;
import net.momirealms.customcrops.managers.CropManager; import net.momirealms.customcrops.managers.CropManager;
import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@@ -33,7 +34,9 @@ public class TimerTask extends BukkitRunnable {
@Override @Override
public void run() { public void run() {
if (!MainConfig.autoGrow) return; if (!MainConfig.autoGrow) return;
for (World world : MainConfig.getWorldsList()) { for (String worldName : MainConfig.getWorldNameList()) {
World world = Bukkit.getWorld(worldName);
if (world != null) {
long time = world.getTime(); long time = world.getTime();
if (time > 900 && time < 1001) { if (time > 900 && time < 1001) {
cropManager.grow(world, MainConfig.timeToGrow, MainConfig.timeToWork, MainConfig.timeToDry, false, false); cropManager.grow(world, MainConfig.timeToGrow, MainConfig.timeToWork, MainConfig.timeToDry, false, false);
@@ -44,3 +47,4 @@ public class TimerTask extends BukkitRunnable {
} }
} }
} }
}

View File

@@ -26,6 +26,7 @@ softdepend:
- Lands - Lands
- GriefPrevention - GriefPrevention
- CrashClaim - CrashClaim
- BentoBox
commands: commands:
customcrops: customcrops:
usage: /customcrops <args> usage: /customcrops <args>