Skip to content

Commit

Permalink
Require a RegistryWrapper.WrapperLookup in PolymerItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Apr 25, 2024
1 parent 8c95c6a commit c098c1d
Show file tree
Hide file tree
Showing 19 changed files with 202 additions and 47 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ fabric_version=0.97.1+1.20.5

maven_group = eu.pb4

mod_version = 0.8.0-beta.6
mod_version = 0.8.0-beta.7

minecraft_version_supported = ">=1.20.5-"

packet_tweaker_version = 0.5.1+24w03b
packet_tweaker_version = 0.5.2+1.20.5

is_stable = false
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private PolymerCommonUtils(){}

public static final SimpleEvent<ResourcePackChangeCallback> ON_RESOURCE_PACK_STATUS_CHANGE = new SimpleEvent<>();
private static Path cachedClientPath;
private final static String SAFE_CLIENT_SHA1 = "f22497600c8f87685c7d6c7c02239952ece3268e";
private final static String SAFE_CLIENT_SHA1 = "c6b92b2374a629f20802bb284f98a4ee790e950a";
private final static String SAFE_CLIENT_URL = "https://piston-data.mojang.com/v1/objects/" + SAFE_CLIENT_SHA1 + "/client.jar";
private static Path cachedClientJarRoot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.ApiStatus;

Expand Down Expand Up @@ -36,4 +39,11 @@ public static ServerPlayerEntity getPlayer() {
public static boolean isClientThread() {
return MinecraftClient.getInstance().isOnThread();
}

public static RegistryWrapper.WrapperLookup getLookup() {
if (MinecraftClient.getInstance().world != null) {
return MinecraftClient.getInstance().world.getRegistryManager();
}
return DynamicRegistryManager.of(Registries.REGISTRIES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;
Expand All @@ -30,8 +31,8 @@ public Item getPolymerItem(ItemStack itemStack, @Nullable ServerPlayerEntity pla
return Items.PLAYER_HEAD;
}

public ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipType, ServerPlayerEntity player) {
ItemStack out = PolymerItem.super.getPolymerItemStack(itemStack, tooltipType, player);
public ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipType, RegistryWrapper.WrapperLookup lookup, ServerPlayerEntity player) {
ItemStack out = PolymerItem.super.getPolymerItemStack(itemStack, tooltipType, lookup, player);

out.set(DataComponentTypes.PROFILE, PolymerUtils.createProfileComponent(
this.polymerBlock.getPolymerSkinValue(this.getBlock().getDefaultState(), BlockPos.ORIGIN, player),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package eu.pb4.polymer.core.api.item;

import eu.pb4.polymer.core.api.utils.PolymerSyncedObject;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import net.minecraft.block.BlockState;
import net.minecraft.client.item.TooltipType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -34,8 +36,20 @@ public interface PolymerItem extends PolymerSyncedObject<Item> {
* @param player Player for which it's send
* @return Client-side ItemStack
*/
default ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipType, @Nullable ServerPlayerEntity player) {
return PolymerItemUtils.createItemStack(itemStack, tooltipType, player);
default ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipType, RegistryWrapper.WrapperLookup lookup, @Nullable ServerPlayerEntity player) {
if (PolymerImplUtils.POLYMER_ITEM_CLASS_CACHE.getBoolean(this.getClass())) {
RegistryWrapper.WrapperLookup old = null;
if (player == null) {
old = PolymerImplUtils.WRAPPER_LOOKUP_PASSER.get();
PolymerImplUtils.WRAPPER_LOOKUP_PASSER.set(lookup);
}
var x = this.getPolymerItemStack(itemStack, tooltipType, player);
if (player == null) {
PolymerImplUtils.WRAPPER_LOOKUP_PASSER.set(old);
}
return x;
}
return PolymerItemUtils.createItemStack(itemStack, tooltipType, lookup, player);
}


Expand Down Expand Up @@ -65,7 +79,7 @@ default int getPolymerArmorColor(ItemStack itemStack, @Nullable ServerPlayerEnti

/**
* This method allows to modify tooltip text
* If you just want to add your own one, use {@link Item#appendTooltip(ItemStack, World, List, TooltipContext)}
* If you just want to add your own one, use {@link Item#appendTooltip(ItemStack, Item.TooltipContext, List, TooltipType)}
*
* @param tooltip Current tooltip text
* @param stack Server-side ItemStack
Expand All @@ -81,4 +95,12 @@ default Item getPolymerReplacement(ServerPlayerEntity player) {
default boolean handleMiningOnServer(ItemStack tool, BlockState targetBlock, BlockPos pos, ServerPlayerEntity player) {
return false;
}

/**
* @deprecated Replaced with PolymerItem#getPolymerItemStack(ItemStack, TooltipType, ServerPlayerEntity)
*/
@Deprecated(forRemoval = true)
default ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipType, @Nullable ServerPlayerEntity player) {
return PolymerItemUtils.createItemStack(itemStack, tooltipType, player);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package eu.pb4.polymer.core.api.item;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import eu.pb4.polymer.common.api.events.BooleanEvent;
import eu.pb4.polymer.common.api.events.FunctionEvent;
Expand All @@ -9,6 +8,7 @@
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import eu.pb4.polymer.core.api.utils.PolymerUtils;
import eu.pb4.polymer.core.impl.PolymerImpl;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import eu.pb4.polymer.core.impl.TransformingDataComponent;
import eu.pb4.polymer.core.impl.compat.polymc.PolyMcUtils;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
Expand All @@ -18,19 +18,28 @@
import net.minecraft.component.DataComponentType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.*;
import net.minecraft.item.*;
import net.minecraft.item.BlockPredicatesChecker;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.trim.ArmorTrim;
import net.minecraft.nbt.*;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.Unit;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

public final class PolymerItemUtils {
Expand All @@ -48,7 +57,7 @@ public final class PolymerItemUtils {
* You can also return new ItemStack, however please keep previous nbt so other modifications aren't removed if not needed!
*/
public static final FunctionEvent<ItemModificationEventHandler, ItemStack> ITEM_MODIFICATION_EVENT = new FunctionEvent<>();
private static final DataComponentType<?>[] COMPONENTS_TO_COPY = { DataComponentTypes.CAN_BREAK, DataComponentTypes.CAN_PLACE_ON,
private static final DataComponentType<?>[] COMPONENTS_TO_COPY = {DataComponentTypes.CAN_BREAK, DataComponentTypes.CAN_PLACE_ON,
DataComponentTypes.BLOCK_ENTITY_DATA, DataComponentTypes.TRIM,
DataComponentTypes.TOOL,
DataComponentTypes.MAX_STACK_SIZE,
Expand All @@ -73,7 +82,6 @@ public final class PolymerItemUtils {
DataComponentTypes.POTION_CONTENTS,
DataComponentTypes.CUSTOM_NAME,
};

@SuppressWarnings("rawtypes")
private static final List<HideableTooltip> HIDEABLE_TOOLTIPS = List.of(
HideableTooltip.of(DataComponentTypes.ATTRIBUTE_MODIFIERS, AttributeModifiersComponent::withShowInTooltip),
Expand All @@ -84,8 +92,8 @@ public final class PolymerItemUtils {
HideableTooltip.of(DataComponentTypes.CAN_BREAK, BlockPredicatesChecker::withShowInTooltip),
HideableTooltip.of(DataComponentTypes.CAN_PLACE_ON, BlockPredicatesChecker::withShowInTooltip)
);

private static final Set<DataComponentType<?>> UNSYNCED_COMPONENTS = new ObjectOpenCustomHashSet<>(CommonImplUtils.IDENTITY_HASH);
private static final RegistryWrapper.WrapperLookup FALLBACK_LOOKUP = DynamicRegistryManager.of(Registries.REGISTRIES);

private PolymerItemUtils() {
}
Expand All @@ -97,8 +105,8 @@ private PolymerItemUtils() {
* @param player Player being sent to
* @return Client side ItemStack
*/
public static ItemStack getPolymerItemStack(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
return getPolymerItemStack(itemStack, PolymerUtils.getTooltipType(player), player);
public static ItemStack getPolymerItemStack(ItemStack itemStack, RegistryWrapper.WrapperLookup lookup, @Nullable ServerPlayerEntity player) {
return getPolymerItemStack(itemStack, PolymerUtils.getTooltipType(player), lookup, player);
}

/**
Expand All @@ -108,18 +116,19 @@ public static ItemStack getPolymerItemStack(ItemStack itemStack, @Nullable Serve
* @param tooltipContext Tooltip Context
* @param player Player being sent to
* @return Client side ItemStack
*
*/
public static ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipContext, @Nullable ServerPlayerEntity player) {
public static ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipContext, RegistryWrapper.WrapperLookup lookup, @Nullable ServerPlayerEntity player) {
if (getPolymerIdentifier(itemStack) != null) {
return itemStack;
} else if (itemStack.getItem() instanceof PolymerItem item) {
return item.getPolymerItemStack(itemStack, tooltipContext, player);
return item.getPolymerItemStack(itemStack, tooltipContext, lookup, player);
} else if (isPolymerServerItem(itemStack, player)) {
return createItemStack(itemStack, tooltipContext, player);
return createItemStack(itemStack, tooltipContext, lookup, player);
}

if (ITEM_CHECK.invoke((x) -> x.test(itemStack))) {
return createItemStack(itemStack, player);
return createItemStack(itemStack, tooltipContext, lookup, player);
}

return itemStack;
Expand All @@ -131,12 +140,13 @@ public static ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType too
* @param itemStack Client side ItemStack
* @return Server side ItemStack
*/
public static ItemStack getRealItemStack(ItemStack itemStack) {
public static ItemStack getRealItemStack(ItemStack itemStack, RegistryWrapper.WrapperLookup lookup) {
var custom = itemStack.get(DataComponentTypes.CUSTOM_DATA);

if (custom != null && custom.contains(POLYMER_STACK)) {
try {
return custom.get(POLYMER_STACK_CODEC).result().orElse(itemStack);
//noinspection deprecation
return POLYMER_STACK_CODEC.decode(RegistryOps.of(NbtOps.INSTANCE, lookup), NbtOps.INSTANCE.getMap(custom.getNbt()).getOrThrow()).getOrThrow();
} catch (Throwable ignored) {

}
Expand Down Expand Up @@ -228,14 +238,36 @@ public static boolean isPolymerServerItem(ItemStack itemStack, @Nullable ServerP
return ITEM_CHECK.invoke((x) -> x.test(itemStack));
}

public static ItemStack createMinimalItemStack(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
return createMinimalItemStack(itemStack, findLookupWrapper(player), player);
}

private static RegistryWrapper.WrapperLookup findLookupWrapper(ServerPlayerEntity player) {
RegistryWrapper.WrapperLookup lookup;
if (player != null) {
lookup = player.getRegistryManager();
} else {
lookup = PacketContext.get().getRegistryWrapperLookup();

if (lookup == null) {
lookup = PolymerImplUtils.WRAPPER_LOOKUP_PASSER.get();

if (lookup == null) {
lookup = FALLBACK_LOOKUP;
}
}
}
return lookup;
}

/**
* This method creates minimal representation of ItemStack
*
* @param itemStack Server side ItemStack
* @param player Player seeing it
* @return Client side ItemStack
*/
public static ItemStack createMinimalItemStack(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
public static ItemStack createMinimalItemStack(ItemStack itemStack, RegistryWrapper.WrapperLookup lookup, @Nullable ServerPlayerEntity player) {
Item item = itemStack.getItem();
var x = itemStack.get(DataComponentTypes.CUSTOM_MODEL_DATA);
int cmd = x != null ? x.value() : -1;
Expand All @@ -247,7 +279,13 @@ public static ItemStack createMinimalItemStack(ItemStack itemStack, @Nullable Se

ItemStack out = new ItemStack(item, itemStack.getCount());

out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT.with(POLYMER_STACK_CODEC, itemStack).result().get());
try {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(
(NbtCompound) POLYMER_STACK_CODEC.encoder().encodeStart(RegistryOps.of(NbtOps.INSTANCE, lookup), itemStack).getOrThrow()
));
} catch (Throwable e) {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT.with(POLYMER_STACK_ID_CODEC, Registries.ITEM.getId(itemStack.getItem())).getOrThrow());
}

if (cmd != -1) {
out.set(DataComponentTypes.CUSTOM_MODEL_DATA, new CustomModelDataComponent(cmd));
Expand All @@ -274,6 +312,11 @@ public static ItemStack createItemStack(ItemStack itemStack, @Nullable ServerPla
return createItemStack(itemStack, PolymerUtils.getTooltipType(player), player);
}


public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltipContext, @Nullable ServerPlayerEntity player) {
return createItemStack(itemStack, tooltipContext, findLookupWrapper(player), player);
}

/**
* This method creates full (vanilla like) representation of ItemStack
*
Expand All @@ -282,7 +325,7 @@ public static ItemStack createItemStack(ItemStack itemStack, @Nullable ServerPla
* @param player Player seeing it
* @return Client side ItemStack
*/
public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltipContext, @Nullable ServerPlayerEntity player) {
public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltipContext, RegistryWrapper.WrapperLookup lookup, @Nullable ServerPlayerEntity player) {
Item item = itemStack.getItem();
int cmd = -1;
int color = -1;
Expand Down Expand Up @@ -312,8 +355,14 @@ public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltip
out.set((DataComponentType) key, (Object) itemStack.get(key));
}
}
try {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(
(NbtCompound) POLYMER_STACK_CODEC.encoder().encodeStart(RegistryOps.of(NbtOps.INSTANCE, lookup), itemStack).getOrThrow()
));
} catch (Throwable e) {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT.with(POLYMER_STACK_ID_CODEC, Registries.ITEM.getId(itemStack.getItem())).getOrThrow());
}

out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT.with(POLYMER_STACK_CODEC, itemStack).result().get());

if (cmd == -1 && itemStack.contains(DataComponentTypes.CUSTOM_MODEL_DATA)) {
out.set(DataComponentTypes.CUSTOM_MODEL_DATA, itemStack.get(DataComponentTypes.CUSTOM_MODEL_DATA));
Expand Down Expand Up @@ -419,7 +468,7 @@ public static boolean isPolymerComponent(DataComponentType<?> type) {
}

public static ItemStack getClientItemStack(ItemStack stack, ServerPlayerEntity player) {
var out = getPolymerItemStack(stack, player);
var out = getPolymerItemStack(stack, player.getRegistryManager(), player);
if (CompatStatus.POLYMC) {
out = PolyMcUtils.toVanilla(out, player);
}
Expand All @@ -439,6 +488,7 @@ private record HideableTooltip<T>(DataComponentType<T> type, Predicate<T> should
public static <T> HideableTooltip<T> of(DataComponentType<T> type, TooltipSetter<T> setter) {
return new HideableTooltip<>(type, x -> true, setter);
}

public static <T> HideableTooltip<T> of(DataComponentType<T> type, Predicate<T> shouldSet, TooltipSetter<T> setter) {
return new HideableTooltip<>(type, shouldSet, setter);
}
Expand All @@ -451,4 +501,19 @@ interface TooltipSetter<T> {
T setTooltip(T val, boolean value);
}
}

@Deprecated(forRemoval = true)
public static ItemStack getRealItemStack(ItemStack itemStack) {
return getRealItemStack(itemStack, findLookupWrapper(null));
}

@Deprecated(forRemoval = true)
public static ItemStack getPolymerItemStack(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
return getPolymerItemStack(itemStack, PolymerUtils.getTooltipType(player), player);
}

@Deprecated(forRemoval = true)
public static ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipContext, @Nullable ServerPlayerEntity player) {
return getPolymerItemStack(itemStack, tooltipContext, findLookupWrapper(player), player);
}
}
Loading

0 comments on commit c098c1d

Please sign in to comment.