From 96769d420f3b90edf546ad1d76def68a0d24d93b Mon Sep 17 00:00:00 2001 From: Uraneptus Date: Tue, 22 Oct 2024 23:45:45 +0200 Subject: [PATCH] fixed stress test results --- .../java/vazkii/morphtool/ClientHandler.java | 3 +- .../vazkii/morphtool/MorphingHandler.java | 55 +++++++++++++------ .../data_components/ToolContentComponent.java | 15 ++++- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/main/java/vazkii/morphtool/ClientHandler.java b/src/main/java/vazkii/morphtool/ClientHandler.java index fe2fa37..e07ff06 100644 --- a/src/main/java/vazkii/morphtool/ClientHandler.java +++ b/src/main/java/vazkii/morphtool/ClientHandler.java @@ -72,7 +72,7 @@ public void onMouseEvent(InputEvent.MouseScrollingEvent event) { } //Manual Scroll for Morph (excluding looked at a mod block incase it also needs scrolling) - if (event.getScrollDeltaY() != 0 && player.isCrouching() && !modlook.equals(mod)) { //TODO scroll delta y or x? + if (event.getScrollDeltaY() != 0 && player.isCrouching() && !modlook.equals(mod)) { if (mainHandItem.has(Registries.TOOL_CONTENT) && mainHandItem.get(Registries.TOOL_CONTENT) != null) { ToolContentComponent contents = mainHandItem.get(Registries.TOOL_CONTENT); mod = event.getScrollDeltaY() < 0 ? nextMod(contents, mod) : previousMod(contents, mod); @@ -81,7 +81,6 @@ public void onMouseEvent(InputEvent.MouseScrollingEvent event) { event.setCanceled(true); } } - if (newStack != mainHandItem && !ItemStack.isSameItemSameComponents(newStack, mainHandItem)) { var inventory = player.getInventory(); inventory.setItem(ConfigHandler.invertHandShift.get() ? inventory.getContainerSize() - 1 : inventory.selected, newStack); diff --git a/src/main/java/vazkii/morphtool/MorphingHandler.java b/src/main/java/vazkii/morphtool/MorphingHandler.java index 9276b47..f73e9dc 100644 --- a/src/main/java/vazkii/morphtool/MorphingHandler.java +++ b/src/main/java/vazkii/morphtool/MorphingHandler.java @@ -44,21 +44,31 @@ public void onItemDropped(ItemTossEvent event) { @SubscribeEvent public void onItemBroken(PlayerDestroyItemEvent event) { - removeItemFromTool(event.getEntity(), event.getOriginal(), true, (ItemStack morph) -> event.getEntity().setItemInHand(event.getHand(), morph)); + removeItemFromTool(event.getEntity(), event.getOriginal(), true, morph -> event.getEntity().setItemInHand(event.getHand(), morph)); } - //TODO next fix this public static void removeItemFromTool(Entity e, ItemStack stack, boolean itemBroken, Consumer consumer) { if (stack != null && !stack.isEmpty() && isMorphTool(stack) && !stack.is(Registries.MORPH_TOOL.get())) { ToolContentComponent contents = stack.get(Registries.TOOL_CONTENT); + if (contents == null) return; + ToolContentComponent.Mutable mutable = new ToolContentComponent.Mutable(contents); //CompoundTag morphData = stack.getTag().getCompound(TAG_MORPH_TOOL_DATA).copy(); - ItemStack morph = makeMorphedStack(stack, MINECRAFT); - String mod = getModFromStack(stack); + mutable.remove(stack); + + stack.set(Registries.TOOL_CONTENT, mutable.toImmutable()); + + ItemStack morph = makeMorphedStack(stack, MINECRAFT, true); + + + + /* List newStacks = new ArrayList<>(List.copyOf(contents.getItems())); newStacks.remove(getStackFromMod(contents, mod)); ToolContentComponent newContents = new ToolContentComponent(newStacks); + */ + /* CompoundTag newMorphData = morph.getTag().getCompound(TAG_MORPH_TOOL_DATA); newMorphData.remove(getModFromStack(stack)); @@ -66,13 +76,16 @@ public static void removeItemFromTool(Entity e, ItemStack stack, boolean itemBro */ if (!itemBroken) { - if (!e.getCommandSenderWorld().isClientSide) { + if (!e.getCommandSenderWorld().isClientSide) { ItemEntity newItem = new ItemEntity(e.getCommandSenderWorld(), e.getX(), e.getY(), e.getZ(), morph); e.getCommandSenderWorld().addFreshEntity(newItem); } ItemStack copy = stack.copy(); - copy.set(Registries.TOOL_CONTENT, newContents); + copy.remove(Registries.TOOL_CONTENT); + copy.remove(Registries.IS_MORPH_TOOL); + copy.remove(DataComponents.CUSTOM_NAME); + copy.remove(Registries.OG_DISPLAY_NAME); /* CompoundTag copyCmp = copy.getTag(); if (copyCmp == null) { @@ -128,16 +141,17 @@ public static ItemStack getShiftStackForMod(ItemStack stack, String mod) { return stack; } - return makeMorphedStack(stack, mod); + return makeMorphedStack(stack, mod, false); } - public static ItemStack makeMorphedStack(ItemStack currentStack, String targetMod) { + public static ItemStack makeMorphedStack(ItemStack currentStack, String targetMod, boolean calledOnRemove) { String currentMod = getModFromStack(currentStack); ToolContentComponent currentContent = currentStack.get(Registries.TOOL_CONTENT); + currentStack.remove(Registries.TOOL_CONTENT); ToolContentComponent newStackComponent = new ToolContentComponent(List.of(currentStack)); if (currentContent == null) return ItemStack.EMPTY; - ToolContentComponent.Mutable mutable = getMutable(currentContent, newStackComponent, currentMod); + ToolContentComponent.Mutable mutable = getMutable(currentContent, newStackComponent, currentMod, calledOnRemove); ItemStack stack; if (targetMod.equals(MINECRAFT)) { @@ -190,13 +204,9 @@ public static ItemStack makeMorphedStack(ItemStack currentStack, String targetMo */ - Component hoverName = stack.getHoverName(); - if (!stack.has(Registries.OG_DISPLAY_NAME)) { - stack.set(Registries.OG_DISPLAY_NAME, hoverName); - } else { - hoverName = stack.get(Registries.OG_DISPLAY_NAME); - } + + Component hoverName = getOrSetOGName(stack); Component stackName = Component.literal(hoverName.getString()).setStyle(Style.EMPTY.applyFormats(ChatFormatting.GREEN)); Component comp = Component.translatable("morphtool.sudo_name", stackName); @@ -208,7 +218,18 @@ public static ItemStack makeMorphedStack(ItemStack currentStack, String targetMo return stack; } - private static ToolContentComponent.Mutable getMutable(ToolContentComponent currentContent, ToolContentComponent newStackComponent, String currentMod) { + private static Component getOrSetOGName(ItemStack stack) { + Component hoverName = stack.getHoverName(); + if (!stack.has(Registries.OG_DISPLAY_NAME)) { + stack.set(Registries.OG_DISPLAY_NAME, hoverName); + } else { + hoverName = stack.get(Registries.OG_DISPLAY_NAME); + } + + return hoverName; + } + + private static ToolContentComponent.Mutable getMutable(ToolContentComponent currentContent, ToolContentComponent newStackComponent, String currentMod, boolean calledOnRemove) { ToolContentComponent.Mutable currentContentMutable = new ToolContentComponent.Mutable(currentContent); ToolContentComponent.Mutable newStackComponentMutable = new ToolContentComponent.Mutable(newStackComponent); @@ -222,7 +243,7 @@ private static ToolContentComponent.Mutable getMutable(ToolContentComponent curr */ - if (!currentMod.equalsIgnoreCase(MINECRAFT) && !currentMod.equalsIgnoreCase(MorphTool.MOD_ID)) { + if (!currentMod.equalsIgnoreCase(MINECRAFT) && !currentMod.equalsIgnoreCase(MorphTool.MOD_ID) && !calledOnRemove) { currentContentMutable.tryInsert(newStackComponent.getItems().getFirst()); } return currentContentMutable; diff --git a/src/main/java/vazkii/morphtool/data_components/ToolContentComponent.java b/src/main/java/vazkii/morphtool/data_components/ToolContentComponent.java index a85c5c1..d73c3b7 100644 --- a/src/main/java/vazkii/morphtool/data_components/ToolContentComponent.java +++ b/src/main/java/vazkii/morphtool/data_components/ToolContentComponent.java @@ -1,17 +1,22 @@ package vazkii.morphtool.data_components; import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.BundleContents; +import org.apache.commons.lang3.math.Fraction; import java.util.ArrayList; import java.util.List; public class ToolContentComponent { public static final ToolContentComponent EMPTY = new ToolContentComponent(List.of()); - public static final Codec CODEC = ItemStack.CODEC.listOf().xmap(ToolContentComponent::new, component -> component.items); + public static final Codec CODEC = ItemStack.CODEC + .listOf() + .flatXmap(ToolContentComponent::checkAndCreate, component -> DataResult.success(component.items)); public static final StreamCodec STREAM_CODEC = ItemStack.STREAM_CODEC .apply(ByteBufCodecs.list()) .map(ToolContentComponent::new, component -> component.items); @@ -21,6 +26,14 @@ public ToolContentComponent(List contents) { this.items = contents; } + private static DataResult checkAndCreate(List p_381706_) { + try { + return DataResult.success(new ToolContentComponent(p_381706_)); + } catch (ArithmeticException arithmeticexception) { + return DataResult.error(() -> "Excessive total bundle weight"); + } + } + public boolean isEmpty() { return this.items.isEmpty(); }