Adds some more fixes and improvements for vehicle teleportation

This commit is contained in:
Kristian Knarvik 2021-06-11 20:46:14 +02:00
parent b1aa53c1a9
commit 0fe2a5b380
7 changed files with 49 additions and 26 deletions

View File

@ -12,18 +12,6 @@ public class BloxPopulator {
private Material nextMat; private Material nextMat;
private Axis nextAxis; private Axis nextAxis;
/**
* Instantiates a new block populator
*
* @param blockLocation <p>The location to start from</p>
* @param material <p>The material to populate</p>
*/
public BloxPopulator(BlockLocation blockLocation, Material material) {
this.blockLocation = blockLocation;
nextMat = material;
nextAxis = null;
}
/** /**
* Instantiates a new block populator * Instantiates a new block populator
* *

View File

@ -80,6 +80,7 @@ public class VehicleEventListener implements Listener {
Stargate.log.warning(Stargate.getString("prefox") + "Unable to find portal destination"); Stargate.log.warning(Stargate.getString("prefox") + "Unable to find portal destination");
return; return;
} }
Stargate.debug("vehicleTeleport", destinationPortal.getWorld() + " " + destinationPortal.getId());
destinationPortal.teleport(vehicle, entrancePortal); destinationPortal.teleport(vehicle, entrancePortal);
} }
} }

View File

@ -391,7 +391,7 @@ public class GateHandler {
* *
* @param gateFolder <p>The folder containing gate config files</p> * @param gateFolder <p>The folder containing gate config files</p>
*/ */
public static void populateDefaults(String gateFolder) { private static void populateDefaults(String gateFolder) {
loadGateFromJar("nethergate.gate", gateFolder); loadGateFromJar("nethergate.gate", gateFolder);
loadGateFromJar("watergate.gate", gateFolder); loadGateFromJar("watergate.gate", gateFolder);
} }

View File

@ -33,6 +33,18 @@ public class GateLayout {
readLayout(); readLayout();
} }
/**
* Gets two of the corners of the gate layout creating the smallest box the gate can be contained within
*
* @return <p>Two of the gate's corners</p>
*/
public RelativeBlockVector[] getCorners() {
return new RelativeBlockVector[] {
new RelativeBlockVector(0, 0, 0),
new RelativeBlockVector(layout[0].length - 1, layout.length - 1, 1)
};
}
/** /**
* Gets the character array describing this layout * Gets the character array describing this layout
* *

View File

@ -13,6 +13,7 @@ import net.knarcraft.stargate.utility.DirectionHelper;
import net.knarcraft.stargate.utility.EntityHelper; import net.knarcraft.stargate.utility.EntityHelper;
import net.knarcraft.stargate.utility.SignHelper; import net.knarcraft.stargate.utility.SignHelper;
import org.bukkit.Axis; import org.bukkit.Axis;
import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -38,6 +39,9 @@ import java.util.Random;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
/**
* This class represents a portal in space which points to one or several other portals
*/
public class Portal { public class Portal {
// Gate location block info // Gate location block info
@ -54,6 +58,7 @@ public class Portal {
private BlockLocation button; private BlockLocation button;
private BlockLocation[] frame; private BlockLocation[] frame;
private BlockLocation[] entrances; private BlockLocation[] entrances;
// Gate information // Gate information
private String name; private String name;
private String destination; private String destination;
@ -551,7 +556,7 @@ public class Portal {
// Close this gate, then the dest gate. // Close this gate, then the dest gate.
Material closedType = gate.getPortalClosedBlock(); Material closedType = gate.getPortalClosedBlock();
for (BlockLocation inside : getEntrances()) { for (BlockLocation inside : getEntrances()) {
Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, closedType)); Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, closedType, null));
} }
updatePortalClosedState(); updatePortalClosedState();
@ -663,6 +668,8 @@ public class Portal {
exit = stargatePortalEvent.getExit(); exit = stargatePortalEvent.getExit();
} }
loadChunks();
// If no event is passed in, assume it's a teleport, and act as such // If no event is passed in, assume it's a teleport, and act as such
if (event == null) { if (event == null) {
exit.setYaw(this.getRotation()); exit.setYaw(this.getRotation());
@ -698,8 +705,6 @@ public class Portal {
Location traveller = vehicle.getLocation(); Location traveller = vehicle.getLocation();
Location exit = getExit(vehicle, traveller); Location exit = getExit(vehicle, traveller);
adjustRotation(traveller, exit, origin);
double velocity = vehicle.getVelocity().length(); double velocity = vehicle.getVelocity().length();
// Stop and teleport // Stop and teleport
@ -707,7 +712,10 @@ public class Portal {
// Get new velocity // Get new velocity
final Vector newVelocity = new Vector(modX, 0.0F, modZ); final Vector newVelocity = new Vector(modX, 0.0F, modZ);
//-z
Stargate.debug("teleport", modX + " " + modZ);
newVelocity.multiply(velocity); newVelocity.multiply(velocity);
adjustRotation(traveller, exit, origin);
List<Entity> passengers = vehicle.getPassengers(); List<Entity> passengers = vehicle.getPassengers();
World vehicleWorld = exit.getWorld(); World vehicleWorld = exit.getWorld();
@ -716,6 +724,8 @@ public class Portal {
return; return;
} }
loadChunks();
if (!passengers.isEmpty()) { if (!passengers.isEmpty()) {
if (vehicle instanceof RideableMinecart || vehicle instanceof Boat) { if (vehicle instanceof RideableMinecart || vehicle instanceof Boat) {
putPassengersInNewVehicle(vehicle, passengers, vehicleWorld, exit, newVelocity); putPassengersInNewVehicle(vehicle, passengers, vehicleWorld, exit, newVelocity);
@ -770,6 +780,7 @@ public class Portal {
private void handleVehiclePassengers(List<Entity> passengers, Vehicle targetVehicle, Location exit) { private void handleVehiclePassengers(List<Entity> passengers, Vehicle targetVehicle, Location exit) {
for (Entity passenger : passengers) { for (Entity passenger : passengers) {
passenger.eject(); passenger.eject();
//TODO: Fix random java.lang.IllegalStateException: Removing entity while ticking!
if (!passenger.teleport(exit)) { if (!passenger.teleport(exit)) {
Stargate.debug("handleVehiclePassengers", "Failed to teleport passenger"); Stargate.debug("handleVehiclePassengers", "Failed to teleport passenger");
} }
@ -794,9 +805,11 @@ public class Portal {
exitLocation = exit.modRelativeLoc(0D, 0D, 1, traveller.getYaw(), exitLocation = exit.modRelativeLoc(0D, 0D, 1, traveller.getYaw(),
traveller.getPitch(), modX * back, 1, modZ * back); traveller.getPitch(), modX * back, 1, modZ * back);
double entitySize = EntityHelper.getEntityMaxSize(entity); if (entity != null) {
if (entitySize > 1) { double entitySize = EntityHelper.getEntityMaxSize(entity);
exitLocation = preventExitSuffocation(relativeExit, exitLocation, entity); if (entitySize > 1) {
exitLocation = preventExitSuffocation(relativeExit, exitLocation, entity);
}
} }
} else { } else {
Stargate.log.log(Level.WARNING, Stargate.getString("prefix") + "Missing destination point in .gate file " + gate.getFilename()); Stargate.log.log(Level.WARNING, Stargate.getString("prefix") + "Missing destination point in .gate file " + gate.getFilename());
@ -892,13 +905,16 @@ public class Portal {
} }
/** /**
* Checks whether the chunk the portal is located at is loaded * Loads the chunks at the portal's corners
*
* @return <p>True if the chunk containing the portal is loaded</p>
*/ */
public boolean isChunkLoaded() { public void loadChunks() {
//TODO: Improve this in the case where the portal sits between two chunks //TODO: Improve this in the case where the portal sits between two chunks
return getWorld().isChunkLoaded(topLeft.getBlock().getChunk()); for (RelativeBlockVector vector : gate.getLayout().getCorners()) {
Chunk chunk = getBlockAt(vector).getChunk();
if (!getWorld().isChunkLoaded(chunk)) {
chunk.load();
}
}
} }
/** /**

View File

@ -0,0 +1,7 @@
package net.knarcraft.stargate.portal;
public class PortalDirection {
}

View File

@ -15,7 +15,6 @@ import be.seeseemelk.mockbukkit.MockBukkit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
public class GateLayoutTest { public class GateLayoutTest {
@ -40,7 +39,7 @@ public class GateLayoutTest {
expected.add(new RelativeBlockVector(1, 3, 0)); expected.add(new RelativeBlockVector(1, 3, 0));
expected.add(new RelativeBlockVector(2, 3, 0)); expected.add(new RelativeBlockVector(2, 3, 0));
Set<RelativeBlockVector> exits = layout.getExits().keySet(); List<RelativeBlockVector> exits = layout.getExits();
exits.forEach((blockVector) -> assertTrue(expected.contains(blockVector))); exits.forEach((blockVector) -> assertTrue(expected.contains(blockVector)));
} }