Skip to content

Commit

Permalink
Sync goo crafting every few seconds, to avoid lag and tick accel offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Sep 11, 2024
1 parent d54fd02 commit df7acd3
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ public GooBlockBE_Base(BlockEntityType<?> type, BlockPos pos, BlockState state)
}
}

public void updateSideCounter(Direction direction, int newCounter) {
public boolean updateSideCounter(Direction direction, int newCounter) {
int oldCounter = sidedCounters.get(direction);
sidedCounters.put(direction, newCounter);
if (oldCounter >= 0 && newCounter == -1 && level.isClientSide) {
spawnParticles(direction);
}
int duration = sidedDurations.get(direction);
if (newCounter <= 0 || duration <= 0)
return false;
// Every 3 seconds or so
return (newCounter % (60 * counterReducer())) == 0;
}

public int counterReducer() {
Expand Down Expand Up @@ -91,13 +96,17 @@ public void spawnParticles(Direction side) {
}

public void tickCounters() {
boolean needsUpdate = false;
for (Direction direction : Direction.values()) {
int sideCounter = sidedCounters.get(direction);
if (sideCounter > 0) {
sideCounter = Math.max(sideCounter - counterReducer(), 0);
updateSideCounter(direction, sideCounter);
if (updateSideCounter(direction, sideCounter))
needsUpdate = true;
}
}
if (needsUpdate && !level.isClientSide)
markDirtyClient();
}

public void checkSides() {
Expand All @@ -107,8 +116,8 @@ public void checkSides() {
if (gooSpreadRecipe != null) {
if (sideCounter == -1 && this.getBlockState().getValue(ALIVE)) { //Valid Recipe and not running yet
sideCounter = gooSpreadRecipe.getCraftingDuration();
updateSideCounter(direction, sideCounter);
sidedDurations.put(direction, sideCounter);
updateSideCounter(direction, sideCounter);
markDirtyClient(); //Either way, update the client with the new sideCounters
} else if (sideCounter == 0) { //Craftings done!
setBlockToTarget(gooSpreadRecipe, direction);
Expand Down Expand Up @@ -234,6 +243,7 @@ public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt, H

public void markDirtyClient() {
this.setChanged();
System.out.println("Dirty!");
if (this.getLevel() != null) {
BlockState state = this.getLevel().getBlockState(this.getBlockPos());
this.getLevel().sendBlockUpdated(this.getBlockPos(), state, state, 3);
Expand Down

0 comments on commit df7acd3

Please sign in to comment.