Adds a command for getting current launchpad information

This commit is contained in:
Kristian Knarvik 2024-04-06 17:35:37 +02:00
parent 762f8e14dc
commit 1a9ad558ec
10 changed files with 124 additions and 15 deletions

View File

@ -21,6 +21,7 @@ If you alter several launchpad values in succession, they'll all be applied to t
| Command | Arguments | Description |
|-------------------------------|-----------------------------------------------------|----------------------------------------------------------------------------------------------------------|
| /launchpad check | | Checks whether the block the player is currently looking at is a launchpad, and prints info about it. |
| /launchpad add | | Makes the clicked block into a launchpad. |
| /launchpad remove | | Removes the clicked block as a launchpad. |
| /launchpad abort | | Clears any unprocessed launchpad modifications. |

View File

@ -6,7 +6,7 @@
<groupId>net.knarcraft</groupId>
<artifactId>Launchpad</artifactId>
<version>1.0</version>
<version>1.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Launchpad</name>
@ -97,7 +97,7 @@
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId>
<version>1.2.2</version>
<version>1.2.5</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -3,9 +3,12 @@ package net.knarcraft.launchpad.command;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.config.LaunchpadMessage;
import net.knarcraft.launchpad.launchpad.LaunchpadBlock;
import net.knarcraft.launchpad.launchpad.LaunchpadBlockHandler;
import net.knarcraft.launchpad.launchpad.ModificationAction;
import net.knarcraft.launchpad.launchpad.ModificationRequest;
import net.knarcraft.launchpad.launchpad.ModificationRequestHandler;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -58,6 +61,9 @@ public class LaunchpadCommand implements CommandExecutor {
ModificationRequestHandler.addRequest(player.getUniqueId(), new ModificationRequest(
ModificationAction.VERTICAL_VELOCITY, nullArgument(arguments[2])));
}
case CHECK -> {
return checkLaunchpad(player, stringFormatter);
}
}
stringFormatter.displaySuccessMessage(commandSender, LaunchpadMessage.SUCCESS_CLICK_BLOCK);
@ -90,4 +96,30 @@ public class LaunchpadCommand implements CommandExecutor {
return argument.equalsIgnoreCase("null") ? null : argument;
}
/**
* Checks which launchpad is looked at, and prints information about it
*
* @param player <p>The player trying to check information about a launchpad</p>
* @param stringFormatter <p>The string formatter to use for output</p>
* @return <p>Whether the player used the command correctly</p>
*/
private boolean checkLaunchpad(@NotNull Player player, @NotNull StringFormatter stringFormatter) {
Block targetBlock = player.getTargetBlockExact(7);
if (targetBlock == null) {
stringFormatter.displaySuccessMessage(player, LaunchpadMessage.ERROR_NOT_LOOKING_AT_BLOCK);
return false;
}
LaunchpadBlock launchpadBlock = LaunchpadBlockHandler.getLaunchpadBlock(targetBlock);
if (launchpadBlock != null) {
stringFormatter.displaySuccessMessage(player, launchpadBlock.toString());
} else {
if (!Launchpad.getInstance().getConfiguration().isNotLaunchpadMaterial(targetBlock.getType())) {
stringFormatter.displaySuccessMessage(player, new LaunchpadBlock(targetBlock, true).toString());
} else {
stringFormatter.displaySuccessMessage(player, LaunchpadMessage.ERROR_NO_LAUNCHPAD);
}
}
return true;
}
}

View File

@ -44,6 +44,26 @@ public enum LaunchpadMessage implements TranslatableMessage {
* The message to display when a launchpad has been added or modified
*/
SUCCESS_MODIFIED_LAUNCHPAD,
/**
* The message to display when the player is not looking at a block, which is required
*/
ERROR_NOT_LOOKING_AT_BLOCK,
/**
* The message to display when the player is not looking at a launchpad
*/
ERROR_NO_LAUNCHPAD,
/**
* The format to use for printing launchpad information
*/
SUCCESS_LAUNCHPAD_INFO,
/**
* The suffix to add when a launchpad's value is inherited
*/
SUCCESS_LAUNCHPAD_INFO_INHERITED,
;
@Override

View File

@ -1,11 +1,16 @@
package net.knarcraft.launchpad.launchpad;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.config.LaunchpadMessage;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/**
* A block registered as a launchpad
*/
@ -15,17 +20,20 @@ public class LaunchpadBlock {
private double horizontalVelocity;
private double verticalVelocity;
private @Nullable BlockFace fixedDirection;
private final boolean isImplicit;
/**
* Instantiates a new launchpad block
*
* @param block <p>The block to register this launchpad block for</p>
* @param block <p>The block to register this launchpad block for</p>
* @param isImplicit <p>Whether the block in question is implicitly a launchpad, or has been manually created</p>
*/
public LaunchpadBlock(@NotNull Block block) {
public LaunchpadBlock(@NotNull Block block, boolean isImplicit) {
this.block = block;
this.horizontalVelocity = -1;
this.verticalVelocity = -1;
this.fixedDirection = null;
this.isImplicit = isImplicit;
}
/**
@ -35,13 +43,15 @@ public class LaunchpadBlock {
* @param horizontalVelocity <p>The horizontal velocity of the launchpad</p>
* @param verticalVelocity <p>The vertical velocity of the launchpad</p>
* @param fixedDirection <p>The fixed direction of the launchpad</p>
* @param isImplicit <p>Whether the block in question is implicitly a launchpad, or has been manually created</p>
*/
public LaunchpadBlock(@NotNull Block block, double horizontalVelocity, double verticalVelocity,
@Nullable BlockFace fixedDirection) {
@Nullable BlockFace fixedDirection, boolean isImplicit) {
this.block = block;
setHorizontalVelocity(horizontalVelocity);
setVerticalVelocity(verticalVelocity);
setFixedDirection(fixedDirection);
this.isImplicit = isImplicit;
}
/**
@ -139,4 +149,36 @@ public class LaunchpadBlock {
return this.verticalVelocity;
}
@Override
public String toString() {
StringFormatter stringFormatter = Launchpad.getInstance().getStringFormatter();
String inheritedSuffix = stringFormatter.getUnformattedColoredMessage(
LaunchpadMessage.SUCCESS_LAUNCHPAD_INFO_INHERITED);
String directionString = fixedDirection != null ? fixedDirection.name() : "None";
Location location = block.getLocation();
String coordinates = location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ();
if (this.isImplicit) {
coordinates += inheritedSuffix;
}
String hVelocity;
if (this.horizontalVelocity > 0) {
hVelocity = String.valueOf(this.horizontalVelocity);
} else {
hVelocity = getHorizontalVelocity() + inheritedSuffix;
}
String vVelocity;
if (this.verticalVelocity > 0) {
vVelocity = String.valueOf(this.verticalVelocity);
} else {
vVelocity = getVerticalVelocity() + inheritedSuffix;
}
return stringFormatter.replacePlaceholders(LaunchpadMessage.SUCCESS_LAUNCHPAD_INFO,
List.of("{location}", "{horizontalVelocity}", "{verticalVelocity}", "{fixedDirection}"),
List.of(coordinates, hVelocity, vVelocity, directionString));
}
}

View File

@ -152,7 +152,7 @@ public final class LaunchpadBlockHandler {
fixedDirection = BlockFace.valueOf(configurationSection.getString(key + ".fixedDirection"));
}
launchpadBlocks.put(launchpadBlock, new LaunchpadBlock(launchpadBlock, horizontalVelocity, verticalVelocity,
fixedDirection));
fixedDirection, false));
}
}

View File

@ -43,7 +43,11 @@ public enum ModificationAction {
* The action of setting both velocities at once
*/
VELOCITIES("velocities", 2),
;
/**
* The action of checking launchpad information for the looked at block
*/
CHECK("check", 0);
private final @NotNull String commandName;
private final int neededArguments;

View File

@ -77,7 +77,7 @@ public class LaunchpadModifyListener implements Listener {
return false;
}
existingLaunchpad = new LaunchpadBlock(block);
existingLaunchpad = new LaunchpadBlock(block, false);
}
switch (request.modificationAction()) {

View File

@ -14,7 +14,7 @@ commands:
permission: launchpad.reload
launchpad:
usage: |
/<command> <add | remove | abort>
/<command> <check | add | remove | abort>
/<command> <verticalVelocity | horizontalVelocity | fixedDirection> <value>
/<command> velocities <horizontalVelocity> <verticalVelocity>
description: Used to alter launchpads

View File

@ -1,7 +1,17 @@
en:
ERROR_PLAYER_ONLY: "This command must be used by a player"
ERROR_NOT_WHITELISTED: "The block could not be modified, as it's not whitelisted. If you want to abort changing a launchpad, use \"/launchpad abort\""
SUCCESS_PLUGIN_RELOADED: "Plugin reloaded!"
SUCCESS_MODIFICATIONS_CLEARED: "Cleared your launchpad modification queue"
SUCCESS_CLICK_BLOCK: "Click the launchpad you want to create or modify"
SUCCESS_MODIFIED_LAUNCHPAD: "The clicked block's launchpad properties have been modified"
ERROR_PLAYER_ONLY: "&7This command must be used by a player"
ERROR_NOT_WHITELISTED: "&7The block could not be modified, as it's not whitelisted. If you want to abort changing a launchpad, use \"/launchpad abort\""
SUCCESS_PLUGIN_RELOADED: "&7Plugin reloaded!"
SUCCESS_MODIFICATIONS_CLEARED: "&7Cleared your launchpad modification queue"
SUCCESS_CLICK_BLOCK: "&7Click the launchpad you want to create or modify"
SUCCESS_MODIFIED_LAUNCHPAD: "&7The clicked block's launchpad properties have been modified"
ERROR_NOT_LOOKING_AT_BLOCK: "&7You are not currently looking at a block"
ERROR_NO_LAUNCHPAD: "&7The block you are looking at is not a launchpad"
SUCCESS_LAUNCHPAD_INFO: |
&f- &7Launchpad info &f-
&7Location: {location}
&7Horizontal Velocity: {horizontalVelocity}
&7Vertical Velocity: {verticalVelocity}
&7Fixed Direction: {fixedDirection}
SUCCESS_LAUNCHPAD_INFO_INHERITED: " &0[&fInherited&0]"