enhanced drills and container interactivity

This commit is contained in:
Jottyfan 2022-02-26 21:37:21 +01:00
parent 9adb81e431
commit 2f117d21db
9 changed files with 75 additions and 45 deletions

View File

@ -9,7 +9,7 @@
loader_version=0.12.12
# Mod Properties
mod_version = 1.18.1.6
mod_version = 1.18.1.7
maven_group = de.jottyfan.minecraft
archives_base_name = quickiefabric

View File

@ -26,6 +26,9 @@ public class DrillBlockEastEntity extends DrillBlockEntity {
DrillBlockEastEntity dbe = (DrillBlockEastEntity) be;
List<BlockPos> list = new ArrayList<>();
list.add(pos.east());
list.add(pos.east().up());
list.add(pos.east().up().up());
list.add(pos.east().up().up().up());
list.add(pos.east().down()); // must be last position
DrillBlockEntity.tick(world, pos, state, dbe, MAXDRILLSTEP, list);
}

View File

@ -26,8 +26,8 @@ public abstract class DrillBlockEntity extends BlockEntity {
if (be != null) {
world.setBlockState(from, Blocks.AIR.getDefaultState());
world.setBlockState(to, bs);
world.removeBlockEntity(from);
world.addBlockEntity(be);
world.removeBlockEntity(from);
}
}

View File

@ -26,6 +26,9 @@ public class DrillBlockNorthEntity extends DrillBlockEntity {
DrillBlockNorthEntity dbe = (DrillBlockNorthEntity) be;
List<BlockPos> list = new ArrayList<>();
list.add(pos.north());
list.add(pos.north().up());
list.add(pos.north().up().up());
list.add(pos.north().up().up().up());
list.add(pos.north().down()); // must be last position
DrillBlockEntity.tick(world, pos, state, dbe, MAXDRILLSTEP, list);
}

View File

@ -25,6 +25,9 @@ public class DrillBlockSouthEntity extends DrillBlockEntity {
DrillBlockSouthEntity dbe = (DrillBlockSouthEntity) be;
List<BlockPos> list = new ArrayList<>();
list.add(pos.south());
list.add(pos.south().up());
list.add(pos.south().up().up());
list.add(pos.south().up().up().up());
list.add(pos.south().down()); // must be last position
DrillBlockEntity.tick(world, pos, state, dbe, MAXDRILLSTEP, list);
}

View File

@ -26,6 +26,9 @@ public class DrillBlockWestEntity extends DrillBlockEntity {
DrillBlockWestEntity dbe = (DrillBlockWestEntity) be;
List<BlockPos> list = new ArrayList<>();
list.add(pos.west());
list.add(pos.west().up());
list.add(pos.west().up().up());
list.add(pos.west().up().up().up());
list.add(pos.west().down()); // must be last position
DrillBlockEntity.tick(world, pos, state, dbe, MAXDRILLSTEP, list);
}

View File

@ -10,12 +10,8 @@ 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.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.ChestBlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
@ -77,7 +73,8 @@ public class ItemBackpack extends Item {
}
/**
* sort the content of the backpack; for spreaded items, combine them until the itemstack size is reached
* sort the content of the backpack; for spreaded items, combine them until the
* itemstack size is reached
*
* @param itemStack the itemStack of the backpack
* @return the itemStack of the sorted backpack
@ -105,44 +102,65 @@ public class ItemBackpack extends Item {
BlockPos pos = context.getBlockPos();
World world = context.getWorld();
PlayerEntity player = context.getPlayer();
BlockState blockState = world.getBlockState(pos);
Block block = blockState.getBlock();
if (Blocks.CHEST.equals(block)) {
BlockEntity entity = world.getBlockEntity(pos);
BlockEntityType<?> type = entity.getType();
if (BlockEntityType.CHEST.equals(type)) {
ChestBlockEntity cbe = (ChestBlockEntity) entity;
if (entity != null && entity instanceof LootableContainerBlockEntity) {
BackpackInventory bi = new BackpackInventory(context.getStack());
bi.setHand(context.getHand());
if (cbe.isEmpty()) {
LootableContainerBlockEntity blockEntity = (LootableContainerBlockEntity) entity;
if (bi.isEmpty()) { // fill backpack
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);
if (slot < blockEntity.size()) {
bi.setStack(slot, blockEntity.getStack(slot));
blockEntity.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);
} else { // empty backpack as long as possible
for (int slot = 0; slot < ItemBackpack.SLOTSIZE; slot++) {
List<Integer> slots = findSlots(blockEntity, bi.getStack(slot));
for (Integer barrelSlot : slots) {
ItemStack barrelStack = blockEntity.getStack(barrelSlot);
ItemStack backpackStack = bi.getStack(slot);
int foundCount = barrelStack.getCount();
int transferCount = 64 - foundCount;
int newBackpackCount = backpackStack.getCount() - transferCount;
if (newBackpackCount < 0) {
transferCount = backpackStack.getCount();
newBackpackCount = 0;
}
// if it was air, replace it with something
blockEntity.setStack(barrelSlot, new ItemStack(backpackStack.getItem(), foundCount + transferCount));
backpackStack.decrement(transferCount);
}
}
bi.onClose(player);
if (!world.isClient) {
player.sendMessage(new TranslatableText("msg.backpack.transfer.cleared"), false);
}
}
}
return super.useOnBlock(context);
}
/**
* find the numbers of the slots that contain items of stack or have Air
*
* @param blockEntity the block entity
* @param stack the item stack
* @return a list of found stack positions; an empty list at least
*/
private final List<Integer> findSlots(LootableContainerBlockEntity blockEntity, ItemStack stack) {
List<Integer> list = new ArrayList<>();
if (!stack.isEmpty())
for (int i = 0; i < blockEntity.size(); i++) {
ItemStack containerStack = blockEntity.getStack(i);
if (containerStack.isEmpty() || containerStack.getItem().equals(stack.getItem())) {
list.add(i);
}
}
return list;
}
}

View File

@ -76,8 +76,8 @@
"msg.buildingplan.failonplayer": "Der Bau wurde abgelehnt, um Spieler %s nicht zu gefährden.",
"msg.itemhoarder.summary": "Der Itemsauger enthält: %s",
"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.filled": "Der Rucksack wurde befüllt.",
"msg.backpack.transfer.cleared": "Der Rucksackinhalt wurde soweit möglich geleert.",
"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."
}

View File

@ -76,8 +76,8 @@
"msg.buildingplan.failonplayer": "The building execution was rejected because of %s who could be injured.",
"msg.itemhoarder.summary": "The item hoarder contains: %s",
"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.filled": "Filled the backpack.",
"msg.backpack.transfer.cleared": "Cleared the backpack as much as possible.",
"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."
}