Skip to content

Commit

Permalink
General updates for v1.2.1
Browse files Browse the repository at this point in the history
+Added override permission for player damage on boost
+Added a gitignore
*Updated version tags
*Updated Maven jar plugin
*Code style updates
*Reduced nesting
  • Loading branch information
NyarukoDev committed Jan 9, 2020
1 parent 6493791 commit 889a5cc
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
target
*.iml
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Each of the changes (with the current exception of firework star rocket damage)

## Permissions:
* `elytrabalance.overrides.itemdamage`
* `elytrabalance.overrides.boostplayerdamage`
* `elytrabalance.overrides.breakremoval`
* `elytrabalance.overrides.eat`
* `elytrabalance.overrides.fix`
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

<groupId>io.nyaruko</groupId>
<artifactId>ElytraBalance</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<packaging>jar</packaging>

<name>ElytraBalance</name>
<url>http://maven.apache.org</url>
<url>https://github.com/NyarukoDev/ElytraBalance</url>

<profiles>
<profile>
Expand All @@ -18,7 +18,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<version>3.2.0</version>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/nyaruko/elytrabalance/Config.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.nyaruko.elytrabalance;

//ConfigVersion 2

public class Config {
//Default to '1' in case of pre-versioning config
public int configVersion = 1;

public boolean removeElytraOnBreak = false;
public int itemDamageOnRocketUse = 57;
public boolean playerDamageOnNoStarRocketUse = true;
Expand Down
52 changes: 27 additions & 25 deletions src/main/java/io/nyaruko/elytrabalance/ElytraBalance.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@
import org.bukkit.plugin.java.JavaPlugin;

public class ElytraBalance extends JavaPlugin {
/**
* Update this whenever the config is altered
*/
private static final int CONFIG_VERSION = 2;
private static final String VERSION = "1.2.1";
private static Config config;

final String version = "1.2.1";
//Update this whenever the config is altered
final int configVersion = 2;

@Override
public void onEnable() {
initConfig();
registerEvents();

if (isEnabled()) {
getLogger().info("ElytraBalance v" + version + " successfully loaded.");
if(isEnabled()) {
getLogger().log(Level.INFO, "ElytraBalance v{} successfully loaded.", VERSION);
} else {
getLogger().severe("ElytraBalance v" + version + " failed to load.");
getLogger().log(Level.SEVERE, "ElytraBalance v{} failed to load.", VERSION);
}
}

private void initConfig() {
//Ensure plugin data folder exists
if (!getDataFolder().exists() && !getDataFolder().mkdir()) {
if(!getDataFolder().exists() && !getDataFolder().mkdir()) {
getLogger().log(Level.SEVERE, "Failed to create data folder.");
this.setEnabled(false);
}
Expand All @@ -57,24 +58,25 @@ private void initConfig() {

//Update config to latest version
//Re-saves the config file as the latest version but with existing user values
if (config.configVersion < configVersion) {
config.configVersion = configVersion;
if(config.configVersion < CONFIG_VERSION) {
config.configVersion = CONFIG_VERSION;
saveConfig(configFile);
}
} else {
//Create config file if not exists
try {
if (configFile.createNewFile()) {
config = new Config(configVersion);
saveConfig(configFile);
} else {
getLogger().log(Level.SEVERE, "Failed to create plugin config.");
this.setEnabled(false);
}
} catch (IOException e) {
getLogger().log(Level.SEVERE, e.getMessage());
return;
}

//Create config file if not exists
try {
if(configFile.createNewFile()) {
config = new Config(CONFIG_VERSION);
saveConfig(configFile);
} else {
getLogger().log(Level.SEVERE, "Failed to create plugin config.");
this.setEnabled(false);
}
} catch (IOException e) {
getLogger().log(Level.SEVERE, e.getMessage());
this.setEnabled(false);
}
}

Expand All @@ -83,16 +85,16 @@ private void saveConfig(File configFile) {
Gson file = new GsonBuilder().setPrettyPrinting().create();
file.toJson(config, writer);
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Failed to save plugin config.");
getLogger().log(Level.SEVERE, "Failed to save plugin config: {}", e.getMessage());
this.setEnabled(false);
}
}

private void registerEvents() {
if (!config.canConsumeFoodInFlight) {
if(!config.canConsumeFoodInFlight) {
getServer().getPluginManager().registerEvents(new EatListener(), this);
}
if (config.removeElytraOnBreak) {
if(config.removeElytraOnBreak) {
getServer().getPluginManager().registerEvents(new BreakListener(), this);
}
getServer().getPluginManager().registerEvents(new BoostListener(), this);
Expand Down
47 changes: 26 additions & 21 deletions src/main/java/io/nyaruko/elytrabalance/listeners/BoostListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,37 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;

public class BoostListener implements Listener {
/**
* Listener for elytra boost events (playerDamageOnNoStarRocketUse)
* Player damage amounts defined with additionalDamagePerStarRocketUse & damagePerNoStarRocketUse
* Elytra ItemStack damage defined with itemDamageOnRocketUse
*/
@EventHandler(priority = EventPriority.NORMAL)
public void onBoost(PlayerInteractEvent event) {
Player p = event.getPlayer();
if (p.isGliding()) {
ItemStack is = event.getItem();
if ((is != null) && (is.getType() == Material.FIREWORK_ROCKET)) {
if (((FireworkMeta) is.getItemMeta()).hasEffects()) {
p.damage(ElytraBalance.getConfigModel().additionalDamagePerStarRocketUse);
} else if (ElytraBalance.getConfigModel().playerDamageOnNoStarRocketUse) {
p.damage(ElytraBalance.getConfigModel().damagePerNoStarRocketUse);
}
if (!p.hasPermission("elytrabalance.overrides.itemdamage")) {
ItemStack elytra = p.getInventory().getChestplate();
if(elytra != null) {
ItemMeta meta = elytra.getItemMeta();
if(meta != null) {
int durability = ((Damageable) meta).getDamage() + ElytraBalance.getConfigModel().itemDamageOnRocketUse;
if (durability > 431) {
durability = 431;
}
((Damageable) meta).setDamage(durability);
}
}
ItemStack heldItem = event.getItem();
if(!p.isGliding() || heldItem == null || heldItem.getType() != Material.FIREWORK_ROCKET) return;

//Should deal player damage checks
if(!p.hasPermission("elytrabalance.overrides.boostplayerdamage")) {
if(((FireworkMeta) heldItem.getItemMeta()).hasEffects()) {
p.damage(ElytraBalance.getConfigModel().additionalDamagePerStarRocketUse);
} else if(ElytraBalance.getConfigModel().playerDamageOnNoStarRocketUse) {
p.damage(ElytraBalance.getConfigModel().damagePerNoStarRocketUse);
}
}

//Should damage elytra checks
if(!p.hasPermission("elytrabalance.overrides.itemdamage")) {
ItemStack elytra = p.getInventory().getChestplate();
if(elytra != null) {
Damageable m = (Damageable) elytra.getItemMeta();
if(m != null) {
int durability = m.getDamage() + ElytraBalance.getConfigModel().itemDamageOnRocketUse;
durability = Math.min(durability, 431);
m.setDamage(durability);
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/io/nyaruko/elytrabalance/listeners/BreakListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@
import org.bukkit.inventory.meta.ItemMeta;

public class BreakListener implements Listener {
/**
* Remove on break listener (removeElytraOnBreak)
*/
@EventHandler(priority = EventPriority.HIGHEST)
public void onBreak(PlayerItemDamageEvent event) {
Player p = event.getPlayer();
ItemStack is = p.getInventory().getChestplate();
if (is != null) {
ItemMeta im = is.getItemMeta();
if(im != null) {
int durability = ((Damageable) im).getDamage();
if(is.getType() == Material.ELYTRA && !p.hasPermission("elytrabalance.overrides.breakremoval") && durability >= 430) {
ItemStack replacement = new ItemStack(Material.AIR);
p.getInventory().setChestplate(replacement);
event.setCancelled(true);
}
}
ItemMeta im;

if(is == null || (im = is.getItemMeta()) == null) return;

int durability = ((Damageable) im).getDamage();
if(is.getType() == Material.ELYTRA && !p.hasPermission("elytrabalance.overrides.breakremoval") && durability >= 430) {
ItemStack replacement = new ItemStack(Material.AIR);
p.getInventory().setChestplate(replacement);
event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import org.bukkit.event.player.PlayerItemConsumeEvent;

public class EatListener implements Listener {
/**
* Listener for limiting use of consumables in flight (canConsumeFoodInFlight)
*/
@EventHandler(priority = EventPriority.NORMAL)
public void onEat(PlayerItemConsumeEvent event) {
Player p = event.getPlayer();
if ((p.isGliding()) && (!p.hasPermission("elytrabalance.overrides.eat"))) {
if(p.isGliding() && !p.hasPermission("elytrabalance.overrides.eat")) {
event.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@
import org.bukkit.event.player.PlayerItemMendEvent;

public class FixListener implements Listener {

//Repair (Anvil)
/**
* Listener for anvil repairs (canRepairElytra)
*/
@EventHandler(priority = EventPriority.HIGHEST)
public void onFix(PrepareAnvilEvent event) {
Player p = (Player) event.getView().getPlayer();
if ((event.getResult().getType() == Material.ELYTRA) && (!ElytraBalance.getConfigModel().canRepairElytra) && (!p.hasPermission("elytrabalance.overrides.fix"))) {
if(event.getResult() != null && event.getResult().getType() == Material.ELYTRA && !ElytraBalance.getConfigModel().canRepairElytra && !p.hasPermission("elytrabalance.overrides.fix")) {
event.setResult(null);
p.updateInventory();
}
}

//Mend (Enchantment)
/**
* Listener for mend enchantment repairs (canMendElytra)
*/
@EventHandler(priority = EventPriority.HIGHEST)
public void onFix(PlayerItemMendEvent event) {
Player p = event.getPlayer();
if((event.getItem().getType() == Material.ELYTRA) && (!ElytraBalance.getConfigModel().canMendElytra) && (!p.hasPermission("elytrabalance.overrides.mend"))){
if(event.getItem().getType() == Material.ELYTRA && !ElytraBalance.getConfigModel().canMendElytra && !p.hasPermission("elytrabalance.overrides.mend")){
event.setRepairAmount(0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: ElytraBalance
version: 1.1
version: 1.2.1
authors: [Nyaruko]
load: postworld
load: POSTWORLD
prefix: EB
main: io.nyaruko.elytrabalance.ElytraBalance

Expand Down

0 comments on commit 889a5cc

Please sign in to comment.