Adds a launchpad material whitelist #8

This commit is contained in:
Kristian Knarvik 2023-07-03 19:45:16 +02:00
parent 4dd92c21ab
commit bbfd22e046
6 changed files with 97 additions and 32 deletions

View File

@ -32,10 +32,11 @@ If you alter several launchpad values in succession, they'll all be applied to t
## Configuration
| Node | Type | Description |
|------------------------------------------------------------|----------------|--------------------------------------------------------------------------------------------------------------|
| launchpad.materials | List | A list of materials which are always treated as launchpads, without the need for manual registration. |
| launchpad.verticalVelocity | Decimal number | The vertical (upwards) velocity applied to launchpads if not specified otherwise. |
| launchpad.horizontalVelocity | Decimal number | The horizontal (sideways) velocity applied to launchpads if not specified otherwise. |
| launchpad.materialVelocities.<MATERIAL>.horizontalVelocity | Decimal number | The horizontal (sideways) velocity applied to launchpads of type <MATERIAL> if not overridden for the block. |
| launchpad.materialVelocities.<MATERIAL>.verticalVelocity | Decimal number | The vertical (sideways) velocity applied to launchpads of type <MATERIAL> if not overridden for the block. |
| Node | Type | Description |
|------------------------------------------------------------|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| launchpad.materials | List | A list of materials, or material tags (+TAG_NAME), which are always treated as launchpads, without the need for manual registration. |
| launchpad.materialWhitelist | List | A list of materials, or material tags (+TAG_NAME), which can be manually turned into launchpads. Use this to prevent unwanted blocks from being turned into launchpads. |
| launchpad.verticalVelocity | Decimal number | The vertical (upwards) velocity applied to launchpads if not specified otherwise. |
| launchpad.horizontalVelocity | Decimal number | The horizontal (sideways) velocity applied to launchpads if not specified otherwise. |
| launchpad.materialVelocities.<MATERIAL>.horizontalVelocity | Decimal number | The horizontal (sideways) velocity applied to launchpads of type <MATERIAL> if not overridden for the block. |
| launchpad.materialVelocities.<MATERIAL>.verticalVelocity | Decimal number | The vertical (sideways) velocity applied to launchpads of type <MATERIAL> if not overridden for the block. |

24
pom.xml
View File

@ -12,7 +12,7 @@
<name>Launchpad</name>
<properties>
<java.version>1.8</java.version>
<java.version>16</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@ -23,8 +23,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>16</source>
<target>16</target>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
@ -39,6 +39,20 @@
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>net.knarcraft:knarlib</artifact>
<includes>
<include>net/knarcraft/knarlib/**</include>
</includes>
</filter>
<filter>
<excludes>
<exclude>*.MF</exclude>
<exclude>*.yml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
@ -78,11 +92,11 @@
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.1</version>
<scope>compile</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>KnarLib</artifactId>
<artifactId>knarlib</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>

View File

@ -19,8 +19,6 @@ public class LaunchpadCommand implements CommandExecutor {
return false;
}
// TODO: Properly allow nulling (unsetting) values
if (arguments.length < 1) {
return false;
}

View File

@ -21,7 +21,8 @@ public class LaunchpadConfiguration {
private @NotNull FileConfiguration fileConfiguration;
private double horizontalVelocity;
private double verticalVelocity;
private final @NotNull Set<Material> launchpadMaterials = new HashSet<>();
private @NotNull Set<Material> launchpadMaterials = new HashSet<>();
private @NotNull Set<Material> materialWhitelist = new HashSet<>();
/**
* Instantiate a new launch pad configuration
@ -46,15 +47,9 @@ public class LaunchpadConfiguration {
"The \"launchpad\" configuration section is missing.");
return;
}
this.launchpadMaterials.clear();
List<?> materials = launchpadSection.getList("materials");
if (materials != null) {
this.launchpadMaterials.addAll(MaterialHelper.loadMaterialList(materials));
} else {
this.launchpadMaterials.add(Material.LIGHT_WEIGHTED_PRESSURE_PLATE);
}
// If a non-block material is specified, simply ignore it
this.launchpadMaterials.removeIf((item) -> !item.isBlock());
this.launchpadMaterials = loadMaterials(launchpadSection, "materials");
this.materialWhitelist = loadMaterials(launchpadSection, "materialWhitelist");
this.horizontalVelocity = launchpadSection.getDouble("horizontalVelocity");
this.verticalVelocity = launchpadSection.getDouble("verticalVelocity");
@ -73,6 +68,16 @@ public class LaunchpadConfiguration {
return !this.launchpadMaterials.contains(material);
}
/**
* Checks whether the given material is whitelisted
*
* @param material <p>The material to check</p>
* @return <p>True if the material is whitelisted</p>
*/
public boolean isMaterialWhitelisted(@NotNull Material material) {
return this.materialWhitelist.isEmpty() || this.materialWhitelist.contains(material);
}
/**
* Gets the default horizontal velocity for launchpads
*
@ -103,4 +108,23 @@ public class LaunchpadConfiguration {
return Math.max(this.verticalVelocity, 0);
}
/**
* Loads a list of materials as a set
*
* @param launchpadSection <p>The configuration section to read</p>
* @param key <p>The configuration key containing the materials</p>
* @return <p>The loaded materials as a set</p>
*/
private Set<Material> loadMaterials(ConfigurationSection launchpadSection, String key) {
Set<Material> loadedMaterials = new HashSet<>();
List<?> materialWhitelist = launchpadSection.getList(key);
if (materialWhitelist != null) {
loadedMaterials.addAll(MaterialHelper.loadMaterialList(materialWhitelist));
}
// If a non-block material is specified, simply ignore it
loadedMaterials.removeIf((item) -> !item.isBlock());
return loadedMaterials;
}
}

View File

@ -1,5 +1,6 @@
package net.knarcraft.launchpad.listener;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.launchpad.LaunchpadBlock;
import net.knarcraft.launchpad.launchpad.LaunchpadBlockHandler;
import net.knarcraft.launchpad.launchpad.ModificationRequest;
@ -14,6 +15,7 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
import java.util.UUID;
/**
* A listener for application of launchpad requests
@ -27,19 +29,31 @@ public class LaunchpadModifyListener implements Listener {
return;
}
Set<ModificationRequest> requests = ModificationRequestHandler.getRequests(event.getPlayer().getUniqueId());
UUID playerId = event.getPlayer().getUniqueId();
Set<ModificationRequest> requests = ModificationRequestHandler.getRequests(playerId);
if (requests == null || requests.isEmpty()) {
return;
}
boolean completeSuccess = true;
for (ModificationRequest request : requests) {
handleRequest(request, clicked);
boolean success = handleRequest(request, clicked);
if (!success) {
completeSuccess = false;
// Re-schedule the request to allow the player to click a valid block
ModificationRequestHandler.addRequest(playerId, request);
}
}
event.setUseItemInHand(Event.Result.DENY);
event.setUseInteractedBlock(Event.Result.DENY);
if (completeSuccess) {
event.setUseItemInHand(Event.Result.DENY);
event.setUseInteractedBlock(Event.Result.DENY);
event.getPlayer().sendMessage("Modified launchpad at " + clicked.getLocation());
event.getPlayer().sendMessage("Modified launchpad!");
} else {
event.getPlayer().sendMessage("The block could not be modified, as it's not whitelisted. If you want " +
"to abort changing a launchpad, use \"/launchpad abort\"");
}
}
/**
@ -47,19 +61,27 @@ public class LaunchpadModifyListener implements Listener {
*
* @param request <p>The modification request to handle</p>
* @param block <p>The block to perform the request on</p>
* @return <p>True if the request was successfully handled</p>
*/
private void handleRequest(@NotNull ModificationRequest request, @NotNull Block block) {
private boolean handleRequest(@NotNull ModificationRequest request, @NotNull Block block) {
LaunchpadBlock existingLaunchpad = LaunchpadBlockHandler.getLaunchpadBlock(block);
boolean isLaunchpad = existingLaunchpad != null;
if (!isLaunchpad) {
// Only allow modification of whitelisted launchpad materials
if (!Launchpad.getInstance().getConfiguration().isMaterialWhitelisted(block.getType())) {
return false;
}
existingLaunchpad = new LaunchpadBlock(block);
}
switch (request.modificationAction()) {
case REMOVE -> {
LaunchpadBlockHandler.unregisterLaunchpadBlock(block);
return;
if (isLaunchpad) {
LaunchpadBlockHandler.unregisterLaunchpadBlock(block);
return true;
}
}
case VERTICAL_VELOCITY -> {
if (request.value() != null) {
@ -91,6 +113,8 @@ public class LaunchpadModifyListener implements Listener {
// Save if the existing launchpad was modified
LaunchpadBlockHandler.saveAll();
}
return true;
}
}

View File

@ -4,6 +4,10 @@ launchpad:
materials:
- LIGHT_WEIGHTED_PRESSURE_PLATE # This is the gold pressure plate
- HEAVY_WEIGHTED_PRESSURE_PLATE # This is the iron pressure plate
# A whitelist for the materials that can be manually set as a launchpad. If non-empty, only the materials listed can
# be clicked after executing "/launchpad add" or a different launchpad-editing command. As with materials, this
# supports tags like +PRESSURE_PLATES
materialWhitelist: [ ]
# The default vertical (upwards) velocity for all launchpads
verticalVelocity: 0.2
# The default horizontal (outwards) velocity for all launchpads