forbid backpack to place itself into it

This commit is contained in:
Jörg Henke 2020-09-26 16:04:15 +02:00
parent 515f3a6d28
commit 2ec6179053
9 changed files with 52 additions and 112 deletions

View File

@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.9.3+build.207 loader_version=0.9.3+build.207
# Mod Properties # Mod Properties
mod_version = 1.16.3.0 mod_version = 1.16.3.1
maven_group = de.jottyfan.minecraft maven_group = de.jottyfan.minecraft
archives_base_name = quickiefabric archives_base_name = quickiefabric

View File

@ -29,7 +29,7 @@ import net.minecraft.world.World;
public class BlockLavahoarder extends Block { public class BlockLavahoarder extends Block {
public BlockLavahoarder() { public BlockLavahoarder() {
super(FabricBlockSettings.of(Material.STONE).hardness(2.5f)); super(FabricBlockSettings.of(Material.STONE).hardness(2.5f).lightLevel(16));
} }
@Override @Override
@ -39,7 +39,6 @@ public class BlockLavahoarder extends Block {
return list; return list;
} }
private static final String stringOf(BlockPos pos) { private static final String stringOf(BlockPos pos) {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
buf.append(pos.getX()).append(":"); buf.append(pos.getX()).append(":");

View File

@ -2,30 +2,25 @@ package de.jottyfan.minecraft.quickiefabric.container;
import de.jottyfan.minecraft.quickiefabric.items.ItemBackpack; import de.jottyfan.minecraft.quickiefabric.items.ItemBackpack;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories; import net.minecraft.inventory.SimpleInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents; import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.collection.DefaultedList;
/** /**
* *
* @author jotty * @author jotty
* *
*/ */
public class BackpackInventory implements Inventory { public class BackpackInventory extends SimpleInventory {
private final DefaultedList<ItemStack> stacks;
private final BackpackScreenHandler handler;
private Hand hand; private Hand hand;
private BackpackInventory(CompoundTag tag, BackpackScreenHandler handler) { private BackpackInventory(CompoundTag tag, BackpackScreenHandler handler) {
this.handler = handler; super(ItemBackpack.SLOTSIZE);
this.stacks = DefaultedList.ofSize(ItemBackpack.SLOTSIZE, ItemStack.EMPTY); readItemsFromTag(super.size(), tag);
readItemsFromTag(this.stacks, tag);
} }
public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player, public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player,
@ -41,64 +36,15 @@ public class BackpackInventory implements Inventory {
return new BackpackInventory(stack.getTag().getCompound("backpack"), handler); return new BackpackInventory(stack.getTag().getCompound("backpack"), handler);
} }
public DefaultedList<ItemStack> getList() {
return stacks;
}
@Override
public void clear() {
stacks.clear();
markDirty();
}
@Override
public boolean canPlayerUse(PlayerEntity player) {
return true;
}
@Override
public ItemStack getStack(int slot) {
return slot >= stacks.size() ? ItemStack.EMPTY : stacks.get(slot);
}
@Override
public boolean isEmpty() {
return this.stacks.stream().allMatch(ItemStack::isEmpty);
}
@Override
public ItemStack removeStack(int slot) {
return Inventories.removeStack(this.stacks, slot);
}
@Override
public ItemStack removeStack(int slot, int amount) {
return Inventories.splitStack(stacks, slot, amount);
}
@Override
public void setStack(int slot, ItemStack stack) {
this.stacks.set(slot, stack);
}
@Override
public int size() {
return stacks.size();
}
@Override
public void markDirty() {
}
@Override @Override
public void onOpen(PlayerEntity player) { public void onOpen(PlayerEntity player) {
Inventory.super.onOpen(player); super.onOpen(player);
player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f); player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f);
} }
@Override @Override
public void onClose(PlayerEntity player) { public void onClose(PlayerEntity player) {
Inventory.super.onClose(player); super.onClose(player);
ItemStack stack = player.getStackInHand(hand); ItemStack stack = player.getStackInHand(hand);
if (stack != null) { if (stack != null) {
if (!stack.hasTag()) { if (!stack.hasTag()) {
@ -110,24 +56,23 @@ public class BackpackInventory implements Inventory {
player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f); player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f);
} }
private void readItemsFromTag(DefaultedList<ItemStack> inventory, CompoundTag tag) { private void readItemsFromTag(Integer size, CompoundTag tag) {
ListTag listTag = tag.getList("items", 10); ListTag listTag = tag.getList("items", 10);
for (int i = 0; i < listTag.size(); ++i) { for (int i = 0; i < listTag.size(); ++i) {
CompoundTag compoundTag = listTag.getCompound(i); CompoundTag compoundTag = listTag.getCompound(i);
int j = compoundTag.getInt("slot"); int slot = compoundTag.getInt("slot");
if (j >= 0 && j < inventory.size()) { if (slot >= 0 && slot < size) {
inventory.set(j, ItemStack.fromTag(compoundTag)); super.setStack(slot, ItemStack.fromTag(compoundTag));
} }
} }
} }
private CompoundTag writeItemsToTag() { private CompoundTag writeItemsToTag() {
DefaultedList<ItemStack> inventory = stacks;
ListTag listTag = new ListTag(); ListTag listTag = new ListTag();
for (int i = 0; i < inventory.size(); ++i) { for (int i = 0; i < super.size(); ++i) {
ItemStack itemStack = (ItemStack) inventory.get(i); ItemStack itemStack = (ItemStack) super.getStack(i);
if (!itemStack.isEmpty()) { if (!(itemStack == null) && !itemStack.isEmpty()) {
CompoundTag compoundTag = new CompoundTag(); CompoundTag compoundTag = new CompoundTag();
compoundTag.putInt("slot", i); compoundTag.putInt("slot", i);
compoundTag = itemStack.toTag(compoundTag); compoundTag = itemStack.toTag(compoundTag);

View File

@ -22,10 +22,8 @@ import net.minecraft.util.Identifier;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class BackpackScreen extends HandledScreen<BackpackScreenHandler> public class BackpackScreen extends HandledScreen<BackpackScreenHandler>
implements ScreenHandlerProvider<BackpackScreenHandler> { implements ScreenHandlerProvider<BackpackScreenHandler> {
private final static Identifier TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, private final static Identifier TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, "textures/gui/backpack.png");
"textures/gui/backpack.png"); private final static Identifier SLOT_TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, "textures/gui/slot.png");
private final static Identifier SLOT_TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC,
"textures/gui/slot.png");
private final Integer containerHeight = 222; private final Integer containerHeight = 222;
private final Integer containerWidth = 176; private final Integer containerWidth = 176;
@ -38,18 +36,14 @@ public class BackpackScreen extends HandledScreen<BackpackScreenHandler>
super.init(); super.init();
this.x = (this.width - this.containerWidth) / 2; this.x = (this.width - this.containerWidth) / 2;
} }
private void drawSlots(MatrixStack matrices, int guiX, int guiY) { private void drawSlots(MatrixStack matrices, int guiX, int guiY) {
this.client.getTextureManager().bindTexture(SLOT_TEXTURE); this.client.getTextureManager().bindTexture(SLOT_TEXTURE);
for (int y = 0; y < (ItemBackpack.SLOTSIZE / 9); y++) for (int y = 0; y < (ItemBackpack.SLOTSIZE / 9); y++) {
for (int x = 0; x < 9; x++) { for (int x = 0; x < 9; x++) {
this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (y * 18), 0, 0, 18, 18); this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (y * 18), 0, 0, 18, 18);
} }
}
if ((ItemBackpack.SLOTSIZE % 9) != 0)
for (int x = 0; x < (ItemBackpack.SLOTSIZE % 9); x++) {
this.drawTexture(matrices, guiX + 7 + (x * 18), guiY + 17 + (ItemBackpack.SLOTSIZE / 9 * 18), 0, 0, 18, 18);
}
} }
@Override @Override
@ -72,7 +66,6 @@ public class BackpackScreen extends HandledScreen<BackpackScreenHandler>
@Override @Override
protected void drawForeground(MatrixStack matrixStack, int i, int j) { 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.title, 8.0F, -20.0F, 0xffcccccc); // αrgb
this.textRenderer.draw(matrixStack, this.playerInventory.getDisplayName(), 8.0F, 101f, 4210752);
} }
} }

View File

@ -1,8 +1,5 @@
package de.jottyfan.minecraft.quickiefabric.container; package de.jottyfan.minecraft.quickiefabric.container;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager; import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
@ -17,22 +14,18 @@ import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
public class BackpackScreenHandler extends ScreenHandler { public class BackpackScreenHandler extends ScreenHandler {
private final Logger LOGGER = LogManager.getLogger(BackpackScreenHandler.class);
private final BackpackInventory backpackInventory; private final BackpackInventory backpackInventory;
private final PlayerEntity player; private final PlayerEntity player;
private final PlayerInventory playerInventory; private final ItemStack thisStack;
private final Hand hand; private final Hand hand;
public BackpackScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) { public BackpackScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) {
super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId); super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
this.playerInventory = playerInventory;
this.player = playerInventory.player; this.player = playerInventory.player;
ItemStack stack = buf.readItemStack(); thisStack = buf.readItemStack();
this.hand = buf.readByte() > 0 ? Hand.MAIN_HAND : Hand.OFF_HAND; this.hand = buf.readByte() > 0 ? Hand.MAIN_HAND : Hand.OFF_HAND;
this.backpackInventory = BackpackInventory.getInventory(this, player, stack); this.backpackInventory = BackpackInventory.getInventory(this, player, thisStack);
int spacing = 112;
backpackInventory.onOpen(player); backpackInventory.onOpen(player);
for (int y = 0; y < 6; y++) { for (int y = 0; y < 6; y++) {
@ -42,18 +35,18 @@ public class BackpackScreenHandler extends ScreenHandler {
} }
for (int y = 0; y < 3; ++y) { for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 9; ++x) { for (int x = 0; x < 9; ++x) {
this.addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, spacing + y * 18)); this.addSlot(new Slot(playerInventory, x + (y * 9) + 9, 8 + x * 18, 112 + 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, 8 + x * 18, 170));
} }
} }
public PlayerEntity getPlayer() { public PlayerEntity getPlayer() {
return this.player; return this.player;
} }
public Inventory getInventory() { public Inventory getInventory() {
return backpackInventory; return backpackInventory;
} }
@ -92,16 +85,20 @@ public class BackpackScreenHandler extends ScreenHandler {
} }
return copy; return copy;
} }
@Override @Override
public ItemStack onSlotClick(int i, int j, SlotActionType actionType, PlayerEntity playerEntity) { public ItemStack onSlotClick(int slotId, int quickCraftData, SlotActionType actionType, PlayerEntity playerEntity) {
LOGGER.info("click on slot at {},{} with actionType {} by {}", i, j, actionType, playerEntity); if (slotId >= 0) {
// TODO: prevent the one slot that contains the active backpack item ItemStack stack = getSlot(slotId).getStack();
return super.onSlotClick(i, j, actionType, playerEntity); if (stack.getName().equals(thisStack.getName())) {
return stack;
}
}
return super.onSlotClick(slotId, quickCraftData, actionType, playerEntity);
}
@Environment(EnvType.CLIENT)
public int getRows() {
return 6;
} }
@Environment(EnvType.CLIENT)
public int getRows() {
return 6;
}
} }

View File

@ -40,9 +40,9 @@ import net.minecraft.world.gen.GenerationStep;
import net.minecraft.world.gen.decorator.ChanceDecoratorConfig; import net.minecraft.world.gen.decorator.ChanceDecoratorConfig;
import net.minecraft.world.gen.decorator.Decorator; import net.minecraft.world.gen.decorator.Decorator;
import net.minecraft.world.gen.decorator.RangeDecoratorConfig; import net.minecraft.world.gen.decorator.RangeDecoratorConfig;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.Feature; import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.OreFeatureConfig; import net.minecraft.world.gen.feature.OreFeatureConfig;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.SimpleBlockFeatureConfig; import net.minecraft.world.gen.feature.SimpleBlockFeatureConfig;
/** /**
@ -146,7 +146,7 @@ public class RegistryManager {
registerItem(QuickieTools.SPEEDPOWDERPICKAXE, "speedpowderpickaxe"); registerItem(QuickieTools.SPEEDPOWDERPICKAXE, "speedpowderpickaxe");
registerItem(QuickieTools.SPEEDPOWDERSHOVEL, "speedpowdershovel"); registerItem(QuickieTools.SPEEDPOWDERSHOVEL, "speedpowdershovel");
} }
public static final void registerContainer() { public static final void registerContainer() {
// ScreenHandlerRegistry.registerSimple(BACKPACK_CONTAINTER, (syncId, inventory) -> new BackpackScreenHandler(syncId, inventory)); // ScreenHandlerRegistry.registerSimple(BACKPACK_CONTAINTER, (syncId, inventory) -> new BackpackScreenHandler(syncId, inventory));
} }

View File

@ -18,11 +18,12 @@ import net.minecraft.item.ToolMaterials;
*/ */
public class ToolSpeedpowderPickaxe extends PickaxeItem implements ToolRangeable { public class ToolSpeedpowderPickaxe extends PickaxeItem implements ToolRangeable {
public static final Integer DEFAULT_HARVEST_RANGE = 3;
private HarvestRange range; private HarvestRange range;
public ToolSpeedpowderPickaxe() { public ToolSpeedpowderPickaxe() {
super(ToolMaterials.DIAMOND, 4, 2.0f, new Item.Settings().group(RegistryManager.QUICKIEFABRIC_GROUP)); super(ToolMaterials.DIAMOND, 4, 2.0f, new Item.Settings().group(RegistryManager.QUICKIEFABRIC_GROUP));
this.range = new HarvestRange(3); this.range = new HarvestRange(DEFAULT_HARVEST_RANGE);
} }
@Override @Override
@ -40,6 +41,10 @@ public class ToolSpeedpowderPickaxe extends PickaxeItem implements ToolRangeable
return Lists.newArrayList(block); return Lists.newArrayList(block);
} }
public void setPlusRange(Integer plusRange) {
range.addXYZ(plusRange);
}
// @Override // @Override
// public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) { // public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
// CommonToolCode.onItemRightClick(worldIn, playerIn, handIn); // CommonToolCode.onItemRightClick(worldIn, playerIn, handIn);

View File

@ -23,11 +23,12 @@ import net.minecraft.util.registry.Registry;
*/ */
public class ToolSpeedpowderShovel extends ShovelItem implements ToolRangeable { public class ToolSpeedpowderShovel extends ShovelItem implements ToolRangeable {
private static final Logger LOGGER = LogManager.getLogger(ToolSpeedpowderShovel.class); private static final Logger LOGGER = LogManager.getLogger(ToolSpeedpowderShovel.class);
public static final Integer DEFAULT_HARVEST_RANGE = 3;
public HarvestRange range; public HarvestRange range;
public ToolSpeedpowderShovel() { public ToolSpeedpowderShovel() {
super(ToolMaterials.DIAMOND, 4, 2.0f, new Item.Settings().group(RegistryManager.QUICKIEFABRIC_GROUP)); super(ToolMaterials.DIAMOND, 4, 2.0f, new Item.Settings().group(RegistryManager.QUICKIEFABRIC_GROUP));
this.range = new HarvestRange(3); this.range = new HarvestRange(DEFAULT_HARVEST_RANGE);
} }
@Override @Override

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 35 KiB