From 01acf08ad45b56cb24cca4797183f8f60451afb0 Mon Sep 17 00:00:00 2001 From: "Phil M. H" Date: Sat, 2 Mar 2019 15:48:54 -0500 Subject: [PATCH 1/5] 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 From 4c76d42ef4a0cd37e70df38acde31e8d0542dfff Mon Sep 17 00:00:00 2001 From: "Phil M. H" Date: Sat, 2 Mar 2019 15:55:58 -0500 Subject: [PATCH 2/5] Remove unused imports --- .../WayofTime/bloodmagic/block/BlockDimensionalPortal.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java b/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java index 8c737b2c..825f055f 100644 --- a/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java +++ b/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java @@ -10,14 +10,10 @@ 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; From d7b8c829ae94c6fe451cc10119582b038d54a3dd Mon Sep 17 00:00:00 2001 From: "Phil M. H" Date: Fri, 8 Mar 2019 02:27:14 -0500 Subject: [PATCH 3/5] Removed the null check in BlockDimensionalPortal Neither ServerStartingEvent or ServerStartedEvent, were able to initialized the LocationsHandler correctly, so it was placed in onWorldLoad --- .../bloodmagic/block/BlockDimensionalPortal.java | 16 +++++++--------- .../bloodmagic/ritual/types/RitualPortal.java | 2 -- .../util/handler/event/GenericHandler.java | 8 ++++++++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java b/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java index 825f055f..51b3e9d0 100644 --- a/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java +++ b/src/main/java/WayofTime/bloodmagic/block/BlockDimensionalPortal.java @@ -76,20 +76,18 @@ public class BlockDimensionalPortal extends BlockInteger { if (!world.isRemote && world.getTileEntity(pos) instanceof TileDimensionalPortal) { TileDimensionalPortal tile = (TileDimensionalPortal) world.getTileEntity(pos); - LocationsHandler.verifyIsInitialized(); ArrayList linkedLocations = LocationsHandler.getLinkedLocations(tile.portalID); 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()); + 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()))) { - portal = linkedLocations.get(index-1); // if most recent PortalLocaiton = this, get the 2nd most recent - } else { - portal = linkedLocations.get(index); - } - + 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 { diff --git a/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java b/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java index 6dc0849f..6ab16b86 100644 --- a/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java +++ b/src/main/java/WayofTime/bloodmagic/ritual/types/RitualPortal.java @@ -86,7 +86,6 @@ public class RitualPortal extends Ritual { } } } - LocationsHandler.verifyIsInitialized(); LocationsHandler.addLocation(name, new PortalLocation(x, y + 1, z, world.provider.getDimension())); portalRitualTag.setString(PORTAL_ID_TAG, name); return true; @@ -151,7 +150,6 @@ public class RitualPortal extends Ritual { int z = masterRitualStone.getBlockPos().getZ(); EnumFacing direction = masterRitualStone.getDirection(); - 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) { 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 fda8d9da..0467c951 100644 --- a/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java +++ b/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java @@ -32,6 +32,7 @@ import WayofTime.bloodmagic.potion.BMPotionUtils; import WayofTime.bloodmagic.potion.PotionEventHandlers; import WayofTime.bloodmagic.ritual.IMasterRitualStone; import WayofTime.bloodmagic.ritual.RitualManager; +import WayofTime.bloodmagic.ritual.portal.LocationsHandler; import WayofTime.bloodmagic.soul.DemonWillHolder; import WayofTime.bloodmagic.util.Constants; import WayofTime.bloodmagic.util.Utils; @@ -79,6 +80,8 @@ import net.minecraftforge.event.entity.player.PlayerPickupXpEvent; import net.minecraftforge.event.world.ExplosionEvent; import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.Mod.EventHandler; +import net.minecraftforge.fml.common.event.FMLServerStartedEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; @@ -467,6 +470,7 @@ public class GenericHandler { targetTaskMapMap.computeIfAbsent(world, k -> new HashMap<>()); PotionEventHandlers.flightListMap.computeIfAbsent(world, k -> new ArrayList<>()); PotionEventHandlers.noGravityListMap.computeIfAbsent(world, k -> new ArrayList<>()); + LocationsHandler.verifyIsInitialized();; } @SubscribeEvent @@ -479,4 +483,8 @@ public class GenericHandler { PotionEventHandlers.flightListMap.get(world).clear(); PotionEventHandlers.noGravityListMap.get(world).clear(); } + + @EventHandler + public static void onServerStarted(FMLServerStartedEvent event) { + } } From 27a7302010f5367ec0f49442f0d6fac795f30ab0 Mon Sep 17 00:00:00 2001 From: "Phil M. Harner" Date: Sun, 10 Mar 2019 17:46:27 -0400 Subject: [PATCH 4/5] Removed unused import and method. --- .../bloodmagic/util/handler/event/GenericHandler.java | 5 ----- 1 file changed, 5 deletions(-) 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 0467c951..e7189224 100644 --- a/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java +++ b/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java @@ -80,8 +80,6 @@ import net.minecraftforge.event.entity.player.PlayerPickupXpEvent; import net.minecraftforge.event.world.ExplosionEvent; import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.fml.common.Mod.EventHandler; -import net.minecraftforge.fml.common.event.FMLServerStartedEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; @@ -484,7 +482,4 @@ public class GenericHandler { PotionEventHandlers.noGravityListMap.get(world).clear(); } - @EventHandler - public static void onServerStarted(FMLServerStartedEvent event) { - } } From 8845923dff51cbe4fc3da6d0286c83d74679d37c Mon Sep 17 00:00:00 2001 From: "Phil M. H" Date: Sat, 13 Apr 2019 17:30:37 -0400 Subject: [PATCH 5/5] Removed extra semi-colon. --- .../WayofTime/bloodmagic/util/handler/event/GenericHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e7189224..894760a0 100644 --- a/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java +++ b/src/main/java/WayofTime/bloodmagic/util/handler/event/GenericHandler.java @@ -468,7 +468,7 @@ public class GenericHandler { targetTaskMapMap.computeIfAbsent(world, k -> new HashMap<>()); PotionEventHandlers.flightListMap.computeIfAbsent(world, k -> new ArrayList<>()); PotionEventHandlers.noGravityListMap.computeIfAbsent(world, k -> new ArrayList<>()); - LocationsHandler.verifyIsInitialized();; + LocationsHandler.verifyIsInitialized(); } @SubscribeEvent