Make ItemInventory use SimpleInventory
All checks were successful
Twine/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-06-16 20:38:55 -04:00
parent 6d74e6b663
commit 5bafb80470
3 changed files with 29 additions and 63 deletions

View File

@ -60,6 +60,7 @@ public class BoatInventoryWrapper implements Inventory {
@Override @Override
public void onOpen(PlayerEntity player) { public void onOpen(PlayerEntity player) {
inventory.onOpen(player);
if (mode.getOpenSound() != null) { if (mode.getOpenSound() != null) {
BoatUtil.playSound(entity, mode.getOpenSound()); BoatUtil.playSound(entity, mode.getOpenSound());
} }
@ -67,6 +68,7 @@ public class BoatInventoryWrapper implements Inventory {
@Override @Override
public void onClose(PlayerEntity player) { public void onClose(PlayerEntity player) {
inventory.onClose(player);
if (mode.getCloseSound() != null) { if (mode.getCloseSound() != null) {
BoatUtil.playSound(entity, mode.getCloseSound()); BoatUtil.playSound(entity, mode.getCloseSound());
} }

View File

@ -2,85 +2,49 @@ package com.thebrokenrail.twine.util.inventory;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories; import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.collection.DefaultedList;
public class ItemInventory implements Inventory { public class ItemInventory extends SimpleInventory {
private final int size;
private final ItemStack stack; private final ItemStack stack;
private final DefaultedList<ItemStack> inv; private boolean isLoadingOrSaving = false;
public ItemInventory(int size, ItemStack stack) { public ItemInventory(int size, ItemStack stack) {
this.size = size; super(size);
this.stack = stack; this.stack = stack;
inv = load(); load();
} }
protected ItemStack getHolder() { protected ItemStack getHolder() {
return stack; return stack;
} }
private DefaultedList<ItemStack> load() { private void load() {
DefaultedList<ItemStack> list = DefaultedList.ofSize(size, ItemStack.EMPTY); isLoadingOrSaving = true;
DefaultedList<ItemStack> list = DefaultedList.ofSize(size(), ItemStack.EMPTY);
Inventories.fromTag(stack.getOrCreateTag().getCompound("BlockEntityTag"), list); Inventories.fromTag(stack.getOrCreateTag().getCompound("BlockEntityTag"), list);
return list; for (int i = 0; i < size(); i++) {
} setStack(i, list.get(i));
private void save(DefaultedList<ItemStack> list) {
Inventories.toTag(stack.getOrCreateTag().getCompound("BlockEntityTag"), list);
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
for (ItemStack item : inv) {
if (item != ItemStack.EMPTY) {
return false;
}
} }
return true; isLoadingOrSaving = false;
} }
@Override private void save() {
public ItemStack getStack(int slot) { isLoadingOrSaving = true;
return inv.get(slot); DefaultedList<ItemStack> list = DefaultedList.ofSize(size(), ItemStack.EMPTY);
} for (int i = 0; i < size(); i++) {
list.set(i, getStack(i).copy());
@Override }
public ItemStack removeStack(int slot, int amount) { Inventories.toTag(stack.getOrCreateSubTag("BlockEntityTag"), list);
return inv.get(slot).split(amount); isLoadingOrSaving = false;
}
@Override
public ItemStack removeStack(int slot) {
ItemStack item = inv.get(slot);
return item.split(item.getCount());
}
@Override
public void setStack(int slot, ItemStack stack) {
inv.set(slot, stack);
} }
@Override @Override
public void markDirty() { public void markDirty() {
save(inv); super.markDirty();
} if (!isLoadingOrSaving) {
save();
@Override
public boolean canPlayerUse(PlayerEntity player) {
return true;
}
@Override
public void clear() {
for (int i = 0; i < size; i++) {
inv.set(i, ItemStack.EMPTY);
} }
} }
} }

View File

@ -14,22 +14,22 @@
"advancements.twine.chest_boat.title": "TEAM SWAMP!", "advancements.twine.chest_boat.title": "TEAM SWAMP!",
"advancements.twine.chest_boat.description": "Put a Chest in a Boat", "advancements.twine.chest_boat.description": "Put a Chest in a Boat",
"advancements.twine.ender_chest_boat.title": "Why not?", "advancements.twine.ender_chest_boat.title": "Why Not?",
"advancements.twine.ender_chest_boat.description": "Put an Ender Chest in a Boat", "advancements.twine.ender_chest_boat.description": "Put an Ender Chest in a Boat",
"advancements.twine.shulker_box_boat.title": "Why? Just why?", "advancements.twine.shulker_box_boat.title": "Why? Just Why?",
"advancements.twine.shulker_box_boat.description": "Put a Shulker Box in a Boat", "advancements.twine.shulker_box_boat.description": "Put a Shulker Box in a Boat",
"advancements.twine.small_backpack.title": "On the go!", "advancements.twine.small_backpack.title": "On The Go!",
"advancements.twine.small_backpack.description": "Construct a Small Backpack with a Crafting Table", "advancements.twine.small_backpack.description": "Construct a Small Backpack with a Crafting Table",
"advancements.twine.large_backpack.title": "Go big or go home!", "advancements.twine.large_backpack.title": "Go Big Or Go Home!",
"advancements.twine.large_backpack.description": "Construct a Large Backpack by combing a Small Backpack and a Gold Ingot in a Smithing Table", "advancements.twine.large_backpack.description": "Construct a Large Backpack by combing a Small Backpack and a Gold Ingot in a Smithing Table",
"advancements.twine.barrel_boat.title": "Off-Brand Chest-Boat", "advancements.twine.barrel_boat.title": "Off-Brand Chest-Boat",
"advancements.twine.barrel_boat.description": "Put a Barrel in a Boat", "advancements.twine.barrel_boat.description": "Put a Barrel in a Boat",
"advancements.twine.diviner.title": "Divination", "advancements.twine.diviner.title": "Divination Master",
"advancements.twine.diviner.description": "Construct a Diviner with a Crafting Table", "advancements.twine.diviner.description": "Construct a Diviner with a Crafting Table",
"item.twine.diviner": "Diviner", "item.twine.diviner": "Diviner",