Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EntityMapHolder extends SavedData {
- private static final String DATA_NAME = "entityMap";
- private Map<Integer, BlockPos> blockEntityMap = new HashMap<>();
- public void registerBlockEntity(int id, BlockEntity blockEntity) {
- BlockPos pos = blockEntity.getBlockPos();
- blockEntityMap.put(id, pos);
- this.setDirty();
- this.save(new CompoundTag());
- //Ascendance.LOGGER.info("Registered! New map: " + blockEntityMap);
- }
- public void unregisterBlockEntity(int id) {
- blockEntityMap.remove(id);
- setDirty();
- }
- public Optional<BlockPos> getBlockEntityPos(int id) {
- BlockPos pos = blockEntityMap.get(id);
- return Optional.ofNullable(pos);
- }
- public EntityMapHolder(){
- }
- @Override
- public CompoundTag save(CompoundTag nbt) {
- ListTag mapList = new ListTag();
- for (Map.Entry<Integer, BlockPos> entry : blockEntityMap.entrySet()) {
- CompoundTag entryNbt = new CompoundTag();
- entryNbt.putInt("id", entry.getKey());
- entryNbt.putLong("pos", entry.getValue().asLong());
- mapList.add(entryNbt);
- }
- nbt.put(DATA_NAME, mapList);
- return nbt;
- }
- public EntityMapHolder(CompoundTag nbt) {
- ListTag mapList = nbt.getList(DATA_NAME, 10); // 10 is the NBT type for CompoundTag
- for (int i = 0; i < mapList.size(); i++) {
- CompoundTag entryNbt = mapList.getCompound(i);
- int id = entryNbt.getInt("id");
- long posLong = entryNbt.getLong("pos");
- BlockPos pos = BlockPos.of(posLong);
- blockEntityMap.put(id, pos);
- }
- }
- public static EntityMapHolder get(ServerLevel world) {
- return world.getDataStorage().computeIfAbsent(EntityMapHolder::new, EntityMapHolder::new, DATA_NAME);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement