proceeding the backpack
This commit is contained in:
parent
fed1d21176
commit
66854d5466
@ -5,16 +5,50 @@ 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.Inventory;
|
||||||
import net.minecraft.item.ItemStack;
|
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;
|
import net.minecraft.util.collection.DefaultedList;
|
||||||
|
|
||||||
public class BackpackInventory implements Inventory {
|
public class BackpackInventory implements Inventory {
|
||||||
public static final int SECTION_SIZE = 9;
|
public static final int SECTION_SIZE = 9;
|
||||||
private final DefaultedList<ItemStack> stacks;
|
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.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) {
|
public DefaultedList<ItemStack> getList(DefaultedList<ItemStack> dl) {
|
||||||
@ -75,6 +109,25 @@ public class BackpackInventory implements Inventory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void markDirty() {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,4 +63,11 @@ public class BackpackScreen extends HandledScreen<BackpackScreenHandler> impleme
|
|||||||
|
|
||||||
drawSlots(matrices, guiX, guiY);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import net.minecraft.inventory.Inventory;
|
|||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.screen.ScreenHandler;
|
import net.minecraft.screen.ScreenHandler;
|
||||||
import net.minecraft.screen.slot.Slot;
|
import net.minecraft.screen.slot.Slot;
|
||||||
import net.minecraft.screen.slot.SlotActionType;
|
|
||||||
import net.minecraft.util.Hand;
|
import net.minecraft.util.Hand;
|
||||||
import net.minecraft.util.collection.DefaultedList;
|
import net.minecraft.util.collection.DefaultedList;
|
||||||
|
|
||||||
@ -22,34 +21,28 @@ public class BackpackScreenHandler extends ScreenHandler {
|
|||||||
public BackpackScreenHandler(int syncId, PlayerInventory playerInventory) {
|
public BackpackScreenHandler(int syncId, PlayerInventory playerInventory) {
|
||||||
super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
|
super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
|
||||||
this.player = playerInventory.player;
|
this.player = playerInventory.player;
|
||||||
this.backpackInventory = new BackpackInventory(this);
|
this.backpackInventory = ItemBackpack.getInventory(this, hand, player);
|
||||||
backpackSlots = ItemBackpack.SLOTSIZE;
|
backpackSlots = ItemBackpack.SLOTSIZE;
|
||||||
|
|
||||||
int spacing;
|
int spacing = 112;
|
||||||
if (backpackSlots % 9 == 0) {
|
|
||||||
spacing = 30 + (backpackSlots / 9) * 18 + ((backpackSlots / 9) < 5 ? 0 : 2);
|
for (int y = 0; y < 6; y++) {
|
||||||
} else {
|
|
||||||
spacing = 30 + (backpackSlots / 9 + 1) * 18 + ((backpackSlots / 9) < 5 ? 0 : 2);
|
|
||||||
}
|
|
||||||
for (int y = 0; y < (backpackSlots / 9); y++) {
|
|
||||||
for (int x = 0; x < 9; ++x) {
|
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 y = 0; y < 3; ++y) {
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int x = 0; x < 9; ++x) {
|
for (int x = 0; x < 9; ++x) {
|
||||||
this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing));
|
this.addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, spacing + y * 18));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (int x = 0; x < 9; ++x) {
|
||||||
|
this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PlayerEntity getPlayer() {
|
||||||
|
return this.player;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -57,6 +50,7 @@ public class BackpackScreenHandler extends ScreenHandler {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onContentChanged(Inventory inv) {
|
public void onContentChanged(Inventory inv) {
|
||||||
super.onContentChanged(inv);
|
super.onContentChanged(inv);
|
||||||
if (inv == this.backpackInventory) {
|
if (inv == this.backpackInventory) {
|
||||||
@ -64,6 +58,7 @@ public class BackpackScreenHandler extends ScreenHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void close(PlayerEntity player) {
|
public void close(PlayerEntity player) {
|
||||||
super.close(player);
|
super.close(player);
|
||||||
this.backpackInventory.onClose(player);
|
this.backpackInventory.onClose(player);
|
||||||
@ -87,31 +82,19 @@ public class BackpackScreenHandler extends ScreenHandler {
|
|||||||
} else if (!this.insertItem(clickedStack, 0, backpackSlots, false)) {
|
} else if (!this.insertItem(clickedStack, 0, backpackSlots, false)) {
|
||||||
return ItemStack.EMPTY;
|
return ItemStack.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clickedStack.isEmpty()) {
|
if (clickedStack.isEmpty()) {
|
||||||
clickedSlot.setStack(ItemStack.EMPTY);
|
clickedSlot.setStack(ItemStack.EMPTY);
|
||||||
} else {
|
} else {
|
||||||
clickedSlot.markDirty();
|
clickedSlot.markDirty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return copy;
|
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) {
|
public BackpackScreenHandler withHand(Hand hand) {
|
||||||
this.hand = hand;
|
this.hand = hand;
|
||||||
DefaultedList<ItemStack> ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY);
|
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) {
|
if (ad.size() == 0) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -125,4 +108,8 @@ public class BackpackScreenHandler extends ScreenHandler {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Hand getHand() {
|
||||||
|
return hand;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,8 +48,8 @@ public class RegistryManager {
|
|||||||
|
|
||||||
public static final String QUICKIEFABRIC = "quickiefabric";
|
public static final String QUICKIEFABRIC = "quickiefabric";
|
||||||
|
|
||||||
public static final Identifier BACKPACK_CONTAINTER = new Identifier(QUICKIEFABRIC, "backpack");
|
public static final Identifier BACKPACK_IDENTIFIER = new Identifier(QUICKIEFABRIC, "backpack");
|
||||||
public static final ScreenHandlerType<BackpackScreenHandler> BACKPACK_SCREEN_HANDLER = ScreenHandlerRegistry.registerSimple(RegistryManager.BACKPACK_CONTAINTER, BackpackScreenHandler::new);
|
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))
|
public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(QuickieItems.SPEEDPOWDER))
|
||||||
.appendItems(stacks -> {
|
.appendItems(stacks -> {
|
||||||
|
@ -3,10 +3,10 @@ package de.jottyfan.minecraft.quickiefabric.items;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
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.container.BackpackScreenHandler;
|
||||||
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
|
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.inventory.Inventories;
|
|
||||||
import net.minecraft.item.DyeableItem;
|
import net.minecraft.item.DyeableItem;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
@ -15,10 +15,7 @@ import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
|
|||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.util.ActionResult;
|
import net.minecraft.util.ActionResult;
|
||||||
import net.minecraft.util.Hand;
|
import net.minecraft.util.Hand;
|
||||||
import net.minecraft.util.Identifier;
|
|
||||||
import net.minecraft.util.TypedActionResult;
|
import net.minecraft.util.TypedActionResult;
|
||||||
import net.minecraft.util.collection.DefaultedList;
|
|
||||||
import net.minecraft.util.registry.Registry;
|
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,7 +24,7 @@ import net.minecraft.world.World;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ItemBackpack extends Item implements DyeableItem {
|
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 final static Integer SLOTSIZE = 54;
|
||||||
|
|
||||||
public ItemBackpack() {
|
public ItemBackpack() {
|
||||||
@ -37,31 +34,43 @@ public class ItemBackpack extends Item implements DyeableItem {
|
|||||||
@Override
|
@Override
|
||||||
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
||||||
if (!world.isClient) {
|
if (!world.isClient) {
|
||||||
for (Identifier id : Registry.SCREEN_HANDLER.getIds()) {
|
|
||||||
LOGGER.info("found registered screen {}", id.toString());
|
|
||||||
}
|
|
||||||
SimpleNamedScreenHandlerFactory factory = new SimpleNamedScreenHandlerFactory(
|
SimpleNamedScreenHandlerFactory factory = new SimpleNamedScreenHandlerFactory(
|
||||||
(id, inventory, buf) -> new BackpackScreenHandler(id, player.inventory).withHand(hand),
|
(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);
|
player.openHandledScreen(factory);
|
||||||
}
|
}
|
||||||
return new TypedActionResult<ItemStack>(ActionResult.PASS, player.getStackInHand(hand));
|
return new TypedActionResult<ItemStack>(ActionResult.PASS, player.getStackInHand(hand));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setInventory(ItemStack backpack, DefaultedList<ItemStack> list) {
|
public static BackpackInventory getInventory(BackpackScreenHandler handler, Hand hand, PlayerEntity player) {
|
||||||
CompoundTag nbt = backpack.getOrCreateTag();
|
ItemStack stack = player.getMainHandStack();
|
||||||
Inventories.toTag(nbt, list);
|
if (stack != null) {
|
||||||
backpack.setTag(nbt);
|
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) {
|
// public static void setInventory(ItemStack backpack, DefaultedList<ItemStack> list) {
|
||||||
CompoundTag nbt;
|
// CompoundTag nbt = backpack.getOrCreateTag();
|
||||||
if (backpack.hasTag()) {
|
// Inventories.toTag(nbt, list);
|
||||||
nbt = backpack.getTag();
|
// backpack.setTag(nbt);
|
||||||
} else {
|
// }
|
||||||
nbt = new CompoundTag();
|
//
|
||||||
}
|
// public static void getInventory(ItemStack backpack, DefaultedList<ItemStack> defaultedList) {
|
||||||
Inventories.fromTag(nbt, defaultedList);
|
// CompoundTag nbt;
|
||||||
backpack.setTag(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 |
Loading…
x
Reference in New Issue
Block a user