From 01acf08ad45b56cb24cca4797183f8f60451afb0 Mon Sep 17 00:00:00 2001 From: "Phil M. H" Date: Sat, 2 Mar 2019 15:48:54 -0500 Subject: [PATCH] Gate of the fold complete --- .../block/BlockDimensionalPortal.java | 41 ++++++++-------- .../ritual/portal/LocationsHandler.java | 47 ++++++++----------- .../bloodmagic/ritual/types/RitualPortal.java | 14 +++--- 3 files changed, 47 insertions(+), 55 deletions(-) diff --git a/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java b/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java index 50f69dbc..8c737b2c 100644 --- a/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java +++ b/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java @@ -10,10 +10,14 @@ import WayofTime.bloodmagic.teleport.Teleports; import WayofTime.bloodmagic.tile.TileDimensionalPortal; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; +import net.minecraft.client.audio.Sound; import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.SoundEvents; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockRenderLayer; import net.minecraft.util.EnumParticleTypes; +import net.minecraft.util.SoundCategory; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; @@ -76,27 +80,24 @@ 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); + LocationsHandler.verifyIsInitialized(); + 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) { + if (world.getTileEntity(tile.getMasterStonePos()) != null && world.getTileEntity(tile.getMasterStonePos()) instanceof IMasterRitualStone) { + IMasterRitualStone masterRitualStone = (IMasterRitualStone) world.getTileEntity(tile.getMasterStonePos()); + 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()))) { + portal = linkedLocations.get(index-1); // if most recent PortalLocaiton = this, get the 2nd most recent + } else { + 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..6dc0849f 100644 --- a/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java +++ b/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java @@ -86,11 +86,10 @@ 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.verifyIsInitialized(); + LocationsHandler.addLocation(name, new PortalLocation(x, y + 1, z, world.provider.getDimension())); + portalRitualTag.setString(PORTAL_ID_TAG, name); + return true; } return false; } @@ -152,7 +151,8 @@ 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.verifyIsInitialized(); + 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 +172,7 @@ public class RitualPortal extends Ritual { } } - portalRitualTag.removeTag(PORTAL_ID_TAG); + portalRitualTag.removeTag(portalRitualTag.getString(PORTAL_ID_TAG)); } @Override