pathetic/src/main/java/ch/deletescape/pathetic/LastSteppedOnStore.java

41 lines
1.1 KiB
Java

package ch.deletescape.pathetic;
import net.minecraft.util.math.BlockPos;
import java.util.HashMap;
import java.util.Map;
public class LastSteppedOnStore {
private static final Map<BlockPos, Long> LAST_STEPPED_ON = new HashMap<>();
// TODO: adjust depending on our needs
private static final long GC_TIME = 100;
public static long get(BlockPos pos) {
Long val = LAST_STEPPED_ON.get(pos);
if (val == null) {
return 0;
}
return val;
}
public static long putIfAbsent(BlockPos pos, long currentTime) {
Long val = LAST_STEPPED_ON.putIfAbsent(pos, currentTime);
if (val == null) {
gc(currentTime);
return 0;
}
return val;
}
// TODO: this is honestly an incredibly horrible idea lmao
private static void gc(long currentTime) {
if (LAST_STEPPED_ON.size() > 100) {
for (BlockPos key : LAST_STEPPED_ON.keySet()) {
if (currentTime - LAST_STEPPED_ON.get(key) >= GC_TIME) {
LAST_STEPPED_ON.remove(key);
}
}
}
}
}