9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-26 10:29:10 +00:00
This commit is contained in:
XiaoMoMi
2024-05-07 22:08:57 +08:00
parent 28cb26845b
commit e390976cb7
14 changed files with 204 additions and 63 deletions

View File

@@ -17,8 +17,6 @@
package net.momirealms.customcrops;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
import de.tr7zw.changeme.nbtapi.utils.VersionChecker;
import net.momirealms.antigrieflib.AntiGriefLib;
import net.momirealms.customcrops.api.CustomCropsPlugin;
import net.momirealms.customcrops.api.event.CustomCropsReloadEvent;
@@ -39,10 +37,10 @@ import net.momirealms.customcrops.mechanic.misc.migrator.Migration;
import net.momirealms.customcrops.mechanic.requirement.RequirementManagerImpl;
import net.momirealms.customcrops.mechanic.world.WorldManagerImpl;
import net.momirealms.customcrops.scheduler.SchedulerImpl;
import net.momirealms.customcrops.util.NBTUtils;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@@ -99,7 +97,7 @@ public class CustomCropsPluginImpl extends CustomCropsPlugin {
this.hologramManager = new HologramManager(this);
this.commandManager.init();
this.integrationManager.init();
this.disableNBTAPILogs();
NBTUtils.disableNBTAPILogs();
Migration.tryUpdating();
this.reload();
if (ConfigManager.metrics()) new Metrics(this, 16593);
@@ -142,42 +140,6 @@ public class CustomCropsPluginImpl extends CustomCropsPlugin {
EventUtils.fireAndForget(new CustomCropsReloadEvent(this));
}
/**
* Disable NBT API logs
*/
private void disableNBTAPILogs() {
MinecraftVersion.disableBStats();
MinecraftVersion.disableUpdateCheck();
VersionChecker.hideOk = true;
try {
Field field = MinecraftVersion.class.getDeclaredField("version");
field.setAccessible(true);
MinecraftVersion minecraftVersion;
try {
minecraftVersion = MinecraftVersion.valueOf(getVersionManager().getServerVersion().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);
}
}
@Override
public void debug(String debug) {
if (ConfigManager.debug()) {

View File

@@ -42,14 +42,14 @@ public enum Dependency {
ASM(
"org.ow2.asm",
"asm",
"9.1",
"9.7",
null,
"asm"
),
ASM_COMMONS(
"org.ow2.asm",
"asm-commons",
"9.1",
"9.7",
null,
"asm-commons"
),
@@ -118,7 +118,7 @@ public enum Dependency {
NBT_API(
"de{}tr7zw",
"item-nbt-api",
"2.12.3",
"2.12.4",
"codemc",
"item-nbt-api",
Relocation.of("changeme", "de{}tr7zw{}changeme")
@@ -134,7 +134,7 @@ public enum Dependency {
BIOME_API(
"com{}github{}Xiao-MoMi",
"BiomeAPI",
"0.3",
"0.6",
"jitpack",
"biome-api",
Relocation.of("biomeapi", "net{}momirealms{}biomeapi")

View File

@@ -45,8 +45,6 @@ import net.momirealms.customcrops.api.util.LocationUtils;
import net.momirealms.customcrops.api.util.LogUtils;
import net.momirealms.customcrops.mechanic.item.custom.itemsadder.ItemsAdderListener;
import net.momirealms.customcrops.mechanic.item.custom.itemsadder.ItemsAdderProvider;
import net.momirealms.customcrops.mechanic.item.custom.oraxen.OraxenListener;
import net.momirealms.customcrops.mechanic.item.custom.oraxen.OraxenProvider;
import net.momirealms.customcrops.mechanic.item.custom.oraxenlegacy.LegacyOraxenListener;
import net.momirealms.customcrops.mechanic.item.custom.oraxenlegacy.LegacyOraxenProvider;
import net.momirealms.customcrops.mechanic.item.function.CFunction;
@@ -81,6 +79,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.*;
public class ItemManagerImpl implements ItemManager {
@@ -128,8 +127,18 @@ public class ItemManagerImpl implements ItemManager {
this.deadCrops = new HashSet<>();
if (Bukkit.getPluginManager().getPlugin("Oraxen") != null) {
if (Bukkit.getPluginManager().getPlugin("Oraxen").getDescription().getVersion().startsWith("2")) {
listener = new OraxenListener(this);
customProvider = new OraxenProvider();
try {
Class<?> oraxenListenerClass = Class.forName("net.momirealms.customcrops.mechanic.item.custom.oraxen.OraxenListener");
Constructor<?> oraxenListenerConstructor = oraxenListenerClass.getDeclaredConstructor(ItemManager.class);
oraxenListenerConstructor.setAccessible(true);
this.listener = (AbstractCustomListener) oraxenListenerConstructor.newInstance(this);
Class<?> oraxenProviderClass = Class.forName("net.momirealms.customcrops.mechanic.item.custom.oraxen.OraxenProvider");
Constructor<?> oraxenProviderConstructor = oraxenProviderClass.getDeclaredConstructor(ItemManager.class);
oraxenProviderConstructor.setAccessible(true);
this.customProvider = (CustomProvider) oraxenProviderConstructor.newInstance();
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
} else {
listener = new LegacyOraxenListener(this);
customProvider = new LegacyOraxenProvider();

View File

@@ -1,108 +0,0 @@
/*
* Copyright (C) <2022> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customcrops.mechanic.item.custom.oraxen;
import io.th0rgal.oraxen.api.events.custom_block.noteblock.OraxenNoteBlockBreakEvent;
import io.th0rgal.oraxen.api.events.custom_block.noteblock.OraxenNoteBlockPlaceEvent;
import io.th0rgal.oraxen.api.events.custom_block.stringblock.OraxenStringBlockBreakEvent;
import io.th0rgal.oraxen.api.events.custom_block.stringblock.OraxenStringBlockPlaceEvent;
import io.th0rgal.oraxen.api.events.furniture.OraxenFurnitureBreakEvent;
import io.th0rgal.oraxen.api.events.furniture.OraxenFurnitureInteractEvent;
import io.th0rgal.oraxen.api.events.furniture.OraxenFurniturePlaceEvent;
import net.momirealms.customcrops.api.mechanic.item.custom.AbstractCustomListener;
import net.momirealms.customcrops.api.util.LocationUtils;
import net.momirealms.customcrops.mechanic.item.ItemManagerImpl;
import org.bukkit.event.EventHandler;
public class OraxenListener extends AbstractCustomListener {
public OraxenListener(ItemManagerImpl itemManager) {
super(itemManager);
}
@EventHandler (ignoreCancelled = true)
public void onBreakCustomNoteBlock(OraxenNoteBlockBreakEvent event) {
this.itemManager.handlePlayerBreakBlock(
event.getPlayer(),
event.getBlock(),
event.getMechanic().getItemID(),
event
);
}
@EventHandler (ignoreCancelled = true)
public void onBreakCustomStringBlock(OraxenStringBlockBreakEvent event) {
this.itemManager.handlePlayerBreakBlock(
event.getPlayer(),
event.getBlock(),
event.getMechanic().getItemID(),
event
);
}
@EventHandler (ignoreCancelled = true)
public void onPlaceCustomBlock(OraxenNoteBlockPlaceEvent event) {
super.onPlaceBlock(
event.getPlayer(),
event.getBlock(),
event.getMechanic().getItemID(),
event
);
}
@EventHandler (ignoreCancelled = true)
public void onPlaceCustomBlock(OraxenStringBlockPlaceEvent event) {
super.onPlaceBlock(
event.getPlayer(),
event.getBlock(),
event.getMechanic().getItemID(),
event
);
}
@EventHandler (ignoreCancelled = true)
public void onPlaceFurniture(OraxenFurniturePlaceEvent event) {
super.onPlaceFurniture(
event.getPlayer(),
event.getBlock().getLocation(),
event.getMechanic().getItemID(),
event
);
}
@EventHandler (ignoreCancelled = true)
public void onBreakFurniture(OraxenFurnitureBreakEvent event) {
super.onBreakFurniture(
event.getPlayer(),
LocationUtils.toBlockLocation(event.getBaseEntity().getLocation()),
event.getMechanic().getItemID(),
event
);
}
@EventHandler (ignoreCancelled = true)
public void onInteractFurniture(OraxenFurnitureInteractEvent event) {
super.onInteractFurniture(
event.player(),
LocationUtils.toBlockLocation(event.baseEntity().getLocation()),
event.mechanic().getItemID(),
event.baseEntity(),
event
);
}
}

View File

@@ -1,104 +0,0 @@
/*
* Copyright (C) <2022> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customcrops.mechanic.item.custom.oraxen;
import io.th0rgal.oraxen.api.OraxenBlocks;
import io.th0rgal.oraxen.api.OraxenFurniture;
import io.th0rgal.oraxen.api.OraxenItems;
import io.th0rgal.oraxen.items.ItemBuilder;
import io.th0rgal.oraxen.mechanics.Mechanic;
import io.th0rgal.oraxen.mechanics.provided.gameplay.furniture.FurnitureMechanic;
import net.momirealms.customcrops.api.mechanic.item.custom.CustomProvider;
import net.momirealms.customcrops.api.util.LogUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Rotation;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
public class OraxenProvider implements CustomProvider {
@Override
public boolean removeBlock(Location location) {
Block block = location.getBlock();
if (block.getType() == Material.AIR) {
return false;
}
block.setType(Material.AIR);
return true;
}
@Override
public void placeCustomBlock(Location location, String id) {
OraxenBlocks.place(id, location);
}
@Override
public Entity placeFurniture(Location location, String id) {
Entity entity = OraxenFurniture.place(id, location, Rotation.NONE, BlockFace.UP);
if (entity == null) {
LogUtils.warn("Furniture(" + id +") doesn't exist in Oraxen configs. Please double check if that furniture exists.");
}
return entity;
}
@Override
public void removeFurniture(Entity entity) {
OraxenFurniture.remove(entity, null);
}
@Override
public String getBlockID(Block block) {
Mechanic mechanic = OraxenBlocks.getCustomBlockMechanic(block.getLocation());
if (mechanic == null) {
return block.getType().name();
}
return mechanic.getItemID();
}
@Override
public String getItemID(ItemStack itemStack) {
return OraxenItems.getIdByItem(itemStack);
}
@Override
public ItemStack getItemStack(String id) {
if (id == null) return new ItemStack(Material.AIR);
ItemBuilder builder = OraxenItems.getItemById(id);
if (builder == null) {
return null;
}
return builder.build();
}
@Override
public String getEntityID(Entity entity) {
FurnitureMechanic mechanic = OraxenFurniture.getFurnitureMechanic(entity);
if (mechanic == null) {
return entity.getType().name();
}
return mechanic.getItemID();
}
@Override
public boolean isFurniture(Entity entity) {
return OraxenFurniture.isFurniture(entity);
}
}

View File

@@ -0,0 +1,61 @@
package net.momirealms.customcrops.util;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
import de.tr7zw.changeme.nbtapi.utils.VersionChecker;
import net.momirealms.customcrops.api.CustomCropsPlugin;
import org.bukkit.Bukkit;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class NBTUtils {
private NBTUtils() {}
public static void disableNBTAPILogs() {
MinecraftVersion.disableBStats();
MinecraftVersion.disableUpdateCheck();
VersionChecker.hideOk = true;
try {
Field field = MinecraftVersion.class.getDeclaredField("version");
field.setAccessible(true);
MinecraftVersion minecraftVersion;
try {
minecraftVersion = MinecraftVersion.valueOf(CustomCropsPlugin.get().getVersionManager().getServerVersion().replace("v", "MC"));
} catch (Exception ex) {
minecraftVersion = VERSION_TO_REVISION.getOrDefault(Bukkit.getServer().getBukkitVersion().split("-")[0],
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);
}
}
private static final Map<String, MinecraftVersion> VERSION_TO_REVISION = new HashMap<>() {
{
this.put("1.20", MinecraftVersion.MC1_20_R1);
this.put("1.20.1", MinecraftVersion.MC1_20_R1);
this.put("1.20.2", MinecraftVersion.MC1_20_R2);
this.put("1.20.3", MinecraftVersion.MC1_20_R3);
this.put("1.20.4", MinecraftVersion.MC1_20_R3);
this.put("1.20.5", MinecraftVersion.MC1_20_R4);
this.put("1.20.6", MinecraftVersion.MC1_20_R4);
}
};
}