-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: block listening system + entity fluid caching
- Loading branch information
Showing
22 changed files
with
638 additions
and
30 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
9 changes: 9 additions & 0 deletions
9
src/main/java/me/jellysquid/mods/lithium/common/block/BlockListeningSection.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,9 @@ | ||
package me.jellysquid.mods.lithium.common.block; | ||
|
||
import me.jellysquid.mods.lithium.common.entity.block_tracking.SectionedBlockChangeTracker; | ||
|
||
public interface BlockListeningSection { | ||
|
||
void addToCallback(ListeningBlockStatePredicate blockGroup, SectionedBlockChangeTracker tracker); | ||
void removeFromCallback(ListeningBlockStatePredicate blockGroup, SectionedBlockChangeTracker tracker); | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/me/jellysquid/mods/lithium/common/block/ListeningBlockStatePredicate.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,10 @@ | ||
package me.jellysquid.mods.lithium.common.block; | ||
|
||
public abstract class ListeningBlockStatePredicate extends TrackedBlockStatePredicate { | ||
public static int LISTENING_MASK; | ||
|
||
protected ListeningBlockStatePredicate(int index) { | ||
super(index); | ||
LISTENING_MASK |= (1 << this.getIndex()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...a/me/jellysquid/mods/lithium/common/entity/block_tracking/ChunkSectionChangeCallback.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,54 @@ | ||
package me.jellysquid.mods.lithium.common.entity.block_tracking; | ||
|
||
import me.jellysquid.mods.lithium.common.block.BlockListeningSection; | ||
import me.jellysquid.mods.lithium.common.block.BlockStateFlags; | ||
import me.jellysquid.mods.lithium.common.block.ListeningBlockStatePredicate; | ||
|
||
import java.util.ArrayList; | ||
|
||
public final class ChunkSectionChangeCallback { | ||
private final ArrayList<SectionedBlockChangeTracker>[] trackers; | ||
private short listeningMask; | ||
|
||
public ChunkSectionChangeCallback() { | ||
//noinspection unchecked | ||
this.trackers = new ArrayList[BlockStateFlags.NUM_LISTENING_FLAGS]; | ||
this.listeningMask = 0; | ||
} | ||
|
||
public short onBlockChange(int flagIndex, BlockListeningSection section) { | ||
ArrayList<SectionedBlockChangeTracker> sectionedBlockChangeTrackers = this.trackers[flagIndex]; | ||
this.trackers[flagIndex] = null; | ||
//noinspection ForLoopReplaceableByForEach | ||
for (int i = 0; i < sectionedBlockChangeTrackers.size(); i++) { | ||
sectionedBlockChangeTrackers.get(i).setChanged(section); | ||
} | ||
this.listeningMask &= ~(1 << flagIndex); | ||
|
||
return this.listeningMask; | ||
} | ||
|
||
public short addTracker(SectionedBlockChangeTracker tracker, ListeningBlockStatePredicate blockGroup) { | ||
int blockGroupIndex = blockGroup.getIndex(); | ||
ArrayList<SectionedBlockChangeTracker> sectionedBlockChangeTrackers = this.trackers[blockGroupIndex]; | ||
if (sectionedBlockChangeTrackers == null) { | ||
this.trackers[blockGroupIndex] = (sectionedBlockChangeTrackers = new ArrayList<>()); | ||
} | ||
sectionedBlockChangeTrackers.add(tracker); | ||
|
||
this.listeningMask |= (1 << blockGroupIndex); | ||
return this.listeningMask; | ||
} | ||
|
||
public short removeTracker(SectionedBlockChangeTracker tracker, ListeningBlockStatePredicate blockGroup) { | ||
int blockGroupIndex = blockGroup.getIndex(); | ||
ArrayList<SectionedBlockChangeTracker> sectionedBlockChangeTrackers = this.trackers[blockGroupIndex]; | ||
if (sectionedBlockChangeTrackers != null) { | ||
sectionedBlockChangeTrackers.remove(tracker); | ||
if (sectionedBlockChangeTrackers.isEmpty()) { | ||
this.listeningMask &= ~(1 << blockGroup.getIndex()); | ||
} | ||
} | ||
return this.listeningMask; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...main/java/me/jellysquid/mods/lithium/common/entity/block_tracking/FluidListeningInfo.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,67 @@ | ||
package me.jellysquid.mods.lithium.common.entity.block_tracking; | ||
|
||
import it.unimi.dsi.fastutil.objects.Reference2LongArrayMap; | ||
import net.minecraft.fluid.Fluid; | ||
import net.minecraft.registry.tag.TagKey; | ||
import net.minecraft.util.math.Box; | ||
import net.minecraft.world.World; | ||
|
||
public final class FluidListeningInfo { | ||
private static final int MIN_STATIONARY_COUNT = 16; | ||
private Box cachedPos; | ||
private SectionedFluidChangeTracker tracker; | ||
private Reference2LongArrayMap<TagKey<Fluid>> lastNotTouchedFluidTimes; | ||
private int stationary; //Stationary field can be left out, but is intended to avoid spamming the block tracking system with entities that are currently moving. | ||
|
||
public FluidListeningInfo() { | ||
this.tracker = null; | ||
this.lastNotTouchedFluidTimes = null; | ||
this.cachedPos = null; | ||
this.stationary = 0; | ||
} | ||
|
||
public void updateTracker(Box boundingBox, World world) { | ||
if (!boundingBox.equals(this.cachedPos)) { | ||
if (this.tracker != null) { | ||
if (!this.tracker.matchesMovedBox(boundingBox)) { | ||
this.tracker.unregister(); | ||
this.tracker = null; | ||
} | ||
} | ||
this.cachedPos = boundingBox; | ||
this.stationary = 0; | ||
if (this.lastNotTouchedFluidTimes != null) { | ||
this.lastNotTouchedFluidTimes.clear(); | ||
} | ||
return; | ||
} | ||
if (this.stationary >= MIN_STATIONARY_COUNT) { | ||
if (this.tracker == null && this.lastNotTouchedFluidTimes != null && !this.lastNotTouchedFluidTimes.isEmpty()) { | ||
this.tracker = SectionedFluidChangeTracker.registerAt(world, boundingBox); | ||
} | ||
} else { | ||
this.stationary++; | ||
} | ||
} | ||
|
||
public boolean cachedIsNotTouchingFluid(TagKey<Fluid> tag) { | ||
if (this.stationary >= MIN_STATIONARY_COUNT && this.tracker != null && this.lastNotTouchedFluidTimes != null) { | ||
long cachedTime = this.lastNotTouchedFluidTimes.getOrDefault(tag, Long.MIN_VALUE); | ||
return this.tracker.isUnchangedSince(cachedTime); | ||
} | ||
return false; | ||
} | ||
|
||
public void cacheNotTouchingFluid(TagKey<Fluid> tag, long time) { | ||
if (this.lastNotTouchedFluidTimes == null) { | ||
this.lastNotTouchedFluidTimes = new Reference2LongArrayMap<>(); | ||
} | ||
this.lastNotTouchedFluidTimes.put(tag, time); | ||
} | ||
|
||
public void remove() { | ||
if (this.tracker != null) { | ||
this.tracker.unregister(); | ||
} | ||
} | ||
} |
Oops, something went wrong.