Make ItemInventory use SimpleInventory
All checks were successful
Twine/pipeline/head This commit looks good
All checks were successful
Twine/pipeline/head This commit looks good
This commit is contained in:
parent
6d74e6b663
commit
5bafb80470
@ -60,6 +60,7 @@ public class BoatInventoryWrapper implements Inventory {
|
||||
|
||||
@Override
|
||||
public void onOpen(PlayerEntity player) {
|
||||
inventory.onOpen(player);
|
||||
if (mode.getOpenSound() != null) {
|
||||
BoatUtil.playSound(entity, mode.getOpenSound());
|
||||
}
|
||||
@ -67,6 +68,7 @@ public class BoatInventoryWrapper implements Inventory {
|
||||
|
||||
@Override
|
||||
public void onClose(PlayerEntity player) {
|
||||
inventory.onClose(player);
|
||||
if (mode.getCloseSound() != null) {
|
||||
BoatUtil.playSound(entity, mode.getCloseSound());
|
||||
}
|
||||
|
@ -2,85 +2,49 @@ package com.thebrokenrail.twine.util.inventory;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.Inventories;
|
||||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.inventory.SimpleInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.collection.DefaultedList;
|
||||
|
||||
public class ItemInventory implements Inventory {
|
||||
private final int size;
|
||||
public class ItemInventory extends SimpleInventory {
|
||||
private final ItemStack stack;
|
||||
private final DefaultedList<ItemStack> inv;
|
||||
private boolean isLoadingOrSaving = false;
|
||||
|
||||
public ItemInventory(int size, ItemStack stack) {
|
||||
this.size = size;
|
||||
super(size);
|
||||
this.stack = stack;
|
||||
inv = load();
|
||||
load();
|
||||
}
|
||||
|
||||
protected ItemStack getHolder() {
|
||||
return stack;
|
||||
}
|
||||
|
||||
private DefaultedList<ItemStack> load() {
|
||||
DefaultedList<ItemStack> list = DefaultedList.ofSize(size, ItemStack.EMPTY);
|
||||
private void load() {
|
||||
isLoadingOrSaving = true;
|
||||
DefaultedList<ItemStack> list = DefaultedList.ofSize(size(), ItemStack.EMPTY);
|
||||
Inventories.fromTag(stack.getOrCreateTag().getCompound("BlockEntityTag"), list);
|
||||
return list;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
setStack(i, list.get(i));
|
||||
}
|
||||
isLoadingOrSaving = false;
|
||||
}
|
||||
|
||||
private void save(DefaultedList<ItemStack> list) {
|
||||
Inventories.toTag(stack.getOrCreateTag().getCompound("BlockEntityTag"), list);
|
||||
private void save() {
|
||||
isLoadingOrSaving = true;
|
||||
DefaultedList<ItemStack> list = DefaultedList.ofSize(size(), ItemStack.EMPTY);
|
||||
for (int i = 0; i < size(); i++) {
|
||||
list.set(i, getStack(i).copy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
for (ItemStack item : inv) {
|
||||
if (item != ItemStack.EMPTY) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStack(int slot) {
|
||||
return inv.get(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack removeStack(int slot, int amount) {
|
||||
return inv.get(slot).split(amount);
|
||||
}
|
||||
|
||||
@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);
|
||||
Inventories.toTag(stack.getOrCreateSubTag("BlockEntityTag"), list);
|
||||
isLoadingOrSaving = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markDirty() {
|
||||
save(inv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(PlayerEntity player) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
for (int i = 0; i < size; i++) {
|
||||
inv.set(i, ItemStack.EMPTY);
|
||||
super.markDirty();
|
||||
if (!isLoadingOrSaving) {
|
||||
save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,22 +14,22 @@
|
||||
"advancements.twine.chest_boat.title": "TEAM SWAMP!",
|
||||
"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.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.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.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.barrel_boat.title": "Off-Brand Chest-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",
|
||||
|
||||
"item.twine.diviner": "Diviner",
|
||||
|
Reference in New Issue
Block a user