proceeding the backpack

This commit is contained in:
Jörg Henke 2020-08-15 18:25:11 +02:00
parent fed1d21176
commit 66854d5466
6 changed files with 120 additions and 64 deletions

View File

@ -5,16 +5,50 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.collection.DefaultedList;
public class BackpackInventory implements Inventory {
public static final int SECTION_SIZE = 9;
private final DefaultedList<ItemStack> stacks;
private BackpackScreenHandler container;
private final BackpackScreenHandler handler;
public BackpackInventory(BackpackScreenHandler container) {
public BackpackInventory(CompoundTag tag, BackpackScreenHandler handler) {
this.handler = handler;
this.stacks = DefaultedList.ofSize(SECTION_SIZE * ItemBackpack.SLOTSIZE, ItemStack.EMPTY);
this.container = container;
readItemsFromTag(this.stacks, tag);
}
private void readItemsFromTag(DefaultedList<ItemStack> inventory, CompoundTag tag) {
ListTag listTag = tag.getList("items", 10);
for (int i = 0; i < listTag.size(); ++i) {
CompoundTag compoundTag = listTag.getCompound(i);
int j = compoundTag.getInt("slot");
if (j >= 0 && j < inventory.size()) {
inventory.set(j, ItemStack.fromTag(compoundTag));
}
}
}
private CompoundTag writeItemsToTag() {
DefaultedList<ItemStack> inventory = stacks;
ListTag listTag = new ListTag();
for (int i = 0; i < inventory.size(); ++i) {
ItemStack itemStack = (ItemStack) inventory.get(i);
if (!itemStack.isEmpty()) {
CompoundTag compoundTag = new CompoundTag();
compoundTag.putInt("slot", i);
itemStack.toTag(compoundTag);
listTag.add(compoundTag);
}
}
CompoundTag tag = new CompoundTag();
tag.put("items", listTag);
return tag;
}
public DefaultedList<ItemStack> getList(DefaultedList<ItemStack> dl) {
@ -75,6 +109,25 @@ public class BackpackInventory implements Inventory {
@Override
public void markDirty() {
this.container.onContentChanged(this);
handler.onContentChanged(this);
}
@Override
public void onOpen(PlayerEntity player) {
Inventory.super.onOpen(player);
player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f);
}
@Override
public void onClose(PlayerEntity player) {
Inventory.super.onClose(player);
ItemStack stack = player.getMainHandStack();
if (stack != null) {
if (!stack.hasTag()) {
stack.setTag(new CompoundTag());
}
stack.getTag().put("backpack", writeItemsToTag());
}
player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f);
}
}

View File

@ -63,4 +63,11 @@ public class BackpackScreen extends HandledScreen<BackpackScreenHandler> impleme
drawSlots(matrices, guiX, guiY);
}
@Override
protected void drawForeground(MatrixStack matrixStack, int i, int j)
{
this.textRenderer.draw(matrixStack, this.title, 8.0F, -20.0F, 4210752);
this.textRenderer.draw(matrixStack, this.playerInventory.getDisplayName(), 8.0F, 101f, 4210752);
}
}

View File

@ -8,7 +8,6 @@ import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.Hand;
import net.minecraft.util.collection.DefaultedList;
@ -22,25 +21,16 @@ public class BackpackScreenHandler extends ScreenHandler {
public BackpackScreenHandler(int syncId, PlayerInventory playerInventory) {
super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
this.player = playerInventory.player;
this.backpackInventory = new BackpackInventory(this);
this.backpackInventory = ItemBackpack.getInventory(this, hand, player);
backpackSlots = ItemBackpack.SLOTSIZE;
int spacing;
if (backpackSlots % 9 == 0) {
spacing = 30 + (backpackSlots / 9) * 18 + ((backpackSlots / 9) < 5 ? 0 : 2);
} else {
spacing = 30 + (backpackSlots / 9 + 1) * 18 + ((backpackSlots / 9) < 5 ? 0 : 2);
}
for (int y = 0; y < (backpackSlots / 9); y++) {
int spacing = 112;
for (int y = 0; y < 6; y++) {
for (int x = 0; x < 9; ++x) {
this.addSlot(new Slot(backpackInventory, x + y * 9, 8 + x * 18, 18 + y * 18));
this.addSlot(new Slot(backpackInventory, x + y * 9, 8 + x * 18, -10 + y * 18));
}
}
if ((backpackSlots % 9) != 0) {
for (int x = 0; x < (backpackSlots % 9); x++) {
this.addSlot(
new Slot(backpackInventory, x + (backpackSlots / 9) * 9, 8 + x * 18, 18 + (backpackSlots / 9) * 18));
}
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 9; ++x) {
this.addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, spacing + y * 18));
@ -50,6 +40,9 @@ public class BackpackScreenHandler extends ScreenHandler {
this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing));
}
}
public PlayerEntity getPlayer() {
return this.player;
}
@Override
@ -57,6 +50,7 @@ public class BackpackScreenHandler extends ScreenHandler {
return true;
}
@Override
public void onContentChanged(Inventory inv) {
super.onContentChanged(inv);
if (inv == this.backpackInventory) {
@ -64,6 +58,7 @@ public class BackpackScreenHandler extends ScreenHandler {
}
}
@Override
public void close(PlayerEntity player) {
super.close(player);
this.backpackInventory.onClose(player);
@ -87,31 +82,19 @@ public class BackpackScreenHandler extends ScreenHandler {
} else if (!this.insertItem(clickedStack, 0, backpackSlots, false)) {
return ItemStack.EMPTY;
}
if (clickedStack.isEmpty()) {
clickedSlot.setStack(ItemStack.EMPTY);
} else {
clickedSlot.markDirty();
}
}
return copy;
}
@Override
public ItemStack onSlotClick(int i, int j, SlotActionType slotActionType, PlayerEntity playerEntity) {
if (i > 0) {
if (this.getSlot(i).getStack().equals(playerEntity.getStackInHand(hand))) {
return ItemStack.EMPTY;
}
}
return super.onSlotClick(i, j, slotActionType, playerEntity);
}
public BackpackScreenHandler withHand(Hand hand) {
this.hand = hand;
DefaultedList<ItemStack> ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY);
ItemBackpack.getInventory(player.getStackInHand(this.hand), ad);
// ItemBackpack.getInventory(player.getStackInHand(this.hand), ad);
if (ad.size() == 0) {
return this;
}
@ -125,4 +108,8 @@ public class BackpackScreenHandler extends ScreenHandler {
}
return this;
}
public Hand getHand() {
return hand;
}
}

View File

@ -48,8 +48,8 @@ public class RegistryManager {
public static final String QUICKIEFABRIC = "quickiefabric";
public static final Identifier BACKPACK_CONTAINTER = new Identifier(QUICKIEFABRIC, "backpack");
public static final ScreenHandlerType<BackpackScreenHandler> BACKPACK_SCREEN_HANDLER = ScreenHandlerRegistry.registerSimple(RegistryManager.BACKPACK_CONTAINTER, BackpackScreenHandler::new);
public static final Identifier BACKPACK_IDENTIFIER = new Identifier(QUICKIEFABRIC, "backpack");
public static final ScreenHandlerType<BackpackScreenHandler> BACKPACK_SCREEN_HANDLER = ScreenHandlerRegistry.registerSimple(RegistryManager.BACKPACK_IDENTIFIER, BackpackScreenHandler::new);
public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(QuickieItems.SPEEDPOWDER))
.appendItems(stacks -> {

View File

@ -3,10 +3,10 @@ package de.jottyfan.minecraft.quickiefabric.items;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.jottyfan.minecraft.quickiefabric.container.BackpackInventory;
import de.jottyfan.minecraft.quickiefabric.container.BackpackScreenHandler;
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.item.DyeableItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@ -15,10 +15,7 @@ import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
/**
@ -27,7 +24,7 @@ import net.minecraft.world.World;
*
*/
public class ItemBackpack extends Item implements DyeableItem {
private static final Logger LOGGER = LogManager.getLogger(ItemBackpack.class);
private final static Logger LOGGER = LogManager.getLogger(ItemBackpack.class);
public final static Integer SLOTSIZE = 54;
public ItemBackpack() {
@ -37,31 +34,43 @@ public class ItemBackpack extends Item implements DyeableItem {
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (!world.isClient) {
for (Identifier id : Registry.SCREEN_HANDLER.getIds()) {
LOGGER.info("found registered screen {}", id.toString());
}
SimpleNamedScreenHandlerFactory factory = new SimpleNamedScreenHandlerFactory(
(id, inventory, buf) -> new BackpackScreenHandler(id, player.inventory).withHand(hand),
Text.method_30163(RegistryManager.BACKPACK_CONTAINTER.toString()));
Text.method_30163(RegistryManager.BACKPACK_IDENTIFIER.toString()));
player.openHandledScreen(factory);
}
return new TypedActionResult<ItemStack>(ActionResult.PASS, player.getStackInHand(hand));
}
public static void setInventory(ItemStack backpack, DefaultedList<ItemStack> list) {
CompoundTag nbt = backpack.getOrCreateTag();
Inventories.toTag(nbt, list);
backpack.setTag(nbt);
public static BackpackInventory getInventory(BackpackScreenHandler handler, Hand hand, PlayerEntity player) {
ItemStack stack = player.getMainHandStack();
if (stack != null) {
if (!stack.hasTag()) {
stack.setTag(new CompoundTag());
}
if (!stack.getTag().contains("backpack")) {
stack.getTag().put("backpack", new CompoundTag());
}
} else {
LOGGER.warn("player.getMainHandStack() is null");
}
return new BackpackInventory(stack.getTag().getCompound("backpack"), handler);
}
public static void getInventory(ItemStack backpack, DefaultedList<ItemStack> defaultedList) {
CompoundTag nbt;
if (backpack.hasTag()) {
nbt = backpack.getTag();
} else {
nbt = new CompoundTag();
}
Inventories.fromTag(nbt, defaultedList);
backpack.setTag(nbt);
}
// public static void setInventory(ItemStack backpack, DefaultedList<ItemStack> list) {
// CompoundTag nbt = backpack.getOrCreateTag();
// Inventories.toTag(nbt, list);
// backpack.setTag(nbt);
// }
//
// public static void getInventory(ItemStack backpack, DefaultedList<ItemStack> defaultedList) {
// CompoundTag nbt;
// if (backpack.hasTag()) {
// nbt = backpack.getTag();
// } else {
// nbt = new CompoundTag();
// }
// Inventories.fromTag(nbt, defaultedList);
// backpack.setTag(nbt);
// }
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB