Unicopia/src/main/java/com/minelittlepony/unicopia/client/ClientBlockDestructionManager.java

85 lines
2.7 KiB
Java
Raw Normal View History

2021-01-26 21:32:19 +01:00
package com.minelittlepony.unicopia.client;
import java.util.SortedSet;
import com.google.common.collect.Sets;
2023-04-30 11:46:33 +02:00
import com.minelittlepony.unicopia.server.world.BlockDestructionManager;
2022-09-16 17:54:46 +02:00
2021-01-26 21:32:19 +01:00
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minecraft.client.render.BlockBreakingInfo;
import net.minecraft.util.math.BlockPos;
public class ClientBlockDestructionManager {
private final Long2ObjectMap<Destruction> destructions = new Long2ObjectOpenHashMap<>();
private final Long2ObjectMap<SortedSet<BlockBreakingInfo>> combined = new Long2ObjectOpenHashMap<>();
private final Object locker = new Object();
public void setBlockDestruction(long pos, float amount) {
2021-01-26 21:32:19 +01:00
synchronized (locker) {
if (amount <= 0 || amount > BlockDestructionManager.MAX_DAMAGE) {
destructions.remove(pos);
2021-01-26 21:32:19 +01:00
} else {
destructions.computeIfAbsent(pos, p -> new Destruction(pos)).set(amount);
2021-01-26 21:32:19 +01:00
}
}
}
public void tick(Long2ObjectMap<SortedSet<BlockBreakingInfo>> vanilla) {
synchronized (locker) {
destructions.long2ObjectEntrySet().removeIf(entry -> entry.getValue().tick());
combined.clear();
if (!destructions.isEmpty()) {
destructions.forEach((pos, value) -> {
combined.computeIfAbsent(pos.longValue(), p -> Sets.newTreeSet()).add(value.info);
});
vanilla.forEach((pos, value) -> {
combined.computeIfAbsent(pos.longValue(), p -> Sets.newTreeSet()).addAll(value);
});
}
}
}
public Long2ObjectMap<SortedSet<BlockBreakingInfo>> getCombinedDestructions(Long2ObjectMap<SortedSet<BlockBreakingInfo>> vanilla) {
return destructions.isEmpty() ? vanilla : combined;
}
private class Destruction {
int age = 50;
BlockBreakingInfo info;
Destruction(long pos) {
this.info = new BlockBreakingInfo(0, BlockPos.fromLong(pos));
2021-01-26 21:32:19 +01:00
}
boolean tick() {
if (age-- > 0) {
return false;
}
int amount = info.getStage();
if (amount >= 0) {
amount--;
set(amount);
}
return amount < 0 || age-- <= 0;
}
void set(float amount) {
2021-01-26 21:32:19 +01:00
this.age = 50;
info.setStage(amount >= 0 && amount < BlockDestructionManager.MAX_DAMAGE ? (int)amount : BlockDestructionManager.UNSET_DAMAGE);
2021-01-26 21:32:19 +01:00
}
}
public interface Source {
ClientBlockDestructionManager getDestructionManager();
int getTicks();
}
}