Unicopia/src/main/java/com/minelittlepony/unicopia/BlockDestructionManager.java

228 lines
7.6 KiB
Java
Raw Normal View History

2021-01-26 21:32:19 +01:00
package com.minelittlepony.unicopia;
import java.util.Arrays;
import java.util.List;
2022-09-12 16:07:49 +02:00
import java.util.function.Supplier;
2022-09-12 16:07:49 +02:00
import com.google.common.base.Suppliers;
2021-01-26 21:32:19 +01:00
import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.network.MsgBlockDestruction;
2022-09-12 16:07:49 +02:00
import com.minelittlepony.unicopia.util.NbtSerialisable;
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.block.BlockState;
2022-09-12 16:07:49 +02:00
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
2021-01-26 21:32:19 +01:00
import net.minecraft.server.world.ServerWorld;
import net.minecraft.server.world.ThreadedAnvilChunkStorage;
2021-01-26 21:32:19 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
2022-09-12 16:07:49 +02:00
import net.minecraft.world.PersistentState;
2021-01-26 21:32:19 +01:00
import net.minecraft.world.World;
2022-09-12 16:07:49 +02:00
public class BlockDestructionManager extends PersistentState {
public static final int DESTRUCTION_COOLDOWN = 50;
2021-01-26 21:32:19 +01:00
public static final int UNSET_DAMAGE = -1;
public static final int MAX_DAMAGE = 10;
private final Destruction emptyDestruction = new Destruction();
2021-01-26 21:32:19 +01:00
private final World world;
2022-09-12 16:07:49 +02:00
private final Long2ObjectMap<Chunk> chunks = new Long2ObjectOpenHashMap<>();
2021-01-26 21:32:19 +01:00
private final Object locker = new Object();
2022-09-12 16:07:49 +02:00
public static Supplier<BlockDestructionManager> create(World world) {
if (world instanceof ServerWorld serverWorld) {
return Suppliers.memoize(() -> {
return serverWorld.getPersistentStateManager().getOrCreate(
compound -> new BlockDestructionManager(world, compound),
() -> new BlockDestructionManager(world),
"unicopia:destruction_manager"
);
});
}
return Suppliers.memoize(() -> new BlockDestructionManager(world));
}
BlockDestructionManager(World world) {
2021-01-26 21:32:19 +01:00
this.world = world;
}
2022-09-12 16:07:49 +02:00
BlockDestructionManager(World world, NbtCompound compound) {
this(world);
NbtCompound d = compound.getCompound("chunks");
d.getKeys().forEach(id -> {
chunks.computeIfAbsent(Long.valueOf(id), Chunk::new).fromNBT(d.getCompound(id));
});
}
@Override
public NbtCompound writeNbt(NbtCompound compound) {
NbtCompound destructions = new NbtCompound();
this.chunks.forEach((id, chunk) -> {
destructions.put(id.toString(), chunk.toNBT());
});
compound.put("chunks", destructions);
return compound;
}
2022-09-12 16:07:49 +02:00
public int getBlockDestruction(BlockPos pos) {
return getChunk(pos).getBlockDestruction(pos);
2021-01-26 21:32:19 +01:00
}
2022-09-12 16:07:49 +02:00
private Chunk getChunk(BlockPos pos) {
return chunks.computeIfAbsent(new ChunkPos(pos).toLong(), Chunk::new);
2021-01-26 21:32:19 +01:00
}
public void setBlockDestruction(BlockPos pos, int amount) {
synchronized (locker) {
2022-09-12 16:07:49 +02:00
getChunk(pos).setBlockDestruction(pos, amount);
markDirty();
2021-01-26 21:32:19 +01:00
}
}
public int damageBlock(BlockPos pos, int amount) {
if (amount == 0) {
return getBlockDestruction(pos);
}
2021-01-26 21:32:19 +01:00
amount = Math.max(getBlockDestruction(pos), 0) + amount;
setBlockDestruction(pos, amount);
return amount;
}
public void onBlockChanged(BlockPos pos, BlockState oldState, BlockState newstate) {
if (oldState.getBlock() != newstate.getBlock()) {
2022-09-12 16:07:49 +02:00
setBlockDestruction(pos, UNSET_DAMAGE);
}
}
2021-01-26 21:32:19 +01:00
public void tick() {
synchronized (locker) {
2022-09-12 16:07:49 +02:00
chunks.long2ObjectEntrySet().removeIf(entry -> entry.getValue().tick());
if (world instanceof ServerWorld) {
2022-09-12 16:07:49 +02:00
chunks.forEach((chunkPos, chunk) -> chunk.sendUpdates((ServerWorld)world));
}
}
}
2022-09-12 16:07:49 +02:00
private class Chunk implements NbtSerialisable {
private final Long2ObjectMap<Destruction> destructions = new Long2ObjectOpenHashMap<>();
private final long pos;
Chunk(long pos) {
this.pos = pos;
}
public int getBlockDestruction(BlockPos pos) {
return destructions.getOrDefault(pos.asLong(), emptyDestruction).amount;
}
public void setBlockDestruction(BlockPos pos, int amount) {
destructions.computeIfAbsent(pos.asLong(), p -> new Destruction()).set(amount);
}
boolean tick() {
destructions.long2ObjectEntrySet().removeIf(e -> e.getValue().tick());
return destructions.isEmpty();
}
void sendUpdates(ServerWorld world) {
if (!world.getChunkManager().isChunkLoaded(ChunkPos.getPackedX(pos), ChunkPos.getPackedZ(pos))) {
return;
}
ThreadedAnvilChunkStorage storage = world.getChunkManager().threadedAnvilChunkStorage;
2021-12-22 10:15:09 +01:00
List<ServerPlayerEntity> players = storage.getPlayersWatchingChunk(new ChunkPos(pos), false);
if (!players.isEmpty()) {
Long2ObjectOpenHashMap<Integer> values = new Long2ObjectOpenHashMap<>();
destructions.forEach((blockPos, item) -> {
if (item.dirty) {
item.dirty = false;
values.put(blockPos.longValue(), (Integer)item.amount);
}
});
MsgBlockDestruction msg = new MsgBlockDestruction(values);
if (msg.toBuffer().writerIndex() > 1048576) {
throw new IllegalStateException("Payload may not be larger than 1048576 bytes. Here's what we were trying to send: ["
+ values.size() + "]\n"
+ Arrays.toString(values.values().stream().mapToInt(Integer::intValue).toArray()));
}
players.forEach(player -> {
if (player instanceof ServerPlayerEntity) {
2021-12-22 10:15:09 +01:00
Channel.SERVER_BLOCK_DESTRUCTION.send(player, msg);
}
});
}
2021-01-26 21:32:19 +01:00
}
2022-09-12 16:07:49 +02:00
@Override
public void toNBT(NbtCompound compound) {
NbtCompound states = new NbtCompound();
destructions.forEach((id, state) -> {
states.put(id.toString(), state.toNBT());
});
compound.put("states", states);
}
@Override
public void fromNBT(NbtCompound compound) {
NbtCompound d = compound.getCompound("states");
chunks.clear();
d.getKeys().forEach(id -> {
destructions.computeIfAbsent(Long.valueOf(id), i -> new Destruction()).fromNBT(d.getCompound(id));
});
}
2021-01-26 21:32:19 +01:00
}
2022-09-12 16:07:49 +02:00
private class Destruction implements NbtSerialisable {
int amount = UNSET_DAMAGE;
int age = DESTRUCTION_COOLDOWN;
boolean dirty;
2021-01-26 21:32:19 +01:00
boolean tick() {
if (age-- > 0) {
return false;
}
if (amount >= 0) {
set(amount - 1);
}
return amount < 0 || age-- <= 0;
}
void set(int amount) {
2022-09-12 16:07:49 +02:00
this.age = DESTRUCTION_COOLDOWN;
2021-01-26 21:32:19 +01:00
this.amount = amount >= 0 && amount < MAX_DAMAGE ? amount : UNSET_DAMAGE;
this.dirty = true;
2021-01-26 21:32:19 +01:00
}
2022-09-12 16:07:49 +02:00
@Override
public void toNBT(NbtCompound compound) {
compound.putInt("destruction", amount);
compound.putInt("age", age);
}
@Override
public void fromNBT(NbtCompound compound) {
amount = compound.getInt("destruction");
age = compound.getInt("age");
dirty = true;
}
2021-01-26 21:32:19 +01:00
}
public interface Source {
BlockDestructionManager getDestructionManager();
}
}