1
0
Fork 0

implement pipes

This commit is contained in:
Sen 2025-08-13 14:40:11 +02:00
parent 90c25e5fd7
commit 6e5a1b7e37
Signed by: sen
GPG key ID: 3AC50A6F47D1B722

View file

@ -10,545 +10,419 @@ import common.block.tech.BlockPipe;
import common.entity.Entity; import common.entity.Entity;
import common.entity.item.EntityItem; import common.entity.item.EntityItem;
import common.entity.npc.EntityNPC; import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.inventory.IInventory; import common.inventory.IInventory;
import common.inventory.ISidedInventory; import common.inventory.ISidedInventory;
import common.item.ItemStack; import common.item.ItemStack;
import common.tags.TagObject; import common.tags.TagObject;
import common.util.BlockPos; import common.util.BlockPos;
import common.util.BoundingBox; import common.util.BoundingBox;
import common.util.ExtMath;
import common.util.Facing; import common.util.Facing;
import common.vars.Vars; import common.vars.Vars;
import common.world.State; import common.world.State;
import common.world.World;
public class TileEntityPipe extends TileEntity implements IInventory, ITickable {
public class TileEntityPipe extends TileEntity implements IInventory, ITickable private ItemStack stack = null;
{ private int cooldown = -1;
private ItemStack inventory = null;
private int cooldown = -1; public void readTags(TagObject tag) {
super.readTags(tag);
public void readTags(TagObject tag) this.cooldown = tag.getInt("Cooldown");
{ this.stack = ItemStack.readFromTag(tag.getObject("Item"));
super.readTags(tag); }
this.cooldown = tag.getInt("Cooldown");
this.inventory = ItemStack.readFromTag(tag.getObject("Item")); public void writeTags(TagObject tag) {
} super.writeTags(tag);
tag.setInt("Cooldown", this.cooldown);
public void writeTags(TagObject tag) if(this.stack != null) {
{ TagObject item = new TagObject();
super.writeTags(tag); this.stack.writeTags(item);
tag.setInt("Cooldown", this.cooldown); tag.setObject("Item", item);
if (this.inventory != null) }
{ }
TagObject item = new TagObject();
this.inventory.writeTags(item); public int getSizeInventory() {
tag.setObject("Item", item); return 1;
} }
}
public ItemStack getStackInSlot(int index) {
public int getSizeInventory() return this.stack;
{ }
return 1;
} public ItemStack decrStackSize(int index, int count) {
if(this.stack != null) {
public ItemStack getStackInSlot(int index) if(this.stack.getSize() <= count) {
{ ItemStack itemstack1 = this.stack;
return this.inventory; this.stack = null;
} return itemstack1;
}
public ItemStack decrStackSize(int index, int count) else {
{ ItemStack itemstack = this.stack.split(count);
if (this.inventory != null)
{ if(this.stack.isEmpty()) {
if (this.inventory.getSize() <= count) this.stack = null;
{ }
ItemStack itemstack1 = this.inventory;
this.inventory = null; return itemstack;
return itemstack1; }
} }
else else {
{ return null;
ItemStack itemstack = this.inventory.split(count); }
}
if (this.inventory.isEmpty())
{ public ItemStack removeStackFromSlot(int index) {
this.inventory = null; if(this.stack != null) {
} ItemStack itemstack = this.stack;
this.stack = null;
return itemstack; return itemstack;
} }
} else {
else return null;
{ }
return null; }
}
} public void setInventorySlotContents(int index, ItemStack stack) {
this.stack = stack;
public ItemStack removeStackFromSlot(int index) }
{
if (this.inventory != null) public void clear() {
{ this.stack = null;
ItemStack itemstack = this.inventory; }
this.inventory = null;
return itemstack; public boolean isUseableByPlayer(EntityNPC player) {
} return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
else }
{
return null; public void update() {
} if(this.world != null && !this.world.client) {
} --this.cooldown;
public void setInventorySlotContents(int index, ItemStack stack) if(!this.isOnTransferCooldown()) {
{ this.setTransferCooldown(0);
this.inventory = stack; this.updatePipe();
} }
}
public void clear() }
{
this.inventory = null; protected boolean updatePipe() {
} if(!this.isOnTransferCooldown()) {
State state = this.getState();
public boolean isUseableByPlayer(EntityNPC player) if(!(state.getBlock() instanceof BlockPipe) /* || !this.decrPower() */) // TODO: power
{ return false;
return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D; boolean flag = false;
}
if(!this.isEmpty()) {
public void update() flag = this.transferOut();
{ }
if (this.world != null && !this.world.client)
{ if(!this.isFull()) {
--this.cooldown; flag |= this.transferIn(false);
}
if (!this.isOnTransferCooldown())
{ if(flag) {
this.setTransferCooldown(0); this.setTransferCooldown(Vars.pipeDelay);
this.updatePipe(); this.markDirty();
} return true;
} }
} }
public boolean updatePipe() return false;
{ }
if (this.world != null && !this.world.client)
{ private boolean isEmpty() {
if (!this.isOnTransferCooldown()) return this.stack == null;
{ }
State state = this.getState();
if(!(state.getBlock() instanceof BlockPipe) /* || !this.decrPower() */) // TODO: power private boolean isFull() {
return false; return this.stack != null && this.stack.isFull();
boolean flag = false; }
if (!this.isEmpty()) public void setTransferCooldown(int ticks) {
{ this.cooldown = ticks;
flag = this.transferItemsOut(); }
}
public boolean isOnTransferCooldown() {
if (!this.isFull()) return this.cooldown > 0;
{ }
flag = captureDroppedItems(this, state.getValue(BlockPipe.FACING) != Facing.UP) || flag;
} public boolean mayTransfer() {
return this.cooldown <= 1;
if (flag) }
{
this.setTransferCooldown(Vars.pipeDelay); public int getColor() {
this.markDirty(); return 0x0040ff;
return true; }
}
} // out
return false; private static boolean isInventoryFull(IInventory inv, Facing side) {
} if(inv instanceof ISidedInventory sided) {
else int[] sides = sided.getSlotsForFace(side);
{
return false; for(int k = 0; k < sides.length; ++k) {
} ItemStack stack = sided.getStackInSlot(sides[k]);
}
if(stack == null || !stack.isFull()) {
private boolean isEmpty() return false;
{ }
return this.inventory == null; }
} }
else {
private boolean isFull() int i = inv.getSizeInventory();
{
return this.inventory != null && this.inventory.isFull(); for(int j = 0; j < i; ++j) {
} ItemStack stack = inv.getStackInSlot(j);
private boolean transferItemsOut() if(stack == null || !stack.isFull()) {
{ return false;
IInventory iinventory = this.getInventoryForTransfer(); }
}
if (iinventory == null) }
{
return false; return true;
} }
else
{ protected boolean transferOut() {
State state = this.getState(); State state = this.getState();
Facing enumfacing = state.getBlock() instanceof BlockPipe ? state.getValue(BlockPipe.FACING).getOpposite() : Facing.DOWN; IInventory iinventory = this.getInventory(this.pos.offset(state.getValue(BlockPipe.FACING)), false);
if(iinventory == null)
if (isInventoryFull(iinventory, enumfacing)) return false;
{ Facing dir = state.getValue(BlockPipe.FACING).getOpposite();
return false; if(isInventoryFull(iinventory, dir))
} return false;
else for(int i = 0; i < this.getSizeInventory(); ++i) {
{ if(this.getStackInSlot(i) != null) {
for (int i = 0; i < this.getSizeInventory(); ++i) ItemStack itemstack = this.getStackInSlot(i).copy();
{ ItemStack itemstack1 = putStackInInventoryAllSlots(iinventory, this.decrStackSize(i, 1), dir);
if (this.getStackInSlot(i) != null)
{ if(itemstack1 == null || itemstack1.isEmpty()) {
ItemStack itemstack = this.getStackInSlot(i).copy(); iinventory.markDirty();
ItemStack itemstack1 = putStackInInventoryAllSlots(iinventory, this.decrStackSize(i, 1), enumfacing); return true;
}
if (itemstack1 == null || itemstack1.isEmpty())
{ this.setInventorySlotContents(i, itemstack);
iinventory.markDirty(); }
return true; }
} return false;
}
this.setInventorySlotContents(i, itemstack);
} // in
}
private static boolean isInventoryEmpty(IInventory inv, Facing side) {
return false; if(inv instanceof ISidedInventory sided) {
} int[] slots = sided.getSlotsForFace(side);
}
} for(int i = 0; i < slots.length; ++i) {
if(sided.getStackInSlot(slots[i]) != null) {
public static boolean isInventoryFull(IInventory inventoryIn, Facing side) return false;
{ }
if (inventoryIn instanceof ISidedInventory) }
{ }
ISidedInventory isidedinventory = (ISidedInventory)inventoryIn; else {
int[] aint = isidedinventory.getSlotsForFace(side); int size = inv.getSizeInventory();
for (int k = 0; k < aint.length; ++k) for(int k = 0; k < size; ++k) {
{ if(inv.getStackInSlot(k) != null) {
ItemStack itemstack1 = isidedinventory.getStackInSlot(aint[k]); return false;
}
if (itemstack1 == null || !itemstack1.isFull()) }
{ }
return false;
} return true;
} }
}
else private static boolean putDropInInventoryAllSlots(IInventory p_145898_0_, EntityItem itemIn) {
{ boolean flag = false;
int i = inventoryIn.getSizeInventory();
if(itemIn == null) {
for (int j = 0; j < i; ++j) return false;
{ }
ItemStack itemstack = inventoryIn.getStackInSlot(j); else {
ItemStack itemstack = itemIn.getEntityItem().copy();
if (itemstack == null || !itemstack.isFull()) ItemStack itemstack1 = putStackInInventoryAllSlots(p_145898_0_, itemstack, (Facing)null);
{
return false; if(itemstack1 != null && !itemstack1.isEmpty()) {
} itemIn.setEntityItemStack(itemstack1);
} }
} else {
flag = true;
return true; itemIn.setDead();
} }
public static boolean isInventoryEmpty(IInventory inventoryIn, Facing side) return flag;
{ }
if (inventoryIn instanceof ISidedInventory) }
{
ISidedInventory isidedinventory = (ISidedInventory)inventoryIn; private static boolean canExtractItemFromSlot(IInventory inventoryIn, ItemStack stack, int index, Facing side) {
int[] aint = isidedinventory.getSlotsForFace(side); return !(inventoryIn instanceof ISidedInventory) || ((ISidedInventory)inventoryIn).canExtractItem(index, stack, side);
}
for (int i = 0; i < aint.length; ++i)
{ private boolean pullItemFromSlot(IInventory inventoryIn, int index, Facing direction) {
if (isidedinventory.getStackInSlot(aint[i]) != null) ItemStack itemstack = inventoryIn.getStackInSlot(index);
{
return false; if(itemstack != null && canExtractItemFromSlot(inventoryIn, itemstack, index, direction)) {
} ItemStack itemstack1 = itemstack.copy();
} ItemStack itemstack2 = putStackInInventoryAllSlots(this, inventoryIn.decrStackSize(index, 1), (Facing)null);
}
else if(itemstack2 == null || itemstack2.isEmpty()) {
{ inventoryIn.markDirty();
int j = inventoryIn.getSizeInventory(); return true;
}
for (int k = 0; k < j; ++k)
{ inventoryIn.setInventorySlotContents(index, itemstack1);
if (inventoryIn.getStackInSlot(k) != null) }
{
return false; return false;
} }
}
} private List<EntityItem> findDroppedItems(BlockPos pos) {
return this.world.<EntityItem>getEntitiesWithinAABB(EntityItem.class,
return true; new BoundingBox((double)pos.getX(), (double)pos.getY(), (double)pos.getZ(), (double)pos.getX() + 1.0, (double)pos.getY() + 1.0, (double)pos.getZ() + 1.0), new Predicate<EntityItem>() {
} public boolean test(EntityItem entity) {
return entity.isEntityAlive();
public static boolean captureDroppedItems(TileEntityPipe te, boolean down) }
{ });
IInventory iinventory = getPipeInventory(te, !down); }
if (iinventory != null) protected boolean transferIn(boolean suck) {
{ State state = this.getState();
Facing enumfacing = down ? Facing.UP : Facing.DOWN; Facing facing = state.getValue(BlockPipe.FACING);
for(Facing dir : Facing.values()) {
if (isInventoryEmpty(iinventory, enumfacing)) IInventory inv = dir == facing ? null : this.getInventory(this.pos.offset(dir), suck && dir == facing.getOpposite());
{ if(inv != null) {
return false; Facing opposite = dir.getOpposite();
}
if(isInventoryEmpty(inv, opposite)) {
if (iinventory instanceof ISidedInventory) continue;
{ }
ISidedInventory isidedinventory = (ISidedInventory)iinventory;
int[] aint = isidedinventory.getSlotsForFace(enumfacing); if(inv instanceof ISidedInventory sided) {
int[] slots = sided.getSlotsForFace(opposite);
for (int i = 0; i < aint.length; ++i)
{ for(int i = 0; i < slots.length; ++i) {
if (pullItemFromSlot(te, iinventory, aint[i], enumfacing)) if(this.pullItemFromSlot(inv, slots[i], opposite)) {
{ return true;
return true; }
} }
} }
} else {
else int size = inv.getSizeInventory();
{
int j = iinventory.getSizeInventory(); for(int k = 0; k < size; ++k) {
if(this.pullItemFromSlot(inv, k, opposite)) {
for (int k = 0; k < j; ++k) return true;
{ }
if (pullItemFromSlot(te, iinventory, k, enumfacing)) }
{ }
return true; }
} else if(suck && dir == facing.getOpposite()) {
} for(EntityItem entity : this.findDroppedItems(this.pos.offset(dir))) {
} if(putDropInInventoryAllSlots(this, entity)) {
} return true;
else if(down) }
{ }
for (EntityItem entityitem : findDroppedItems(te.getWorld(), te.getXPos(), te.getYPos() + 1.0D, te.getZPos())) }
{ }
if (putDropInInventoryAllSlots(te, entityitem))
{ return false;
return true; }
}
} // common
}
private static boolean canInsertItemInSlot(IInventory inventoryIn, ItemStack stack, int index, Facing side) {
return false; return !inventoryIn.isItemValidForSlot(index, stack) ? false : !(inventoryIn instanceof ISidedInventory) || ((ISidedInventory)inventoryIn).canInsertItem(index, stack, side);
} }
public static boolean pullItemFromSlot(TileEntityPipe pipe, IInventory inventoryIn, int index, Facing direction) private static boolean canCombine(ItemStack stack1, ItemStack stack2) {
{ return stack1.getItem() != stack2.getItem() ? false : (stack1.isOverLimit() ? false : ItemStack.dataEquals(stack1, stack2));
ItemStack itemstack = inventoryIn.getStackInSlot(index); }
if (itemstack != null && canExtractItemFromSlot(inventoryIn, itemstack, index, direction)) private static ItemStack insertStack(IInventory inventoryIn, ItemStack stack, int index, Facing side) {
{ ItemStack itemstack = inventoryIn.getStackInSlot(index);
ItemStack itemstack1 = itemstack.copy();
ItemStack itemstack2 = putStackInInventoryAllSlots(pipe, inventoryIn.decrStackSize(index, 1), (Facing)null); if(canInsertItemInSlot(inventoryIn, stack, index, side)) {
boolean flag = false;
if (itemstack2 == null || itemstack2.isEmpty())
{ if(itemstack == null) {
inventoryIn.markDirty(); inventoryIn.setInventorySlotContents(index, stack);
return true; stack = null;
} flag = true;
}
inventoryIn.setInventorySlotContents(index, itemstack1); else if(canCombine(itemstack, stack)) {
} int i = stack.getMaxStackSize() - itemstack.getSize();
int j = Math.min(stack.getSize(), i);
return false; stack.decrSize(j);
} itemstack.incrSize(j);
flag = j > 0;
public static boolean putDropInInventoryAllSlots(IInventory p_145898_0_, EntityItem itemIn) }
{
boolean flag = false; if(flag) {
if(inventoryIn instanceof TileEntityPipe) {
if (itemIn == null) TileEntityPipe pipe = (TileEntityPipe)inventoryIn;
{
return false; if(pipe.mayTransfer()) {
} pipe.setTransferCooldown(Vars.pipeDelay);
else }
{
ItemStack itemstack = itemIn.getEntityItem().copy(); inventoryIn.markDirty();
ItemStack itemstack1 = putStackInInventoryAllSlots(p_145898_0_, itemstack, (Facing)null); }
if (itemstack1 != null && !itemstack1.isEmpty()) inventoryIn.markDirty();
{ }
itemIn.setEntityItemStack(itemstack1); }
}
else return stack;
{ }
flag = true;
itemIn.setDead(); private static ItemStack putStackInInventoryAllSlots(IInventory inventoryIn, ItemStack stack, Facing side) {
} if(inventoryIn instanceof ISidedInventory && side != null) {
ISidedInventory isidedinventory = (ISidedInventory)inventoryIn;
return flag; int[] aint = isidedinventory.getSlotsForFace(side);
}
} for(int k = 0; k < aint.length && stack != null && !stack.isEmpty(); ++k) {
stack = insertStack(inventoryIn, stack, aint[k], side);
public static ItemStack putStackInInventoryAllSlots(IInventory inventoryIn, ItemStack stack, Facing side) }
{ }
if (inventoryIn instanceof ISidedInventory && side != null) else {
{ int i = inventoryIn.getSizeInventory();
ISidedInventory isidedinventory = (ISidedInventory)inventoryIn;
int[] aint = isidedinventory.getSlotsForFace(side); for(int j = 0; j < i && stack != null && !stack.isEmpty(); ++j) {
stack = insertStack(inventoryIn, stack, j, side);
for (int k = 0; k < aint.length && stack != null && !stack.isEmpty(); ++k) }
{ }
stack = insertStack(inventoryIn, stack, aint[k], side);
} if(stack != null && stack.isEmpty()) {
} stack = null;
else }
{
int i = inventoryIn.getSizeInventory(); return stack;
}
for (int j = 0; j < i && stack != null && !stack.isEmpty(); ++j)
{ private IInventory getEntityInventory(BlockPos pos) {
stack = insertStack(inventoryIn, stack, j, side); List<Entity> list = this.world.getEntitiesInAABBexcluding((Entity)null,
} new BoundingBox(this.getXPos() - 0.5D, this.getYPos() - 0.5D, this.getZPos() - 0.5D, this.getXPos() + 0.5D, this.getYPos() + 0.5D, this.getZPos() + 0.5D), new Predicate<Entity>() {
} public boolean test(Entity entity) {
return entity instanceof IInventory && !(entity instanceof EntityLiving) && entity.isEntityAlive();
if (stack != null && stack.isEmpty()) }
{ });
stack = null; return list.size() > 0 ? (IInventory)list.get(this.world.rand.zrange(list.size())) : null;
} }
return stack; private IInventory getInventory(BlockPos blockpos, boolean suck) {
} IInventory inv = null;
Block block = this.world.getState(blockpos).getBlock();
public static boolean canInsertItemInSlot(IInventory inventoryIn, ItemStack stack, int index, Facing side) if(block instanceof ITileEntityProvider) {
{ TileEntity te = this.world.getTileEntity(blockpos);
return !inventoryIn.isItemValidForSlot(index, stack) ? false : !(inventoryIn instanceof ISidedInventory) || ((ISidedInventory)inventoryIn).canInsertItem(index, stack, side);
} if(te instanceof IInventory) {
inv = (IInventory)te;
public static boolean canExtractItemFromSlot(IInventory inventoryIn, ItemStack stack, int index, Facing side)
{ if(inv instanceof TileEntityChest && block instanceof BlockChest) {
return !(inventoryIn instanceof ISidedInventory) || ((ISidedInventory)inventoryIn).canExtractItem(index, stack, side); inv = ((BlockChest)block).getChest(this.world, blockpos);
} }
}
public static ItemStack insertStack(IInventory inventoryIn, ItemStack stack, int index, Facing side) }
{ return inv == null && suck ? this.getEntityInventory(blockpos) : inv;
ItemStack itemstack = inventoryIn.getStackInSlot(index); }
if (canInsertItemInSlot(inventoryIn, stack, index, side))
{
boolean flag = false;
if (itemstack == null)
{
inventoryIn.setInventorySlotContents(index, stack);
stack = null;
flag = true;
}
else if (canCombine(itemstack, stack))
{
int i = stack.getMaxStackSize() - itemstack.getSize();
int j = Math.min(stack.getSize(), i);
stack.decrSize(j);
itemstack.incrSize(j);
flag = j > 0;
}
if (flag)
{
if (inventoryIn instanceof TileEntityPipe)
{
TileEntityPipe pipe = (TileEntityPipe)inventoryIn;
if (pipe.mayTransfer())
{
pipe.setTransferCooldown(Vars.pipeDelay);
}
inventoryIn.markDirty();
}
inventoryIn.markDirty();
}
}
return stack;
}
private IInventory getInventoryForTransfer()
{
State state = this.getState();
Facing enumfacing = state.getBlock() instanceof BlockPipe ? state.getValue(BlockPipe.FACING) : Facing.DOWN;
return getInventoryAtPosition(this.getWorld(), (double)(this.pos.getX() + enumfacing.getFrontOffsetX()), (double)(this.pos.getY() + enumfacing.getFrontOffsetY()), (double)(this.pos.getZ() + enumfacing.getFrontOffsetZ()));
}
public static IInventory getPipeInventory(TileEntityPipe pipe, boolean down)
{
return getInventoryAtPosition(pipe.getWorld(), pipe.getXPos(), pipe.getYPos() + (down ? -1.0 : 1.0), pipe.getZPos());
}
public static List<EntityItem> findDroppedItems(World world, double x, double y, double z)
{
return world.<EntityItem>getEntitiesWithinAABB(EntityItem.class, new BoundingBox(x - 0.5D, y - 0.5D, z - 0.5D, x + 0.5D, y + 0.5D, z + 0.5D), new Predicate<EntityItem>() {
public boolean test(EntityItem entity) {
return entity.isEntityAlive();
}
});
}
public static IInventory getInventoryAtPosition(World worldIn, double x, double y, double z)
{
IInventory iinventory = null;
int i = ExtMath.floord(x);
int j = ExtMath.floord(y);
int k = ExtMath.floord(z);
BlockPos blockpos = new BlockPos(i, j, k);
Block block = worldIn.getState(blockpos).getBlock();
if (block instanceof ITileEntityProvider)
{
TileEntity tileentity = worldIn.getTileEntity(blockpos);
if (tileentity instanceof IInventory)
{
iinventory = (IInventory)tileentity;
if (iinventory instanceof TileEntityChest && block instanceof BlockChest)
{
iinventory = ((BlockChest)block).getChest(worldIn, blockpos);
}
}
}
if (iinventory == null)
{
List<Entity> list = worldIn.getEntitiesInAABBexcluding((Entity)null, new BoundingBox(x - 0.5D, y - 0.5D, z - 0.5D, x + 0.5D, y + 0.5D, z + 0.5D), new Predicate<Entity>() {
public boolean test(Entity entity) {
return entity instanceof IInventory && entity.isEntityAlive();
}
});
if (list.size() > 0)
{
iinventory = (IInventory)list.get(worldIn.rand.zrange(list.size()));
}
}
return iinventory;
}
public static boolean canCombine(ItemStack stack1, ItemStack stack2)
{
return stack1.getItem() != stack2.getItem() ? false : (stack1.isOverLimit() ? false : ItemStack.dataEquals(stack1, stack2));
}
public void setTransferCooldown(int ticks)
{
this.cooldown = ticks;
}
public boolean isOnTransferCooldown()
{
return this.cooldown > 0;
}
public boolean mayTransfer()
{
return this.cooldown <= 1;
}
public int getColor() {
return 0x0040ff;
}
} }