Skip to content

Commit

Permalink
Lynx cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lynxplay committed Jan 3, 2025
1 parent e173da1 commit 4dd11f8
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 202 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
--- a/net/minecraft/core/component/DataComponentPatch.java
+++ b/net/minecraft/core/component/DataComponentPatch.java
@@ -86,6 +_,12 @@
buffer.writeVarInt(0);
buffer.writeVarInt(0);
} else {
+ // Paper start - data sanitization for items
+ final io.papermc.paper.util.ItemObfuscationSession itemObfuscationSession = value.map.isEmpty()
+ // always pick noop if map is empty, saves us a potential thread local lookup in currentSession.
+ ? io.papermc.paper.util.ItemObfuscationSession.noop()
+ : io.papermc.paper.util.ItemObfuscationSession.currentSession();
+ // Paper end - data sanitization for items
int i = 0;
int i1 = 0;

@@ -93,7 +_,7 @@
value.map
)) {
if (entry.getValue().isPresent()) {
- i++;
+ if (!io.papermc.paper.util.DataSanitizationUtil.shouldDrop(entry.getKey())) i++; // Paper
+ if (!io.papermc.paper.util.ItemComponentSanitizer.shouldDrop(itemObfuscationSession, entry.getKey())) i++; // Paper - data sanitization for items
} else {
i1++;
}
@@ -106,6 +_,7 @@
value.map
)) {
Optional<?> optional = entryx.getValue();
+ optional = io.papermc.paper.util.DataSanitizationUtil.override(entryx.getKey(), entryx.getValue()); // Paper
+ optional = io.papermc.paper.util.ItemComponentSanitizer.override(itemObfuscationSession, entryx.getKey(), entryx.getValue()); // Paper - data sanitization for items
if (optional.isPresent()) {
DataComponentType<?> dataComponentType = entryx.getKey();
DataComponentType.STREAM_CODEC.encode(buffer, dataComponentType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
--- a/net/minecraft/network/protocol/game/ClientboundSetEntityDataPacket.java
+++ b/net/minecraft/network/protocol/game/ClientboundSetEntityDataPacket.java
@@ -2,6 +_,8 @@

import java.util.ArrayList;
import java.util.List;
+
+import io.papermc.paper.util.ItemObfuscationBinding;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.Packet;
@@ -19,9 +_,11 @@
}

private static void pack(List<SynchedEntityData.DataValue<?>> dataValues, RegistryFriendlyByteBuf buffer) {
+ try (io.papermc.paper.util.DataSanitizationUtil.DataSanitizer ignored = io.papermc.paper.util.DataSanitizationUtil.start(true)) { // Paper - data sanitization
+ try (io.papermc.paper.util.ItemObfuscationSession ignored = io.papermc.paper.util.ItemObfuscationSession.start(true)) { // Paper - data sanitization
for (SynchedEntityData.DataValue<?> dataValue : dataValues) {
dataValue.write(buffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
buffer.writeVarInt(this.entity);
int size = this.slots.size();

+ try (io.papermc.paper.util.DataSanitizationUtil.DataSanitizer ignored = io.papermc.paper.util.DataSanitizationUtil.start(this.sanitize)) { // Paper - data sanitization
+ try (io.papermc.paper.util.ItemObfuscationSession ignored = io.papermc.paper.util.ItemObfuscationSession.start(this.sanitize)) { // Paper - data sanitization
for (int i = 0; i < size; i++) {
Pair<EquipmentSlot, ItemStack> pair = this.slots.get(i);
EquipmentSlot equipmentSlot = pair.getFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
buffer.writeVarInt(0);
} else {
- buffer.writeVarInt(value.getCount());
+ buffer.writeVarInt(io.papermc.paper.util.DataSanitizationUtil.sanitizeCount(value, value.getCount())); // Paper
+ buffer.writeVarInt(io.papermc.paper.util.ItemComponentSanitizer.sanitizeCount(io.papermc.paper.util.ItemObfuscationSession.currentSession(), value, value.getCount())); // Paper - potentially sanitize count
ITEM_STREAM_CODEC.encode(buffer, value.getItemHolder());
+ // Spigot start - filter
+ // value = value.copy();
+ // CraftItemStack.setItemMeta(value, CraftItemStack.getItemMeta(value)); // Paper - This is no longer with raw NBT being handled in metadata
+ // Paper start - adventure; conditionally render translatable components
+ boolean prev = net.minecraft.network.chat.ComponentSerialization.DONT_RENDER_TRANSLATABLES.get();
+ try (io.papermc.paper.util.DataSanitizationUtil.SafeAutoClosable unused = io.papermc.paper.util.DataSanitizationUtil.passContext(value)) {
+ try (final io.papermc.paper.util.SafeAutoClosable ignored = io.papermc.paper.util.ItemObfuscationSession.withContext(value)) {
+ net.minecraft.network.chat.ComponentSerialization.DONT_RENDER_TRANSLATABLES.set(true);
DataComponentPatch.STREAM_CODEC.encode(buffer, value.components.asPatch());
+ } finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import io.papermc.paper.configuration.constraint.Constraints;
import io.papermc.paper.configuration.type.number.DoubleOr;
import io.papermc.paper.configuration.type.number.IntOr;
import io.papermc.paper.util.DataSanitizationUtil;
import io.papermc.paper.util.ItemObfuscationBinding;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerboundPlaceRecipePacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
Expand All @@ -22,8 +19,6 @@
import org.spongepowered.configurate.objectmapping.meta.Required;
import org.spongepowered.configurate.objectmapping.meta.Setting;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;
Expand Down Expand Up @@ -375,21 +370,24 @@ public class Obfuscation extends ConfigurationPart {
public class Items extends ConfigurationPart {

public boolean enableItemObfuscation = false;
public DataSanitizationUtil.AssetObfuscationConfiguration allModels = new DataSanitizationUtil.AssetObfuscationConfiguration(true,
public ItemObfuscationBinding.AssetObfuscationConfiguration allModels = new ItemObfuscationBinding.AssetObfuscationConfiguration(
true,
Set.of(DataComponents.LODESTONE_TRACKER),
Set.of()
);

public Map<String, DataSanitizationUtil.AssetObfuscationConfiguration> modelOverrides = Map.of(
net.minecraft.world.item.Items.ELYTRA.components().get(DataComponents.ITEM_MODEL).toString(), new DataSanitizationUtil.AssetObfuscationConfiguration(true,
public Map<String, ItemObfuscationBinding.AssetObfuscationConfiguration> modelOverrides = Map.of(
net.minecraft.world.item.Items.ELYTRA.components().get(DataComponents.ITEM_MODEL).toString(),
new ItemObfuscationBinding.AssetObfuscationConfiguration(
true,
Set.of(DataComponents.DAMAGE),
Set.of()
)
);

@PostProcess
public void computeOverridenTypes() {
DataSanitizationUtil.compute(this);
public void bindDataSanatizer() {
ItemObfuscationBinding.bind(this);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.item.component.LodestoneTracker;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import org.jspecify.annotations.NullMarked;

final class ItemComponentSanitizer {
@NullMarked
public final class ItemComponentSanitizer {

/*
* This returns for types, that when configured to be serialized, should instead return these objects.
Expand All @@ -31,11 +33,12 @@ final class ItemComponentSanitizer {
}
).build();

private static <T> void put(ImmutableMap.Builder map, DataComponentType<T> type, UnaryOperator<T> object) {
@SuppressWarnings({"rawtypes", "unchecked"})
private static <T> void put(final ImmutableMap.Builder map, final DataComponentType<T> type, final UnaryOperator<T> object) {
map.put(type, object);
}

private static <T> UnaryOperator<T> empty(T object) {
private static <T> UnaryOperator<T> empty(final T object) {
return (unused) -> object;
}

Expand All @@ -52,67 +55,43 @@ private static PotionContents sanitizePotionContents(final PotionContents potion

// We cant use the empty map from enchantments because we want to keep the glow
private static ItemEnchantments dummyEnchantments() {
ItemEnchantments.Mutable obj = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY);
final ItemEnchantments.Mutable obj = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY);
obj.set(MinecraftServer.getServer().registryAccess().lookupOrThrow(Registries.ENCHANTMENT).getRandom(RandomSource.create()).orElseThrow(), 1);
return obj.toImmutable();
}

public static int sanitizeCount(ItemStack itemStack, int count) {
GlobalConfiguration.Anticheat.Obfuscation.Items items = GlobalConfiguration.get().anticheat.obfuscation.items;
// Ignore if we are not obfuscating
if (!items.enableItemObfuscation) {
return count;
}

if (DataSanitizationUtil.DATA_SANITIZER.get().isNotSanitizing()) {
return count;
}
public static int sanitizeCount(final ItemObfuscationSession obfuscationSession, final ItemStack itemStack, final int count) {
if (!ItemObfuscationBinding.ENABLED || obfuscationSession.isNotSanitizing()) return count; // Ignore if we are not obfuscating - first

if (DataSanitizationUtil.getAssetObfuscation(itemStack).sanitizeCount()) {
if (ItemObfuscationBinding.getAssetObfuscation(itemStack).sanitizeCount()) {
return 1;
} else {
return count;
}
}

public static boolean shouldDrop(DataComponentType<?> key) {
// Only drop components on obfuscation
GlobalConfiguration.Anticheat.Obfuscation.Items items = GlobalConfiguration.get().anticheat.obfuscation.items;
if (!items.enableItemObfuscation) {
return false;
}
if (DataSanitizationUtil.DATA_SANITIZER.get().isNotSanitizing()) {
return false;
}
public static boolean shouldDrop(final ItemObfuscationSession obfuscationSession, final DataComponentType<?> key) {
if (!ItemObfuscationBinding.ENABLED || obfuscationSession.isNotSanitizing()) return false; // Ignore if we are not obfuscating

DataSanitizationUtil.ContentScope scope = DataSanitizationUtil.DATA_SANITIZER.get().scope().get();
ItemStack targetItemstack = scope.itemStack();
final ItemStack targetItemstack = obfuscationSession.context().itemStack();

// Only drop if configured to do so.
return DataSanitizationUtil.getAssetObfuscation(targetItemstack).patchStrategy().get(key) == DataSanitizationUtil.BoundObfuscationConfiguration.MutationType.Drop.INSTANCE;
return ItemObfuscationBinding.getAssetObfuscation(targetItemstack).patchStrategy().get(key) == ItemObfuscationBinding.BoundObfuscationConfiguration.MutationType.Drop.INSTANCE;
}

public static Optional<?> override(DataComponentType<?> key, Optional<?> value) {
if (DataSanitizationUtil.DATA_SANITIZER.get().isNotSanitizing()) {
return value;
}
// Only drop components on obfuscation
GlobalConfiguration.Anticheat.Obfuscation.Items items = GlobalConfiguration.get().anticheat.obfuscation.items;
if (!items.enableItemObfuscation) {
return value;
}
public static Optional<?> override(final ItemObfuscationSession itemObfuscationSession, final DataComponentType<?> key, final Optional<?> value) {
if (!ItemObfuscationBinding.ENABLED || itemObfuscationSession.isNotSanitizing()) return value; // Ignore if we are not obfuscating

// Ignore removed values
if (value.isEmpty()) {
return value;
}

DataSanitizationUtil.ContentScope scope = DataSanitizationUtil.DATA_SANITIZER.get().scope().get();
ItemStack targetItemstack = scope.itemStack();
final ItemStack targetItemstack = itemObfuscationSession.context().itemStack();

return switch (DataSanitizationUtil.getAssetObfuscation(targetItemstack).patchStrategy().get(key)) {
case DataSanitizationUtil.BoundObfuscationConfiguration.MutationType.Drop unused -> Optional.empty();
case DataSanitizationUtil.BoundObfuscationConfiguration.MutationType.Sanitize sanitize -> Optional.of(sanitize.sanitizer().apply(value.get()));
return switch (ItemObfuscationBinding.getAssetObfuscation(targetItemstack).patchStrategy().get(key)) {
case final ItemObfuscationBinding.BoundObfuscationConfiguration.MutationType.Drop ignored -> Optional.empty();
case final ItemObfuscationBinding.BoundObfuscationConfiguration.MutationType.Sanitize sanitize -> Optional.of(sanitize.sanitizer().apply(value.get()));
case null -> value;
};
}
Expand Down
Loading

0 comments on commit 4dd11f8

Please sign in to comment.