mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-27 15:33:05 +00:00
refactor: port TileCache to Java
The first port free to change shape rather than preserve it: TileCache has zero downstream references in bytecode and does not appear in guidenh's reflective names. map() became a java.util.Map, add/remove became void, and apply returns a nullable FlaggedTile instead of a scala.Option. No internal caller used any of the discarded return values. Dropped TileCache$, TileCache$FlaggedTile$ and FlaggedTile's case-class machinery. It is only ever a map value, never compared, hashed, printed or destructured. findTile is unchanged, including the branch the reference obscures: its trailing 'case _ => null' is unreachable because Some(FlaggedTile(t, rem)) matches whatever the flag holds, so a tile flagged removed is returned like any other and only the warning is suppressed. That is pinned by a functional test written before the port. 114 plain-JVM tests and 13 Forge server tests pass; the only test change is the two-line adapter that stopped unwrapping a scala.Option.
This commit is contained in:
1 parent
b92d4b7af4
commit
31ec83f91b
6 files changed
+147
-54
No files matched your search
@@ -0,0 +1,82 @@
|
||||
package codechicken.multipart;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import codechicken.lib.vec.BlockCoord;
|
||||
import codechicken.multipart.handler.MultipartProxy;
|
||||
|
||||
/**
|
||||
* In order to maintain tight client/server synchronisation without bandwidth overhead, all data written must be read.
|
||||
* Sometimes the client tile is removed from the chunk before the packet arrives. This class maintains a reference to
|
||||
* all multipart tiles. The original design placed tiles that were invalidated into a map that was cleared upon arrival
|
||||
* of the update packet. Due to other factors that have eluded identification, references to all tiles held until they
|
||||
* are replaced or the world is reloaded.
|
||||
*/
|
||||
public final class TileCache {
|
||||
|
||||
private static final Map<BlockCoord, FlaggedTile> map = new HashMap<>();
|
||||
|
||||
private TileCache() {}
|
||||
|
||||
public static Map<BlockCoord, FlaggedTile> map() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void add(TileMultipart t) {
|
||||
map.put(new BlockCoord(t), new FlaggedTile(t, false));
|
||||
}
|
||||
|
||||
public static void remove(TileMultipart t) {
|
||||
map.put(new BlockCoord(t), new FlaggedTile(t, true));
|
||||
}
|
||||
|
||||
public static FlaggedTile apply(BlockCoord c) {
|
||||
return map.get(c);
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
public static TileMultipart findTile(World world, BlockCoord c) {
|
||||
TileMultipart inWorld = BlockMultipart.getTile(world, c.x, c.y, c.z);
|
||||
if (inWorld != null) {
|
||||
return inWorld;
|
||||
}
|
||||
|
||||
FlaggedTile entry = map.get(c);
|
||||
if (entry == null) {
|
||||
throw new RuntimeException("DC: Client multipart @" + c + " not found");
|
||||
}
|
||||
|
||||
// A tile flagged removed is still returned; the flag only says the warning is expected.
|
||||
if (!entry.removed()) {
|
||||
MultipartProxy.logger().warn(
|
||||
"Client multipart @" + c
|
||||
+ " vanished from world but was recovered. If possible causes can be identified, please report to the github issue tracker.");
|
||||
}
|
||||
return entry.t();
|
||||
}
|
||||
|
||||
public static final class FlaggedTile {
|
||||
|
||||
private final TileMultipart t;
|
||||
private final boolean removed;
|
||||
|
||||
public FlaggedTile(TileMultipart t, boolean removed) {
|
||||
this.t = t;
|
||||
this.removed = removed;
|
||||
}
|
||||
|
||||
public TileMultipart t() {
|
||||
return t;
|
||||
}
|
||||
|
||||
public boolean removed() {
|
||||
return removed;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package codechicken.multipart
|
||||
|
||||
import codechicken.lib.vec.BlockCoord
|
||||
import scala.collection.mutable.{Map => MMap}
|
||||
import net.minecraft.world.World
|
||||
import codechicken.multipart.handler.MultipartProxy
|
||||
|
||||
/** In order to maintain tight client/server synchronisation without bandwidth
|
||||
* overhead, all data written must be read. Sometimes the client tile is
|
||||
* removed from the chunk before the packet arrives. This class maintains a
|
||||
* reference to all multipart tiles. The original design placed tiles that were
|
||||
* invalidated into a map that was cleared upon arrival of the update packet.
|
||||
* Due to other factors that have eluded identification, references to all
|
||||
* tiles held until they are replaced or the world is reloaded.
|
||||
*/
|
||||
object TileCache {
|
||||
case class FlaggedTile(t: TileMultipart, removed: Boolean)
|
||||
|
||||
val map = MMap[BlockCoord, FlaggedTile]()
|
||||
|
||||
def add(t: TileMultipart) = map.put(new BlockCoord(t), FlaggedTile(t, false))
|
||||
def remove(t: TileMultipart) =
|
||||
map.put(new BlockCoord(t), FlaggedTile(t, true))
|
||||
def apply(c: BlockCoord) = map.get(c)
|
||||
def clear() = map.clear()
|
||||
|
||||
def findTile(world: World, c: BlockCoord) =
|
||||
BlockMultipart.getTile(world, c.x, c.y, c.z) match {
|
||||
case null =>
|
||||
apply(c) match {
|
||||
case Some(FlaggedTile(t, rem)) =>
|
||||
if (!rem)
|
||||
MultipartProxy.logger.warn(
|
||||
"Client multipart @" + c + " vanished from world but was recovered. If possible causes can be identified, please report to the github issue tracker."
|
||||
)
|
||||
t
|
||||
case None =>
|
||||
throw new RuntimeException(
|
||||
"DC: Client multipart @" + c + " not found"
|
||||
)
|
||||
case _ =>
|
||||
null
|
||||
}
|
||||
case t => t
|
||||
}
|
||||
}
|
||||
@@ -109,8 +109,7 @@ class TileCacheCharacterizationTest {
|
||||
// The two adapters below are the only places the cache's collection types are named.
|
||||
|
||||
private static TileCache.FlaggedTile entryAt(BlockCoord coord) {
|
||||
scala.Option<TileCache.FlaggedTile> entry = TileCache.apply(coord);
|
||||
return entry.isDefined() ? entry.get() : null;
|
||||
return TileCache.apply(coord);
|
||||
}
|
||||
|
||||
private static int cacheSize() {
|
||||
|
||||
Reference in new issue
Block a user