Gets rid of the rest of the modX and modY usages, and removes some unused code

This commit is contained in:
2021-10-08 18:23:42 +02:00
parent a68dc4b464
commit fff4d8d78b
20 changed files with 150 additions and 279 deletions

View File

@ -26,8 +26,8 @@ public class Gate {
private final Map<Character, Material> types;
//Gate materials
private Material portalOpenBlock;
private Material portalClosedBlock;
private final Material portalOpenBlock;
private final Material portalClosedBlock;
private final Material portalButton;
// Economy information
@ -74,15 +74,6 @@ public class Gate {
return layout;
}
/**
* Gets the material types each layout character represents
*
* @return <p>The material types each layout character represents</p>
*/
public Map<Character, Material> getTypes() {
return types;
}
/**
* Gets the material type used for this gate's control blocks
*
@ -110,15 +101,6 @@ public class Gate {
return portalOpenBlock;
}
/**
* Sets the block to use for the opening when a portal using this gate is open
*
* @param type <p>The block type to use for the opening when open</p>
*/
public void setPortalOpenBlock(Material type) {
portalOpenBlock = type;
}
/**
* Gets the block type to use for the opening when a portal using this gate is closed
*
@ -128,15 +110,6 @@ public class Gate {
return portalClosedBlock;
}
/**
* Sets the block type to use for the opening when a portal using this gate is closed
*
* @param type <p>The block type to use for the opening when closed</p>
*/
public void setPortalClosedBlock(Material type) {
portalClosedBlock = type;
}
/**
* Gets the material to use for a portal's button if using this gate type
*
@ -186,36 +159,33 @@ public class Gate {
* Checks whether a portal's gate matches this gate type
*
* @param topLeft <p>The top-left block of the portal's gate</p>
* @param modX <p>The x modifier used</p>
* @param modZ <p>The z modifier used</p>
* @param yaw <p>The yaw when looking directly outwards</p>
* @return <p>True if this gate matches the portal</p>
*/
public boolean matches(BlockLocation topLeft, int modX, int modZ) {
return matches(topLeft, modX, modZ, false);
public boolean matches(BlockLocation topLeft, double yaw) {
return matches(topLeft, yaw, false);
}
/**
* Checks whether a portal's gate matches this gate type
*
* @param topLeft <p>The top-left block of the portal's gate</p>
* @param modX <p>The x modifier used</p>
* @param modZ <p>The z modifier used</p>
* @param yaw <p>The yaw when looking directly outwards</p>
* @param onCreate <p>Whether this is used in the context of creating a new gate</p>
* @return <p>True if this gate matches the portal</p>
*/
public boolean matches(BlockLocation topLeft, int modX, int modZ, boolean onCreate) {
return verifyGateEntrancesMatch(topLeft, modX, modZ, onCreate) && verifyGateBorderMatches(topLeft, modX, modZ);
public boolean matches(BlockLocation topLeft, double yaw, boolean onCreate) {
return verifyGateEntrancesMatch(topLeft, yaw, onCreate) && verifyGateBorderMatches(topLeft, yaw);
}
/**
* Verifies that all border blocks of a portal gate matches this gate type
*
* @param topLeft <p>The top-left block of the portal</p>
* @param modX <p>The x modifier used</p>
* @param modZ <p>The z modifier used</p>
* @param yaw <p>The yaw when looking directly outwards</p>
* @return <p>True if all border blocks of the gate match the layout</p>
*/
private boolean verifyGateBorderMatches(BlockLocation topLeft, int modX, int modZ) {
private boolean verifyGateBorderMatches(BlockLocation topLeft, double yaw) {
Map<Character, Material> portalTypes = new HashMap<>(types);
for (RelativeBlockVector borderVector : layout.getBorder()) {
int rowIndex = borderVector.getRight();
@ -223,7 +193,7 @@ public class Gate {
Character key = layout.getLayout()[lineIndex][rowIndex];
Material materialInLayout = portalTypes.get(key);
Material materialAtLocation = getBlockAt(topLeft, borderVector, modX, modZ).getType();
Material materialAtLocation = getBlockAt(topLeft, borderVector, yaw).getType();
if (materialInLayout == null) {
portalTypes.put(key, materialAtLocation);
} else if (materialAtLocation != materialInLayout) {
@ -239,19 +209,20 @@ public class Gate {
* Verifies that all entrances of a portal gate matches this gate type
*
* @param topLeft <p>The top-left block of this portal</p>
* @param modX <p>The x modifier used</p>
* @param modZ <p>The z modifier used</p>
* @param yaw <p>The yaw when looking directly outwards</p>
* @param onCreate <p>Whether this is used in the context of creating a new gate</p>
* @return <p>Whether this is used in the context of creating a new gate</p>
*/
private boolean verifyGateEntrancesMatch(BlockLocation topLeft, int modX, int modZ, boolean onCreate) {
private boolean verifyGateEntrancesMatch(BlockLocation topLeft, double yaw, boolean onCreate) {
if (Stargate.ignoreEntrance) {
return true;
}
Stargate.debug("verifyGateEntrancesMatch", String.valueOf(topLeft));
for (RelativeBlockVector entranceVector : layout.getEntrances()) {
Material type = getBlockAt(topLeft, entranceVector, modX, modZ).getType();
Stargate.debug("verifyGateEntrancesMatch", String.valueOf(entranceVector));
Material type = getBlockAt(topLeft, entranceVector, yaw).getType();
// Ignore entrance if it's air and we're creating a new gate
//Ignore entrance if it's air, and we're creating a new gate
if (onCreate && type == Material.AIR) {
continue;
}
@ -270,8 +241,8 @@ public class Gate {
* @param vector <p>The relative block vector</p>
* @return <p>The block at the given relative position</p>
*/
private BlockLocation getBlockAt(BlockLocation topLeft, RelativeBlockVector vector, int modX, int modZ) {
return DirectionHelper.getBlockAt(topLeft, vector, modX, modZ);
private BlockLocation getBlockAt(BlockLocation topLeft, RelativeBlockVector vector, double yaw) {
return DirectionHelper.getBlockAt(topLeft, vector, yaw);
}
/**
@ -371,6 +342,7 @@ public class Gate {
* @param value <p>The value of the config key</p>
* @throws IOException <p>If unable to write to the buffered writer</p>
*/
@SuppressWarnings("SameParameterValue")
private void writeConfig(BufferedWriter bufferedWriter, String key, boolean value) throws IOException {
writeConfig(bufferedWriter, "%s=%b", key, value);
}

View File

@ -378,7 +378,7 @@ public class GateHandler {
}
if (files == null || files.length == 0) {
//The gates folder was not found. Assume this is the first run
//The gates-folder was not found. Assume this is the first run
if (directory.mkdir()) {
populateDefaults(gateFolder);
}

View File

@ -78,30 +78,24 @@ public class Portal {
* @param button <p>The location of the portal's open button</p>
* @param destination <p>The destination defined on the sign's destination line</p>
* @param name <p>The name of the portal defined on the sign's first line</p>
* @param verified <p>Whether the portal's gate has been verified to match its template</p>
* @param network <p>The network the portal belongs to, defined on the sign's network line</p>
* @param gate <p>The gate template this portal uses</p>
* @param ownerUUID <p>The UUID of the gate's owner</p>
* @param ownerName <p>The name of the gate's owner</p>
* @param options <p>A map containing all possible portal options</p>
*/
Portal(PortalLocation portalLocation, BlockLocation button,
String destination, String name, boolean verified, String network, Gate gate, UUID ownerUUID,
String ownerName, Map<PortalOption, Boolean> options) {
Portal(PortalLocation portalLocation, BlockLocation button, String destination, String name, String network,
Gate gate, UUID ownerUUID, String ownerName, Map<PortalOption, Boolean> options) {
this.location = portalLocation;
this.destination = destination;
this.button = button;
this.verified = verified;
this.verified = false;
this.network = network;
this.name = name;
this.gate = gate;
this.ownerUUID = ownerUUID;
this.ownerName = ownerName;
this.options = new PortalOptions(options, destination.length() > 0);
if (verified) {
this.drawSign();
}
}
/**
@ -145,6 +139,7 @@ public class Portal {
*
* @param network <p>The new network for this gate</p>
*/
@SuppressWarnings("unused")
public void setNetwork(String network) {
this.network = network;
}
@ -172,6 +167,7 @@ public class Portal {
*
* @param name <p>The new name of this portal</p>
*/
@SuppressWarnings("unused")
public void setName(String name) {
this.name = filterName(name);
drawSign();
@ -275,6 +271,7 @@ public class Portal {
*
* @param owner <p>The new UUID of this portal's owner</p>
*/
@SuppressWarnings("unused")
public void setOwner(UUID owner) {
this.ownerUUID = owner;
}
@ -348,24 +345,22 @@ public class Portal {
* Open this portal
*
* @param force <p>Whether to force this portal open, even if it's already open for some player</p>
* @return <p>True if the portal was opened</p>
*/
public boolean open(boolean force) {
return open(null, force);
public void open(boolean force) {
open(null, force);
}
/**
* Open this portal
*
* @param force <p>Whether to force this portal open, even if it's already open for some player</p>
* @return <p>True if the portal was opened</p>
*/
public boolean open(Player openFor, boolean force) {
public void open(Player openFor, boolean force) {
//Call the StargateOpenEvent
StargateOpenEvent event = new StargateOpenEvent(openFor, this, force);
Stargate.server.getPluginManager().callEvent(event);
if (event.isCancelled() || (isOpen() && !event.getForce())) {
return false;
return;
}
//Change the opening blocks to the correct type
@ -376,7 +371,6 @@ public class Portal {
}
updatePortalOpenState(openFor);
return true;
}
/**
@ -401,7 +395,9 @@ public class Portal {
destination.getDestinationName().equalsIgnoreCase(getName())) && !destination.isOpen()) {
destination.open(openFor, false);
destination.setDestination(this);
if (destination.isVerified()) destination.drawSign();
if (destination.isVerified()) {
destination.drawSign();
}
}
}
}
@ -636,9 +632,11 @@ public class Portal {
RelativeBlockVector relativeExit = gate.getLayout().getExit();
if (relativeExit != null) {
BlockLocation exit = getBlockAt(relativeExit);
int back = (options.isBackwards()) ? -1 : 1;
exitLocation = exit.modRelativeLoc(0D, 0D, 1, traveller.getYaw(),
traveller.getPitch(), getModX() * back, 1, getModZ() * back);
float yaw = traveller.getYaw();
if (options.isBackwards()) {
yaw += 180;
}
exitLocation = exit.getRelativeLocation(0D, 0D, 1, this.getYaw(), yaw);
if (entity != null) {
double entitySize = EntityHelper.getEntityMaxSize(entity);
@ -791,24 +789,6 @@ public class Portal {
return this.location.getSignLocation();
}
/**
* Gets the x modifier used by this portal
*
* @return <p>The x modifier used by this portal</p>
*/
public int getModX() {
return this.location.getModX();
}
/**
* Gets the z modifier used by this portal
*
* @return <p>The z modifier used by this portal</p>
*/
public int getModZ() {
return this.location.getModZ();
}
/**
* Gets the rotation of this portal
*
@ -864,7 +844,7 @@ public class Portal {
if (!Stargate.verifyPortals) {
return true;
}
return gate.matches(getTopLeft(), getModX(), getModZ());
return gate.matches(getTopLeft(), getYaw());
}
/**
@ -1014,7 +994,7 @@ public class Portal {
* @return <p>The block at the given relative position</p>
*/
public BlockLocation getBlockAt(RelativeBlockVector vector) {
return DirectionHelper.getBlockAt(getTopLeft(), vector, getModX(), getModZ());
return DirectionHelper.getBlockAt(getTopLeft(), vector, getYaw());
}
/**

View File

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

View File

@ -20,7 +20,6 @@ import org.bukkit.block.data.Directional;
import org.bukkit.block.data.type.WallSign;
import org.bukkit.entity.Player;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.util.Vector;
import java.io.BufferedWriter;
import java.io.File;
@ -245,6 +244,7 @@ public class PortalHandler {
//Return early if the sign is not placed on a block, or the block is not a control block
if (idParent == null || GateHandler.getGatesByControlBlock(idParent).length == 0) {
Stargate.debug("createPortal", "Control block not registered");
return null;
}
@ -269,14 +269,10 @@ public class PortalHandler {
//Get the direction the button should be facing
BlockFace buttonFacing = DirectionHelper.getBlockFaceFromYaw(yaw);
//Get the x and z modifiers
Vector direction = DirectionHelper.getDirectionVectorFromYaw(yaw);
//TODO: Figure out how modX and modZ really works and simplify it
int modX = -direction.getBlockZ();
int modZ = direction.getBlockX();
PortalLocation portalLocation = new PortalLocation();
portalLocation.setButtonFacing(buttonFacing).setYaw(yaw).setModX(modX).setModZ(modZ).setSignLocation(signLocation);
portalLocation.setButtonFacing(buttonFacing).setYaw(yaw).setSignLocation(signLocation);
Stargate.debug("createPortal", "Finished getting all portal info");
//Try and find a gate matching the new portal
Gate gate = findMatchingGate(portalLocation, player);
@ -287,6 +283,7 @@ public class PortalHandler {
//If the portal is a bungee portal and invalid, abort here
if (!isValidBungeePortal(portalOptions, player, destinationName, network)) {
Stargate.debug("createPortal", "Portal is an invalid bungee portal");
return null;
}
@ -344,7 +341,7 @@ public class PortalHandler {
}
//Check if a conflict exists
if (conflictsWithExistingPortal(gate, portalLocation.getTopLeft(), modX, modZ, player)) {
if (conflictsWithExistingPortal(gate, portalLocation.getTopLeft(), yaw, player)) {
return null;
}
@ -392,8 +389,7 @@ public class PortalHandler {
//Get all gates with the used type of control blocks
Gate[] possibleGates = GateHandler.getGatesByControlBlock(signParent);
int modX = portalLocation.getModX();
int modZ = portalLocation.getModZ();
double yaw = portalLocation.getYaw();
Gate gate = null;
for (Gate possibleGate : possibleGates) {
@ -403,9 +399,8 @@ public class PortalHandler {
portalLocation.setButtonVector(null);
for (RelativeBlockVector controlVector : vectors) {
//Assuming the top-left location is pointing to the gate's top-left location, check if it's a match
BlockLocation possibleTopLocation = parent.modRelative(-controlVector.getRight(),
-controlVector.getDepth(), -controlVector.getDistance(), modX, 1, modZ);
if (possibleGate.matches(possibleTopLocation, modX, modZ, true)) {
BlockLocation possibleTopLocation = parent.getRelativeLocation(controlVector.invert(), yaw);
if (possibleGate.matches(possibleTopLocation, portalLocation.getYaw(), true)) {
gate = possibleGate;
portalLocation.setTopLeft(possibleTopLocation);
} else {
@ -422,19 +417,16 @@ public class PortalHandler {
*
* @param gate <p>The gate type of the new portal</p>
* @param topLeft <p>The top-left block of the new portal</p>
* @param modX <p>The x-modifier for the new portal</p>
* @param modZ <p>The new z-modifier for the new portal</p>
* @param yaw <p>The yaw when looking directly outwards from the portal</p>
* @param player <p>The player creating the new portal</p>
* @return <p>True if a conflict was found. False otherwise</p>
*/
private static boolean conflictsWithExistingPortal(Gate gate, BlockLocation topLeft, int modX, int modZ,
Player player) {
private static boolean conflictsWithExistingPortal(Gate gate, BlockLocation topLeft, double yaw, Player player) {
//TODO: Make a quicker check. Could just check for control block conflicts if all code is changed to account for
// getting several hits at a single location when checking for the existence of a portal. May make
// everything slower overall? Would make for cooler gates though.
for (RelativeBlockVector borderVector : gate.getLayout().getBorder()) {
BlockLocation borderBlockLocation = topLeft.modRelative(borderVector.getRight(), borderVector.getDepth(),
borderVector.getDistance(), modX, 1, modZ);
BlockLocation borderBlockLocation = topLeft.getRelativeLocation(borderVector, yaw);
if (getByBlock(borderBlockLocation.getBlock()) != null) {
Stargate.debug("createPortal", "Gate conflicts with existing gate");
Stargate.sendErrorMessage(player, Stargate.getString("createConflict"));
@ -463,7 +455,7 @@ public class PortalHandler {
Map<PortalOption, Boolean> portalOptions, String denyMessage,
String[] lines, boolean deny) {
Portal portal = new Portal(portalLocation, null, destinationName, portalName,
false, network, gate, player.getUniqueId(), player.getName(), portalOptions);
network, gate, player.getUniqueId(), player.getName(), portalOptions);
int createCost = EconomyHandler.getCreateCost(player, gate);
@ -569,8 +561,10 @@ public class PortalHandler {
*/
private static void generatePortalButton(Portal portal, BlockLocation topLeft, RelativeBlockVector buttonVector,
BlockFace buttonFacing) {
BlockLocation button = topLeft.modRelative(buttonVector.getRight(), buttonVector.getDepth(),
buttonVector.getDistance() + 1, portal.getModX(), 1, portal.getModZ());
//Go one block outwards to find the button's location rather than the control block's location
BlockLocation button = topLeft.getRelativeLocation(buttonVector.addToVector(
RelativeBlockVector.Property.DISTANCE, 1), portal.getYaw());
Directional buttonData = (Directional) Bukkit.createBlockData(portal.getGate().getPortalButton());
buttonData.setFacing(buttonFacing);
button.getBlock().setBlockData(buttonData);
@ -724,15 +718,15 @@ public class PortalHandler {
adjacentPositions.add(centerLocation);
for (int index = 1; index <= range; index++) {
adjacentPositions.add(centerLocation.makeRelative(index, 0, 0));
adjacentPositions.add(centerLocation.makeRelative(-index, 0, 0));
adjacentPositions.add(centerLocation.makeRelative(0, 0, index));
adjacentPositions.add(centerLocation.makeRelative(0, 0, -index));
adjacentPositions.add(centerLocation.makeRelativeBlockLocation(index, 0, 0));
adjacentPositions.add(centerLocation.makeRelativeBlockLocation(-index, 0, 0));
adjacentPositions.add(centerLocation.makeRelativeBlockLocation(0, 0, index));
adjacentPositions.add(centerLocation.makeRelativeBlockLocation(0, 0, -index));
if (index < range) {
adjacentPositions.add(centerLocation.makeRelative(index, 0, index));
adjacentPositions.add(centerLocation.makeRelative(-index, 0, -index));
adjacentPositions.add(centerLocation.makeRelative(index, 0, -index));
adjacentPositions.add(centerLocation.makeRelative(-index, 0, index));
adjacentPositions.add(centerLocation.makeRelativeBlockLocation(index, 0, index));
adjacentPositions.add(centerLocation.makeRelativeBlockLocation(-index, 0, -index));
adjacentPositions.add(centerLocation.makeRelativeBlockLocation(index, 0, -index));
adjacentPositions.add(centerLocation.makeRelativeBlockLocation(-index, 0, index));
}
}
@ -796,8 +790,8 @@ public class PortalHandler {
builder.append(portal.getName()).append(':');
builder.append(portal.getSignLocation().toString()).append(':');
builder.append((button != null) ? button.toString() : "").append(':');
builder.append(portal.getModX()).append(':');
builder.append(portal.getModZ()).append(':');
builder.append(0).append(':');
builder.append(0).append(':');
builder.append(portal.getYaw()).append(':');
builder.append(portal.getTopLeft().toString()).append(':');
builder.append(portal.getGate().getFilename()).append(':');
@ -958,8 +952,6 @@ public class PortalHandler {
PortalLocation portalLocation = new PortalLocation();
portalLocation.setSignLocation(new BlockLocation(world, portalData[1]));
BlockLocation button = (portalData[2].length() > 0) ? new BlockLocation(world, portalData[2]) : null;
portalLocation.setModX(Integer.parseInt(portalData[3]));
portalLocation.setModZ(Integer.parseInt(portalData[4]));
portalLocation.setYaw(Float.parseFloat(portalData[5]));
portalLocation.setTopLeft(new BlockLocation(world, portalData[6]));
Gate gate = GateHandler.getGateByName(portalData[7]);
@ -995,7 +987,7 @@ public class PortalHandler {
}
//Creates the new portal
Portal portal = new Portal(portalLocation, button, destination, name, false,
Portal portal = new Portal(portalLocation, button, destination, name,
network, gate, ownerUUID, ownerName, getPortalOptions(portalData));
registerPortal(portal);

View File

@ -9,11 +9,10 @@ import org.bukkit.block.BlockFace;
/**
* Keeps track of location related data for a portal
*/
@SuppressWarnings("UnusedReturnValue")
public class PortalLocation {
private BlockLocation topLeft;
private int modX;
private int modZ;
private float yaw;
private BlockLocation signLocation;
private RelativeBlockVector buttonVector;
@ -28,24 +27,6 @@ public class PortalLocation {
return topLeft;
}
/**
* Gets the x-modifier for the portal
*
* @return <p>The x-modifier for the portal</p>
*/
public int getModX() {
return modX;
}
/**
* Gets the z-modifier for the portal
*
* @return <p>The z-modifier for the portal</p>
*/
public int getModZ() {
return modZ;
}
/**
* Gets the yaw for looking outwards from the portal
*
@ -115,28 +96,6 @@ public class PortalLocation {
return this;
}
/**
* Sets the portal's x-modifier
*
* @param modX <p>The portal's new x-modifier</p>
* @return <p>The portal location Object</p>
*/
public PortalLocation setModX(int modX) {
this.modX = modX;
return this;
}
/**
* Sets the portal's z-modifier
*
* @param modZ <p>The portal's new z-modifier</p>
* @return <p>The portal location Object</p>
*/
public PortalLocation setModZ(int modZ) {
this.modZ = modZ;
return this;
}
/**
* Sets the portal's yaw
*