9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-06 15:52:03 +00:00

添加1.20.1 mod

This commit is contained in:
XiaoMoMi
2025-04-16 18:06:41 +08:00
parent e26773d5ee
commit eaddaf4c88
25 changed files with 697 additions and 658 deletions

View File

@@ -11,10 +11,8 @@ import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.momirealms.craftengine.mod.CraftEnginePlugin;
import net.momirealms.craftengine.mod.util.NoteBlockUtils;
import net.momirealms.craftengine.mod.util.Reflections;
import org.bukkit.configuration.file.YamlConfiguration;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
@@ -30,28 +28,22 @@ public class CustomBlocks {
for (Map.Entry<ResourceLocation, Integer> entry : map.entrySet()) {
ResourceLocation replacedBlockId = entry.getKey();
boolean isNoteBlock = replacedBlockId.equals(noteBlock);
try {
Block replacedBlock = (Block) Reflections.method$DefaultedRegistry$get.invoke(BuiltInRegistries.BLOCK, replacedBlockId);
for (int i = 0; i < entry.getValue(); i++) {
ResourceLocation location = ResourceLocation.fromNamespaceAndPath("craftengine", replacedBlockId.getPath() + "_" + i);
ResourceKey<Block> resourceKey = ResourceKey.create(Registries.BLOCK, location);
BlockBehaviour.Properties properties = BlockBehaviour.Properties.of();
if (Reflections.field$BlockBehaviour$Properties$id != null) {
Reflections.field$BlockBehaviour$Properties$id.set(properties, resourceKey);
}
if (!replacedBlock.hasCollision) {
properties.noCollission();
}
CraftEngineBlock block = new CraftEngineBlock(properties);
if (isNoteBlock) {
block.setNoteBlock(true);
NoteBlockUtils.CLIENT_SIDE_NOTE_BLOCKS.add(block.defaultBlockState());
}
Registry.register(BuiltInRegistries.BLOCK, location, block);
Block.BLOCK_STATE_REGISTRY.add(block.defaultBlockState());
Block replacedBlock = BuiltInRegistries.BLOCK.getValue(replacedBlockId);
for (int i = 0; i < entry.getValue(); i++) {
ResourceLocation location = ResourceLocation.fromNamespaceAndPath("craftengine", replacedBlockId.getPath() + "_" + i);
ResourceKey<Block> resourceKey = ResourceKey.create(Registries.BLOCK, location);
BlockBehaviour.Properties properties = BlockBehaviour.Properties.of();
properties.setId(resourceKey);
if (!replacedBlock.hasCollision) {
properties.noCollission();
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
CraftEngineBlock block = new CraftEngineBlock(properties);
if (isNoteBlock) {
block.setNoteBlock(true);
NoteBlockUtils.CLIENT_SIDE_NOTE_BLOCKS.add(block.defaultBlockState());
}
Registry.register(BuiltInRegistries.BLOCK, location, block);
Block.BLOCK_STATE_REGISTRY.add(block.defaultBlockState());
}
}
NoteBlockUtils.CLIENT_SIDE_NOTE_BLOCKS.addAll(net.minecraft.world.level.block.Blocks.NOTE_BLOCK.getStateDefinition().getPossibleStates());
@@ -120,11 +112,7 @@ public class CustomBlocks {
private static BlockState createBlockData(String blockState) {
try {
Object holderLookUp = BuiltInRegistries.BLOCK;
if (Reflections.method$Registry$asLookup != null) {
holderLookUp = Reflections.method$Registry$asLookup.invoke(holderLookUp);
}
BlockStateParser.BlockResult result = (BlockStateParser.BlockResult) Reflections.method$BlockStateParser$parseForBlock.invoke(null, holderLookUp, blockState, false);
BlockStateParser.BlockResult result = BlockStateParser.parseForBlock(BuiltInRegistries.BLOCK, blockState, false);
return result.blockState();
} catch (Exception e) {
e.printStackTrace();

View File

@@ -1,68 +0,0 @@
package net.momirealms.craftengine.mod.util;
import org.bukkit.Bukkit;
import java.lang.reflect.Method;
import java.util.Objects;
public final class BukkitReflectionUtils {
private static final String PREFIX_MC = "net.minecraft.";
private static final String PREFIX_CRAFTBUKKIT = "org.bukkit.craftbukkit";
private static final String CRAFT_SERVER = "CraftServer";
private static final String CB_PKG_VERSION;
public static final int MAJOR_REVISION;
private BukkitReflectionUtils() {}
static {
final Class<?> serverClass;
if (Bukkit.getServer() == null) {
// Paper plugin Bootstrapper 1.20.6+
serverClass = Objects.requireNonNull(ReflectionUtils.getClazz("org.bukkit.craftbukkit.CraftServer"));
} else {
serverClass = Bukkit.getServer().getClass();
}
final String pkg = serverClass.getPackage().getName();
final String nmsVersion = pkg.substring(pkg.lastIndexOf(".") + 1);
if (!nmsVersion.contains("_")) {
int fallbackVersion = -1;
if (Bukkit.getServer() != null) {
try {
final Method getMinecraftVersion = serverClass.getDeclaredMethod("getMinecraftVersion");
fallbackVersion = Integer.parseInt(getMinecraftVersion.invoke(Bukkit.getServer()).toString().split("\\.")[1]);
} catch (final Exception ignored) {
}
} else {
// Paper plugin bootstrapper 1.20.6+
try {
final Class<?> sharedConstants = Objects.requireNonNull(ReflectionUtils.getClazz("net.minecraft.SharedConstants"));
final Method getCurrentVersion = sharedConstants.getDeclaredMethod("getCurrentVersion");
final Object currentVersion = getCurrentVersion.invoke(null);
final Method getName = currentVersion.getClass().getDeclaredMethod("getName");
final String versionName = (String) getName.invoke(currentVersion);
try {
fallbackVersion = Integer.parseInt(versionName.split("\\.")[1]);
} catch (final Exception ignored) {
}
} catch (final ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
MAJOR_REVISION = fallbackVersion;
} else {
MAJOR_REVISION = Integer.parseInt(nmsVersion.split("_")[1]);
}
String name = serverClass.getName();
name = name.substring(PREFIX_CRAFTBUKKIT.length());
name = name.substring(0, name.length() - CRAFT_SERVER.length());
CB_PKG_VERSION = name;
}
public static String assembleCBClass(String className) {
return PREFIX_CRAFTBUKKIT + CB_PKG_VERSION + className;
}
public static String assembleMCClass(String className) {
return PREFIX_MC + className;
}
}

View File

@@ -1,489 +0,0 @@
package net.momirealms.craftengine.mod.util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ReflectionUtils {
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private ReflectionUtils() {}
public static Class<?> getClazz(String... classes) {
for (String className : classes) {
Class<?> clazz = getClazz(className);
if (clazz != null) {
return clazz;
}
}
return null;
}
public static Class<?> getClazz(String clazz) {
try {
return Class.forName(clazz);
} catch (Throwable e) {
return null;
}
}
public static boolean classExists(@NotNull final String clazz) {
try {
Class.forName(clazz);
return true;
} catch (Throwable e) {
return false;
}
}
public static boolean methodExists(@NotNull final Class<?> clazz, @NotNull final String method, @NotNull final Class<?>... parameterTypes) {
try {
clazz.getMethod(method, parameterTypes);
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
@Nullable
public static Field getDeclaredField(final Class<?> clazz, final String field) {
try {
return setAccessible(clazz.getDeclaredField(field));
} catch (NoSuchFieldException e) {
return null;
}
}
@NotNull
public static Field getDeclaredField(@NotNull Class<?> clazz, @NotNull String... possibleNames) {
List<String> possibleNameList = Arrays.asList(possibleNames);
for (Field field : clazz.getDeclaredFields()) {
if (possibleNameList.contains(field.getName())) {
return field;
}
}
throw new RuntimeException("Class " + clazz.getName() + " does not contain a field with possible names " + Arrays.toString(possibleNames));
}
@Nullable
public static Field getDeclaredField(final Class<?> clazz, final int index) {
int i = 0;
for (final Field field : clazz.getDeclaredFields()) {
if (index == i) {
return setAccessible(field);
}
i++;
}
return null;
}
@Nullable
public static Field getInstanceDeclaredField(final Class<?> clazz, final int index) {
int i = 0;
for (final Field field : clazz.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
if (index == i) {
return setAccessible(field);
}
i++;
}
}
return null;
}
@Nullable
public static Field getDeclaredField(final Class<?> clazz, final Class<?> type, int index) {
int i = 0;
for (final Field field : clazz.getDeclaredFields()) {
if (field.getType() == type) {
if (index == i) {
return setAccessible(field);
}
i++;
}
}
return null;
}
@Nullable
public static Field getDeclaredFieldBackwards(final Class<?> clazz, final Class<?> type, int index) {
int i = 0;
Field[] fields = clazz.getDeclaredFields();
for (int j = fields.length - 1; j >= 0; j--) {
Field field = fields[j];
if (field.getType() == type) {
if (index == i) {
return setAccessible(field);
}
i++;
}
}
return null;
}
@Nullable
public static Field getInstanceDeclaredField(@NotNull Class<?> clazz, final Class<?> type, int index) {
int i = 0;
for (final Field field : clazz.getDeclaredFields()) {
if (field.getType() == type && !Modifier.isStatic(field.getModifiers())) {
if (index == i) {
return setAccessible(field);
}
i++;
}
}
return null;
}
@NotNull
public static List<Field> getDeclaredFields(final Class<?> clazz) {
List<Field> fields = new ArrayList<>();
for (Field field : clazz.getDeclaredFields()) {
fields.add(setAccessible(field));
}
return fields;
}
@NotNull
public static List<Field> getInstanceDeclaredFields(@NotNull Class<?> clazz) {
List<Field> list = new ArrayList<>();
for (Field field : clazz.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
list.add(setAccessible(field));
}
}
return list;
}
@NotNull
public static List<Field> getDeclaredFields(@NotNull final Class<?> clazz, @NotNull final Class<?> type) {
List<Field> fields = new ArrayList<>();
for (Field field : clazz.getDeclaredFields()) {
if (field.getType() == type) {
fields.add(setAccessible(field));
}
}
return fields;
}
@NotNull
public static List<Field> getInstanceDeclaredFields(@NotNull Class<?> clazz, @NotNull Class<?> type) {
List<Field> list = new ArrayList<>();
for (Field field : clazz.getDeclaredFields()) {
if (field.getType() == type && !Modifier.isStatic(field.getModifiers())) {
list.add(setAccessible(field));
}
}
return list;
}
@Nullable
public static Method getMethod(final Class<?> clazz, Class<?> returnType, final String[] possibleMethodNames, final Class<?>... parameterTypes) {
outer:
for (Method method : clazz.getMethods()) {
if (method.getParameterCount() != parameterTypes.length) {
continue;
}
Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (types[i] != parameterTypes[i]) {
continue outer;
}
}
for (String name : possibleMethodNames) {
if (name.equals(method.getName())) {
if (returnType.isAssignableFrom(method.getReturnType())) {
return method;
}
}
}
}
return null;
}
@Nullable
public static Method getMethod(final Class<?> clazz, final String[] possibleMethodNames, final Class<?>... parameterTypes) {
outer:
for (Method method : clazz.getMethods()) {
if (method.getParameterCount() != parameterTypes.length) {
continue;
}
Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (types[i] != parameterTypes[i]) {
continue outer;
}
}
for (String name : possibleMethodNames) {
if (name.equals(method.getName())) return method;
}
}
return null;
}
@Nullable
public static Method getMethod(final Class<?> clazz, Class<?> returnType, final Class<?>... parameterTypes) {
outer:
for (Method method : clazz.getMethods()) {
if (method.getParameterCount() != parameterTypes.length) {
continue;
}
Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (types[i] != parameterTypes[i]) {
continue outer;
}
}
if (returnType.isAssignableFrom(method.getReturnType())) return method;
}
return null;
}
@Nullable
public static Method getDeclaredMethod(final Class<?> clazz, Class<?> returnType, final String[] possibleMethodNames, final Class<?>... parameterTypes) {
outer:
for (Method method : clazz.getDeclaredMethods()) {
if (method.getParameterCount() != parameterTypes.length) {
continue;
}
Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (types[i] != parameterTypes[i]) {
continue outer;
}
}
for (String name : possibleMethodNames) {
if (name.equals(method.getName())) {
if (returnType.isAssignableFrom(method.getReturnType())) {
return setAccessible(method);
}
}
}
}
return null;
}
@Nullable
public static Method getDeclaredMethod(final Class<?> clazz, Class<?> returnType, final Class<?>... parameterTypes) {
outer:
for (Method method : clazz.getDeclaredMethods()) {
if (method.getParameterCount() != parameterTypes.length) {
continue;
}
Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (types[i] != parameterTypes[i]) {
continue outer;
}
}
if (returnType.isAssignableFrom(method.getReturnType())) return setAccessible(method);
}
return null;
}
@Nullable
public static Method getMethod(final Class<?> clazz, Class<?> returnType, int index) {
int i = 0;
for (Method method : clazz.getMethods()) {
if (returnType.isAssignableFrom(method.getReturnType())) {
if (i == index) {
return setAccessible(method);
}
i++;
}
}
return null;
}
@Nullable
public static Method getStaticMethod(final Class<?> clazz, Class<?> returnType, final Class<?>... parameterTypes) {
outer:
for (Method method : clazz.getMethods()) {
if (method.getParameterCount() != parameterTypes.length) {
continue;
}
if (!Modifier.isStatic(method.getModifiers())) {
continue;
}
Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (types[i] != parameterTypes[i]) {
continue outer;
}
}
if (returnType.isAssignableFrom(method.getReturnType()))
return setAccessible(method);
}
return null;
}
@Nullable
public static Method getStaticMethod(final Class<?> clazz, Class<?> returnType, String[] possibleNames, final Class<?>... parameterTypes) {
outer:
for (Method method : clazz.getMethods()) {
if (method.getParameterCount() != parameterTypes.length) {
continue;
}
if (!Modifier.isStatic(method.getModifiers())) {
continue;
}
Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (types[i] != parameterTypes[i]) {
continue outer;
}
}
if (returnType.isAssignableFrom(method.getReturnType())) {
for (String name : possibleNames) {
if (name.equals(method.getName())) {
return setAccessible(method);
}
}
}
}
return null;
}
public static Method getStaticMethod(final Class<?> clazz, int index) {
int i = 0;
for (Method method : clazz.getMethods()) {
if (Modifier.isStatic(method.getModifiers())) {
if (i == index) {
return setAccessible(method);
}
i++;
}
}
return null;
}
@Nullable
public static Method getMethod(final Class<?> clazz, int index) {
int i = 0;
for (Method method : clazz.getMethods()) {
if (i == index) {
return setAccessible(method);
}
i++;
}
return null;
}
public static Method getMethodOrElseThrow(final Class<?> clazz, final String[] possibleMethodNames, final Class<?>[] parameterTypes) throws NoSuchMethodException {
Method method = getMethod(clazz, possibleMethodNames, parameterTypes);
if (method == null) {
throw new NoSuchMethodException("No method found with possible names " + Arrays.toString(possibleMethodNames) + " with parameters " +
Arrays.toString(parameterTypes) + " in class " + clazz.getName());
}
return method;
}
@NotNull
public static List<Method> getMethods(@NotNull Class<?> clazz, @NotNull Class<?> returnType, @NotNull Class<?>... parameterTypes) {
List<Method> list = new ArrayList<>();
for (Method method : clazz.getMethods()) {
if (!returnType.isAssignableFrom(method.getReturnType()) // check type
|| method.getParameterCount() != parameterTypes.length // check length
) continue;
Class<?>[] types = method.getParameterTypes();
outer: {
for (int i = 0; i < types.length; i++) {
if (types[i] != parameterTypes[i]) {
break outer;
}
}
list.add(method);
}
}
return list;
}
@NotNull
public static <T extends AccessibleObject> T setAccessible(@NotNull final T o) {
o.setAccessible(true);
return o;
}
@Nullable
public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes) {
try {
return clazz.getConstructor(parameterTypes);
} catch (NoSuchMethodException | SecurityException ignore) {
return null;
}
}
@Nullable
public static Constructor<?> getDeclaredConstructor(Class<?> clazz, Class<?>... parameterTypes) {
try {
return setAccessible(clazz.getDeclaredConstructor(parameterTypes));
} catch (NoSuchMethodException | SecurityException ignore) {
return null;
}
}
@Nullable
public static Constructor<?> getConstructor(Class<?> clazz, int index) {
try {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
if (index < 0 || index >= constructors.length) {
throw new IndexOutOfBoundsException("Invalid constructor index: " + index);
}
return setAccessible(constructors[index]);
} catch (SecurityException e) {
return null;
}
}
@NotNull
public static Constructor<?> getTheOnlyConstructor(Class<?> clazz) {
Constructor<?>[] constructors = clazz.getConstructors();
if (constructors.length != 1) {
throw new RuntimeException("This class is expected to have only one constructor but it has " + constructors.length);
}
return constructors[0];
}
public static MethodHandle unreflectGetter(Field field) throws IllegalAccessException {
try {
return LOOKUP.unreflectGetter(field);
} catch (IllegalAccessException e) {
field.setAccessible(true);
return LOOKUP.unreflectGetter(field);
}
}
public static MethodHandle unreflectMethod(Method method) throws IllegalAccessException {
try {
return LOOKUP.unreflect(method);
} catch (IllegalAccessException e) {
method.setAccessible(true);
return LOOKUP.unreflect(method);
}
}
public static VarHandle findVarHandle(Class<?> clazz, String name, Class<?> type) {
try {
return MethodHandles.privateLookupIn(clazz, LOOKUP)
.findVarHandle(clazz, name, type);
} catch (NoSuchFieldException | SecurityException | IllegalAccessException e) {
return null;
}
}
public static VarHandle findVarHandle(Field field) {
try {
return MethodHandles.privateLookupIn(field.getDeclaringClass(), LOOKUP)
.findVarHandle(field.getDeclaringClass(), field.getName(), field.getType());
} catch (IllegalAccessException | NoSuchFieldException e) {
return null;
}
}
}

View File

@@ -1,37 +0,0 @@
package net.momirealms.craftengine.mod.util;
import net.minecraft.commands.arguments.blocks.BlockStateParser;
import net.minecraft.core.DefaultedRegistry;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockBehaviour;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import static java.util.Objects.requireNonNull;
public class Reflections {
public static final Method method$DefaultedRegistry$get = requireNonNull(
ReflectionUtils.getMethod(
DefaultedRegistry.class, Object.class, ResourceLocation.class
)
);
public static final Field field$BlockBehaviour$Properties$id = ReflectionUtils.getDeclaredField(
BlockBehaviour.Properties.class, ResourceKey.class, 0
);
public static final Method method$BlockStateParser$parseForBlock = requireNonNull(
ReflectionUtils.getStaticMethod(
BlockStateParser.class, BlockStateParser.BlockResult.class, new String[]{"parseForBlock"}, HolderLookup.class, String.class, boolean.class
)
);
public static final Method method$Registry$asLookup = ReflectionUtils.getMethod(
Registry.class, new String[]{"asLookup"}
);
}