9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-24 17:39:30 +00:00

Merge branch 'Xiao-MoMi:dev' into dev

This commit is contained in:
jhqwqmc
2025-04-12 02:55:46 +08:00
committed by GitHub
11 changed files with 123 additions and 23 deletions

View File

@@ -343,8 +343,7 @@ public class RecipeEventListener implements Listener {
ItemStack itemStack = event.getItem();
if (ItemUtils.isEmpty(itemStack)) return;
try {
@SuppressWarnings("unchecked")
Optional<Object> optionalMCRecipe = (Optional<Object>) Reflections.method$RecipeManager$getRecipeFor1.invoke(
Optional<Object> optionalMCRecipe = FastNMS.INSTANCE.method$RecipeManager$getRecipeFor(
BukkitRecipeManager.nmsRecipeManager(),
Reflections.instance$RecipeType$CAMPFIRE_COOKING,
Reflections.constructor$SingleRecipeInput.newInstance(FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack)),

View File

@@ -406,7 +406,7 @@ public class BukkitInjector {
InjectedCacheCheck injectedCacheCheck = (InjectedCacheCheck) thisObj;
Object type = injectedCacheCheck.recipeType();
Object lastRecipe = injectedCacheCheck.lastRecipe();
Optional<Pair<Object, Object>> optionalRecipe = (Optional<Pair<Object, Object>>) Reflections.method$RecipeManager$getRecipeFor0.invoke(mcRecipeManager, type, args[0], args[1], lastRecipe);
Optional<Pair<Object, Object>> optionalRecipe = FastNMS.INSTANCE.method$RecipeManager$getRecipeFor(mcRecipeManager, type, args[0], args[1], lastRecipe);
if (optionalRecipe.isPresent()) {
Pair<Object, Object> pair = optionalRecipe.get();
Object resourceLocation = pair.getFirst();
@@ -474,7 +474,7 @@ public class BukkitInjector {
InjectedCacheCheck injectedCacheCheck = (InjectedCacheCheck) thisObj;
Object type = injectedCacheCheck.recipeType();
Object lastRecipe = injectedCacheCheck.lastRecipe();
Optional<Object> optionalRecipe = (Optional<Object>) Reflections.method$RecipeManager$getRecipeFor0.invoke(mcRecipeManager, type, args[0], args[1], lastRecipe);
Optional<Object> optionalRecipe = (Optional<Object>) FastNMS.INSTANCE.method$RecipeManager$getRecipeFor(mcRecipeManager, type, args[0], args[1], lastRecipe);
if (optionalRecipe.isPresent()) {
Object holder = optionalRecipe.get();
Object id = FastNMS.INSTANCE.field$RecipeHolder$id(holder);
@@ -542,7 +542,7 @@ public class BukkitInjector {
InjectedCacheCheck injectedCacheCheck = (InjectedCacheCheck) thisObj;
Object type = injectedCacheCheck.recipeType();
Object lastRecipe = injectedCacheCheck.lastRecipe();
Optional<Object> optionalRecipe = (Optional<Object>) Reflections.method$RecipeManager$getRecipeFor0.invoke(mcRecipeManager, type, args[0], args[1], lastRecipe);
Optional<Object> optionalRecipe = (Optional<Object>) FastNMS.INSTANCE.method$RecipeManager$getRecipeFor(mcRecipeManager, type, args[0], args[1], lastRecipe);
if (optionalRecipe.isPresent()) {
Object holder = optionalRecipe.get();
Object id = FastNMS.INSTANCE.field$RecipeHolder$id(holder);
@@ -602,7 +602,7 @@ public class BukkitInjector {
InjectedCacheCheck injectedCacheCheck = (InjectedCacheCheck) thisObj;
Object type = injectedCacheCheck.recipeType();
Object lastRecipe = injectedCacheCheck.lastRecipe();
Optional<Object> optionalRecipe = (Optional<Object>) Reflections.method$RecipeManager$getRecipeFor1.invoke(mcRecipeManager, type, args[0], args[1], lastRecipe);
Optional<Object> optionalRecipe = (Optional<Object>) FastNMS.INSTANCE.method$RecipeManager$getRecipeFor(mcRecipeManager, type, args[0], args[1], lastRecipe);
if (optionalRecipe.isPresent()) {
Object holder = optionalRecipe.get();
Object id = FastNMS.INSTANCE.field$RecipeHolder$id(holder);

View File

@@ -8,8 +8,8 @@ import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.bukkit.plugin.network.impl.PacketIds1_20;
import net.momirealms.craftengine.bukkit.plugin.network.impl.PacketIds1_20_5;
import net.momirealms.craftengine.bukkit.plugin.network.id.PacketIds1_20;
import net.momirealms.craftengine.bukkit.plugin.network.id.PacketIds1_20_5;
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.plugin.CraftEngine;
@@ -127,6 +127,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
private void registerPacketHandlers() {
registerNMSPacketConsumer(PacketConsumers.LEVEL_CHUNK_WITH_LIGHT, Reflections.clazz$ClientboundLevelChunkWithLightPacket);
registerNMSPacketConsumer(PacketConsumers.PLAYER_INFO_UPDATE, Reflections.clazz$ClientboundPlayerInfoUpdatePacket);
registerNMSPacketConsumer(PacketConsumers.PLAYER_ACTION, Reflections.clazz$ServerboundPlayerActionPacket);
registerNMSPacketConsumer(PacketConsumers.SWING_HAND, Reflections.clazz$ServerboundSwingPacket);
registerNMSPacketConsumer(PacketConsumers.USE_ITEM_ON, Reflections.clazz$ServerboundUseItemOnPacket);

View File

@@ -321,6 +321,50 @@ public class PacketConsumers {
}
};
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> PLAYER_INFO_UPDATE = (user, event, packet) -> {
try {
if (!user.isOnline()) return;
if (!Config.interceptPlayerInfo()) return;
List<Object> entries = FastNMS.INSTANCE.field$ClientboundPlayerInfoUpdatePacket$entries(packet);
if (entries instanceof MarkedArrayList) {
return;
}
EnumSet<? extends Enum<?>> enums = FastNMS.INSTANCE.field$ClientboundPlayerInfoUpdatePacket$actions(packet);
outer: {
for (Object entry : enums) {
if (entry == Reflections.instance$ClientboundPlayerInfoUpdatePacket$Action$UPDATE_DISPLAY_NAME) {
break outer;
}
}
return;
}
List<Object> newEntries = new MarkedArrayList<>();
Reflections.field$ClientboundPlayerInfoUpdatePacket$entries.set(packet, newEntries);
for (Object entry : entries) {
Object mcComponent = FastNMS.INSTANCE.field$ClientboundPlayerInfoUpdatePacket$Entry$displayName(entry);
if (mcComponent == null) {
newEntries.add(entry);
continue;
}
String json = ComponentUtils.minecraftToJson(mcComponent);
Map<String, Component> tokens = CraftEngine.instance().imageManager().matchTags(json);
if (tokens.isEmpty()) {
newEntries.add(entry);
continue;
}
Component component = AdventureHelper.jsonToComponent(json);
for (Map.Entry<String, Component> token : tokens.entrySet()) {
component = component.replaceText(b -> b.matchLiteral(token.getKey()).replacement(token.getValue()));
}
Object newEntry = FastNMS.INSTANCE.constructor$ClientboundPlayerInfoUpdatePacket$Entry(entry, ComponentUtils.adventureToMinecraft(component));
newEntries.add(newEntry);
}
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundPlayerInfoUpdatePacket", e);
}
};
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> TEAM_1_20 = (user, event) -> {
if (!Config.interceptTeam()) return;
try {

View File

@@ -1,4 +1,4 @@
package net.momirealms.craftengine.bukkit.plugin.network.impl;
package net.momirealms.craftengine.bukkit.plugin.network.id;
import com.google.gson.JsonElement;
import net.momirealms.craftengine.bukkit.nms.FastNMS;

View File

@@ -1,4 +1,4 @@
package net.momirealms.craftengine.bukkit.plugin.network.impl;
package net.momirealms.craftengine.bukkit.plugin.network.id;
import net.momirealms.craftengine.bukkit.plugin.network.PacketIds;
import net.momirealms.craftengine.bukkit.util.Reflections;

View File

@@ -1,4 +1,4 @@
package net.momirealms.craftengine.bukkit.plugin.network.impl;
package net.momirealms.craftengine.bukkit.plugin.network.id;
import net.momirealms.craftengine.bukkit.plugin.network.PacketIds;

View File

@@ -1766,6 +1766,36 @@ public class Reflections {
)
);
public static final Field field$ClientboundPlayerInfoUpdatePacket$entries = requireNonNull(
ReflectionUtils.getDeclaredField(
clazz$ClientboundPlayerInfoUpdatePacket, List.class, 0
)
);
public static final Class<?> clazz$ClientboundPlayerInfoUpdatePacket$Action = requireNonNull(
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("network.protocol.game.ClientboundPlayerInfoUpdatePacket$Action"),
BukkitReflectionUtils.assembleMCClass("network.protocol.game.ClientboundPlayerInfoUpdatePacket$a")
)
);
public static final Method method$ClientboundPlayerInfoUpdatePacket$Action$values = requireNonNull(
ReflectionUtils.getStaticMethod(
clazz$ClientboundPlayerInfoUpdatePacket$Action, clazz$ClientboundPlayerInfoUpdatePacket$Action.arrayType()
)
);
public static final Object instance$ClientboundPlayerInfoUpdatePacket$Action$UPDATE_DISPLAY_NAME;
static {
try {
Object[] values = (Object[]) method$ClientboundPlayerInfoUpdatePacket$Action$values.invoke(null);
instance$ClientboundPlayerInfoUpdatePacket$Action$UPDATE_DISPLAY_NAME = values[5];
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@Deprecated
public static final Field field$ClientboundLevelChunkWithLightPacket$chunkData = requireNonNull(
ReflectionUtils.getDeclaredField(
@@ -4811,17 +4841,23 @@ public class Reflections {
.map(it -> ReflectionUtils.getConstructor(it, clazz$ItemStack))
.orElse(null);
// 1.20.1-1.21.1
public static final Method method$RecipeManager$getRecipeFor0 =
ReflectionUtils.getMethod(
clazz$RecipeManager, Optional.class, clazz$RecipeType, clazz$Container, clazz$Level, clazz$ResourceLocation
);
// 1.21.2+
public static final Method method$RecipeManager$getRecipeFor1 =
ReflectionUtils.getMethod(
clazz$RecipeManager, Optional.class, clazz$RecipeType, clazz$RecipeInput, clazz$Level, clazz$ResourceKey
);
// // 1.20.1-1.20.6
// public static final Method method$RecipeManager$getRecipeFor0 =
// ReflectionUtils.getMethod(
// clazz$RecipeManager, Optional.class, clazz$RecipeType, clazz$Container, clazz$Level, clazz$ResourceLocation
// );
//
// // 1.21.1
// public static final Method method$RecipeManager$getRecipeFor2 =
// ReflectionUtils.getMethod(
// clazz$RecipeManager, Optional.class, clazz$RecipeType, clazz$RecipeInput, clazz$Level, clazz$ResourceLocation
// );
//
// // 1.21.2+
// public static final Method method$RecipeManager$getRecipeFor1 =
// ReflectionUtils.getMethod(
// clazz$RecipeManager, Optional.class, clazz$RecipeType, clazz$RecipeInput, clazz$Level, clazz$ResourceKey
// );
// 1.21+
public static final Field field$SingleRecipeInput$item = Optional.ofNullable(clazz$SingleRecipeInput)

View File

@@ -652,6 +652,10 @@ public class Config {
return instance.image$intercept_packets$armor_stand;
}
public static boolean interceptPlayerInfo() {
return instance.image$intercept_packets$player_info;
}
public YamlDocument loadOrCreateYamlData(String fileName) {
File file = new File(this.plugin.dataFolderFile(), fileName);
if (!file.exists()) {

View File

@@ -0,0 +1,16 @@
package net.momirealms.craftengine.core.util;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
public class MarkedArrayList<T> extends ArrayList<T> {
public MarkedArrayList() {
}
public MarkedArrayList(@NotNull Collection<? extends T> c) {
super(c);
}
}

View File

@@ -51,7 +51,7 @@ byte_buddy_version=1.17.5
ahocorasick_version=0.6.3
snake_yaml_version=2.4
anti_grief_version=0.13
nms_helper_version=0.55
nms_helper_version=0.56.2
# Ignite Dependencies
mixinextras_version=0.4.1
mixin_version=0.15.2+mixin.0.8.7