Skip to content

Commit

Permalink
fix some shulker box nbt loading errors, closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIllusiveC4 committed Sep 20, 2019
1 parent 1dee4b5 commit 34dc50f
Showing 1 changed file with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ShulkerBoxTileEntity;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootParameterSets;
import net.minecraft.world.storage.loot.LootParameters;
import net.minecraft.world.storage.loot.LootTable;
import net.minecraftforge.fml.network.PacketDistributor;
import top.theillusivec4.curios.api.CuriosAPI;
import top.theillusivec4.curiousshulkerboxes.common.capability.CurioShulkerBox;
Expand All @@ -50,7 +56,6 @@
public class CurioShulkerBoxInventory implements IInventory, INamedContainerProvider {

protected NonNullList<ItemStack> items;
protected ITextComponent customName;

private ItemStack shulkerBox;
private String identifier;
Expand Down Expand Up @@ -81,10 +86,17 @@ public void openInventory(@Nonnull PlayerEntity player) {

if (!player.isSpectator()) {

CompoundNBT nbttagcompound = shulkerBox.getChildTag("BlockEntityTag");
CompoundNBT tag = shulkerBox.getChildTag("BlockEntityTag");

if (nbttagcompound != null) {
this.loadFromNbt(nbttagcompound);
if (tag != null) {

if (tag.contains("LootTable", 8)) {
String lootTable = tag.getString("LootTable");
long lootSeed = tag.getLong("LootTableSeed");
this.fillWithLoot(new ResourceLocation(lootTable), lootSeed, player);
} else {
this.loadFromNbt(tag);
}
}

CuriosAPI.getCurio(shulkerBox).ifPresent(curio -> {
Expand Down Expand Up @@ -112,6 +124,8 @@ public void closeInventory(@Nonnull PlayerEntity player) {
CompoundNBT nbttagcompound = shulkerBox.getChildTag("BlockEntityTag");

if (nbttagcompound != null) {
nbttagcompound.remove("LootTable");
nbttagcompound.remove("LootTableSeed");
this.saveToNbt(nbttagcompound);
}

Expand Down Expand Up @@ -140,10 +154,6 @@ public void loadFromNbt(CompoundNBT compound) {
if (compound.contains("Items", 9)) {
ItemStackHelper.loadAllItems(compound, this.items);
}

if (compound.contains("CustomName", 8)) {
this.customName = ITextComponent.Serializer.fromJson(compound.getString("CustomName"));
}
}

public CompoundNBT saveToNbt(CompoundNBT compound) {
Expand Down Expand Up @@ -223,7 +233,22 @@ public boolean isUsableByPlayer(@Nonnull PlayerEntity player) {
@Override
public ITextComponent getDisplayName() {

return new TranslationTextComponent("container.shulkerBox");
return shulkerBox.getDisplayName();
}

public void fillWithLoot(ResourceLocation lootTable, long lootTableSeed, PlayerEntity player) {

if (player.world.getServer() != null) {
LootTable loottable = player.world.getServer().getLootTableManager()
.getLootTableFromLocation(lootTable);
LootContext.Builder lootcontext$builder = (new LootContext.Builder(
(ServerWorld) player.world))
.withParameter(LootParameters.POSITION, new BlockPos(player.getPosition()))
.withSeed(lootTableSeed);
lootcontext$builder.withLuck(player.getLuck())
.withParameter(LootParameters.THIS_ENTITY, player);
loottable.fillInventory(this, lootcontext$builder.build(LootParameterSets.CHEST));
}
}

@Nullable
Expand Down

0 comments on commit 34dc50f

Please sign in to comment.