Skip to content

Commit

Permalink
Fix crash when placing toolbox, closes #6084
Browse files Browse the repository at this point in the history
  • Loading branch information
malte0811 committed Oct 27, 2024
1 parent 7f8d605 commit b4538d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.world.item.crafting.Ingredient;

import java.util.*;
Expand Down Expand Up @@ -111,4 +113,14 @@ List<Pair<K, T>> mapToList(Map<K, T> m)
{
return m.entrySet().stream().map(e -> Pair.of(e.getKey(), e.getValue())).toList();
}

public static <T> T fromNbtOrThrow(Codec<T> codec, Tag data)
{
return codec.decode(NbtOps.INSTANCE, data).getOrThrow().getFirst();
}

public static <T> Tag toNbtOrThrow(Codec<T> codec, T object)
{
return codec.encodeStart(NbtOps.INSTANCE, object).getOrThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import blusunrize.immersiveengineering.api.IEApi;
import blusunrize.immersiveengineering.api.IEProperties;
import blusunrize.immersiveengineering.api.utils.codec.IECodecs;
import blusunrize.immersiveengineering.common.blocks.IEBaseBlockEntity;
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.*;
import blusunrize.immersiveengineering.common.blocks.PlacementLimitation;
Expand All @@ -27,7 +28,6 @@
import net.minecraft.core.NonNullList;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.ContainerHelper;
Expand All @@ -38,6 +38,7 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.storage.loot.LootContext;
Expand All @@ -57,7 +58,7 @@ public class ToolboxBlockEntity extends IEBaseBlockEntity implements IStateBased
{
private final NonNullList<ItemStack> inventory = NonNullList.withSize(ToolboxItem.SLOT_COUNT, ItemStack.EMPTY);
public Component name;
private ListTag enchantments;
private ItemEnchantments enchantments = ItemEnchantments.EMPTY;

public ToolboxBlockEntity(BlockPos pos, BlockState state)
{
Expand All @@ -69,8 +70,7 @@ public void readCustomNBT(CompoundTag nbt, boolean descPacket, Provider provider
{
if(nbt.contains("name", Tag.TAG_STRING))
this.name = Component.Serializer.fromJson(nbt.getString("name"), provider);
if(nbt.contains("enchantments", Tag.TAG_LIST))
this.enchantments = nbt.getList("enchantments", Tag.TAG_COMPOUND);
this.enchantments = IECodecs.fromNbtOrThrow(ItemEnchantments.CODEC, nbt.get("enchantments"));
if(!descPacket)
ContainerHelper.loadAllItems(nbt, inventory, provider);
}
Expand All @@ -80,8 +80,7 @@ public void writeCustomNBT(CompoundTag nbt, boolean descPacket, Provider provide
{
if(this.name!=null)
nbt.putString("name", Component.Serializer.toJson(this.name, provider));
if(this.enchantments!=null)
nbt.put("enchantments", this.enchantments);
nbt.put("enchantments", IECodecs.toNbtOrThrow(ItemEnchantments.CODEC, this.enchantments));
if(!descPacket)
ContainerHelper.saveAllItems(nbt, inventory, provider);
}
Expand All @@ -104,13 +103,11 @@ public ItemInteractionResult interact(Direction side, Player player, Interaction
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}

//TODO
//@Override
//@Nullable
//public ITextComponent getDisplayName()
//{
// return name!=null?new TextComponentString(name): new TextComponentTranslation("item.immersiveengineering.toolbox.name");
//}
@Override
public Component getDisplayName()
{
return name!=null?name: Component.translatable("item.immersiveengineering.toolbox.name");
}

@Override
public boolean canUseGui(Player player)
Expand Down Expand Up @@ -161,9 +158,8 @@ public void getBlockEntityDrop(LootContext context, Consumer<ItemStack> drop)
Tools.TOOLBOX.get().setContainedItems(stack, inventory);
if(this.name!=null)
stack.set(DataComponents.CUSTOM_NAME, this.name);
if(true) throw new IllegalStateException();
//if(enchantments!=null)
// stack.set(DataComponents.ENCHANTMENTS, enchantments);
if(enchantments!=null)
stack.set(DataComponents.ENCHANTMENTS, enchantments);
drop.accept(stack);
}

Expand All @@ -181,10 +177,9 @@ public void onBEPlaced(BlockPlaceContext ctx)
this.setChanged();
}

if(true) throw new IllegalStateException();
//if(stack.hasCustomHoverName())
// this.name = stack.getHoverName();
//enchantments = stack.getEnchantmentTags();
if(stack.has(DataComponents.CUSTOM_NAME))
this.name = stack.getHoverName();
enchantments = stack.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY);
}
}

Expand Down

0 comments on commit b4538d6

Please sign in to comment.