Skip to content

Commit

Permalink
Version 1.6.0: Added wing percentage durability gauge.
Browse files Browse the repository at this point in the history
  • Loading branch information
totemo committed May 24, 2016
1 parent f04d540 commit cc53501
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 33 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ WingCommander
=============
A Minecraft 1.9 Bukkit plugin that makes elytra capable of powered flight.

![WingCommander 1.6.0](https://raw.github.com/totemo/WingCommander/master/images/screenshot.png)


Features
--------
Expand All @@ -10,6 +12,8 @@ Features
as the accelerator when the player is off the ground and wearing elytra.
* Optionally shows the player a colour-coded altimeter when in powered flight.
* Optionally shows the player a speedometer (in blocks/sec) when in powered flight.
* Optionally shows the player a wingometer (percentage wing durability remaining)
when in powered flight.
* Optionally damages the player above a configurable altitude (due to the lack
of breathable air).
* The visibility of gauges can be controlled by the `/gauge` command and is
Expand Down Expand Up @@ -69,6 +73,8 @@ Configuration
| `speedometer.enabled` | If true, players can use the speedometer; otherwise it is not visible for anybody. |
| `speedometer.max` | Speed above which the speedometer reads full. |
| `speedometer.colour` | Colour of the speedometer. |
| `wingometer.enabled` | If true, players can use the wingometer; otherwise it is not visible for anybody. |
| `wingometer.colours` | Map from integer (quoted as string) percentage wing durability remaining to bar colour (BLUE, GREEN, PINK, PURPLE, RED, WHITE, YELLOW). For each number, the specified colour is shown when wing durability falls below that percentage. |
| `vacuum.enabled` | If true, vacuum asphyxiation damage is enabled. |
| `vacuum.altitude` | Altitude above which the player takes asphyxiation damage due to the vacuum. |
| `vacuum.damage` | Asphyxiation damage per tick due to the vacuum. Note: in reality, damage cool downs prevent this from happening on every tick. |
Expand Down
7 changes: 7 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ speedometer:
max: 2.0
colour: BLUE

wingometer:
enabled: true
colours:
'10': RED
'30': YELLOW
'101': GREEN

vacuum:
enabled: true
altitude: 300.0
Expand Down
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ commands:
description: Toggle or set visibility of a specific gauge or all gauges.
permission: wingcommander.fly
usage: |
/<command> [altitude|speed] [off|on]: Toggle or set visibility of a specific gauge or all gauges.
/<command> [altitude|speed|wings] [off|on]: Toggle or set visibility of a specific gauge or all gauges.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>io.totemo</groupId>
<name>WingCommander</name>
<artifactId>${project.name}</artifactId>
<version>1.5.0</version>
<version>1.6.0</version>
<packaging>jar</packaging>
<description>Enables powered flight with elytra.</description>
<url>https://github.com/totemo/${project.name}</url>
Expand Down
85 changes: 71 additions & 14 deletions src/io/totemo/wingcommander/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.totemo.wingcommander;

import java.util.Map.Entry;
import java.util.TreeMap;

import org.bukkit.Effect;
Expand Down Expand Up @@ -157,6 +158,18 @@ public class Configuration {
*/
public BarColor SPEEDOMETER_COLOUR;

/**
* If true, players can use the wingometer (wing condition percentage);
* otherwise it is not visible for anybody.
*/
public boolean WINGOMETER_ENABLED;

/**
* Wingometer BossBar colours, indexed by the percentage durability below
* which they are active.
*/
public TreeMap<Integer, BarColor> WINGOMETER_COLOURS = new TreeMap<Integer, BarColor>();

/**
* If true, vacuum asphyxiation damage is enabled.
*/
Expand Down Expand Up @@ -213,20 +226,7 @@ public void reload() {

ALTIMETER_ENABLED = WingCommander.PLUGIN.getConfig().getBoolean("altimeter.enabled");
ALTIMETER_CEILING = WingCommander.PLUGIN.getConfig().getDouble("altimeter.ceiling");
ALTIMETER_COLOURS.clear();
ALTIMETER_COLOURS.put(999999, BarColor.PURPLE);
ConfigurationSection altimeterColours = WingCommander.PLUGIN.getConfig().getConfigurationSection("altimeter.colours");
for (String key : altimeterColours.getKeys(false)) {
String value = altimeterColours.getString(key);
try {
int altitude = Integer.parseInt(key);
ALTIMETER_COLOURS.put(altitude, BarColor.valueOf(value));
} catch (NumberFormatException ex) {
WingCommander.PLUGIN.getLogger().warning("Non-integer altitude value: " + key);
} catch (IllegalArgumentException ex) {
WingCommander.PLUGIN.getLogger().warning("Invalid altimeter colour: " + value);
}
}
ALTIMETER_COLOURS = loadBarColourMap("altimeter.colours", "altitude", BarColor.PURPLE);

SPEEDOMETER_ENABLED = WingCommander.PLUGIN.getConfig().getBoolean("speedometer.enabled");
SPEEDOMETER_MAX = WingCommander.PLUGIN.getConfig().getDouble("speedometer.max");
Expand All @@ -238,6 +238,9 @@ public void reload() {
SPEEDOMETER_COLOUR = BarColor.BLUE;
}

WINGOMETER_ENABLED = WingCommander.PLUGIN.getConfig().getBoolean("wingometer.enabled");
WINGOMETER_COLOURS = loadBarColourMap("wingometer.colours", "wing durability", BarColor.WHITE);

VACUUM_ENABLED = WingCommander.PLUGIN.getConfig().getBoolean("vacuum.enabled");
VACUUM_ALTITUDE = WingCommander.PLUGIN.getConfig().getDouble("vacuum.altitude");
VACUUM_DAMAGE = WingCommander.PLUGIN.getConfig().getDouble("vacuum.damage");
Expand All @@ -263,4 +266,58 @@ protected Sound loadSound(String path, String description) {
return null;
}
}

// ------------------------------------------------------------------------
/**
* Load a configuration map from integer threshold to corresponding
* BarColor.
*
* Below each threshold key, the bar colour is the corresponding value.
* Keys, though integers, must be formatted as quoted strings.
*
* @param path the path to the map configuration section.
* @param description a displayable string describing what the thresholds
* and colours signify, e.g. "altitude".
* @param maxColour the default colour for any values above the highest
* threshold listed in the configuration section.
*/
protected TreeMap<Integer, BarColor> loadBarColourMap(String path, String description, BarColor maxColour) {
TreeMap<Integer, BarColor> colours = new TreeMap<Integer, BarColor>();
colours.put(Integer.MAX_VALUE, maxColour);
ConfigurationSection section = WingCommander.PLUGIN.getConfig().getConfigurationSection(path);
for (String key : section.getKeys(false)) {
String valueString = section.getString(key);
try {
int value = Integer.parseInt(key);
colours.put(value, BarColor.valueOf(valueString));
} catch (NumberFormatException ex) {
WingCommander.PLUGIN.getLogger().warning("Non-integer " + description + " value: " + key);
} catch (IllegalArgumentException ex) {
WingCommander.PLUGIN.getLogger().warning("Invalid " + description + " colour: " + valueString);
}
}
return colours;
}

// ------------------------------------------------------------------------
/**
* Return the BarColor corresponding to the specified numeric key in the
* specified colour map.
*
* Note that key does not have to exactly match one of the colour
* thresholds; any value below the threshold is assigned that colour.
*
* @param map one of the colour map configuration settings.
* @param key the number used to look up a colour from the map.
* @return the BarColor corresponding to key.
*/
protected BarColor getBarColor(TreeMap<Integer, BarColor> map, int key) {
for (Entry<Integer, BarColor> entry : map.entrySet()) {
if (key < entry.getKey()) {
return entry.getValue();
}
}
// Not actually reachable unless key == Integer.MAX_VALUE.
return null;
}
} // class Configuration
73 changes: 57 additions & 16 deletions src/io/totemo/wingcommander/PlayerState.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.totemo.wingcommander;

import java.util.Map.Entry;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -36,6 +34,10 @@ public PlayerState(Player player, YamlConfiguration config) {
_speedBossBar.addPlayer(_player);
_speedBossBar.setProgress(0);
_speedBossBar.setVisible(false);
_wingsBossBar = Bukkit.getServer().createBossBar("Wings", BarColor.GREEN, BarStyle.SEGMENTED_20);
_wingsBossBar.addPlayer(_player);
_wingsBossBar.setProgress(1.0);
_wingsBossBar.setVisible(false);
load(config);
}

Expand Down Expand Up @@ -123,6 +125,26 @@ public boolean isSpeedometerShown() {
return _showSpeedometer;
}

// ------------------------------------------------------------------------
/**
* Set or toggle visibility of the wingometer.
*
* @param visibility the new visibility state, or null to toggle.
*/
public void showWingometer(Boolean visibility) {
_showWingometer = (visibility == null) ? !_showWingometer : visibility;
}

// ------------------------------------------------------------------------
/**
* Return true if the wingometer is shown.
*
* @return true if the wingometer is shown.
*/
public boolean isWingometerShown() {
return _showWingometer;
}

// ------------------------------------------------------------------------
/**
* Save this player's preferences to the specified configuration.
Expand All @@ -134,6 +156,7 @@ public void save(YamlConfiguration config) {
section.set("name", _player.getName());
section.set("altimeter", _showAltimeter);
section.set("speedometer", _showSpeedometer);
section.set("wingometer", _showWingometer);
}

// ------------------------------------------------------------------------
Expand All @@ -146,12 +169,10 @@ protected void load(YamlConfiguration config) {
ConfigurationSection section = config.getConfigurationSection(_player.getUniqueId().toString());
if (section == null) {
section = config.createSection(_player.getUniqueId().toString());
_showAltimeter = true;
_showSpeedometer = true;
} else {
_showAltimeter = section.getBoolean("altimeter");
_showSpeedometer = section.getBoolean("speedometer");
}
_showAltimeter = section.getBoolean("altimeter", true);
_showSpeedometer = section.getBoolean("speedometer", true);
_showWingometer = section.getBoolean("wingometer", true);
}

// ------------------------------------------------------------------------
Expand All @@ -165,31 +186,40 @@ protected void updateBossBars() {
if (WingCommander.CONFIG.ALTIMETER_ENABLED && _showAltimeter &&
WingCommander.isFlightCapable(_player) && _player.isGliding()) {

_altitudeBossBar.setVisible(true);
double altitude = _player.getLocation().getY();
for (Entry<Integer, BarColor> entry : WingCommander.CONFIG.ALTIMETER_COLOURS.entrySet()) {
if (altitude < entry.getKey()) {
_altitudeBossBar.setColor(entry.getValue());
break;
}
}
_altitudeBossBar.setColor(WingCommander.CONFIG.getBarColor(WingCommander.CONFIG.ALTIMETER_COLOURS, (int) altitude));
_altitudeBossBar.setTitle(String.format("Altitude: %d", (int) altitude));
_altitudeBossBar.setProgress(Math.min(1.0, Math.max(0.0, altitude / WingCommander.CONFIG.ALTIMETER_CEILING)));
_altitudeBossBar.setVisible(true);
} else {
_altitudeBossBar.setVisible(false);
}

if (WingCommander.CONFIG.SPEEDOMETER_ENABLED && _showSpeedometer &&
WingCommander.isFlightCapable(_player) && _player.isGliding()) {

_speedBossBar.setVisible(true);

double speed = _player.getVelocity().length();
_speedBossBar.setTitle(String.format("Speed: %3.1f", 20 * speed));
_speedBossBar.setProgress(Math.min(1.0, Math.max(0.0, speed / WingCommander.CONFIG.SPEEDOMETER_MAX)));
_speedBossBar.setVisible(true);
} else {
_speedBossBar.setVisible(false);
}

if (WingCommander.CONFIG.WINGOMETER_ENABLED && _showWingometer &&
WingCommander.isFlightCapable(_player) && _player.isGliding()) {

ItemStack chest = _player.getEquipment().getChestplate();
int remainingDurability = Material.ELYTRA.getMaxDurability() - chest.getDurability();
double fraction = remainingDurability / (double) Material.ELYTRA.getMaxDurability();
int percentage = (int) (100 * fraction);
_wingsBossBar.setColor(WingCommander.CONFIG.getBarColor(WingCommander.CONFIG.WINGOMETER_COLOURS, percentage));
_wingsBossBar.setTitle(String.format("Wings: %d%%", percentage));
_wingsBossBar.setProgress(Math.min(1.0, Math.max(0.0, fraction)));
_wingsBossBar.setVisible(true);
} else {
_wingsBossBar.setVisible(false);
}
} // updateBossBars

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -312,6 +342,11 @@ protected void accelerate(double acceleration) {
*/
protected BossBar _speedBossBar;

/**
* BossBar used to display the player's wing durability percentage.
*/
protected BossBar _wingsBossBar;

/**
* If true, the altimeter is visible (notwithstanding other requirements for
* it to be shown).
Expand All @@ -324,4 +359,10 @@ protected void accelerate(double acceleration) {
*/
protected boolean _showSpeedometer;

/**
* If true, the wingometer is visible (notwithstanding other requirements
* for it to be shown).
*/
protected boolean _showWingometer;

} // class PlayerState
7 changes: 6 additions & 1 deletion src/io/totemo/wingcommander/WingCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected void cmdGauge(CommandSender sender, String[] args) {
}

if (args.length == 0 || (args.length == 1 && args[0].equalsIgnoreCase("help")) || args.length > 2) {
sender.sendMessage(ChatColor.DARK_AQUA + "/gauge [altitude|speed] [off|on]" +
sender.sendMessage(ChatColor.DARK_AQUA + "/gauge [altitude|speed|wings] [off|on]" +
ChatColor.WHITE + " - Toggle or set the visibility of a specific gauge or all gauges.");
} else {
String gauge;
Expand Down Expand Up @@ -202,6 +202,7 @@ protected void cmdGauge(CommandSender sender, String[] args) {
if (gauge == null) {
state.showAltimeter(visibility);
state.showSpeedometer(visibility);
state.showWingometer(visibility);
sender.sendMessage(ChatColor.DARK_AQUA + "All gauges will be " +
(visibility ? "shown." : "hidden."));
} else if (gauge.equalsIgnoreCase("altitude")) {
Expand All @@ -212,6 +213,10 @@ protected void cmdGauge(CommandSender sender, String[] args) {
state.showSpeedometer(visibility);
sender.sendMessage(ChatColor.DARK_AQUA + "The speedometer will be " +
(state.isSpeedometerShown() ? "shown." : "hidden."));
} else if (gauge.equalsIgnoreCase("wings")) {
state.showWingometer(visibility);
sender.sendMessage(ChatColor.DARK_AQUA + "The wing durability meter will be " +
(state.isWingometerShown() ? "shown." : "hidden."));
} else {
sender.sendMessage(ChatColor.RED + "The gauge name must be 'altitude' or 'speed'.");
}
Expand Down

0 comments on commit cc53501

Please sign in to comment.