destinationOptional = findValidDestination(
+ player,
+ source,
+ new BlockCoord((int) Math.floor(sample.x), (int) Math.floor(sample.y), (int) Math.floor(sample.z)));
+ if (destinationOptional.isPresent()) {
+ return destinationOptional;
+ }
+
+ destinationOptional = findValidDestination(
+ player,
+ source,
+ new BlockCoord((int) Math.floor(sample.x), (int) Math.floor(sample.y) + 1, (int) Math.floor(sample.z)));
+ if (destinationOptional.isPresent()) {
+ return destinationOptional;
+ }
+
+ if (!Config.travelStaffSearchOptimize) {
+ destinationOptional = findValidDestination(
+ player,
+ source,
+ new BlockCoord(
+ (int) Math.floor(sample.x),
+ (int) Math.floor(sample.y) - 1,
+ (int) Math.floor(sample.z)));
+ if (destinationOptional.isPresent()) {
+ return destinationOptional;
+ }
+ }
+
+ return Optional.empty();
+ }
+
+ /**
+ * Returns the provided position if it's valid, or returns empty optional if unsuccessful.
+ *
+ *
+ * To be precise: if the destination is a travel anchor or other block, then the returned coordinates will be one
+ * block higher, to place the player on top of the block.
+ */
+ public Optional findValidDestination(EntityPlayer player, TravelSource source, BlockCoord coord) {
+ // Are we trying to travel to a block (anchor or telepad)?
+ boolean travelToBlock = source != TravelSource.STAFF_BLINK && source != TravelSource.TELEPORT_STAFF_BLINK;
+
+ BlockCoord destination = coord;
+ if (travelToBlock) {
+ TileEntity te = player.worldObj.getTileEntity(coord.x, coord.y, coord.z);
+ if (te instanceof ITravelAccessable) {
+ ITravelAccessable ta = (ITravelAccessable) te;
+ if (!ta.canBlockBeAccessed(player)) {
+ showMessage(player, new ChatComponentTranslation("enderio.gui.travelAccessable.unauthorised"));
+ return Optional.empty();
+ }
+ }
+
+ // We actually want to go on top of the block.
+ destination = coord.getLocation(ForgeDirection.UP);
+ }
+
+ if (!isInRangeTarget(player, destination, source.getMaxDistanceTravelledSq())) {
+ if (travelToBlock) {
+ showMessage(player, new ChatComponentTranslation("enderio.blockTravelPlatform.outOfRange"));
+ }
+ return Optional.empty();
+ }
+ if (!isValidTarget(player, destination, source)) {
+ if (travelToBlock) {
+ showMessage(player, new ChatComponentTranslation("enderio.blockTravelPlatform.invalidTarget"));
+ }
+ return Optional.empty();
+ }
+
+ return Optional.of(destination);
+ }
+
public boolean travelToSelectedTarget(EntityPlayer player, TravelSource source, boolean conserveMomentum) {
return travelToLocation(player, source, selectedCoord, conserveMomentum);
}
public boolean travelToLocation(EntityPlayer player, TravelSource source, BlockCoord coord,
boolean conserveMomentum) {
-
- if (source != TravelSource.STAFF_BLINK && source != TravelSource.TELEPORT_STAFF_BLINK) {
- TileEntity te = player.worldObj.getTileEntity(coord.x, coord.y, coord.z);
- if (te instanceof ITravelAccessable) {
- ITravelAccessable ta = (ITravelAccessable) te;
- if (!ta.canBlockBeAccessed(player)) {
- showMessage(player, new ChatComponentTranslation("enderio.gui.travelAccessable.unauthorised"));
- return false;
- }
- }
+ Optional destinationOptional = findValidDestination(player, source, coord);
+ if (!destinationOptional.isPresent()) {
+ return false;
}
+ BlockCoord destination = destinationOptional.get();
int requiredPower = 0;
- requiredPower = getRequiredPower(player, source, coord);
+ requiredPower = getRequiredPower(player, source, destination);
if (requiredPower < 0) {
return false;
}
- if (!isInRangeTarget(player, coord, source.getMaxDistanceTravelledSq())) {
- if (source != TravelSource.STAFF_BLINK && source != TravelSource.TELEPORT_STAFF_BLINK) {
- showMessage(player, new ChatComponentTranslation("enderio.blockTravelPlatform.outOfRange"));
- }
- return false;
- }
- if (!isValidTarget(player, coord, source)) {
- if (source != TravelSource.STAFF_BLINK && source != TravelSource.TELEPORT_STAFF_BLINK) {
- showMessage(player, new ChatComponentTranslation("enderio.blockTravelPlatform.invalidTarget"));
- }
- return false;
- }
- if (doClientTeleport(player, coord, source, requiredPower, conserveMomentum)) {
+ if (doClientTeleport(player, destinationOptional.get(), source, requiredPower, conserveMomentum)) {
for (int i = 0; i < 6; ++i) {
player.worldObj.spawnParticle(
"portal",
@@ -764,6 +842,17 @@ public class TravelController {
return true;
}
+ public boolean isValidTarget(EntityPlayer player, BlockCoord bc, TravelSource source) {
+ if (bc == null) {
+ return false;
+ }
+ World w = player.worldObj;
+ BlockCoord baseLoc = bc;
+
+ return canTeleportTo(player, source, baseLoc, w)
+ && canTeleportTo(player, source, baseLoc.getLocation(ForgeDirection.UP), w);
+ }
+
public int getRequiredPower(EntityPlayer player, TravelSource source, BlockCoord coord) {
if (!isTravelItemActive(player, true)) {
return 0;
@@ -808,21 +897,6 @@ public class TravelController {
return Math.sqrt(getDistanceSquared(player, coord));
}
- private boolean isValidTarget(EntityPlayer player, BlockCoord bc, TravelSource source) {
- if (bc == null) {
- return false;
- }
- World w = player.worldObj;
- BlockCoord baseLoc = bc;
- if (source != TravelSource.STAFF_BLINK && source != TravelSource.TELEPORT_STAFF_BLINK) {
- // targeting a block so go one up
- baseLoc = bc.getLocation(ForgeDirection.UP);
- }
-
- return canTeleportTo(player, source, baseLoc, w)
- && canTeleportTo(player, source, baseLoc.getLocation(ForgeDirection.UP), w);
- }
-
private boolean canTeleportTo(EntityPlayer player, TravelSource source, BlockCoord bc, World w) {
if (bc.y < 1) {
return false;
@@ -904,6 +978,7 @@ public class TravelController {
if (selectedBlock instanceof ITravelAccessable) {
ITravelAccessable travelBlock = (ITravelAccessable) selectedBlock;
BlockCoord targetBlock = new BlockCoord(currentBlock.x, y, currentBlock.z);
+ BlockCoord destination = targetBlock.getLocation(ForgeDirection.UP);
if (Config.travelAnchorSkipWarning) {
if (travelBlock.getRequiresPassword(player)) {
@@ -914,55 +989,67 @@ public class TravelController {
&& !travelBlock.canUiBeAccessed(player)) {
showMessage(player, new ChatComponentTranslation("enderio.gui.travelAccessable.skipPrivate"));
}
- if (!isValidTarget(player, targetBlock, TravelSource.BLOCK)) {
+ if (!isValidTarget(player, destination, TravelSource.BLOCK)) {
showMessage(player, new ChatComponentTranslation("enderio.gui.travelAccessable.skipObstructed"));
}
}
- if (travelBlock.canBlockBeAccessed(player) && isValidTarget(player, targetBlock, TravelSource.BLOCK)) {
+ if (travelBlock.canBlockBeAccessed(player) && isValidTarget(player, destination, TravelSource.BLOCK)) {
return targetBlock;
}
}
return null;
}
+ /**
+ * Returns a row-sorted table with angle in radians as the row, distance as the column, and block coordinate as the
+ * value, holding all blocks which lie within the specified viewing angle and distance.
+ *
+ *
+ * The table is sorted by angle ascending, so the smallest angle will be first.
+ *
+ * @param player The player to search from.
+ * @param blocks The blocks to search through.
+ * @param maxAngle The maximum allowed angle, in radians.
+ * @param maxDistance The maximum allowed distance.
+ */
+ private RowSortedTable findBlocksWithinAngle(EntityPlayer player,
+ Iterable blocks, double maxAngle, double maxDistance) {
+ Vector3d eye = Util.getEyePositionEio(player);
+ Vector3d look = Util.getLookVecEio(player);
+
+ // Table of angle, distance, and block coordinate holding found blocks.
+ // Angle is the row, so this table will automatically be sorted by angle ascending.
+ RowSortedTable foundBlocks = TreeBasedTable.create();
+
+ for (BlockCoord p : blocks) {
+ Vector3d block = new Vector3d(p.x + 0.5, p.y + 0.5, p.z + 0.5);
+ block.sub(eye);
+ double distance = block.length();
+ if (distance > maxDistance) {
+ continue;
+ }
+ block.normalize();
+
+ double angle = Math.acos(look.dot(block));
+ if (angle <= maxAngle) {
+ foundBlocks.put(angle, distance, p);
+ }
+ }
+
+ return foundBlocks;
+ }
+
@SideOnly(Side.CLIENT)
private void updateSelectedTarget(EntityPlayer player) {
- selectedCoord = null;
if (candidates.isEmpty()) {
+ selectedCoord = null;
return;
}
- double closestDistance = Double.MAX_VALUE;
- for (BlockCoord bc : candidates.keySet()) {
- if (!bc.equals(onBlockCoord)) {
-
- double d = addRatio(bc);
- if (d < closestDistance) {
- selectedCoord = bc;
- closestDistance = d;
- }
- }
- }
-
- if (selectedCoord != null) {
-
- Vector3d blockCenter = new Vector3d(selectedCoord.x + 0.5, selectedCoord.y + 0.5, selectedCoord.z + 0.5);
- Vector2d blockCenterPixel = currentView.getScreenPoint(blockCenter);
-
- Vector2d screenMidPixel = new Vector2d(
- Minecraft.getMinecraft().displayWidth,
- Minecraft.getMinecraft().displayHeight);
- screenMidPixel.scale(0.5);
-
- double pixDist = blockCenterPixel.distance(screenMidPixel);
- double rat = pixDist / Minecraft.getMinecraft().displayHeight;
- if (rat != rat) {
- rat = 0;
- }
- if (rat > 0.07) {
- selectedCoord = null;
- }
- }
+ // Find the nearest travel anchor within roughly 10 degrees.
+ Optional selectedBlock = findBlocksWithinAngle(player, candidates.keySet(), 0.175, Double.MAX_VALUE)
+ .values().stream().findFirst();
+ selectedCoord = selectedBlock.orElse(null);
}
@SideOnly(Side.CLIENT)
diff --git a/src/main/java/crazypants/enderio/teleport/anchor/TileTravelAnchor.java b/src/main/java/crazypants/enderio/teleport/anchor/TileTravelAnchor.java
index 6cc1e8f1..f34e2d71 100644
--- a/src/main/java/crazypants/enderio/teleport/anchor/TileTravelAnchor.java
+++ b/src/main/java/crazypants/enderio/teleport/anchor/TileTravelAnchor.java
@@ -337,6 +337,9 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
public void validate() {
super.validate();
if (isTravelSource()) {
+ // Note: travelDestinations is a SetMultimap, which does not allow duplicate entries.
+ // So it's okay if this gets called multiple times with the same BlockCoord.
+ // Though, I think that it's not currently getting called multiple times, anyway.
TravelController.instance.travelDestinations.put(worldObj.provider.dimensionId, getLocation());
}
}
@@ -346,10 +349,4 @@ public class TileTravelAnchor extends TileEntityEio implements ITravelAccessable
super.invalidate();
TravelController.instance.travelDestinations.remove(worldObj.provider.dimensionId, getLocation());
}
-
- @Override
- public void onChunkUnload() {
- super.onChunkUnload();
- TravelController.instance.travelDestinations.remove(worldObj.provider.dimensionId, getLocation());
- }
}
diff --git a/src/main/java/crazypants/enderio/teleport/packet/PacketLongDistanceTravelEvent.java b/src/main/java/crazypants/enderio/teleport/packet/PacketLongDistanceTravelEvent.java
index 0beb1a9e..a8ef8f13 100644
--- a/src/main/java/crazypants/enderio/teleport/packet/PacketLongDistanceTravelEvent.java
+++ b/src/main/java/crazypants/enderio/teleport/packet/PacketLongDistanceTravelEvent.java
@@ -7,7 +7,10 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.server.S12PacketEntityVelocity;
+import net.minecraft.tileentity.TileEntity;
+import net.minecraft.util.ChatComponentTranslation;
import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.common.util.ForgeDirection;
import com.enderio.core.common.util.BlockCoord;
import com.enderio.core.common.util.Util;
@@ -18,6 +21,7 @@ import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import crazypants.enderio.Log;
import crazypants.enderio.api.teleport.IItemOfTravel;
+import crazypants.enderio.api.teleport.ITravelAccessable;
import crazypants.enderio.api.teleport.TeleportEntityEvent;
import crazypants.enderio.api.teleport.TravelSource;
import crazypants.enderio.teleport.ItemTeleportStaff;
@@ -30,13 +34,27 @@ public class PacketLongDistanceTravelEvent
boolean conserveMotion;
int entityId;
int source;
+ // Below values are for also attempting a regular blink if the long-distance anchor check fails.
+ boolean alsoDoTeleport;
+ int tpX;
+ int tpY;
+ int tpZ;
public PacketLongDistanceTravelEvent() {}
public PacketLongDistanceTravelEvent(Entity entity, boolean conserveMotion, TravelSource source) {
+ this(entity, conserveMotion, source, false, 0, 0, 0);
+ }
+
+ public PacketLongDistanceTravelEvent(Entity entity, boolean conserveMotion, TravelSource source,
+ boolean alsoDoTeleport, int tpX, int tpY, int tpZ) {
this.conserveMotion = conserveMotion;
this.entityId = entity instanceof EntityPlayer ? -1 : entity.getEntityId();
this.source = source.ordinal();
+ this.alsoDoTeleport = alsoDoTeleport;
+ this.tpX = tpX;
+ this.tpY = tpY;
+ this.tpZ = tpZ;
}
@Override
@@ -44,6 +62,10 @@ public class PacketLongDistanceTravelEvent
buf.writeBoolean(conserveMotion);
buf.writeInt(entityId);
buf.writeInt(source);
+ buf.writeBoolean(alsoDoTeleport);
+ buf.writeInt(tpX);
+ buf.writeInt(tpY);
+ buf.writeInt(tpZ);
}
@Override
@@ -51,6 +73,10 @@ public class PacketLongDistanceTravelEvent
conserveMotion = buf.readBoolean();
entityId = buf.readInt();
source = buf.readInt();
+ alsoDoTeleport = buf.readBoolean();
+ tpX = buf.readInt();
+ tpY = buf.readInt();
+ tpZ = buf.readInt();
}
@Override
@@ -69,7 +95,14 @@ public class PacketLongDistanceTravelEvent
TravelSource source = TravelSource.values()[message.source];
- if (!validate(toTp, source)) {
+ if (!validate(
+ toTp,
+ source,
+ message.conserveMotion,
+ message.alsoDoTeleport,
+ message.tpX,
+ message.tpY,
+ message.tpZ)) {
Log.LOGGER.warn(
Log.securityMarker,
"Player {} tried to tp without valid prereq.",
@@ -77,35 +110,82 @@ public class PacketLongDistanceTravelEvent
return null;
}
- doServerTeleport(toTp, message.conserveMotion, source);
+ doServerTeleport(
+ toTp,
+ message.conserveMotion,
+ source,
+ message.alsoDoTeleport,
+ message.tpX,
+ message.tpY,
+ message.tpZ);
return null;
}
- private static boolean validate(EntityPlayerMP toTp, TravelSource source) {
+ private static boolean validate(EntityPlayerMP toTp, TravelSource source, boolean conserveMotion,
+ boolean alsoDoTeleport, int tpX, int tpY, int tpZ) {
ItemStack equippedItem = toTp.getCurrentEquippedItem();
switch (source) {
case STAFF:
+ // Not allowed to do teleport staff teleport with regular staff.
+ if (alsoDoTeleport) {
+ return false;
+ }
return equippedItem != null && equippedItem.getItem() instanceof IItemOfTravel
&& ((IItemOfTravel) equippedItem.getItem()).isActive(toTp, equippedItem);
case TELEPORT_STAFF:
// tp staff is creative version of traveling staff
// no energy check or anything else needed
// but the player must actually be equipped with one of these
+ if (alsoDoTeleport) {
+ String error = TravelController.instance
+ .validatePacketTravelEvent(toTp, tpX, tpY, tpZ, 0, conserveMotion, source);
+ if (error != null) {
+ return false;
+ }
+ }
return equippedItem != null && equippedItem.getItem() instanceof ItemTeleportStaff;
+
default:
// all other types are not allowed
return false;
}
}
- public static boolean doServerTeleport(Entity toTp, boolean conserveMotion, TravelSource source) {
- EntityPlayer player = toTp instanceof EntityPlayer ? (EntityPlayer) toTp : null;
- Optional travelDestination = TravelController.instance.findTravelDestination(player, source);
- if (!travelDestination.isPresent()) {
+ public static boolean doServerTeleport(Entity toTp, boolean conserveMotion, TravelSource source,
+ boolean alsoDoTeleport, int tpX, int tpY, int tpZ) {
+ if (!(toTp instanceof EntityPlayer)) {
return false;
}
- BlockCoord destination = travelDestination.get();
+ EntityPlayer player = (EntityPlayer) toTp;
+
+ Optional travelDestination = TravelController.instance.findTravelDestination(player, source);
+ if (!travelDestination.isPresent()) {
+ if (alsoDoTeleport) {
+ return PacketTravelEvent.doServerTeleport(toTp, tpX, tpY, tpZ, 0, conserveMotion, source);
+ }
+ return false;
+ }
+
+ BlockCoord targetBlock = travelDestination.get();
+ TileEntity te = player.worldObj.getTileEntity(targetBlock.x, targetBlock.y, targetBlock.z);
+ if (te instanceof ITravelAccessable) {
+ ITravelAccessable ta = (ITravelAccessable) te;
+ if (!ta.canBlockBeAccessed(player)) {
+ TravelController
+ .showMessage(player, new ChatComponentTranslation("enderio.gui.travelAccessable.unauthorised"));
+ return false;
+ }
+ }
+
+ // We're teleporting to a block, so go one above.
+ BlockCoord destination = targetBlock.getLocation(ForgeDirection.UP);
+ if (!TravelController.instance.isValidTarget(player, destination, source)) {
+ TravelController
+ .showMessage(player, new ChatComponentTranslation("enderio.blockTravelPlatform.invalidTarget"));
+ return false;
+ }
+
int x = destination.x;
int y = destination.y;
int z = destination.z;
@@ -113,7 +193,7 @@ public class PacketLongDistanceTravelEvent
if (powerUse < 0) {
return false;
}
- if (player != null && player.getCurrentEquippedItem() != null
+ if (player.getCurrentEquippedItem() != null
&& player.getCurrentEquippedItem().getItem() instanceof IItemOfTravel) {
int used = ((IItemOfTravel) player.getCurrentEquippedItem().getItem())
.canExtractInternal(player.getCurrentEquippedItem(), powerUse);
@@ -134,32 +214,26 @@ public class PacketLongDistanceTravelEvent
toTp.playSound(source.sound, 1.0F, 1.0F);
- if (player != null) {
- player.setPositionAndUpdate(x + 0.5, y + 1.1, z + 0.5);
- } else {
- toTp.setPosition(x, y, z);
+ player.setPositionAndUpdate(x + 0.5, y + 0.1, z + 0.5);
+
+ player.worldObj.playSoundEffect(x, y, z, source.sound, 1.0F, 1.0F);
+ player.fallDistance = 0;
+
+ if (conserveMotion) {
+ Vector3d velocityVex = Util.getLookVecEio(player);
+ S12PacketEntityVelocity p = new S12PacketEntityVelocity(
+ toTp.getEntityId(),
+ velocityVex.x,
+ velocityVex.y,
+ velocityVex.z);
+ ((EntityPlayerMP) player).playerNetServerHandler.sendPacket(p);
}
- toTp.worldObj.playSoundEffect(x, y, z, source.sound, 1.0F, 1.0F);
- toTp.fallDistance = 0;
-
- if (player != null) {
- if (conserveMotion) {
- Vector3d velocityVex = Util.getLookVecEio(player);
- S12PacketEntityVelocity p = new S12PacketEntityVelocity(
- toTp.getEntityId(),
- velocityVex.x,
- velocityVex.y,
- velocityVex.z);
- ((EntityPlayerMP) player).playerNetServerHandler.sendPacket(p);
- }
-
- if (powerUse > 0 && player.getCurrentEquippedItem() != null
- && player.getCurrentEquippedItem().getItem() instanceof IItemOfTravel) {
- ItemStack item = player.getCurrentEquippedItem().copy();
- ((IItemOfTravel) item.getItem()).extractInternal(item, powerUse);
- toTp.setCurrentItemOrArmor(0, item);
- }
+ if (powerUse > 0 && player.getCurrentEquippedItem() != null
+ && player.getCurrentEquippedItem().getItem() instanceof IItemOfTravel) {
+ ItemStack item = player.getCurrentEquippedItem().copy();
+ ((IItemOfTravel) item.getItem()).extractInternal(item, powerUse);
+ toTp.setCurrentItemOrArmor(0, item);
}
return true;
diff --git a/src/main/java/crazypants/enderio/teleport/packet/PacketTravelEvent.java b/src/main/java/crazypants/enderio/teleport/packet/PacketTravelEvent.java
index cb31e488..82230b17 100644
--- a/src/main/java/crazypants/enderio/teleport/packet/PacketTravelEvent.java
+++ b/src/main/java/crazypants/enderio/teleport/packet/PacketTravelEvent.java
@@ -118,7 +118,7 @@ public class PacketTravelEvent implements IMessage, IMessageHandler