mirror of
https://github.com/HbmMods/Hbm-s-Nuclear-Tech-GIT.git
synced 2026-01-25 10:32:49 +00:00
screwdriver adjustable conveyor splitters
This commit is contained in:
parent
716e429772
commit
b4ae72513b
@ -1,14 +1,18 @@
|
|||||||
package com.hbm.blocks.network;
|
package com.hbm.blocks.network;
|
||||||
|
|
||||||
|
import api.hbm.block.IToolable;
|
||||||
import api.hbm.conveyor.IConveyorBelt;
|
import api.hbm.conveyor.IConveyorBelt;
|
||||||
import api.hbm.conveyor.IConveyorItem;
|
import api.hbm.conveyor.IConveyorItem;
|
||||||
import api.hbm.conveyor.IConveyorPackage;
|
import api.hbm.conveyor.IConveyorPackage;
|
||||||
import api.hbm.conveyor.IEnterableBlock;
|
import api.hbm.conveyor.IEnterableBlock;
|
||||||
import com.hbm.blocks.BlockDummyable;
|
import com.hbm.blocks.BlockDummyable;
|
||||||
|
import com.hbm.blocks.ILookOverlay;
|
||||||
import com.hbm.blocks.ITooltipProvider;
|
import com.hbm.blocks.ITooltipProvider;
|
||||||
import com.hbm.entity.item.EntityMovingItem;
|
import com.hbm.entity.item.EntityMovingItem;
|
||||||
import com.hbm.lib.RefStrings;
|
import com.hbm.lib.RefStrings;
|
||||||
import com.hbm.tileentity.network.TileEntityCraneSplitter;
|
import com.hbm.tileentity.network.TileEntityCraneSplitter;
|
||||||
|
import com.hbm.util.I18nUtil;
|
||||||
|
|
||||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||||
import cpw.mods.fml.relauncher.Side;
|
import cpw.mods.fml.relauncher.Side;
|
||||||
import cpw.mods.fml.relauncher.SideOnly;
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
@ -21,11 +25,13 @@ import net.minecraft.util.IIcon;
|
|||||||
import net.minecraft.util.MathHelper;
|
import net.minecraft.util.MathHelper;
|
||||||
import net.minecraft.util.Vec3;
|
import net.minecraft.util.Vec3;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
import net.minecraftforge.client.event.RenderGameOverlayEvent.Pre;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CraneSplitter extends BlockDummyable implements IConveyorBelt, IEnterableBlock, ITooltipProvider {
|
public class CraneSplitter extends BlockDummyable implements IConveyorBelt, IEnterableBlock, ITooltipProvider, IToolable, ILookOverlay {
|
||||||
|
|
||||||
@SideOnly(Side.CLIENT) public IIcon iconTopLeft;
|
@SideOnly(Side.CLIENT) public IIcon iconTopLeft;
|
||||||
@SideOnly(Side.CLIENT) public IIcon iconTopRight;
|
@SideOnly(Side.CLIENT) public IIcon iconTopRight;
|
||||||
@ -96,26 +102,16 @@ public class CraneSplitter extends BlockDummyable implements IConveyorBelt, IEnt
|
|||||||
TileEntity tile = world.getTileEntity(x, y, z);
|
TileEntity tile = world.getTileEntity(x, y, z);
|
||||||
if(!(tile instanceof TileEntityCraneSplitter)) return;
|
if(!(tile instanceof TileEntityCraneSplitter)) return;
|
||||||
TileEntityCraneSplitter splitter = (TileEntityCraneSplitter) tile;
|
TileEntityCraneSplitter splitter = (TileEntityCraneSplitter) tile;
|
||||||
boolean pos = splitter.getPosition();
|
|
||||||
ItemStack stack = entity.getItemStack();
|
|
||||||
ForgeDirection rot = ForgeDirection.getOrientation(splitter.getBlockMetadata() - offset).getRotation(ForgeDirection.DOWN);
|
ForgeDirection rot = ForgeDirection.getOrientation(splitter.getBlockMetadata() - offset).getRotation(ForgeDirection.DOWN);
|
||||||
|
|
||||||
if(stack.stackSize % 2 == 0) {
|
ItemStack[] splits = splitter.splitStack(entity.getItemStack());
|
||||||
stack.stackSize /= 2;
|
|
||||||
spawnMovingItem(world, x, y, z, stack.copy());
|
spawnMovingItem(world, x, y, z, splits[0]);
|
||||||
spawnMovingItem(world, x + rot.offsetX, y, z + rot.offsetZ, stack.copy());
|
spawnMovingItem(world, x + rot.offsetX, y, z + rot.offsetZ, splits[1]);
|
||||||
} else {
|
|
||||||
int baseSize = stack.stackSize /= 2;
|
|
||||||
stack.stackSize = baseSize + (pos ? 0 : 1);
|
|
||||||
spawnMovingItem(world, x, y, z, stack.copy());
|
|
||||||
stack.stackSize = baseSize + (pos ? 1 : 0);
|
|
||||||
spawnMovingItem(world, x + rot.offsetX, y, z + rot.offsetZ, stack.copy());
|
|
||||||
splitter.setPosition(!pos);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void spawnMovingItem(World world, int x, int y, int z, ItemStack stack) {
|
private void spawnMovingItem(World world, int x, int y, int z, ItemStack stack) {
|
||||||
if(stack.stackSize <= 0) return;
|
if(stack == null || stack.stackSize <= 0) return;
|
||||||
EntityMovingItem moving = new EntityMovingItem(world);
|
EntityMovingItem moving = new EntityMovingItem(world);
|
||||||
Vec3 pos = Vec3.createVectorHelper(x + 0.5, y + 0.5, z + 0.5);
|
Vec3 pos = Vec3.createVectorHelper(x + 0.5, y + 0.5, z + 0.5);
|
||||||
Vec3 snap = this.getClosestSnappingPosition(world, x, y, z, pos);
|
Vec3 snap = this.getClosestSnappingPosition(world, x, y, z, pos);
|
||||||
@ -158,8 +154,51 @@ public class CraneSplitter extends BlockDummyable implements IConveyorBelt, IEnt
|
|||||||
return ForgeDirection.getOrientation(meta).getRotation(ForgeDirection.UP);
|
return ForgeDirection.getOrientation(meta).getRotation(ForgeDirection.UP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
@Override
|
@Override
|
||||||
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean ext) {
|
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean ext) {
|
||||||
this.addStandardInfo(stack, player, list, ext);
|
this.addStandardInfo(stack, player, list, ext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onScrew(World world, EntityPlayer player, int x, int y, int z, int side, float fX, float fY, float fZ, ToolType tool) {
|
||||||
|
if(world.isRemote) return true;
|
||||||
|
if(tool != ToolType.SCREWDRIVER) return false;
|
||||||
|
|
||||||
|
int[] pos = this.findCore(world, x, y, z);
|
||||||
|
if(pos == null) return false;
|
||||||
|
|
||||||
|
TileEntity te = world.getTileEntity(pos[0], pos[1], pos[2]);
|
||||||
|
if(!(te instanceof TileEntityCraneSplitter)) return false;
|
||||||
|
|
||||||
|
TileEntityCraneSplitter crane = (TileEntityCraneSplitter) te;
|
||||||
|
|
||||||
|
// The core of the dummy is always the left hand block
|
||||||
|
boolean isLeft = x == pos[0] && y == pos[1] && z == pos[2];
|
||||||
|
int adjust = player.isSneaking() ? -1 : 1;
|
||||||
|
|
||||||
|
if(isLeft) {
|
||||||
|
crane.leftRatio = (byte)MathHelper.clamp_int(crane.leftRatio + adjust, 1, 16);
|
||||||
|
} else {
|
||||||
|
crane.rightRatio = (byte)MathHelper.clamp_int(crane.rightRatio + adjust, 1, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void printHook(Pre event, World world, int x, int y, int z) {
|
||||||
|
int[] pos = this.findCore(world, x, y, z);
|
||||||
|
if(pos == null) return;
|
||||||
|
|
||||||
|
TileEntity te = world.getTileEntity(pos[0], pos[1], pos[2]);
|
||||||
|
if(!(te instanceof TileEntityCraneSplitter)) return;
|
||||||
|
|
||||||
|
TileEntityCraneSplitter crane = (TileEntityCraneSplitter) te;
|
||||||
|
|
||||||
|
List<String> text = new ArrayList<>();
|
||||||
|
text.add("Splitter ratio: " + crane.leftRatio + ":" + crane.rightRatio);
|
||||||
|
|
||||||
|
ILookOverlay.printGeneric(event, I18nUtil.resolveKey(getUnlocalizedName() + ".name"), 0xffff00, 0x404000, text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,31 +1,87 @@
|
|||||||
package com.hbm.tileentity.network;
|
package com.hbm.tileentity.network;
|
||||||
|
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import com.hbm.tileentity.TileEntityLoadedBase;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
|
||||||
|
|
||||||
public class TileEntityCraneSplitter extends TileEntity {
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
|
|
||||||
|
public class TileEntityCraneSplitter extends TileEntityLoadedBase {
|
||||||
|
|
||||||
/* false: left belt is preferred, true: right belt is preferred */
|
/* false: left belt is preferred, true: right belt is preferred */
|
||||||
private boolean position;
|
private boolean position;
|
||||||
|
private byte remaining; // count until position swaps
|
||||||
public void setPosition(boolean pos) {
|
|
||||||
this.position = pos;
|
public byte leftRatio = 1;
|
||||||
this.worldObj.markTileEntityChunkModified(this.xCoord, this.yCoord, this.zCoord, this);
|
public byte rightRatio = 1;
|
||||||
|
|
||||||
|
// Splits the input stack into two, based on current ratio and internal state
|
||||||
|
public ItemStack[] splitStack(ItemStack stack) {
|
||||||
|
int left = 0;
|
||||||
|
int right = 0;
|
||||||
|
int count = stack.stackSize;
|
||||||
|
|
||||||
|
if(remaining <= 0) remaining = position ? rightRatio : leftRatio;
|
||||||
|
|
||||||
|
while(count > 0) {
|
||||||
|
int toExtract = Math.min(remaining, count);
|
||||||
|
|
||||||
|
remaining -= toExtract;
|
||||||
|
count -= toExtract;
|
||||||
|
if(position) right += toExtract; else left += toExtract;
|
||||||
|
|
||||||
|
if(remaining <= 0) {
|
||||||
|
position = !position;
|
||||||
|
remaining = position ? rightRatio : leftRatio;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack leftStack = stack.copy();
|
||||||
|
ItemStack rightStack = stack.copy();
|
||||||
|
leftStack.stackSize = left;
|
||||||
|
rightStack.stackSize = right;
|
||||||
|
|
||||||
|
worldObj.markTileEntityChunkModified(xCoord, yCoord, zCoord, this);
|
||||||
|
return new ItemStack[] { leftStack, rightStack };
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getPosition() {
|
public void updateEntity() {
|
||||||
return this.position;
|
if(worldObj.isRemote) return;
|
||||||
|
networkPackNT(15);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readFromNBT(NBTTagCompound nbt) {
|
public void readFromNBT(NBTTagCompound nbt) {
|
||||||
super.readFromNBT(nbt);
|
super.readFromNBT(nbt);
|
||||||
this.position = nbt.getBoolean("pos");
|
|
||||||
|
position = nbt.getBoolean("pos");
|
||||||
|
remaining = nbt.getByte("count");
|
||||||
|
|
||||||
|
leftRatio = nbt.getByte("left");
|
||||||
|
rightRatio = nbt.getByte("right");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeToNBT(NBTTagCompound nbt) {
|
public void writeToNBT(NBTTagCompound nbt) {
|
||||||
super.writeToNBT(nbt);
|
super.writeToNBT(nbt);
|
||||||
nbt.setBoolean("pos", this.position);
|
|
||||||
|
nbt.setBoolean("pos", position);
|
||||||
|
nbt.setByte("count", remaining);
|
||||||
|
|
||||||
|
nbt.setByte("left", leftRatio);
|
||||||
|
nbt.setByte("right", rightRatio);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(ByteBuf buf) {
|
||||||
|
buf.writeByte(leftRatio);
|
||||||
|
buf.writeByte(rightRatio);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deserialize(ByteBuf buf) {
|
||||||
|
leftRatio = buf.readByte();
|
||||||
|
rightRatio = buf.readByte();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user