diff --git a/src/main/java/com/myplugin/MyPlugin.java b/src/main/java/com/myplugin/MyPlugin.java index b6248b9..5f0ad1a 100644 --- a/src/main/java/com/myplugin/MyPlugin.java +++ b/src/main/java/com/myplugin/MyPlugin.java @@ -2,6 +2,8 @@ import com.myplugin.command.DragonBallCommand; import com.myplugin.command.commands.RaceCommand; +import com.myplugin.events.ExperienceHandler; +import com.myplugin.events.MobSpawnManager; import com.myplugin.events.PlayerAttackEvent; import com.myplugin.lib.Logger; import com.myplugin.lib.PlayerDataManager; @@ -31,6 +33,7 @@ public void onEnable() { this.random = new Random(); this.saveDefaultConfig(); this.setConfigValues(); + Bukkit.spigot().getConfig().set("settings.attribute.maxHealth", 10240); Logger.debug("Debug mode is enabled! You will receive a lot more message in your server console now, disable this if you want your outpost cleaner!"); Logger.debug("Initializing PlayerDataManager. . ."); @@ -40,6 +43,11 @@ public void onEnable() { this.barManager = new BossBarManager(this); Logger.debug("Loading event PlayerAttackEvent"); new PlayerAttackEvent(this); + Logger.debug("Loading event MobSpawnManager"); + new MobSpawnManager(this); + Logger.debug("Loading event ExperienceHandler"); + new ExperienceHandler(this); + Logger.debug("Loading command RaceCommand"); diff --git a/src/main/java/com/myplugin/events/ExperienceHandler.java b/src/main/java/com/myplugin/events/ExperienceHandler.java new file mode 100644 index 0000000..02c1df4 --- /dev/null +++ b/src/main/java/com/myplugin/events/ExperienceHandler.java @@ -0,0 +1,90 @@ +package com.myplugin.events; + +import com.myplugin.MyPlugin; +import com.myplugin.lib.Logger; +import com.myplugin.lib.PlayerData; +import com.myplugin.lib.PlayerDataManager; +import com.myplugin.lib.events.TriggerConfigUpdate; +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDeathEvent; + +import java.util.HashMap; + +public class ExperienceHandler implements Listener { + + private final MyPlugin plugin; + private final PlayerDataManager manager; + private HashMap vanillaMobs; + + public ExperienceHandler(final MyPlugin plugin) { + this.plugin = plugin; + this.manager = this.plugin.getDataManager(); + this.setConfigValues(); + + Bukkit.getPluginManager().registerEvents(this, this.plugin); + } + + @EventHandler + public void onConfigReload(final TriggerConfigUpdate e) { + this.setConfigValues(); + } + + @EventHandler + public void playerKill(final EntityDeathEvent e) { + if (e.getEntity().getKiller() != null) { + final Player killer = e.getEntity().getKiller(); + final PlayerData data = this.manager.getPlayerData(killer.getUniqueId()); + final MobExp exp = this.vanillaMobs.get(e.getEntityType()); + if (exp.getMax() <= 0 && exp.getMin() <= 0) return; + final int randExp = this.plugin.getRandom().nextInt(exp.getMax() - exp.getMin()) + exp.getMin(); + data.addExperience(randExp); + Logger.debug("Player with UUID: " + killer.getUniqueId() + " receive " + randExp + " EXP for killing a " + e.getEntityType().toString()); + } + } + + public void setConfigValues() { + this.vanillaMobs = new HashMap<>(); + final FileConfiguration config = this.plugin.getConfig(); + final ConfigurationSection expSettings = config.getConfigurationSection("ExpSettings"); + if (expSettings == null) { + Logger.error("Could not find field 'ExpSettings' inside of config.yml"); + return; + } + + for (String s : expSettings.getKeys(false)) { + final EntityType type = EntityType.valueOf(s); + final ConfigurationSection sec = expSettings.getConfigurationSection(s); + if (sec != null) { + final int min = sec.getInt("ExpMin"); + final int max = sec.getInt("ExpMax"); + final MobExp exp = new MobExp(min, max); + this.vanillaMobs.put(type, exp); + } else Logger.error("Could not find config path ExpSettings." + s); + } + } + + private static final class MobExp { + + private final int min; + private final int max; + + public MobExp(final int min, final int max) { + this.min = min; + this.max = max; + } + + public final int getMin() { + return this.min; + } + + public final int getMax() { + return this.max; + } + } +} diff --git a/src/main/java/com/myplugin/events/MobSpawnManager.java b/src/main/java/com/myplugin/events/MobSpawnManager.java new file mode 100644 index 0000000..a56e45e --- /dev/null +++ b/src/main/java/com/myplugin/events/MobSpawnManager.java @@ -0,0 +1,101 @@ +package com.myplugin.events; + +import com.myplugin.MyPlugin; +import com.myplugin.lib.Logger; +import com.myplugin.lib.events.TriggerConfigUpdate; +import org.bukkit.Bukkit; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Bee; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Zombie; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.CreatureSpawnEvent; + +import java.util.HashMap; +import java.util.Set; + +public class MobSpawnManager implements Listener { + + private HashMap mobStats; + private Set entitySet; + private final MyPlugin plugin; + + public MobSpawnManager(final MyPlugin plugin) { + this.plugin = plugin; + this.setConfigValues(); + this.entitySet = this.mobStats.keySet(); + + Bukkit.getPluginManager().registerEvents(this, this.plugin); + } + + @EventHandler + public void onConfigUpdate(final TriggerConfigUpdate e) { + this.setConfigValues(); + this.entitySet = this.mobStats.keySet(); + } + + @EventHandler + public void onMobSpawn(final CreatureSpawnEvent e) { + final EntityType type = e.getEntityType(); + if (this.entitySet.contains(type)) { + Logger.debug("Loading MobSpawn data for: " + type); + final MobStat stat = this.mobStats.get(type); + final AttributeInstance attackInstance = e.getEntity().getAttribute(Attribute.GENERIC_ATTACK_DAMAGE); + final AttributeInstance healthInstance = e.getEntity().getAttribute(Attribute.GENERIC_MAX_HEALTH); + if (attackInstance != null) { + e.getEntity().getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).setBaseValue(stat.getMobAttack()); + } + + if (healthInstance != null) { + e.getEntity().getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(stat.getMobHealth()); + e.getEntity().setHealth(stat.getMobHealth()); + } + } + } + + private void setConfigValues() { + this.mobStats = new HashMap<>(); + final FileConfiguration config = this.plugin.getConfig(); + final ConfigurationSection mobSec = config.getConfigurationSection("MobSettings"); + if (mobSec != null) { + for (String key : mobSec.getKeys(false)) { + final EntityType type = EntityType.valueOf(key); + final ConfigurationSection data = mobSec.getConfigurationSection(key); + if (data != null) { + final int health = data.getInt("Health"); + final int attack = data.getInt("Attack"); + final MobStat stat = new MobStat(health, attack); + this.mobStats.put(type, stat); + Logger.debug("Successfully loaded MobSettings for: " + type.toString()); + } else Logger.error("Config path MobSettings." + key+ " was null when got"); + } + } else Logger.error("MobSetting value from config was null when got"); + + Logger.log("Successfully loaded " + this.mobStats + " ModStats to override for vanilla spawns!s"); + } + + private static class MobStat { + + private final int mobHealth; + private final int mobAttack; + + public MobStat(final int health, final int attack) { + this.mobHealth = health; + this.mobAttack = attack; + } + + public final int getMobHealth() { + return this.mobHealth; + } + + public final int getMobAttack() { + return this.mobAttack; + } + + } +} diff --git a/src/main/java/com/myplugin/events/PlayerAttackEvent.java b/src/main/java/com/myplugin/events/PlayerAttackEvent.java index b9b2241..810b223 100644 --- a/src/main/java/com/myplugin/events/PlayerAttackEvent.java +++ b/src/main/java/com/myplugin/events/PlayerAttackEvent.java @@ -15,8 +15,6 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; -import static com.myplugin.MyPlugin.getPercentOf; - public class PlayerAttackEvent implements Listener { private final PlayerDataManager manager; @@ -28,7 +26,6 @@ public PlayerAttackEvent(final MyPlugin plugin) { @EventHandler public void onEntityDamage(final EntityDamageByEntityEvent e) { - final boolean playerHitsMob = e.getDamager() instanceof Player && !(e.getEntity() instanceof Player); final boolean playerHitsPlayer = e.getEntity() instanceof Player && e.getDamager() instanceof Player; final boolean mobHitsPlayer = !(e.getDamager() instanceof Player) && e.getEntity() instanceof Player; @@ -40,7 +37,6 @@ public void onEntityDamage(final EntityDamageByEntityEvent e) { Logger.debug("Player with UUID: " + p.getUniqueId().toString() + " dealt " + damage + " to " + e.getEntity().getType().toString()); e.setDamage(damage); this.spawnParticle(e.getEntity()); - } else if (playerHitsPlayer) { final Player hurtPlayer = (Player) e.getEntity(); final Player attackingPlayer = (Player) e.getEntity(); @@ -54,11 +50,23 @@ public void onEntityDamage(final EntityDamageByEntityEvent e) { } else if (mobHitsPlayer) { final Player player = (Player) e.getEntity(); final PlayerData data = this.manager.getPlayerData(player.getUniqueId()); + Logger.debug("Current Health before hit by mob: " + data.getPlayerHealth()); data.applyDamage((int)e.getDamage()); this.spawnParticle(player); Logger.debug("Mob: " + e.getDamager().getType() + " dealt ~"+ e.getDamage() + " to " + player.getUniqueId().toString()); e.setDamage(0D); + } else e.setDamage(0D); + } + @EventHandler + public void onPlayerHurt(final EntityDamageEvent e) { + if (e.getEntity() instanceof Player) { + if (e.getCause() != EntityDamageEvent.DamageCause.ENTITY_ATTACK || e.getCause() != EntityDamageEvent.DamageCause.ENTITY_ATTACK) { + final Player p = (Player) e.getEntity(); + final PlayerData data = this.manager.getPlayerData(p.getUniqueId()); + data.applyDamage((int)e.getDamage()); + e.setDamage(0D); + } } } diff --git a/src/main/java/com/myplugin/events/PlayerFalls.java b/src/main/java/com/myplugin/events/PlayerFalls.java new file mode 100644 index 0000000..799a107 --- /dev/null +++ b/src/main/java/com/myplugin/events/PlayerFalls.java @@ -0,0 +1,7 @@ +package com.myplugin.events; + +import org.bukkit.event.EventHandler; + +public class PlayerFalls { + +} diff --git a/src/main/java/com/myplugin/events/PlayerKillsEntity.java b/src/main/java/com/myplugin/events/PlayerKillsEntity.java deleted file mode 100644 index 24482d0..0000000 --- a/src/main/java/com/myplugin/events/PlayerKillsEntity.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.myplugin.events; - -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityDeathEvent; - -public class PlayerKillsEntity implements Listener { - - - - public PlayerKillsEntity() { - - } - - @EventHandler - public void onDeath(final EntityDeathEvent e) { - if (e.getEntity().getKiller() != null) { - final Player killer = e.getEntity().getKiller(); - final EntityType killed = e.getEntityType(); - - } - } - -} diff --git a/src/main/java/com/myplugin/lib/ExperienceHandler.java b/src/main/java/com/myplugin/lib/ExperienceHandler.java deleted file mode 100644 index a111592..0000000 --- a/src/main/java/com/myplugin/lib/ExperienceHandler.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.myplugin.lib; - -import com.myplugin.MyPlugin; -import com.myplugin.lib.events.TriggerConfigUpdate; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.EntityType; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; - -import java.util.HashMap; - -public class ExperienceHandler implements Listener { - - private final MyPlugin plugin; - private HashMap vanillaMobs; - - public ExperienceHandler(final MyPlugin plugin) { - this.plugin = plugin; - this.vanillaMobs = new HashMap<>(); - } - - @EventHandler - public void onConfigReload(final TriggerConfigUpdate e) { - - } - - public void setConfigValues() { - final FileConfiguration config = this.plugin.getConfig(); - final ConfigurationSection expSettings = config.getConfigurationSection("ExpSettings"); - if (expSettings == null) { - Logger.error("Could not find field 'ExpSettings' inside of config.yml"); - return; - } - - for (String s : expSettings.getKeys(false)) { - final EntityType type = EntityType.valueOf(s); - final ConfigurationSection sec = expSettings.getConfigurationSection(s); - - } - } -} diff --git a/src/main/java/com/myplugin/lib/PlayerData.java b/src/main/java/com/myplugin/lib/PlayerData.java index 95d1c49..5476208 100644 --- a/src/main/java/com/myplugin/lib/PlayerData.java +++ b/src/main/java/com/myplugin/lib/PlayerData.java @@ -12,6 +12,7 @@ import com.myplugin.lib.events.TriggerBossBarUpdate; import com.myplugin.lib.events.TriggerConfigUpdate; import com.myplugin.lib.events.TriggerDataUpdate; +import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; @@ -20,8 +21,7 @@ import java.util.UUID; -import static com.myplugin.MyPlugin.getDoublePercentOf; -import static com.myplugin.MyPlugin.getPercentOf; +import static com.myplugin.MyPlugin.*; public class PlayerData implements Listener { @@ -148,6 +148,10 @@ public void setConfigValues() { * Think of these on a as instance basis */ + public final Player getPlayer() { + return Bukkit.getPlayer(this.uuid); + } + public final int getPlayerStrength() { final JsonElement strPointsSpent = this.talentPoints.get(PlayerProperty.STRENGTH.toString()); if (strPointsSpent == null) return -1; @@ -159,15 +163,18 @@ public final int getPlayerMaxHealth() { final JsonElement hpPointsSpent = this.talentPoints.get(PlayerProperty.MAX_HEALTH.toString()); if (hpPointsSpent == null) return -1; final int playerLevel = this.getPlayerLevel(); - Logger.debug("getPlayerMaxHealth #1 " + (this.perSpMaxHpInc)); - Logger.debug("getPlayerMaxHealth #2 " + (this.perLvlMaxHpInc)); return (hpPointsSpent.getAsInt() * this.perSpMaxHpInc) + (playerLevel * this.perLvlMaxHpInc); } public final int getPlayerHealth() { final JsonElement health = this.playerStats.get(PlayerProperty.CURRENT_HEALTH.toString()); if (health == null) return -1; - return health.getAsInt(); + int hp = health.getAsInt(); + if (hp > this.getPlayerMaxHealth()) { + hp = this.getPlayerMaxHealth(); + this.setPlayerHealth(hp); + } + return hp; } public final int getPlayerKi() { @@ -253,11 +260,9 @@ public final int getTalentPoints() { } public final int getMaxExperience() { - int maxExp = 0; + int maxExp = this.maxStartingExp; for (int x = 1; x <= this.getPlayerLevel(); x++) { - if (x == 1) { - maxExp += getPercentOf(this.maxStartingExp, this.expIncrease); - } else maxExp += getPercentOf(maxExp, this.expIncrease); + maxExp += getPercentOf(maxExp, this.expIncrease); } return maxExp; } @@ -370,6 +375,34 @@ public final void addPlayerDefense(final int amt) { this.setPlayerDefense(current); } + public final void addExperience(final int amt) { + final int max = this.getMaxExperience(); + Logger.debug("Current max EXP: " + max); + final int current = this.getPlayerExp() + amt; + if (current >= max) { + this.addPlayerLevel(); + final int leftOver = max - current; + if (leftOver > 0) { + this.addExperience(leftOver); + } else { + this.playerStats.remove(PlayerProperty.CURRENT_EXP.toString()); + this.playerStats.addProperty(PlayerProperty.CURRENT_EXP.toString(), leftOver); + } + } else { + this.playerStats.remove(PlayerProperty.CURRENT_EXP.toString()); + this.playerStats.addProperty(PlayerProperty.CURRENT_EXP.toString(), current); + } + } + + public final void addPlayerLevel() { + final int level = this.getPlayerLevel() +1; + this.playerStats.remove(PlayerProperty.LEVEL.toString()); + this.playerStats.addProperty(PlayerProperty.LEVEL.toString(), level); + if (this.getPlayer() != null) { + this.getPlayer().sendMessage(Component.text(ofString("&aSuccessfully leveled up to level: &b" + level))); + } + } + public final void setPlayerRace(final Race race) { this.playerStats.remove(PlayerProperty.RACE.toString()); this.playerStats.addProperty(PlayerProperty.RACE.toString(), race.toString()); @@ -392,14 +425,11 @@ public final void setPlayerRace(final Race race) { } public final void reset(final boolean killPlayer) { - if (killPlayer) { - final Player p = Bukkit.getPlayer(this.uuid); - if (p != null) { - p.setHealth(0D); - this.setPlayerHealth(this.getPlayerMaxHealth()); - this.setPlayerKi(this.getPlayerMaxKi()); - this.setPlayerStamina(this.getPlayerMaxStamina()); - } + final Player p = Bukkit.getPlayer(this.uuid); + this.setPlayerHealth(this.getPlayerMaxHealth()); + this.setPlayerKi(this.getPlayerMaxKi()); + if (killPlayer && p != null) { + p.setHealth(0D); } } @@ -415,4 +445,8 @@ public final int calculateDamage() { public final void triggerUpdate() { Bukkit.getPluginManager().callEvent(new TriggerDataUpdate(this.uuid, this)); } + + public final void triggerBarUpdate() { + Bukkit.getPluginManager().callEvent(new TriggerBossBarUpdate(this.uuid, this)); + } } diff --git a/src/main/java/com/myplugin/lib/dragonball/gui/guis/ConfirmRaceGui.java b/src/main/java/com/myplugin/lib/dragonball/gui/guis/ConfirmRaceGui.java index eb9901f..c44c4cb 100644 --- a/src/main/java/com/myplugin/lib/dragonball/gui/guis/ConfirmRaceGui.java +++ b/src/main/java/com/myplugin/lib/dragonball/gui/guis/ConfirmRaceGui.java @@ -6,6 +6,7 @@ import com.myplugin.lib.PlayerDataManager; import com.myplugin.lib.dragonball.Race; import com.myplugin.lib.dragonball.gui.Gui; +import com.myplugin.lib.events.TriggerBossBarUpdate; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -88,6 +89,7 @@ public void onThisInventoryClick(final InventoryClickEvent e) { data.setPlayerRace(this.race); data.triggerUpdate(); this.plugin.getBarManager().loadPlayerBar(p.getUniqueId()); + Bukkit.getPluginManager().callEvent(new TriggerBossBarUpdate(p.getUniqueId(), data)); p.sendMessage(Component.text(ofString("&aSuccessfully set you're race to &b" + this.race.toString().toLowerCase()))); } else if (slot == 14) { p.closeInventory(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e9e1ff9..742ec85 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -89,22 +89,85 @@ DevelopmentSettings: Debug: true ExpSettings: - ZOMBIE: - Exp: 10 - BABY_ZOMBIE: - Exp: 5 + ExpMin: 10 + ExpMax: 20 CREEPER: - Exp: 20 + ExpMin: 25 + ExpMax: 50 SKELETON: - Exp: 15 + ExpMin: 5 + ExpMax: 15 + SPIDER: + ExpMin: 10 + ExpMax: 20 + GHAST: + ExpMin: 15 + ExpMax: 30 + COW: + ExpMin: 5 + ExpMax: 10 + CHICKEN: + ExpMin: 5 + ExpMax: 10 + SHEEP: + ExpMin: 5 + ExpMax: 10 ENDERMAN: - Exp: 25 + ExpMin: 20 + ExpMax: 35 + SILVERFISH: + ExpMin: 10 + ExpMax: 20 + HUSK: + ExpMin: 5 + ExpMax: 15 BLAZE: - Exp: 23 + ExpMin: 10 + ExpMax: 25 + WITHER_SKELETON: + ExpMin: 30 + ExpMax: 50 MobSettings: ZOMBIE: Health: 40 - Attack: + Attack: 15 + CREEPER: + Health: 60 + Attack: 60 + SKELETON: + Health: 55 + Attack: 20 + SPIDER: + Health: 35 + Attack: 30 + GHAST: + Health: 70 + Attack: 40 + COW: + Health: 100 + Attack: 10 + CHICKEN: + Health: 50 + Attack: 5 + SHEEP: + Health: 75 + Attack: 10 + ENDERMAN: + Health: 125 + Attack: 40 + SILVERFISH: + Health: 40 + Attack: 20 + HUSK: + Health: 55 + Attack: 20 + BLAZE: + Health: 110 + Attack: 35 + WITHER_SKELETON: + Health: 180 + Attack: 45 +