some steps forward in menu container server client stuff
This commit is contained in:
parent
988a867099
commit
1303489a90
@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
|
||||
loader_version=0.8.8+build.202
|
||||
|
||||
# Mod Properties
|
||||
mod_version = 1.16.1.7
|
||||
mod_version = 1.16.1.8
|
||||
maven_group = de.jottyfan.minecraft
|
||||
archives_base_name = quickiefabric
|
||||
|
||||
|
@ -9,6 +9,7 @@ import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.sound.SoundCategory;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.collection.DefaultedList;
|
||||
|
||||
public class BackpackInventory implements Inventory {
|
||||
@ -22,6 +23,19 @@ public class BackpackInventory implements Inventory {
|
||||
readItemsFromTag(this.stacks, tag);
|
||||
}
|
||||
|
||||
public static BackpackInventory getInventory(BackpackScreenHandler handler, 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());
|
||||
}
|
||||
}
|
||||
return new BackpackInventory(stack.getTag().getCompound("backpack"), handler);
|
||||
}
|
||||
|
||||
private void readItemsFromTag(DefaultedList<ItemStack> inventory, CompoundTag tag) {
|
||||
ListTag listTag = tag.getList("items", 10);
|
||||
for (int i = 0; i < listTag.size(); ++i) {
|
||||
|
@ -6,6 +6,7 @@ import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.screen.ScreenHandler;
|
||||
import net.minecraft.screen.slot.Slot;
|
||||
import net.minecraft.util.Hand;
|
||||
@ -18,14 +19,17 @@ public class BackpackScreenHandler extends ScreenHandler {
|
||||
private int backpackSlots;
|
||||
private Hand hand;
|
||||
|
||||
public BackpackScreenHandler(int syncId, PlayerInventory playerInventory) {
|
||||
public BackpackScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) {
|
||||
super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
|
||||
this.player = playerInventory.player;
|
||||
this.backpackInventory = ItemBackpack.getInventory(this, hand, player);
|
||||
ItemStack stack = buf.readItemStack();
|
||||
this.hand = buf.readInt() == 0 ? Hand.MAIN_HAND : Hand.OFF_HAND;
|
||||
this.backpackInventory = BackpackInventory.getInventory(this, player);
|
||||
backpackSlots = ItemBackpack.SLOTSIZE;
|
||||
|
||||
int spacing = 112;
|
||||
|
||||
backpackInventory.onOpen(player);
|
||||
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, -10 + y * 18));
|
||||
@ -39,6 +43,20 @@ public class BackpackScreenHandler extends ScreenHandler {
|
||||
for (int x = 0; x < 9; ++x) {
|
||||
this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing));
|
||||
}
|
||||
playerInventory.onOpen(player);
|
||||
|
||||
DefaultedList<ItemStack> ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY);
|
||||
if (ad.size() == 0) {
|
||||
return;
|
||||
}
|
||||
for (int x = 0; x < 9; x++) {
|
||||
for (int y = 0; y < backpackSlots / 9; y++) {
|
||||
this.getSlot(x + y * 9).setStack(ad.get(x + y * 9));
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < (backpackSlots % 9); x++) {
|
||||
this.getSlot(x + (backpackSlots / 9) * 9).setStack(ad.get(x + (backpackSlots / 9) * 9));
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerEntity getPlayer() {
|
||||
@ -91,24 +109,6 @@ public class BackpackScreenHandler extends ScreenHandler {
|
||||
return copy;
|
||||
}
|
||||
|
||||
public BackpackScreenHandler withHand(Hand hand) {
|
||||
this.hand = hand;
|
||||
DefaultedList<ItemStack> ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY);
|
||||
// ItemBackpack.getInventory(player.getStackInHand(this.hand), ad);
|
||||
if (ad.size() == 0) {
|
||||
return this;
|
||||
}
|
||||
for (int x = 0; x < 9; x++) {
|
||||
for (int y = 0; y < backpackSlots / 9; y++) {
|
||||
this.getSlot(x + y * 9).setStack(ad.get(x + y * 9));
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < (backpackSlots % 9); x++) {
|
||||
this.getSlot(x + (backpackSlots / 9) * 9).setStack(ad.get(x + (backpackSlots / 9) * 9));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Hand getHand() {
|
||||
return hand;
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public class RegistryManager {
|
||||
public static final String QUICKIEFABRIC = "quickiefabric";
|
||||
|
||||
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 ScreenHandlerType<BackpackScreenHandler> BACKPACK_SCREEN_HANDLER = ScreenHandlerRegistry.registerExtended(RegistryManager.BACKPACK_IDENTIFIER, BackpackScreenHandler::new);
|
||||
|
||||
public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(QuickieItems.SPEEDPOWDER))
|
||||
.appendItems(stacks -> {
|
||||
|
@ -1,17 +1,18 @@
|
||||
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 io.netty.buffer.Unpooled;
|
||||
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.item.DyeableItem;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
|
||||
import net.minecraft.item.ItemUsageContext;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.screen.ScreenHandler;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.Hand;
|
||||
@ -24,7 +25,6 @@ import net.minecraft.world.World;
|
||||
*
|
||||
*/
|
||||
public class ItemBackpack extends Item implements DyeableItem {
|
||||
private final static Logger LOGGER = LogManager.getLogger(ItemBackpack.class);
|
||||
public final static Integer SLOTSIZE = 54;
|
||||
|
||||
public ItemBackpack() {
|
||||
@ -34,43 +34,34 @@ public class ItemBackpack extends Item implements DyeableItem {
|
||||
@Override
|
||||
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
||||
if (!world.isClient) {
|
||||
SimpleNamedScreenHandlerFactory factory = new SimpleNamedScreenHandlerFactory(
|
||||
(id, inventory, buf) -> new BackpackScreenHandler(id, player.inventory).withHand(hand),
|
||||
Text.method_30163(RegistryManager.BACKPACK_IDENTIFIER.toString()));
|
||||
player.openHandledScreen(factory);
|
||||
ItemStack itemStack = player.getActiveItem();
|
||||
player.openHandledScreen(new ExtendedScreenHandlerFactory() {
|
||||
@Override
|
||||
public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) {
|
||||
packetByteBuf.writeItemStack(itemStack);
|
||||
packetByteBuf.writeInt(hand.equals(Hand.MAIN_HAND) ? 0 : 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Text getDisplayName() {
|
||||
return Text.method_30163(RegistryManager.BACKPACK_IDENTIFIER.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
|
||||
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
|
||||
buf.writeItemStack(itemStack);
|
||||
buf.writeInt(hand.equals(Hand.MAIN_HAND) ? 0 : 1);
|
||||
return new BackpackScreenHandler(syncId, inv, buf);
|
||||
}
|
||||
});
|
||||
}
|
||||
return new TypedActionResult<ItemStack>(ActionResult.PASS, player.getStackInHand(hand));
|
||||
}
|
||||
|
||||
public static BackpackInventory getInventory(BackpackScreenHandler handler, Hand hand, PlayerEntity player) {
|
||||
ItemStack stack = player.getMainHandStack();
|
||||
if (stack != null) {
|
||||
if (!stack.hasTag()) {
|
||||
stack.setTag(new CompoundTag());
|
||||
@Override
|
||||
public ActionResult useOnBlock(ItemUsageContext context) {
|
||||
// TODO implement unloading by right clicking on a chest; if left click, load as much as possible
|
||||
return super.useOnBlock(context);
|
||||
}
|
||||
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 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);
|
||||
// }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user