Advertisement
Scouter456

Untitled

Jul 22nd, 2023
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. public class EntityMapHolder extends SavedData {
  2.     private static final String DATA_NAME = "entityMap";
  3.     private Map<Integer, BlockPos> blockEntityMap = new HashMap<>();
  4.  
  5.     public void registerBlockEntity(int id, BlockEntity blockEntity) {
  6.         BlockPos pos = blockEntity.getBlockPos();
  7.         blockEntityMap.put(id, pos);
  8.         this.setDirty();
  9.         this.save(new CompoundTag());
  10.         //Ascendance.LOGGER.info("Registered! New map: " + blockEntityMap);
  11.  
  12.     }
  13.  
  14.     public void unregisterBlockEntity(int id) {
  15.         blockEntityMap.remove(id);
  16.         setDirty();
  17.     }
  18.  
  19.     public Optional<BlockPos> getBlockEntityPos(int id) {
  20.         BlockPos pos = blockEntityMap.get(id);
  21.         return Optional.ofNullable(pos);
  22.     }
  23.  
  24.     public EntityMapHolder(){
  25.     }
  26.  
  27.     @Override
  28.     public CompoundTag save(CompoundTag nbt) {
  29.         ListTag mapList = new ListTag();
  30.         for (Map.Entry<Integer, BlockPos> entry : blockEntityMap.entrySet()) {
  31.             CompoundTag entryNbt = new CompoundTag();
  32.             entryNbt.putInt("id", entry.getKey());
  33.             entryNbt.putLong("pos", entry.getValue().asLong());
  34.             mapList.add(entryNbt);
  35.         }
  36.         nbt.put(DATA_NAME, mapList);
  37.         return nbt;
  38.     }
  39.  
  40.     public EntityMapHolder(CompoundTag nbt) {
  41.         ListTag mapList = nbt.getList(DATA_NAME, 10);  // 10 is the NBT type for CompoundTag
  42.         for (int i = 0; i < mapList.size(); i++) {
  43.             CompoundTag entryNbt = mapList.getCompound(i);
  44.             int id = entryNbt.getInt("id");
  45.             long posLong = entryNbt.getLong("pos");
  46.             BlockPos pos = BlockPos.of(posLong);
  47.             blockEntityMap.put(id, pos);
  48.         }
  49.     }
  50.  
  51.     public static EntityMapHolder get(ServerLevel world) {
  52.         return world.getDataStorage().computeIfAbsent(EntityMapHolder::new, EntityMapHolder::new, DATA_NAME);
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement