Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Globally sync Water Wheel rotation #5032

Open
wants to merge 4 commits into
base: 1.18.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -48,12 +48,15 @@ public class WatermillBlockEntity extends IEBaseBlockEntity implements IEServerT
public boolean multiblock = false;
private boolean beingBroken = false;
public double perTick;
private double catchupSpeed;
private final CapabilityReference<IRotationAcceptor> outputCap = CapabilityReference.forNeighbor(
this, IRotationAcceptor.CAPABILITY, () -> getFacing().getOpposite()
);
private final CapabilityReference<IRotationAcceptor> reverseOutputCap = CapabilityReference.forNeighbor(
this, IRotationAcceptor.CAPABILITY, this::getFacing
);
private static final int globalSyncDelay = 10*20;
private int globalSyncDelayCounter = 1;

public WatermillBlockEntity(BlockEntityType<WatermillBlockEntity> type, BlockPos pos, BlockState state)
{
Expand All @@ -63,7 +66,7 @@ public WatermillBlockEntity(BlockEntityType<WatermillBlockEntity> type, BlockPos
@Override
public void tickClient()
{
rotation += perTick;
rotation += perTick+catchupSpeed;
rotation %= 1;
}

Expand Down Expand Up @@ -127,10 +130,42 @@ private void setPerTickAndAdvance(double newValue)
{
if(newValue!=perTick)
{
globalSyncDelayCounter = globalSyncDelay;
perTick = newValue;
markContainingBlockForUpdate(null);
}
rotation += perTick;
else if(globalSyncDelayCounter > 0)
globalSyncDelayCounter--;

if(globalSyncDelayCounter==0)
{
float newrot = (float)(level.getGameTime()*perTick);
if(Math.abs((newrot%.125)-(rotation%.125)) < perTick)
{
rotation = (float)(newrot-perTick);
catchupSpeed = 0;
globalSyncDelayCounter--;
markContainingBlockForUpdate(null);
}
else
{
double oldCatchupSpeed = catchupSpeed;
catchupSpeed = perTick/8 < 0.0002?perTick/8: 0.0002;
if(oldCatchupSpeed!=catchupSpeed)
markContainingBlockForUpdate(null);
}

// rotation += perTick+(perTick/2 < 0.002?perTick/2: 0.002);

// if(rotation!=newrot)
// {
// rotation = newrot;
// //update so changing perTick client side a second time AFTER globalSyncDelayCounter went to zero once does not jank from the markContainingBlockForUpdate above
// markContainingBlockForUpdate(null);
// }
}
// else
rotation += perTick+catchupSpeed;
rotation %= 1;
}

Expand Down Expand Up @@ -235,6 +270,7 @@ public void readCustomNBT(CompoundTag nbt, boolean descPacket)
offset = nbt.getIntArray("offset");
rotation = nbt.getFloat("rotation");
perTick = nbt.getDouble("perTick");
catchupSpeed = nbt.getDouble("catchupSpeed");

if(offset==null||offset.length < 2)
offset = new int[]{0, 0};
Expand All @@ -246,6 +282,7 @@ public void writeCustomNBT(CompoundTag nbt, boolean descPacket)
nbt.putIntArray("offset", offset);
nbt.putFloat("rotation", rotation);
nbt.putDouble("perTick", perTick);
nbt.putDouble("catchupSpeed", catchupSpeed);
}

private AABB renderAABB;
Expand Down