Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.48 KB | None | 0 0
  1. package com.zixiken.dimdoors.shared;
  2.  
  3. import com.zixiken.dimdoors.shared.util.Location;
  4. import com.zixiken.dimdoors.DimDoors;
  5. import com.zixiken.dimdoors.shared.util.NBTUtils;
  6. import com.zixiken.dimdoors.shared.world.DimDoorDimensions;
  7.  
  8. import java.util.HashMap;
  9. import java.util.Map;
  10.  
  11. import lombok.Getter;
  12. import net.minecraft.entity.player.EntityPlayerMP;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.nbt.NBTTagList;
  15. import net.minecraft.world.World;
  16.  
  17. /**
  18.  *
  19.  * @author Robijnvogel
  20.  */
  21. public class PocketRegistry {
  22.  
  23.     @Getter private static final int DATA_VERSION = 0; // IMPORTANT: Update this when making changes.
  24.     public static PocketRegistry instance;// TODO: 1 pocket registry per world
  25.  
  26.     @Getter private int gridSize; //determines how much pockets in their dimension are spaced
  27.     @Getter private int maxPocketSize;
  28.     @Getter private int privatePocketSize;
  29.     @Getter private int publicPocketSize;
  30.     private Map<String, Integer> privatePocketMap; // Player UUID -> Pocket ID
  31.     private Map<Integer, Pocket> pockets; // TODO: maybe just a list would be better?
  32.  
  33.     public World world = DimDoors.getDefWorld();
  34.     private int nextFreeID;
  35.  
  36.     /**
  37.      * Creates a new pocket registry based on config files.
  38.      *
  39.      * @return The newly-created pocket registry
  40.      */
  41.     public static PocketRegistry create() {
  42.         PocketRegistry registry = new PocketRegistry();
  43.  
  44.         registry.gridSize = DDConfig.getPocketGridSize();
  45.         registry.maxPocketSize = DDConfig.getMaxPocketSize();
  46.         registry.privatePocketSize = DDConfig.getPrivatePocketSize();
  47.         registry.publicPocketSize = DDConfig.getPublicPocketSize();
  48.  
  49.         registry.nextFreeID = 0;
  50.         registry.pockets = new HashMap<>();
  51.         registry.privatePocketMap = new HashMap<>();
  52.  
  53.         return registry;
  54.     }
  55.  
  56.     /**
  57.      * Loads a pocket registry from NBT.
  58.      *
  59.      * @param nbt The "pockets" NBT tag
  60.      * @return The read pocket registry
  61.      */
  62.     public static PocketRegistry readFromNBT(NBTTagCompound nbt) { // TODO: make non-static to follow convention? (if yes, make create non-static too)
  63.         PocketRegistry registry = new PocketRegistry();
  64.         Integer version = nbt.getInteger("version");
  65.         if (version == null || version != DATA_VERSION) {
  66.             if (upgradeRegistry(nbt, version == null ? -1 : version)) {
  67.                 PocketSavedData.get(registry.world).markDirty(); // Notify that this needs to be saved on world save.
  68.             } else {
  69.                 DimDoors.warn("Failed to upgrade the pocket registry, you'll have to recreate your world!");
  70.                 return null; // TODO: Switch to exception-based system and make it possible to cancel world join and notify the player to recreate world
  71.             }
  72.         }
  73.  
  74.         registry.gridSize = nbt.getInteger("gridSize");
  75.         registry.maxPocketSize = nbt.getInteger("maxPocketSize");
  76.         registry.privatePocketSize = nbt.getInteger("privatePocketSize");
  77.         registry.publicPocketSize = nbt.getInteger("publicPocketSize");
  78.         registry.privatePocketMap = NBTUtils.readMapStringInteger(nbt.getCompoundTag("privatePocketMap"));
  79.  
  80.         NBTTagList pocketsTagList = (NBTTagList) nbt.getTag("pockets");
  81.         registry.pockets = new HashMap<>();
  82.         for (int i = 0; i < pocketsTagList.tagCount(); i++) { //@todo this defeats the purpose of a Map over a List (pockets)
  83.             NBTTagCompound pocketTag = pocketsTagList.getCompoundTagAt(i);
  84.             registry.pockets.put(i, Pocket.readFromNBT(pocketTag));
  85.             registry.nextFreeID++;
  86.         }
  87.  
  88.         return registry;
  89.     }
  90.  
  91.     // TODO: Change to just something like loadOldVersion? That would be simpler to implement on version change (just copy-paste) but will accumulate more code
  92.     private static boolean upgradeRegistry(NBTTagCompound nbt, int oldVersion) { // Before implementing, make sure NBT can be modified from readFromNBT
  93.         if (oldVersion > DATA_VERSION) return false; // TODO: with exceptions, this will notify the player to update the mod
  94.         switch (oldVersion) {
  95.             case -1: // No version tag
  96.                 return false;
  97.             case 0:
  98.                 // Upgrade to 1 or return false
  99.             case 1:
  100.                 // Upgrade to 2 or return false
  101.             case 2:
  102.                 // Upgrade to 3 or return false
  103.             // ...
  104.         }
  105.         return true;
  106.     }
  107.  
  108.     /**
  109.      * Saves a pocket registry to NBT.
  110.      *
  111.      * @param nbt The NBT tag to save to.
  112.      */
  113.     public void writeToNBT(NBTTagCompound nbt) {
  114.         nbt.setInteger("version", DATA_VERSION);
  115.  
  116.         nbt.setInteger("gridSize", gridSize);
  117.         nbt.setInteger("maxPocketSize", maxPocketSize);
  118.         nbt.setInteger("privatePocketSize", privatePocketSize);
  119.         nbt.setInteger("publicPocketSize", publicPocketSize);
  120.         nbt.setTag("privatePocketMap", NBTUtils.writeMapStringInteger(privatePocketMap));
  121.  
  122.         NBTTagList pocketsTagList = new NBTTagList();
  123.         for (int i : pockets.keySet()) {
  124.             pocketsTagList.appendTag(Pocket.writeToNBT(pockets.get(i)));
  125.         }
  126.         nbt.setTag("pockets", pocketsTagList);
  127.     }
  128.  
  129.     /**
  130.      * Registers a new pocket.
  131.      *
  132.      * @param pocket The pocket to register
  133.      */
  134.     public void registerPocket(Pocket pocket) {
  135.         int id = nextFreeID++;
  136.         pockets.put(id, pocket);
  137.         pocket.setID(id);
  138.         PocketSavedData.get(world).markDirty(); // Notify that this needs to be saved on world save
  139.     }
  140.  
  141.     // TODO: function for removing a pocket
  142.  
  143.     public Pocket getPocket(int id) {
  144.         return pockets.get(id);
  145.     }
  146.  
  147.     public int getPrivatePocketID(String playerUUID) {
  148.         Integer id = privatePocketMap.get(playerUUID);
  149.         if (id == null) return -1;
  150.         return id;
  151.     }
  152.  
  153.     public void setPrivatePocketID(String playerUUID, int id) {
  154.         privatePocketMap.put(playerUUID, id);
  155.     }
  156.  
  157.     /**
  158.      * Calculates the Location of a pocket based on the ID.
  159.      *
  160.      * @param id The ID of the pocket
  161.      * @return The Location of the pocket
  162.      */
  163.     public Location getLocationFromID(int id) {
  164.         GridUtils.GridPos pos = GridUtils.numToPos(id);
  165.         return new Location(world, pos.getX() * gridSize * 16, 0, pos.getZ() * gridSize * 16);
  166.     }
  167.  
  168.     /**
  169.      * Calculates the ID of a pocket based on the Location.
  170.      *
  171.      * @param location The location of the pocket
  172.      * @return The ID of the pocket, or -1 if there is no pocket at that location
  173.      */
  174.     public int getIDFromLocation(Location location) {
  175.         if (location != null && !location.getWorld().equals(world)) throw new RuntimeException("Wrong world for this Registry!");
  176.         int x = location.getPos().getX();
  177.         int z = location.getPos().getZ();
  178.         int id = GridUtils.posToNum(new GridUtils.GridPos(x / (gridSize * 16), z / (gridSize * 16)));
  179.         return pockets.containsKey(id) ? id : -1;
  180.     }
  181.  
  182.     public boolean isPlayerAllowedToBeHere(EntityPlayerMP player, Location location) {
  183.         if(!DimDoorDimensions.isPocketDimensionID(location.getDimensionID())) return true;
  184.         int pocketID = getIDFromLocation(location);
  185.         if (pocketID == -1) { // outside of a pocket
  186.             return false;
  187.         } else {
  188.             Pocket pocket = pockets.get(pocketID);
  189.             return pocket.isPlayerAllowedInPocket(player) && pocket.isLocationWithinPocketBounds(location, gridSize);
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement