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
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'
version = '2.2.6-hotfix'
version = '2.2.7.1'
repositories {
mavenCentral()

View File

@@ -81,14 +81,9 @@ public final class CustomCrops extends JavaPlugin {
public void onEnable() {
adventure = BukkitAudiences.create(plugin);
protocolManager = ProtocolLibrary.getProtocolManager();
this.versionHelper = new VersionHelper(this);
AdventureUtil.consoleMessage("[CustomCrops] Running on <white>" + Bukkit.getVersion());
MinecraftVersion.disableBStats();
MinecraftVersion.disablePackageWarning();
MinecraftVersion.disableUpdateCheck();
MinecraftVersion.getVersion();
VersionChecker.hideOk = true;
if (Bukkit.getPluginManager().getPlugin("ItemsAdder") != null) {
MainConfig.customPlugin = "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);
this.cropManager = new CropManager();
this.versionHelper = new VersionHelper();
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
this.placeholderManager = new PlaceholderManager();

View File

@@ -63,7 +63,6 @@ public class SeasonUtils {
case AUTUMN -> MessageConfig.autumn;
case WINTER -> MessageConfig.winter;
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) {
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) {
List<World> worlds = MainConfig.getWorldsList();
List<String> worlds = MainConfig.getWorldNameList();
List<String> worldNames = new ArrayList<>();
for (World world : worlds) {
if (world.getName().startsWith(args.get(0))) {
worldNames.add(world.getName());
for (String world : worlds) {
if (world.startsWith(args.get(0))) {
worldNames.add(world);
}
}
return worldNames;

View File

@@ -38,9 +38,6 @@ import java.util.List;
public class MainConfig {
public static World[] worlds;
public static String[] worldNames;
public static List<World> worldList;
public static List<String> worldNameList;
public static boolean whiteOrBlack;
public static boolean dropLootsInAllWorlds;
@@ -138,21 +135,7 @@ public class MainConfig {
whiteOrBlack = config.getString("worlds.mode","whitelist").equals("whitelist");
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);
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() {
if (whiteOrBlack) {
return worldNameList;

View File

@@ -1,15 +1,27 @@
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 java.lang.reflect.Field;
public class VersionHelper {
private boolean isNewerThan1_19_R2;
private String version;
private final CustomCrops plugin;
public VersionHelper(CustomCrops plugin) {
this.plugin = plugin;
isVersionNewerThan1_19_R2();
disableUseLessInfo();
}
public boolean isVersionNewerThan1_19_R2() {
if (version == null) {
version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
version = plugin.getServer().getClass().getPackage().getName().split("\\.")[3];
String[] split = version.split("_");
int main_ver = Integer.parseInt(split[1]);
if (main_ver >= 20) isNewerThan1_19_R2 = true;
@@ -18,4 +30,37 @@ public class VersionHelper {
}
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
public ItemFrame placeFurniture(Location location, String 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

View File

@@ -56,38 +56,38 @@ public class SeasonPapi extends PlaceholderExpansion {
if (!SeasonConfig.enable) return MessageConfig.seasonDisabled;
switch (params) {
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());
}
case "days_left" -> {
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));
}
case "days_gone" -> {
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);
}
default -> {
if (params.startsWith("current_")) {
World world = Bukkit.getWorld(params.substring(8));
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);
}
if (params.startsWith("days_left_")) {
if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled;
World world = Bukkit.getWorld(params.substring(10));
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));
}
if (params.startsWith("days_gone_")) {
if (!SeasonConfig.auto) return MessageConfig.autoSeasonDisabled;
World world = Bukkit.getWorld(params.substring(10));
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);
}
}

View File

@@ -58,31 +58,16 @@ public class WorldGuardHook implements CCAntiGrief {
@Override
public boolean canPlace(Location location, Player player) {
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
World world = BukkitAdapter.adapt(location.getWorld());
WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform();
if (hasRegion(world, BukkitAdapter.asBlockVector(location))){
RegionQuery query = platform.getRegionContainer().createQuery();
return query.testBuild(BukkitAdapter.adapt(location), localPlayer, PLACE_FLAG);
}
else return true;
RegionQuery query = platform.getRegionContainer().createQuery();
return query.testBuild(BukkitAdapter.adapt(location), localPlayer, PLACE_FLAG);
}
@Override
public boolean canBreak(Location location, Player player) {
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
World world = BukkitAdapter.adapt(location.getWorld());
WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform();
if (hasRegion(world, BukkitAdapter.asBlockVector(location))){
RegionQuery query = platform.getRegionContainer().createQuery();
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;
RegionQuery query = platform.getRegionContainer().createQuery();
return query.testBuild(BukkitAdapter.adapt(location), localPlayer, HARVEST_FLAG);
}
}

View File

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

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

View File

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

View File

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