Skip to content

Commit

Permalink
Fix /polymer client-item command
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Apr 30, 2024
1 parent c098c1d commit 17f80a9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx4G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.20.5
yarn_mappings=1.20.5+build.1
minecraft_version=1.20.6
yarn_mappings=1.20.6+build.1
loader_version=0.15.10

#Fabric api
fabric_version=0.97.1+1.20.5
fabric_version=0.97.6+1.20.6

maven_group = eu.pb4

mod_version = 0.8.0-beta.7
mod_version = 0.8.0-beta.8

minecraft_version_supported = ">=1.20.5-"

Expand Down
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 = "c6b92b2374a629f20802bb284f98a4ee790e950a";
private final static String SAFE_CLIENT_SHA1 = "05b6f1c6b46a29d6ea82b4e0d42190e42402030f";
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 Expand Up @@ -126,16 +126,16 @@ public static void executeWithNetworkingLogic(Runnable runnable) {
*/
public static void executeWithPlayerContext(ServerPlayerEntity player, Runnable runnable) {
var oldPlayer = CommonImplUtils.getPlayer();
var oldTarget = PacketContext.get().getPlayer();
var oldTarget = PacketContext.get().getClientConnection();
var oldPacket = PacketContext.get().getEncodedPacket();

CommonImplUtils.setPlayer(player);
PacketContext.setContext(((CommonNetworkHandlerExt)player.networkHandler).polymerCommon$getConnection(), null);
PacketContext.setContext(player != null ? ((CommonNetworkHandlerExt)player.networkHandler).polymerCommon$getConnection() : null, null);

runnable.run();

CommonImplUtils.setPlayer(oldPlayer);
PacketContext.setContext(oldTarget != null ? ((CommonNetworkHandlerExt)player.networkHandler).polymerCommon$getConnection() : null, oldPacket);
PacketContext.setContext(oldTarget, oldPacket);
}


Expand Down
2 changes: 1 addition & 1 deletion polymer-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ dependencies {
exclude(group: "net.fabricmc")
}

modCompileOnly("xyz.nucleoid:server-translations-api:2.0.0-beta.2+1.19.4-pre2")
modCompileOnly("xyz.nucleoid:server-translations-api:2.3.0+1.20.5-rc2")
//modLocalRuntime("xyz.nucleoid:server-translations-api:2.0.0-beta.2+1.19.4-pre2")

modCompileOnly 'com.github.iPortalTeam:ImmersivePortalsMod:v5.1.0-mc1.20.4'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.pb4.polymer.core.api.item;

import com.mojang.serialization.MapCodec;
import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.common.api.events.BooleanEvent;
import eu.pb4.polymer.common.api.events.FunctionEvent;
import eu.pb4.polymer.common.impl.CommonImplUtils;
Expand Down Expand Up @@ -280,9 +281,11 @@ public static ItemStack createMinimalItemStack(ItemStack itemStack, RegistryWrap
ItemStack out = new ItemStack(item, itemStack.getCount());

try {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(
(NbtCompound) POLYMER_STACK_CODEC.encoder().encodeStart(RegistryOps.of(NbtOps.INSTANCE, lookup), itemStack).getOrThrow()
));
PolymerCommonUtils.executeWithPlayerContext(null, () -> {
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());
}
Expand Down Expand Up @@ -356,9 +359,11 @@ public static ItemStack createItemStack(ItemStack itemStack, TooltipType tooltip
}
}
try {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(
(NbtCompound) POLYMER_STACK_CODEC.encoder().encodeStart(RegistryOps.of(NbtOps.INSTANCE, lookup), itemStack).getOrThrow()
));
PolymerCommonUtils.executeWithPlayerContext(null, () -> {
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import net.minecraft.nbt.visitor.NbtTextFormatter;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryOps;
import net.minecraft.screen.LecternScreenHandler;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
Expand Down Expand Up @@ -466,20 +467,20 @@ private static int creativeTab(CommandContext<ServerCommandSource> context) {

private static int displayClientItem(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var player = context.getSource().getPlayer();
var stack = PolymerItemUtils.getPolymerItemStack(player.getMainHandStack(), player).copy();
var stack = PolymerItemUtils.getPolymerItemStack(player.getMainHandStack(), context.getSource().getRegistryManager(), player).copy();
stack.remove(DataComponentTypes.CUSTOM_DATA);

context.getSource().sendFeedback(() -> (new NbtTextFormatter("")).apply(ItemStack.CODEC.encodeStart(NbtOps.INSTANCE, ServerTranslationUtils.parseFor(player.networkHandler, stack)).result().get()), false);
context.getSource().sendFeedback(() -> (new NbtTextFormatter("")).apply(ItemStack.CODEC.encodeStart(RegistryOps.of(NbtOps.INSTANCE, context.getSource().getRegistryManager()), stack).result().get()), false);

return 1;
}

private static int getClientItem(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var player = context.getSource().getPlayer();

var stack = PolymerItemUtils.getPolymerItemStack(player.getMainHandStack(), player);
var stack = PolymerItemUtils.getPolymerItemStack(player.getMainHandStack(), context.getSource().getRegistryManager(), player);
stack.remove(DataComponentTypes.CUSTOM_DATA);
player.giveItemStack(ServerTranslationUtils.parseFor(player.networkHandler, stack));
player.giveItemStack(stack);
context.getSource().sendFeedback(() -> Text.literal("Given client representation to player"), true);

return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import eu.pb4.polymer.common.impl.CommonImplUtils;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.Version;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.LoreComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.text.Text;
Expand All @@ -22,10 +24,15 @@ public static Text parseFor(ServerPlayNetworkHandler handler, Text text) {
}

public static ItemStack parseFor(ServerPlayNetworkHandler handler, ItemStack stack) {
stack = stack.copy();

if (IS_PRESENT && !CommonImplUtils.isMainPlayer(handler.player)) {
return Localization.itemStack(stack, handler.player);
stack.apply(DataComponentTypes.ITEM_NAME, null, x -> x != null ? parseFor(handler, x) : null);
stack.apply(DataComponentTypes.CUSTOM_NAME, null, x -> x != null ? parseFor(handler, x) : null);
stack.apply(DataComponentTypes.LORE, null, x -> x != null ? new LoreComponent(x.lines()
.stream().map(y -> parseFor(handler, y)).toList()) : null);
}
return stack.copy();
return stack;
}

static {
Expand Down

0 comments on commit 17f80a9

Please sign in to comment.