-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecbb220
commit 552910a
Showing
11 changed files
with
286 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...java/com/refinedmods/refinedstorage2/platform/common/storage/DiskStateChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.refinedmods.refinedstorage2.platform.common.storage; | ||
|
||
import com.refinedmods.refinedstorage2.api.storage.StateTrackedStorage; | ||
|
||
import com.google.common.util.concurrent.RateLimiter; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class DiskStateChangeListener implements StateTrackedStorage.Listener { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(DiskStateChangeListener.class); | ||
|
||
private final BlockEntity blockEntity; | ||
private final RateLimiter rateLimiter = RateLimiter.create(1); | ||
|
||
private boolean syncRequested; | ||
|
||
public DiskStateChangeListener(final BlockEntity blockEntity) { | ||
this.blockEntity = blockEntity; | ||
} | ||
|
||
@Override | ||
public void onStorageStateChanged() { | ||
syncRequested = true; | ||
} | ||
|
||
public void updateIfNecessary() { | ||
if (!syncRequested) { | ||
return; | ||
} | ||
if (!rateLimiter.tryAcquire()) { | ||
return; | ||
} | ||
LOGGER.debug("Disk state change for block at {}", blockEntity.getBlockPos()); | ||
syncRequested = false; | ||
immediateUpdate(); | ||
} | ||
|
||
public void immediateUpdate() { | ||
final Level level = blockEntity.getLevel(); | ||
if (level == null) { | ||
return; | ||
} | ||
level.sendBlockUpdated( | ||
blockEntity.getBlockPos(), | ||
blockEntity.getBlockState(), | ||
blockEntity.getBlockState(), | ||
Block.UPDATE_ALL | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.