Skip to content

Commit

Permalink
Working EXP & Mob Damages
Browse files Browse the repository at this point in the history
Mod damage and health can now be configured in config.yml

EXP is now working but it still has a few "glitches" to sort out and perfect
  • Loading branch information
Burchard36 committed Aug 6, 2021
1 parent 37ea5d8 commit 15979c8
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 98 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/myplugin/MyPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. . .");
Expand All @@ -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");
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/com/myplugin/events/ExperienceHandler.java
Original file line number Diff line number Diff line change
@@ -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<EntityType, MobExp> 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;
}
}
}
101 changes: 101 additions & 0 deletions src/main/java/com/myplugin/events/MobSpawnManager.java
Original file line number Diff line number Diff line change
@@ -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<EntityType, MobStat> mobStats;
private Set<EntityType> 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;
}

}
}
16 changes: 12 additions & 4 deletions src/main/java/com/myplugin/events/PlayerAttackEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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);
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/myplugin/events/PlayerFalls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.myplugin.events;

import org.bukkit.event.EventHandler;

public class PlayerFalls {

}
26 changes: 0 additions & 26 deletions src/main/java/com/myplugin/events/PlayerKillsEntity.java

This file was deleted.

42 changes: 0 additions & 42 deletions src/main/java/com/myplugin/lib/ExperienceHandler.java

This file was deleted.

Loading

0 comments on commit 15979c8

Please sign in to comment.