interact backpack with chest on sneak right click

This commit is contained in:
Jörg Henke 2021-01-02 22:09:14 +01:00
parent 3e16e93cc8
commit d0e63ca87a
4 changed files with 55 additions and 5 deletions

View File

@ -25,6 +25,14 @@ public class BackpackInventory extends SimpleInventory {
public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player, public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player,
ItemStack stack) { ItemStack stack) {
return new BackpackInventory(init(stack).getTag().getCompound("backpack"), handler);
}
public BackpackInventory(ItemStack stack) {
this(init(stack).getTag().getCompound("backpack"), null);
}
private final static ItemStack init(ItemStack stack) {
if (stack != null) { if (stack != null) {
if (!stack.hasTag()) { if (!stack.hasTag()) {
stack.setTag(new CompoundTag()); stack.setTag(new CompoundTag());
@ -33,7 +41,7 @@ public class BackpackInventory extends SimpleInventory {
stack.getTag().put("backpack", new CompoundTag()); stack.getTag().put("backpack", new CompoundTag());
} }
} }
return new BackpackInventory(stack.getTag().getCompound("backpack"), handler); return stack;
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package de.jottyfan.minecraft.quickiefabric.items; package de.jottyfan.minecraft.quickiefabric.items;
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 io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@ -9,6 +10,7 @@ import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.ChestBlockEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item; import net.minecraft.item.Item;
@ -19,6 +21,7 @@ import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult; import net.minecraft.util.TypedActionResult;
@ -36,7 +39,7 @@ public class ItemBackpack extends Item {
public ItemBackpack() { public ItemBackpack() {
super(new Item.Settings().group(RegistryManager.QUICKIEFABRIC_GROUP).maxCount(1)); super(new Item.Settings().group(RegistryManager.QUICKIEFABRIC_GROUP).maxCount(1));
} }
@Override @Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
ItemStack itemStack = player.getStackInHand(hand); ItemStack itemStack = player.getStackInHand(hand);
@ -64,19 +67,52 @@ public class ItemBackpack extends Item {
} }
return new TypedActionResult<ItemStack>(ActionResult.SUCCESS, itemStack); return new TypedActionResult<ItemStack>(ActionResult.SUCCESS, itemStack);
} }
@Override @Override
public ActionResult useOnBlock(ItemUsageContext context) { public ActionResult useOnBlock(ItemUsageContext context) {
BlockPos pos = context.getBlockPos(); BlockPos pos = context.getBlockPos();
World world = context.getWorld(); World world = context.getWorld();
PlayerEntity player = context.getPlayer();
BlockState blockState = world.getBlockState(pos); BlockState blockState = world.getBlockState(pos);
Block block = blockState.getBlock(); Block block = blockState.getBlock();
if (Blocks.CHEST.equals(block)) { if (Blocks.CHEST.equals(block)) {
BlockEntity entity = world.getBlockEntity(pos); BlockEntity entity = world.getBlockEntity(pos);
BlockEntityType<?> type = entity.getType(); BlockEntityType<?> type = entity.getType();
if (BlockEntityType.CHEST.equals(type)) { if (BlockEntityType.CHEST.equals(type)) {
// TODO: create a new dialog for transferring items from backpack to chest and back ChestBlockEntity cbe = (ChestBlockEntity) entity;
// with the help of some buttons (such as all or so) BackpackInventory bi = new BackpackInventory(context.getStack());
bi.setHand(context.getHand());
if (cbe.isEmpty()) {
for (int slot = 0; slot < ItemBackpack.SLOTSIZE; slot++) {
if (slot < cbe.size()) {
cbe.setStack(slot, bi.getStack(slot));
bi.removeStack(slot);
}
}
bi.onClose(player);
if (!world.isClient) {
player.sendMessage(new TranslatableText("msg.backpack.transfer.cleared"), false);
}
} else if (bi.isEmpty()) {
for (int slot = 0; slot < ItemBackpack.SLOTSIZE; slot++) {
if (slot < cbe.size()) {
bi.setStack(slot, cbe.getStack(slot));
cbe.setStack(slot, ItemStack.EMPTY);
}
}
bi.onClose(player);
if (!world.isClient) {
player.sendMessage(new TranslatableText("msg.backpack.transfer.filled"), false);
}
} else {
if (!world.isClient) {
player.sendMessage(new TranslatableText("msg.backpack.transfer.cancel"), false);
}
}
// alternative:
// TODO: create a new dialog for transferring items from backpack to chest and
// back
// with the help of some buttons (such as all or so)
} }
} }
return super.useOnBlock(context); return super.useOnBlock(context);

View File

@ -66,5 +66,8 @@
"msg.buildingplan.failonplayer": "Der Bau wurde abgelehnt, um Spieler %s nicht zu gefährden.", "msg.buildingplan.failonplayer": "Der Bau wurde abgelehnt, um Spieler %s nicht zu gefährden.",
"msg.itemhoarder.summary": "Der Itemsauger enthält: %s", "msg.itemhoarder.summary": "Der Itemsauger enthält: %s",
"msg.notyetimplemented": "leider noch nicht verfügbar", "msg.notyetimplemented": "leider noch nicht verfügbar",
"msg.backpack.transfer.filled": "Der Rucksack wurde aus der Kiste befüllt.",
"msg.backpack.transfer.cleared": "Der Rucksackinhalt wurde in die Kiste geschüttet.",
"msg.backpack.transfer.cancel": "Entweder der Rucksack oder die Kiste sind nicht komplett leer.",
"error.unleveling.inventory.full": "Es ist kein Platz mehr frei, um die Aufwertungen abzulegen." "error.unleveling.inventory.full": "Es ist kein Platz mehr frei, um die Aufwertungen abzulegen."
} }

View File

@ -66,5 +66,8 @@
"msg.buildingplan.failonplayer": "The building execution was rejected because of %s who could be injured.", "msg.buildingplan.failonplayer": "The building execution was rejected because of %s who could be injured.",
"msg.itemhoarder.summary": "The item hoarder contains: %s", "msg.itemhoarder.summary": "The item hoarder contains: %s",
"msg.notyetimplemented": "not yet implemented", "msg.notyetimplemented": "not yet implemented",
"msg.backpack.transfer.filled": "Filled the backpack from the chest.",
"msg.backpack.transfer.cleared": "Filled the chest from the backpack.",
"msg.backpack.transfer.cancel": "Eigther backpack or chest are not completely empty.",
"error.unleveling.inventory.full": "There is no free stack in your inventory for the level ups." "error.unleveling.inventory.full": "There is no free stack in your inventory for the level ups."
} }