Reputation: 481
I'm trying to set the float explosionPower to 10F in the config so I can easily change it through the plugin. How do I do this?
Code:
@EventHandler
@SuppressWarnings("deprecation")
public void onPlayerInteractBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
float explosionPower = 10F;
if (player.getItemInHand().getType() == Material.DIAMOND_SWORD) {
player.getWorld().strikeLightning(player.getTargetBlock(null, 50).getLocation());
player.getWorld().createExplosion(player.getTargetBlock(null, 50).getLocation(), explosionPower);
}
Upvotes: 2
Views: 118
Reputation: 23616
You could get the value of the float in the config via:
float explosionPower = this.getConfig.getFloat("path");
with path being where it is in the config, so with the above code, in the config it would look like this:
path: 10
path
should be changed to something like power
, or explosionPower
.
You would most likely want to do this in your main file. Also, you don't have to use float literal
, so instead of putting 10.0f
, you could just use 10
, and instead of 9.2f
, you could just use 9.2
. Here's an example with your code:
@EventHandler
@SuppressWarnings("deprecation")
public void onPlayerInteractBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
float explosionPower = this.getConfig.getFloat("power"); //this is what we changed
if(player.getItemInHand().getType() == Material.DIAMOND_SWORD) {
player.getWorld().strikeLightning(player.getTargetBlock(null, 50).getLocation());
player.getWorld().createExplosion(player.getTargetBlock(null, 50).getLocation(), explosionPower);
}
}
whereas, with the code above, the config would look like:
power: 10
Also, To make your code better, here is a re-done version of it:
@EventHandler
@SuppressWarnings("deprecation")
public void onPlayerInteractBlock(PlayerInteractEvent event){
Player player = event.getPlayer();
if (player.getItemInHand().getType().equals(Material.DIAMOND_SWORD)){
float explosionPower = this.getConfig.getFloat("power");
player.getWorld().strikeLightning(player.getTargetBlock(null, 50).getLocation());
player.getWorld().createExplosion(player.getTargetBlock(null, 50).getLocation(), explosionPower);
}
}
Upvotes: 1
Reputation: 135
You can write the float to the config as a string and then read it as a string and use Float.valueOf(String s);
to convert it into a float. Here is an example:
float explosionPower = Float.valueOf(yourConfig.getString("pathtolong"));
Upvotes: 0