diff --git a/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java b/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java index 50f69dbc..51b3e9d0 100644 --- a/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java +++ b/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java @@ -76,27 +76,22 @@ public class BlockDimensionalPortal extends BlockInteger { if (!world.isRemote && world.getTileEntity(pos) instanceof TileDimensionalPortal) { TileDimensionalPortal tile = (TileDimensionalPortal) world.getTileEntity(pos); - if (LocationsHandler.getLocationsHandler() != null) { - ArrayList linkedLocations = LocationsHandler.getLocationsHandler().getLinkedLocations(tile.portalID); + ArrayList linkedLocations = LocationsHandler.getLinkedLocations(tile.portalID); - if (linkedLocations != null && !linkedLocations.isEmpty() && linkedLocations.size() > 1) { - if (world.getTileEntity(tile.getMasterStonePos()) != null && world.getTileEntity(tile.getMasterStonePos()) instanceof IMasterRitualStone) { - IMasterRitualStone masterRitualStone = (IMasterRitualStone) world.getTileEntity(tile.getMasterStonePos()); - if (linkedLocations.get(0).equals(new PortalLocation(masterRitualStone.getBlockPos().up(), world.provider.getDimension()))) { - PortalLocation portal = linkedLocations.get(1); - if (portal.getDimension() == world.provider.getDimension()) { - TeleportQueue.getInstance().addITeleport(new Teleports.TeleportSameDim(portal.getX(), portal.getY(), portal.getZ(), entity, masterRitualStone.getOwner(), false)); - } else { - TeleportQueue.getInstance().addITeleport(new Teleports.TeleportToDim(portal.getX(), portal.getY(), portal.getZ(), entity, masterRitualStone.getOwner(), world, portal.getDimension(), false)); - } - } else if (linkedLocations.get(1).equals(new PortalLocation(masterRitualStone.getBlockPos().up(), world.provider.getDimension()))) { - PortalLocation portal = linkedLocations.get(0); - if (portal.getDimension() == world.provider.getDimension()) { - TeleportQueue.getInstance().addITeleport(new Teleports.TeleportSameDim(portal.getX(), portal.getY(), portal.getZ(), entity, masterRitualStone.getOwner(), false)); - } else { - TeleportQueue.getInstance().addITeleport(new Teleports.TeleportToDim(portal.getX(), portal.getY(), portal.getZ(), entity, masterRitualStone.getOwner(), world, portal.getDimension(), false)); - } - } + if (linkedLocations != null && linkedLocations.size() > 1) { + TileEntity tileEntity = world.getTileEntity(tile.getMasterStonePos()); + if (tileEntity instanceof IMasterRitualStone) { + IMasterRitualStone masterRitualStone = (IMasterRitualStone) tileEntity; + PortalLocation portal; + int index = linkedLocations.size() - 1; //index of most recent PortalLocation + if (linkedLocations.get(index).equals(new PortalLocation(masterRitualStone.getBlockPos().up(), world.provider.getDimension()))) + index--; // if most recent PortalLocation = this, get the 2nd most recent + portal = linkedLocations.get(index); + + if (portal.getDimension() == world.provider.getDimension()) { + TeleportQueue.getInstance().addITeleport(new Teleports.TeleportSameDim(portal.getX(), portal.getY(), portal.getZ(), entity, masterRitualStone.getOwner(), false)); + } else { + TeleportQueue.getInstance().addITeleport(new Teleports.TeleportToDim(portal.getX(), portal.getY(), portal.getZ(), entity, masterRitualStone.getOwner(), world, portal.getDimension(), false)); } } } diff --git a/src/main/java/WayofTime/bloodmagic/ritual/portal/LocationsHandler.java b/src/main/java/WayofTime/bloodmagic/ritual/portal/LocationsHandler.java index bdcd9fdd..d402cdbe 100644 --- a/src/main/java/WayofTime/bloodmagic/ritual/portal/LocationsHandler.java +++ b/src/main/java/WayofTime/bloodmagic/ritual/portal/LocationsHandler.java @@ -9,39 +9,30 @@ import java.io.*; import java.util.ArrayList; import java.util.HashMap; -public class LocationsHandler implements Serializable { +public final class LocationsHandler implements Serializable { public static final long serialVersionUID = 10102001; private static final String fileName = String.valueOf(DimensionManager.getCurrentSaveRootDirectory()) + "/" + BloodMagic.MODID + "/PortalLocations.dat"; private static HashMap> portals; - private static LocationsHandler locationsHandler; + private static boolean initialized = false; + + private LocationsHandler() {} - private LocationsHandler() { - portals = new HashMap<>(); - } - - public boolean addLocation(String name, PortalLocation location) { + public static boolean addLocation(String name, PortalLocation location) { ArrayList portalLocations = portals.get(name); if (portalLocations == null) { portals.put(name, new ArrayList<>()); updateFile(fileName, portals); } - if (!portals.get(name).isEmpty() && portals.get(name).size() >= 2) { - BMLog.DEBUG.info("Location {} already exists.", name); - updateFile(fileName, portals); - return false; - } else { - portals.get(name).add(location); - BMLog.DEBUG.info("Adding {}", name); - updateFile(fileName, portals); - return true; - } + portals.get(name).add(location); + BMLog.DEBUG.info("Adding {}", name); + updateFile(fileName, portals); + return true; } - public boolean removeLocation(String name, PortalLocation location) { + public static boolean removeLocation(String name, PortalLocation location) { if (portals.get(name) != null && !portals.get(name).isEmpty()) { - if (portals.get(name).contains(location)) { - portals.get(name).remove(location); + if (portals.get(name).remove(location)) { BMLog.DEBUG.info("Removing {}", name); updateFile(fileName, portals); return true; @@ -54,17 +45,17 @@ public class LocationsHandler implements Serializable { return false; } - public ArrayList getLinkedLocations(String name) { + public static ArrayList getLinkedLocations(String name) { return portals.get(name); } - public static LocationsHandler getLocationsHandler() { - if (locationsHandler == null || loadFile() == null) { - locationsHandler = new LocationsHandler(); - return locationsHandler; - } else { - portals = loadFile(); - return locationsHandler; + public static void verifyIsInitialized() { + if (!initialized) { + HashMap> map = loadFile(); + if(map == null) { + portals = new HashMap<>(); + } + initialized = true; } } diff --git a/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java b/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java index 8ec0b006..6ab16b86 100644 --- a/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java +++ b/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java @@ -86,11 +86,9 @@ public class RitualPortal extends Ritual { } } } - - if (LocationsHandler.getLocationsHandler().addLocation(name, new PortalLocation(x, y + 1, z, world.provider.getDimension()))) { - portalRitualTag.setString(PORTAL_ID_TAG, name); - return true; - } + LocationsHandler.addLocation(name, new PortalLocation(x, y + 1, z, world.provider.getDimension())); + portalRitualTag.setString(PORTAL_ID_TAG, name); + return true; } return false; } @@ -152,7 +150,7 @@ public class RitualPortal extends Ritual { int z = masterRitualStone.getBlockPos().getZ(); EnumFacing direction = masterRitualStone.getDirection(); - LocationsHandler.getLocationsHandler().removeLocation(portalRitualTag.getString(PORTAL_ID_TAG), new PortalLocation(x, y + 1, z, world.provider.getDimension())); + LocationsHandler.removeLocation(portalRitualTag.getString(PORTAL_ID_TAG), new PortalLocation(x, y + 1, z, world.provider.getDimension())); if (direction == EnumFacing.NORTH || direction == EnumFacing.SOUTH) { for (int i = x - 2; i <= x + 2; i++) { @@ -172,7 +170,7 @@ public class RitualPortal extends Ritual { } } - portalRitualTag.removeTag(PORTAL_ID_TAG); + portalRitualTag.removeTag(portalRitualTag.getString(PORTAL_ID_TAG)); } @Override diff --git a/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java b/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java index 7e4cb0cc..0e55d754 100644 --- a/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java +++ b/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java @@ -33,6 +33,7 @@ import WayofTime.bloodmagic.potion.PotionEventHandlers; import WayofTime.bloodmagic.ritual.AreaDescriptor; import WayofTime.bloodmagic.ritual.IMasterRitualStone; import WayofTime.bloodmagic.ritual.RitualManager; +import WayofTime.bloodmagic.ritual.portal.LocationsHandler; import WayofTime.bloodmagic.ritual.types.RitualVeilOfEvil; import WayofTime.bloodmagic.ritual.types.RitualWardOfSacrosanctity; import WayofTime.bloodmagic.soul.DemonWillHolder; @@ -552,6 +553,7 @@ public class GenericHandler { preventSpawnMap.computeIfAbsent(world, k -> new HashMap<>()); PotionEventHandlers.flightListMap.computeIfAbsent(world, k -> new ArrayList<>()); PotionEventHandlers.noGravityListMap.computeIfAbsent(world, k -> new ArrayList<>()); + LocationsHandler.verifyIsInitialized(); } @SubscribeEvent @@ -566,4 +568,5 @@ public class GenericHandler { PotionEventHandlers.flightListMap.remove(world); PotionEventHandlers.noGravityListMap.remove(world); } + }