mirror of
https://github.com/Ellpeck/PrettyPipes.git
synced 2024-11-22 19:58:35 +01:00
made pipe items speed based only
This commit is contained in:
parent
9ea6f7353f
commit
fffcb9da6d
2 changed files with 49 additions and 47 deletions
|
@ -27,8 +27,6 @@ import java.util.function.Consumer;
|
||||||
|
|
||||||
public class PipeItem implements INBTSerializable<CompoundNBT> {
|
public class PipeItem implements INBTSerializable<CompoundNBT> {
|
||||||
|
|
||||||
public static final int PIPE_TIME = 20;
|
|
||||||
|
|
||||||
public ItemStack stack;
|
public ItemStack stack;
|
||||||
public float x;
|
public float x;
|
||||||
public float y;
|
public float y;
|
||||||
|
@ -42,17 +40,13 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
|
||||||
private BlockPos startInventory;
|
private BlockPos startInventory;
|
||||||
private BlockPos destPipe;
|
private BlockPos destPipe;
|
||||||
private BlockPos destInventory;
|
private BlockPos destInventory;
|
||||||
private int pipeTimer;
|
private BlockPos currGoalPos;
|
||||||
private int currentTile;
|
private int currentTile;
|
||||||
private boolean dropOnObstruction;
|
private boolean dropOnObstruction;
|
||||||
private long lastWorldTick;
|
private long lastWorldTick;
|
||||||
|
|
||||||
public PipeItem(ItemStack stack, BlockPos startPipe, BlockPos startInventory) {
|
public PipeItem(ItemStack stack) {
|
||||||
this.stack = stack;
|
this.stack = stack;
|
||||||
this.startInventory = startInventory;
|
|
||||||
this.x = MathHelper.lerp(0.5F, startInventory.getX(), startPipe.getX()) + 0.5F;
|
|
||||||
this.y = MathHelper.lerp(0.5F, startInventory.getY(), startPipe.getY()) + 0.5F;
|
|
||||||
this.z = MathHelper.lerp(0.5F, startInventory.getZ(), startPipe.getZ()) + 0.5F;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public PipeItem(CompoundNBT nbt) {
|
public PipeItem(CompoundNBT nbt) {
|
||||||
|
@ -60,13 +54,21 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
|
||||||
this.deserializeNBT(nbt);
|
this.deserializeNBT(nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDestination(BlockPos startPipe, BlockPos destPipe, BlockPos destInventory, GraphPath<BlockPos, NetworkEdge> path) {
|
public void setDestination(BlockPos startPipe, BlockPos startInventory, BlockPos destPipe, BlockPos destInventory, GraphPath<BlockPos, NetworkEdge> path) {
|
||||||
this.startPipe = startPipe;
|
this.startPipe = startPipe;
|
||||||
|
this.startInventory = startInventory;
|
||||||
this.destPipe = destPipe;
|
this.destPipe = destPipe;
|
||||||
this.destInventory = destInventory;
|
this.destInventory = destInventory;
|
||||||
|
this.currGoalPos = startPipe;
|
||||||
this.path = compilePath(path);
|
this.path = compilePath(path);
|
||||||
this.currentTile = 0;
|
this.currentTile = 0;
|
||||||
this.pipeTimer = 0;
|
|
||||||
|
// initialize position if new
|
||||||
|
if (this.x == 0 && this.y == 0 && this.z == 0) {
|
||||||
|
this.x = MathHelper.lerp(0.5F, startInventory.getX(), startPipe.getX()) + 0.5F;
|
||||||
|
this.y = MathHelper.lerp(0.5F, startInventory.getY(), startPipe.getY()) + 0.5F;
|
||||||
|
this.z = MathHelper.lerp(0.5F, startInventory.getZ(), startPipe.getZ()) + 0.5F;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateInPipe(PipeTileEntity currPipe) {
|
public void updateInPipe(PipeTileEntity currPipe) {
|
||||||
|
@ -77,9 +79,9 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
|
||||||
return;
|
return;
|
||||||
this.lastWorldTick = worldTick;
|
this.lastWorldTick = worldTick;
|
||||||
|
|
||||||
this.pipeTimer++;
|
float speed = 0.05F;
|
||||||
BlockPos goalPos;
|
BlockPos myPos = new BlockPos(this.x, this.y, this.z);
|
||||||
if (this.pipeTimer >= PIPE_TIME) {
|
if (!myPos.equals(currPipe.getPos()) && !myPos.equals(this.startInventory)) {
|
||||||
// we're done with the current pipe, so switch to the next one
|
// we're done with the current pipe, so switch to the next one
|
||||||
currPipe.items.remove(this);
|
currPipe.items.remove(this);
|
||||||
PipeTileEntity next = this.getNextTile(currPipe, true);
|
PipeTileEntity next = this.getNextTile(currPipe, true);
|
||||||
|
@ -97,15 +99,15 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
next.items.add(this);
|
next.items.add(this);
|
||||||
this.pipeTimer = 0;
|
|
||||||
goalPos = next.getPos();
|
|
||||||
}
|
}
|
||||||
} else if (this.pipeTimer > PIPE_TIME / 2) {
|
} else {
|
||||||
|
double dist = new Vec3d(this.currGoalPos).squareDistanceTo(this.x - 0.5F, this.y - 0.5F, this.z - 0.5F);
|
||||||
|
if (dist < speed * speed) {
|
||||||
// we're past the start of the pipe, so move to the center of the next pipe
|
// we're past the start of the pipe, so move to the center of the next pipe
|
||||||
PipeTileEntity next = this.getNextTile(currPipe, false);
|
PipeTileEntity next = this.getNextTile(currPipe, false);
|
||||||
if (next == null) {
|
if (next == null) {
|
||||||
if (this.reachedDestination()) {
|
if (this.reachedDestination()) {
|
||||||
goalPos = this.destInventory;
|
this.currGoalPos = this.destInventory;
|
||||||
} else {
|
} else {
|
||||||
currPipe.items.remove(this);
|
currPipe.items.remove(this);
|
||||||
if (!currPipe.getWorld().isRemote)
|
if (!currPipe.getWorld().isRemote)
|
||||||
|
@ -113,28 +115,26 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
goalPos = next.getPos();
|
this.currGoalPos = next.getPos();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// we're at the start of the pipe, so just move towards its center
|
|
||||||
goalPos = currPipe.getPos();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.lastX = this.x;
|
this.lastX = this.x;
|
||||||
this.lastY = this.y;
|
this.lastY = this.y;
|
||||||
this.lastZ = this.z;
|
this.lastZ = this.z;
|
||||||
|
|
||||||
Vec3d dist = new Vec3d(goalPos.getX() + 0.5F - this.x, goalPos.getY() + 0.5F - this.y, goalPos.getZ() + 0.5F - this.z);
|
Vec3d dist = new Vec3d(this.currGoalPos.getX() + 0.5F - this.x, this.currGoalPos.getY() + 0.5F - this.y, this.currGoalPos.getZ() + 0.5F - this.z);
|
||||||
dist = dist.normalize();
|
dist = dist.normalize();
|
||||||
this.x += dist.x / PIPE_TIME;
|
this.x += dist.x * speed;
|
||||||
this.y += dist.y / PIPE_TIME;
|
this.y += dist.y * speed;
|
||||||
this.z += dist.z / PIPE_TIME;
|
this.z += dist.z * speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPathObstructed(PipeTileEntity currPipe, boolean tryReturn) {
|
private void onPathObstructed(PipeTileEntity currPipe, boolean tryReturn) {
|
||||||
if (!this.dropOnObstruction && tryReturn) {
|
if (!this.dropOnObstruction && tryReturn) {
|
||||||
PipeNetwork network = PipeNetwork.get(currPipe.getWorld());
|
PipeNetwork network = PipeNetwork.get(currPipe.getWorld());
|
||||||
if (network.routeItemToLocation(currPipe.getPos(), this.startPipe, this.startInventory, () -> this)) {
|
if (network.routeItemToLocation(currPipe.getPos(), this.destInventory, this.startPipe, this.startInventory, () -> this)) {
|
||||||
this.dropOnObstruction = true;
|
this.dropOnObstruction = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -177,11 +177,11 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
|
||||||
CompoundNBT nbt = new CompoundNBT();
|
CompoundNBT nbt = new CompoundNBT();
|
||||||
nbt.put("stack", this.stack.serializeNBT());
|
nbt.put("stack", this.stack.serializeNBT());
|
||||||
nbt.put("start_pipe", NBTUtil.writeBlockPos(this.startPipe));
|
nbt.put("start_pipe", NBTUtil.writeBlockPos(this.startPipe));
|
||||||
nbt.put("start_inventory", NBTUtil.writeBlockPos(this.startInventory));
|
nbt.put("start_inv", NBTUtil.writeBlockPos(this.startInventory));
|
||||||
nbt.put("dest_pipe", NBTUtil.writeBlockPos(this.destPipe));
|
nbt.put("dest_pipe", NBTUtil.writeBlockPos(this.destPipe));
|
||||||
nbt.put("dest_inv", NBTUtil.writeBlockPos(this.destInventory));
|
nbt.put("dest_inv", NBTUtil.writeBlockPos(this.destInventory));
|
||||||
|
nbt.put("curr_goal", NBTUtil.writeBlockPos(this.currGoalPos));
|
||||||
nbt.putBoolean("drop_on_obstruction", this.dropOnObstruction);
|
nbt.putBoolean("drop_on_obstruction", this.dropOnObstruction);
|
||||||
nbt.putInt("timer", this.pipeTimer);
|
|
||||||
nbt.putInt("tile", this.currentTile);
|
nbt.putInt("tile", this.currentTile);
|
||||||
nbt.putFloat("x", this.x);
|
nbt.putFloat("x", this.x);
|
||||||
nbt.putFloat("y", this.y);
|
nbt.putFloat("y", this.y);
|
||||||
|
@ -197,11 +197,11 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
|
||||||
public void deserializeNBT(CompoundNBT nbt) {
|
public void deserializeNBT(CompoundNBT nbt) {
|
||||||
this.stack = ItemStack.read(nbt.getCompound("stack"));
|
this.stack = ItemStack.read(nbt.getCompound("stack"));
|
||||||
this.startPipe = NBTUtil.readBlockPos(nbt.getCompound("start_pipe"));
|
this.startPipe = NBTUtil.readBlockPos(nbt.getCompound("start_pipe"));
|
||||||
this.startInventory = NBTUtil.readBlockPos(nbt.getCompound("start_inventory"));
|
this.startInventory = NBTUtil.readBlockPos(nbt.getCompound("start_inv"));
|
||||||
this.destPipe = NBTUtil.readBlockPos(nbt.getCompound("dest_pipe"));
|
this.destPipe = NBTUtil.readBlockPos(nbt.getCompound("dest_pipe"));
|
||||||
this.destInventory = NBTUtil.readBlockPos(nbt.getCompound("dest_inv"));
|
this.destInventory = NBTUtil.readBlockPos(nbt.getCompound("dest_inv"));
|
||||||
|
this.currGoalPos = NBTUtil.readBlockPos(nbt.getCompound("curr_goal"));
|
||||||
this.dropOnObstruction = nbt.getBoolean("drop_on_obstruction");
|
this.dropOnObstruction = nbt.getBoolean("drop_on_obstruction");
|
||||||
this.pipeTimer = nbt.getInt("timer");
|
|
||||||
this.currentTile = nbt.getInt("tile");
|
this.currentTile = nbt.getInt("tile");
|
||||||
this.x = nbt.getFloat("x");
|
this.x = nbt.getFloat("x");
|
||||||
this.y = nbt.getFloat("y");
|
this.y = nbt.getFloat("y");
|
||||||
|
|
|
@ -111,11 +111,11 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
|
||||||
this.refreshNode(edge.endPipe, this.world.getBlockState(edge.endPipe));
|
this.refreshNode(edge.endPipe, this.world.getBlockState(edge.endPipe));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean tryInsertItem(BlockPos startPipePos, BlockPos originInv, ItemStack stack) {
|
public boolean tryInsertItem(BlockPos startPipePos, BlockPos startInventory, ItemStack stack) {
|
||||||
return this.routeItem(startPipePos, stack, () -> new PipeItem(stack, startPipePos, originInv));
|
return this.routeItem(startPipePos, startInventory, stack, () -> new PipeItem(stack));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean routeItem(BlockPos startPipePos, ItemStack stack, Supplier<PipeItem> itemSupplier) {
|
public boolean routeItem(BlockPos startPipePos, BlockPos startInventory, ItemStack stack, Supplier<PipeItem> itemSupplier) {
|
||||||
if (!this.isNode(startPipePos))
|
if (!this.isNode(startPipePos))
|
||||||
return false;
|
return false;
|
||||||
if (!this.world.isBlockLoaded(startPipePos))
|
if (!this.world.isBlockLoaded(startPipePos))
|
||||||
|
@ -127,12 +127,12 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
|
||||||
PipeTileEntity pipe = this.getPipe(pipePos);
|
PipeTileEntity pipe = this.getPipe(pipePos);
|
||||||
BlockPos dest = pipe.getAvailableDestination(stack);
|
BlockPos dest = pipe.getAvailableDestination(stack);
|
||||||
if (dest != null)
|
if (dest != null)
|
||||||
return this.routeItemToLocation(startPipePos, pipe.getPos(), dest, itemSupplier);
|
return this.routeItemToLocation(startPipePos, startInventory, pipe.getPos(), dest, itemSupplier);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean routeItemToLocation(BlockPos startPipePos, BlockPos destPipe, BlockPos destInventory, Supplier<PipeItem> itemSupplier) {
|
public boolean routeItemToLocation(BlockPos startPipePos, BlockPos startInventory, BlockPos destPipe, BlockPos destInventory, Supplier<PipeItem> itemSupplier) {
|
||||||
if (!this.isNode(startPipePos))
|
if (!this.isNode(startPipePos))
|
||||||
return false;
|
return false;
|
||||||
if (!this.world.isBlockLoaded(startPipePos))
|
if (!this.world.isBlockLoaded(startPipePos))
|
||||||
|
@ -141,8 +141,10 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
|
||||||
if (startPipe == null)
|
if (startPipe == null)
|
||||||
return false;
|
return false;
|
||||||
GraphPath<BlockPos, NetworkEdge> path = this.dijkstra.getPath(startPipePos, destPipe);
|
GraphPath<BlockPos, NetworkEdge> path = this.dijkstra.getPath(startPipePos, destPipe);
|
||||||
|
if (path == null)
|
||||||
|
return false;
|
||||||
PipeItem item = itemSupplier.get();
|
PipeItem item = itemSupplier.get();
|
||||||
item.setDestination(startPipePos, destPipe, destInventory, path);
|
item.setDestination(startPipePos, startInventory, destPipe, destInventory, path);
|
||||||
if (!startPipe.items.contains(item))
|
if (!startPipe.items.contains(item))
|
||||||
startPipe.items.add(item);
|
startPipe.items.add(item);
|
||||||
PacketHandler.sendToAllLoaded(this.world, startPipePos, new PacketItemEnterPipe(startPipePos, item));
|
PacketHandler.sendToAllLoaded(this.world, startPipePos, new PacketItemEnterPipe(startPipePos, item));
|
||||||
|
|
Loading…
Reference in a new issue