Fixes some portal closing bugs, a NullPointerException and a typo

Fixes a typo in nn-no
Fixes a bug causing always-on portals not to be closed properly
Fixes a possible NullPointerException in onEntityPortalEnter
Waits for block change request on reload which makes it possible to change the open-material with just a reload
This commit is contained in:
Kristian Knarvik 2021-10-26 16:22:20 +02:00
parent 1c906528f2
commit 5c730eb613
6 changed files with 52 additions and 29 deletions

View File

@ -1,10 +1,13 @@
package net.knarcraft.stargate.config;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.container.BlockChangeRequest;
import net.knarcraft.stargate.listener.BungeeCordListener;
import net.knarcraft.stargate.portal.GateHandler;
import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.PortalHandler;
import net.knarcraft.stargate.portal.PortalRegistry;
import net.knarcraft.stargate.thread.BlockChangeThread;
import net.knarcraft.stargate.utility.FileHelper;
import net.knarcraft.stargate.utility.PortalFileHelper;
import org.bukkit.Bukkit;
@ -121,7 +124,15 @@ public final class StargateConfig {
public void reload(CommandSender sender) {
//Unload all saved data
unload();
//Perform all block change requests to prevent mismatch if a gate's open-material changes. Changing the
// closed-material still requires a restart.
BlockChangeRequest firstElement = Stargate.blockChangeRequestQueue.peek();
while (firstElement != null) {
BlockChangeThread.pollQueue();
firstElement = Stargate.blockChangeRequestQueue.peek();
}
//Store the old enable bungee state in case it changes
boolean oldEnableBungee = stargateGateConfig.enableBungee();
@ -145,7 +156,9 @@ public final class StargateConfig {
activePortal.getPortalActivator().deactivate();
}
//Force all portals to close
closeAllPortals();
closeAllOpenPortals();
PortalHandler.closeAllPortals();
//Clear queues and lists
activePortalsQueue.clear();
openPortalsQueue.clear();
@ -247,9 +260,9 @@ public final class StargateConfig {
/**
* Forces all open portals to close
*/
public void closeAllPortals() {
public void closeAllOpenPortals() {
for (Portal openPortal : openPortalsQueue) {
openPortal.getPortalOpener().closePortal(true);
openPortal.getPortalOpener().closePortal(false);
}
}

View File

@ -60,6 +60,9 @@ public class PortalEventListener implements Listener {
if (entity instanceof Player player && location.getBlock().getType() == Material.END_PORTAL && world != null &&
world.getEnvironment() == World.Environment.THE_END) {
Portal portal = PortalHandler.getByAdjacentEntrance(location);
if (portal == null) {
return;
}
//Remove any old player teleportations in case weird things happen
playersFromTheEnd.removeIf((teleportation -> teleportation.getPlayer() == player));

View File

@ -146,7 +146,7 @@ public class PortalOpener {
*/
public void closePortal(boolean force) {
//No need to close a portal which is already closed
if (!isOpen) {
if (!isOpen()) {
return;
}

View File

@ -21,24 +21,31 @@ public class BlockChangeThread implements Runnable {
long sTime = System.nanoTime();
//Repeat for at most 0.025 seconds
while (System.nanoTime() - sTime < 25000000) {
//Abort if there's no work to be done
BlockChangeRequest blockChangeRequest = Stargate.blockChangeRequestQueue.poll();
if (blockChangeRequest == null) {
return;
}
pollQueue();
}
}
//Change the material of the pulled block
Block block = blockChangeRequest.getBlockLocation().getBlock();
block.setType(blockChangeRequest.getMaterial(), false);
/**
* Polls the block change request queue for any waiting requests
*/
public static void pollQueue() {
//Abort if there's no work to be done
BlockChangeRequest blockChangeRequest = Stargate.blockChangeRequestQueue.poll();
if (blockChangeRequest == null) {
return;
}
if (blockChangeRequest.getMaterial() == Material.END_GATEWAY &&
block.getWorld().getEnvironment() == World.Environment.THE_END) {
//Force a specific location to prevent exit gateway generation
fixEndGatewayGate(block);
} else if (blockChangeRequest.getAxis() != null) {
//If orientation is relevant, adjust the block's orientation
orientBlock(block, blockChangeRequest.getAxis());
}
//Change the material of the pulled block
Block block = blockChangeRequest.getBlockLocation().getBlock();
block.setType(blockChangeRequest.getMaterial(), false);
if (blockChangeRequest.getMaterial() == Material.END_GATEWAY &&
block.getWorld().getEnvironment() == World.Environment.THE_END) {
//Force a specific location to prevent exit gateway generation
fixEndGatewayGate(block);
} else if (blockChangeRequest.getAxis() != null) {
//If orientation is relevant, adjust the block's orientation
orientBlock(block, blockChangeRequest.getAxis());
}
}
@ -47,7 +54,7 @@ public class BlockChangeThread implements Runnable {
*
* @param block <p>The block to fix</p>
*/
private void fixEndGatewayGate(Block block) {
private static void fixEndGatewayGate(Block block) {
EndGateway gateway = (EndGateway) block.getState();
gateway.setExitLocation(block.getLocation());
gateway.setExactTeleport(true);
@ -60,7 +67,7 @@ public class BlockChangeThread implements Runnable {
* @param block <p>The block to orient</p>
* @param axis <p>The axis to use for orienting the block</p>
*/
private void orientBlock(Block block, Axis axis) {
private static void orientBlock(Block block, Axis axis) {
Orientable orientable = (Orientable) block.getBlockData();
orientable.setAxis(axis);
block.setBlockData(orientable);

View File

@ -26,12 +26,12 @@ public final class PermissionHelper {
public static void openPortal(Player player, Portal portal) {
Portal destination = portal.getPortalActivator().getDestination();
//Always-open gate -- Do nothing
//Always-open portal -- Do nothing
if (portal.getOptions().isAlwaysOn()) {
return;
}
//Random gate -- Do nothing
//Random portal -- Do nothing
if (portal.getOptions().isRandom()) {
return;
}
@ -42,16 +42,16 @@ public final class PermissionHelper {
return;
}
//Gate is already open
//Portal is already open
if (portal.isOpen()) {
// Close if this player opened the gate
//Close if this player opened the portal
if (portal.getActivePlayer() == player) {
portal.getPortalOpener().closePortal(false);
}
return;
}
//Gate that someone else is using -- Deny access
//Portal is used by another player -- Deny access
if ((!portal.getOptions().isFixed()) && portal.getPortalActivator().isActive() &&
(portal.getActivePlayer() != player)) {
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("denyMsg"));

View File

@ -34,4 +34,4 @@ signDisconnected=Kopla frå
bungeeDisabled=BungeeCord støtte er slått av.
bungeeDeny=Du har ikkje løyve til å opprette BungeeCord portar.
bungeeEmpty=BungeeCord portar treng bade ein destinasjon og eit nettverk.
bungeeSign=Teleportar til
bungeeSign=Teleporter til