2021-01-26 21:32:19 +01:00
|
|
|
package com.minelittlepony.unicopia;
|
|
|
|
|
2021-02-01 19:33:39 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2022-09-12 16:07:49 +02:00
|
|
|
import java.util.function.Supplier;
|
2021-02-01 19:33:39 +01:00
|
|
|
|
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;
|
2021-01-27 19:07:16 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
2022-09-12 16:07:49 +02:00
|
|
|
import net.minecraft.nbt.NbtCompound;
|
2021-06-26 12:40:35 +02:00
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
2021-01-26 21:32:19 +01:00
|
|
|
import net.minecraft.server.world.ServerWorld;
|
2021-02-01 19:33:39 +01:00
|
|
|
import net.minecraft.server.world.ThreadedAnvilChunkStorage;
|
2021-01-26 21:32:19 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-02-01 19:33:39 +01:00
|
|
|
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;
|
|
|
|
|
2021-01-27 17:03:35 +01:00
|
|
|
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;
|
2021-02-01 19:33:39 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-01-27 16:16:15 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:07:16 +01:00
|
|
|
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-27 19:07:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
2021-01-27 17:03:35 +01:00
|
|
|
|
|
|
|
if (world instanceof ServerWorld) {
|
2022-09-12 16:07:49 +02:00
|
|
|
chunks.forEach((chunkPos, chunk) -> chunk.sendUpdates((ServerWorld)world));
|
2021-02-01 19:33:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:07:49 +02:00
|
|
|
private class Chunk implements NbtSerialisable {
|
2021-02-01 19:33:39 +01:00
|
|
|
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);
|
2021-02-01 19:33:39 +01:00
|
|
|
|
|
|
|
if (!players.isEmpty()) {
|
|
|
|
Long2ObjectOpenHashMap<Integer> values = new Long2ObjectOpenHashMap<>();
|
|
|
|
|
|
|
|
destructions.forEach((blockPos, item) -> {
|
2021-01-27 17:03:35 +01:00
|
|
|
if (item.dirty) {
|
2021-02-01 19:33:39 +01:00
|
|
|
item.dirty = false;
|
|
|
|
values.put(blockPos.longValue(), (Integer)item.amount);
|
2021-01-27 17:03:35 +01:00
|
|
|
}
|
|
|
|
});
|
2021-02-01 19:33:39 +01:00
|
|
|
|
|
|
|
MsgBlockDestruction msg = new MsgBlockDestruction(values);
|
|
|
|
|
2021-06-26 12:40:35 +02:00
|
|
|
if (msg.toBuffer().writerIndex() > 1048576) {
|
2021-02-01 19:33:39 +01:00
|
|
|
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()));
|
2021-01-27 17:03:35 +01:00
|
|
|
}
|
2021-02-01 19:33:39 +01:00
|
|
|
|
2021-06-26 12:40:35 +02:00
|
|
|
players.forEach(player -> {
|
|
|
|
if (player instanceof ServerPlayerEntity) {
|
2021-12-22 10:15:09 +01:00
|
|
|
Channel.SERVER_BLOCK_DESTRUCTION.send(player, msg);
|
2021-06-26 12:40:35 +02:00
|
|
|
}
|
|
|
|
});
|
2021-01-27 17:03:35 +01:00
|
|
|
}
|
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;
|
2021-01-27 17:03:35 +01:00
|
|
|
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;
|
2021-01-27 17:03:35 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|