Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zixiken.dimdoors.shared;
- import com.zixiken.dimdoors.shared.util.Location;
- import com.zixiken.dimdoors.DimDoors;
- import com.zixiken.dimdoors.shared.util.NBTUtils;
- import com.zixiken.dimdoors.shared.world.DimDoorDimensions;
- import java.util.HashMap;
- import java.util.Map;
- import lombok.Getter;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.world.World;
- /**
- *
- * @author Robijnvogel
- */
- public class PocketRegistry {
- @Getter private static final int DATA_VERSION = 0; // IMPORTANT: Update this when making changes.
- public static PocketRegistry instance;// TODO: 1 pocket registry per world
- @Getter private int gridSize; //determines how much pockets in their dimension are spaced
- @Getter private int maxPocketSize;
- @Getter private int privatePocketSize;
- @Getter private int publicPocketSize;
- private Map<String, Integer> privatePocketMap; // Player UUID -> Pocket ID
- private Map<Integer, Pocket> pockets; // TODO: maybe just a list would be better?
- public World world = DimDoors.getDefWorld();
- private int nextFreeID;
- /**
- * Creates a new pocket registry based on config files.
- *
- * @return The newly-created pocket registry
- */
- public static PocketRegistry create() {
- PocketRegistry registry = new PocketRegistry();
- registry.gridSize = DDConfig.getPocketGridSize();
- registry.maxPocketSize = DDConfig.getMaxPocketSize();
- registry.privatePocketSize = DDConfig.getPrivatePocketSize();
- registry.publicPocketSize = DDConfig.getPublicPocketSize();
- registry.nextFreeID = 0;
- registry.pockets = new HashMap<>();
- registry.privatePocketMap = new HashMap<>();
- return registry;
- }
- /**
- * Loads a pocket registry from NBT.
- *
- * @param nbt The "pockets" NBT tag
- * @return The read pocket registry
- */
- public static PocketRegistry readFromNBT(NBTTagCompound nbt) { // TODO: make non-static to follow convention? (if yes, make create non-static too)
- PocketRegistry registry = new PocketRegistry();
- Integer version = nbt.getInteger("version");
- if (version == null || version != DATA_VERSION) {
- if (upgradeRegistry(nbt, version == null ? -1 : version)) {
- PocketSavedData.get(registry.world).markDirty(); // Notify that this needs to be saved on world save.
- } else {
- DimDoors.warn("Failed to upgrade the pocket registry, you'll have to recreate your world!");
- return null; // TODO: Switch to exception-based system and make it possible to cancel world join and notify the player to recreate world
- }
- }
- registry.gridSize = nbt.getInteger("gridSize");
- registry.maxPocketSize = nbt.getInteger("maxPocketSize");
- registry.privatePocketSize = nbt.getInteger("privatePocketSize");
- registry.publicPocketSize = nbt.getInteger("publicPocketSize");
- registry.privatePocketMap = NBTUtils.readMapStringInteger(nbt.getCompoundTag("privatePocketMap"));
- NBTTagList pocketsTagList = (NBTTagList) nbt.getTag("pockets");
- registry.pockets = new HashMap<>();
- for (int i = 0; i < pocketsTagList.tagCount(); i++) { //@todo this defeats the purpose of a Map over a List (pockets)
- NBTTagCompound pocketTag = pocketsTagList.getCompoundTagAt(i);
- registry.pockets.put(i, Pocket.readFromNBT(pocketTag));
- registry.nextFreeID++;
- }
- return registry;
- }
- // TODO: Change to just something like loadOldVersion? That would be simpler to implement on version change (just copy-paste) but will accumulate more code
- private static boolean upgradeRegistry(NBTTagCompound nbt, int oldVersion) { // Before implementing, make sure NBT can be modified from readFromNBT
- if (oldVersion > DATA_VERSION) return false; // TODO: with exceptions, this will notify the player to update the mod
- switch (oldVersion) {
- case -1: // No version tag
- return false;
- case 0:
- // Upgrade to 1 or return false
- case 1:
- // Upgrade to 2 or return false
- case 2:
- // Upgrade to 3 or return false
- // ...
- }
- return true;
- }
- /**
- * Saves a pocket registry to NBT.
- *
- * @param nbt The NBT tag to save to.
- */
- public void writeToNBT(NBTTagCompound nbt) {
- nbt.setInteger("version", DATA_VERSION);
- nbt.setInteger("gridSize", gridSize);
- nbt.setInteger("maxPocketSize", maxPocketSize);
- nbt.setInteger("privatePocketSize", privatePocketSize);
- nbt.setInteger("publicPocketSize", publicPocketSize);
- nbt.setTag("privatePocketMap", NBTUtils.writeMapStringInteger(privatePocketMap));
- NBTTagList pocketsTagList = new NBTTagList();
- for (int i : pockets.keySet()) {
- pocketsTagList.appendTag(Pocket.writeToNBT(pockets.get(i)));
- }
- nbt.setTag("pockets", pocketsTagList);
- }
- /**
- * Registers a new pocket.
- *
- * @param pocket The pocket to register
- */
- public void registerPocket(Pocket pocket) {
- int id = nextFreeID++;
- pockets.put(id, pocket);
- pocket.setID(id);
- PocketSavedData.get(world).markDirty(); // Notify that this needs to be saved on world save
- }
- // TODO: function for removing a pocket
- public Pocket getPocket(int id) {
- return pockets.get(id);
- }
- public int getPrivatePocketID(String playerUUID) {
- Integer id = privatePocketMap.get(playerUUID);
- if (id == null) return -1;
- return id;
- }
- public void setPrivatePocketID(String playerUUID, int id) {
- privatePocketMap.put(playerUUID, id);
- }
- /**
- * Calculates the Location of a pocket based on the ID.
- *
- * @param id The ID of the pocket
- * @return The Location of the pocket
- */
- public Location getLocationFromID(int id) {
- GridUtils.GridPos pos = GridUtils.numToPos(id);
- return new Location(world, pos.getX() * gridSize * 16, 0, pos.getZ() * gridSize * 16);
- }
- /**
- * Calculates the ID of a pocket based on the Location.
- *
- * @param location The location of the pocket
- * @return The ID of the pocket, or -1 if there is no pocket at that location
- */
- public int getIDFromLocation(Location location) {
- if (location != null && !location.getWorld().equals(world)) throw new RuntimeException("Wrong world for this Registry!");
- int x = location.getPos().getX();
- int z = location.getPos().getZ();
- int id = GridUtils.posToNum(new GridUtils.GridPos(x / (gridSize * 16), z / (gridSize * 16)));
- return pockets.containsKey(id) ? id : -1;
- }
- public boolean isPlayerAllowedToBeHere(EntityPlayerMP player, Location location) {
- if(!DimDoorDimensions.isPocketDimensionID(location.getDimensionID())) return true;
- int pocketID = getIDFromLocation(location);
- if (pocketID == -1) { // outside of a pocket
- return false;
- } else {
- Pocket pocket = pockets.get(pocketID);
- return pocket.isPlayerAllowedInPocket(player) && pocket.isLocationWithinPocketBounds(location, gridSize);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement