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 ## Configuration
| Node | Type | Description | | Node | Type | Description |
|------------------------------------------------------------|----------------|--------------------------------------------------------------------------------------------------------------| |------------------------------------------------------------|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| launchpad.materials | List | A list of materials which are always treated as launchpads, without the need for manual registration. | | 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.verticalVelocity | Decimal number | The vertical (upwards) velocity applied to launchpads if not specified otherwise. | | 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.horizontalVelocity | Decimal number | The horizontal (sideways) velocity applied to launchpads if not specified otherwise. | | launchpad.verticalVelocity | Decimal number | The vertical (upwards) 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.horizontalVelocity | Decimal number | The horizontal (sideways) velocity applied to launchpads if not specified otherwise. |
| launchpad.materialVelocities.<MATERIAL>.verticalVelocity | Decimal number | The vertical (sideways) velocity applied to launchpads of type <MATERIAL> if not overridden for the block. | | 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> <name>Launchpad</name>
<properties> <properties>
<java.version>1.8</java.version> <java.version>16</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
@ -23,8 +23,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<source>16</source> <source>${java.version}</source>
<target>16</target> <target>${java.version}</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
@ -39,6 +39,20 @@
</goals> </goals>
<configuration> <configuration>
<createDependencyReducedPom>false</createDependencyReducedPom> <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> </configuration>
</execution> </execution>
</executions> </executions>
@ -78,11 +92,11 @@
<groupId>org.jetbrains</groupId> <groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>24.0.1</version> <version>24.0.1</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.knarcraft</groupId> <groupId>net.knarcraft</groupId>
<artifactId>KnarLib</artifactId> <artifactId>knarlib</artifactId>
<version>1.1</version> <version>1.1</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>

View File

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

View File

@ -21,7 +21,8 @@ public class LaunchpadConfiguration {
private @NotNull FileConfiguration fileConfiguration; private @NotNull FileConfiguration fileConfiguration;
private double horizontalVelocity; private double horizontalVelocity;
private double verticalVelocity; 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 * Instantiate a new launch pad configuration
@ -46,15 +47,9 @@ public class LaunchpadConfiguration {
"The \"launchpad\" configuration section is missing."); "The \"launchpad\" configuration section is missing.");
return; return;
} }
this.launchpadMaterials.clear();
List<?> materials = launchpadSection.getList("materials"); this.launchpadMaterials = loadMaterials(launchpadSection, "materials");
if (materials != null) { this.materialWhitelist = loadMaterials(launchpadSection, "materialWhitelist");
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.horizontalVelocity = launchpadSection.getDouble("horizontalVelocity"); this.horizontalVelocity = launchpadSection.getDouble("horizontalVelocity");
this.verticalVelocity = launchpadSection.getDouble("verticalVelocity"); this.verticalVelocity = launchpadSection.getDouble("verticalVelocity");
@ -73,6 +68,16 @@ public class LaunchpadConfiguration {
return !this.launchpadMaterials.contains(material); 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 * Gets the default horizontal velocity for launchpads
* *
@ -103,4 +108,23 @@ public class LaunchpadConfiguration {
return Math.max(this.verticalVelocity, 0); 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; package net.knarcraft.launchpad.listener;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.launchpad.LaunchpadBlock; import net.knarcraft.launchpad.launchpad.LaunchpadBlock;
import net.knarcraft.launchpad.launchpad.LaunchpadBlockHandler; import net.knarcraft.launchpad.launchpad.LaunchpadBlockHandler;
import net.knarcraft.launchpad.launchpad.ModificationRequest; import net.knarcraft.launchpad.launchpad.ModificationRequest;
@ -14,6 +15,7 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Set; import java.util.Set;
import java.util.UUID;
/** /**
* A listener for application of launchpad requests * A listener for application of launchpad requests
@ -27,19 +29,31 @@ public class LaunchpadModifyListener implements Listener {
return; 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()) { if (requests == null || requests.isEmpty()) {
return; return;
} }
boolean completeSuccess = true;
for (ModificationRequest request : requests) { 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); if (completeSuccess) {
event.setUseInteractedBlock(Event.Result.DENY); 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 request <p>The modification request to handle</p>
* @param block <p>The block to perform the request on</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); LaunchpadBlock existingLaunchpad = LaunchpadBlockHandler.getLaunchpadBlock(block);
boolean isLaunchpad = existingLaunchpad != null; boolean isLaunchpad = existingLaunchpad != null;
if (!isLaunchpad) { if (!isLaunchpad) {
// Only allow modification of whitelisted launchpad materials
if (!Launchpad.getInstance().getConfiguration().isMaterialWhitelisted(block.getType())) {
return false;
}
existingLaunchpad = new LaunchpadBlock(block); existingLaunchpad = new LaunchpadBlock(block);
} }
switch (request.modificationAction()) { switch (request.modificationAction()) {
case REMOVE -> { case REMOVE -> {
LaunchpadBlockHandler.unregisterLaunchpadBlock(block); if (isLaunchpad) {
return; LaunchpadBlockHandler.unregisterLaunchpadBlock(block);
return true;
}
} }
case VERTICAL_VELOCITY -> { case VERTICAL_VELOCITY -> {
if (request.value() != null) { if (request.value() != null) {
@ -91,6 +113,8 @@ public class LaunchpadModifyListener implements Listener {
// Save if the existing launchpad was modified // Save if the existing launchpad was modified
LaunchpadBlockHandler.saveAll(); LaunchpadBlockHandler.saveAll();
} }
return true;
} }
} }

View File

@ -4,6 +4,10 @@ launchpad:
materials: materials:
- LIGHT_WEIGHTED_PRESSURE_PLATE # This is the gold pressure plate - LIGHT_WEIGHTED_PRESSURE_PLATE # This is the gold pressure plate
- HEAVY_WEIGHTED_PRESSURE_PLATE # This is the iron 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 # The default vertical (upwards) velocity for all launchpads
verticalVelocity: 0.2 verticalVelocity: 0.2
# The default horizontal (outwards) velocity for all launchpads # The default horizontal (outwards) velocity for all launchpads