Skip to content

Commit

Permalink
fixed stress test results
Browse files Browse the repository at this point in the history
  • Loading branch information
Uraneptus committed Oct 22, 2024
1 parent 31009e5 commit 96769d4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/main/java/vazkii/morphtool/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
55 changes: 38 additions & 17 deletions src/main/java/vazkii/morphtool/MorphingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,48 @@ 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<ItemStack> 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<ItemStack> 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));
*/

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) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ToolContentComponent> CODEC = ItemStack.CODEC.listOf().xmap(ToolContentComponent::new, component -> component.items);
public static final Codec<ToolContentComponent> CODEC = ItemStack.CODEC
.listOf()
.flatXmap(ToolContentComponent::checkAndCreate, component -> DataResult.success(component.items));
public static final StreamCodec<RegistryFriendlyByteBuf, ToolContentComponent> STREAM_CODEC = ItemStack.STREAM_CODEC
.apply(ByteBufCodecs.list())
.map(ToolContentComponent::new, component -> component.items);
Expand All @@ -21,6 +26,14 @@ public ToolContentComponent(List<ItemStack> contents) {
this.items = contents;
}

private static DataResult<ToolContentComponent> checkAndCreate(List<ItemStack> 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();
}
Expand Down

0 comments on commit 96769d4

Please sign in to comment.