Skip to content

Commit 8d0aab8

Browse files
committed
Code cleanup and refactoring
1 parent 4b66002 commit 8d0aab8

5 files changed

Lines changed: 73 additions & 97 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>ADHDMC</groupId>
88
<artifactId>NerfFarms</artifactId>
9-
<version>0.0.9</version>
9+
<version>0.0.10</version>
1010
<packaging>jar</packaging>
1111

1212
<name>NerfFarms</name>

src/main/java/adhdmc/nerffarms/NerfFarms.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public final class NerfFarms extends JavaPlugin {
1717
public static NerfFarms plugin;
1818
public final MiniMessage miniMessage = MiniMessage.miniMessage();
19-
public final String version = "0.0.9";
19+
public final String version = "0.0.10";
2020

2121
@Override
2222
public void onEnable() {

src/main/java/adhdmc/nerffarms/config/ConfigParser.java

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@
1212

1313
public class ConfigParser {
1414
public enum ModType {EXP, DROPS, BOTH, NEITHER}
15+
public enum ConfigToggles {
16+
//Debug
17+
DEBUG,
18+
//Bypass toggles
19+
ONLY_NERF_HOSTILES, ALLOW_SKELETON_CREEPER_DAMAGE, ALLOW_WITHER_DAMAGE,
20+
//Nerfing checks
21+
REQUIRE_PATH, REQUIRE_LINE_OF_SIGHT, ALLOW_PROJECTILE_DAMAGE
22+
}
1523

1624
private static final HashSet<Material> standOnBlacklist = new HashSet<>();
1725
private static final HashSet<Material> insideBlacklist = new HashSet<>();
1826
private static final HashSet<EntityType> bypassList = new HashSet<>();
1927
private static final HashSet<CreatureSpawnEvent.SpawnReason> spawnReasonList = new HashSet<>();
2028
private static final HashSet<EntityDamageEvent.DamageCause> disallowedDamageTypes = new HashSet<>();
2129
private static ModType modType = ModType.NEITHER;
30+
private static final HashMap<ConfigToggles, Boolean> configToggles = new HashMap<>();
2231
private static int maxDistance = 0;
2332
private static int errorCount = 0;
2433
private static int maxDisallowedDamage = 100;
25-
private static boolean nerfHostilesOnly = true;
26-
private static boolean requirePath = false;
27-
private static boolean requireLineOfSight = false;
28-
private static boolean allowProjectileDamage = true;
29-
private static boolean skeletonsDamageCreepers = true;
30-
private static boolean withersDamageEntities = true;
31-
private static boolean debug = false;
3234

3335
public static void validateConfig() {
3436
//you're doing the best you can, config.
@@ -39,16 +41,10 @@ public static void validateConfig() {
3941
spawnReasonList.clear();
4042
disallowedDamageTypes.clear();
4143
modType = null;
44+
configToggles.clear();
4245
maxDistance = 0;
4346
errorCount = 0;
4447
maxDisallowedDamage = 100;
45-
nerfHostilesOnly = true;
46-
requirePath = false;
47-
requireLineOfSight = false;
48-
allowProjectileDamage = true;
49-
skeletonsDamageCreepers = true;
50-
withersDamageEntities = true;
51-
debug = false;
5248
FileConfiguration config = NerfFarms.plugin.getConfig();
5349
List<String> standStringList = config.getStringList("blacklisted-below");
5450
List<String> inStringList = config.getStringList("blacklisted-in");
@@ -57,7 +53,7 @@ public static void validateConfig() {
5753
List<String> disallowedDamageTypesList = config.getStringList("disallowed-damage-types");
5854
String modificationTypeString = config.getString("modification-type");
5955
int maxDistanceInt = config.getInt("max-distance");
60-
int maxDisallowedDamage = config.getInt("max-disallowed-damage-percent");
56+
int maxDisallowedDamageConfig = config.getInt("max-disallowed-damage-percent");
6157
boolean nerfHostilesBoolean = config.getBoolean("only-nerf-hostiles");
6258
boolean requirePathBoolean = config.getBoolean("require-path");
6359
boolean requireLineOfSightBoolean = config.getBoolean("require-line-of-sight");
@@ -123,7 +119,7 @@ public static void validateConfig() {
123119
for (String type : disallowedDamageTypesList) {
124120
try {
125121
EntityDamageEvent.DamageCause.valueOf(type);
126-
} catch (IllegalArgumentException e) {
122+
} catch (IllegalArgumentException exception) {
127123
NerfFarms.plugin.getLogger().warning(type + " is not a valid damage type. Please check that you have entered this correctly.");
128124
errorCount = errorCount + 1;
129125
continue;
@@ -134,7 +130,7 @@ public static void validateConfig() {
134130
// Determine modType
135131
try {
136132
modType = ModType.valueOf(modificationTypeString);
137-
} catch (IllegalArgumentException e) {
133+
} catch (IllegalArgumentException exception) {
138134
NerfFarms.plugin.getLogger().severe(modificationTypeString + " is not a valid modification type. Plugin will not function properly until this is fixed.");
139135
modType = ModType.NEITHER;
140136
}
@@ -149,20 +145,22 @@ public static void validateConfig() {
149145
}
150146

151147
// Determine Percent Damage from Environment
152-
if (maxDisallowedDamage <= 0 || maxDisallowedDamage > 100) {
148+
if (maxDisallowedDamageConfig <= 0 || maxDisallowedDamageConfig > 100) {
153149
NerfFarms.plugin.getLogger().warning("Percent damage from Environment must be between 1 and 100, setting to 100");
154150
errorCount = errorCount + 1;
155151
maxDisallowedDamage = 100;
152+
} else {
153+
maxDisallowedDamage = maxDisallowedDamageConfig;
156154
}
157155

158156
// Set Booleans
159-
nerfHostilesOnly = nerfHostilesBoolean;
160-
requirePath = requirePathBoolean;
161-
requireLineOfSight = requireLineOfSightBoolean;
162-
allowProjectileDamage = allowProjectileDamageBoolean;
163-
skeletonsDamageCreepers = skeletonsDamageCreepersBoolean;
164-
withersDamageEntities = withersDamageEntitiesBoolean;
165-
debug = debugSetting;
157+
configToggles.put(ConfigToggles.DEBUG, debugSetting);
158+
configToggles.put(ConfigToggles.ONLY_NERF_HOSTILES, nerfHostilesBoolean);
159+
configToggles.put(ConfigToggles.ALLOW_SKELETON_CREEPER_DAMAGE, skeletonsDamageCreepersBoolean);
160+
configToggles.put(ConfigToggles.ALLOW_WITHER_DAMAGE, withersDamageEntitiesBoolean);
161+
configToggles.put(ConfigToggles.REQUIRE_PATH, requirePathBoolean);
162+
configToggles.put(ConfigToggles.REQUIRE_LINE_OF_SIGHT, requireLineOfSightBoolean);
163+
configToggles.put(ConfigToggles.ALLOW_PROJECTILE_DAMAGE, allowProjectileDamageBoolean);
166164
}
167165

168166
public static Set<Material> getStandOnBlackList() {
@@ -203,31 +201,8 @@ public static int getErrorCount() {
203201
return errorCount;
204202
}
205203

206-
public static boolean isNerfHostilesOnly() {
207-
return nerfHostilesOnly;
208-
}
209-
210-
public static boolean isRequirePath() {
211-
return requirePath;
212-
}
213-
214-
public static boolean isRequireLineOfSight() {
215-
return requireLineOfSight;
216-
}
217-
public static boolean isAllowProjectileDamage() {
218-
return allowProjectileDamage;
219-
}
220-
221-
public static boolean isSkeletonsDamageCreepers() {
222-
return skeletonsDamageCreepers;
223-
}
224-
225-
public static boolean isWithersDamageEntities() {
226-
return withersDamageEntities;
227-
}
228-
229-
public static boolean isDebug() {
230-
return debug;
204+
public static Map<ConfigToggles, Boolean> getConfigToggles() {
205+
return Collections.unmodifiableMap(configToggles);
231206
}
232207

233208
public static int getMaxDisallowedDamage() {

src/main/java/adhdmc/nerffarms/listener/MobDamageListener.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,23 @@
1616
import org.bukkit.persistence.PersistentDataType;
1717
import org.bukkit.projectiles.ProjectileSource;
1818

19+
import java.util.Map;
1920
import java.util.Objects;
2021
import java.util.logging.Logger;
2122

2223
public class MobDamageListener implements Listener {
2324
public static final NamespacedKey nerfMob = new NamespacedKey(NerfFarms.plugin, "nerf-mob");
2425
public static final NamespacedKey disallowedDamage = new NamespacedKey(NerfFarms.plugin, "disallowed-damage");
25-
private static boolean debugSetting;
26+
private static final Map<ConfigParser.ConfigToggles, Boolean> configToggles = ConfigParser.getConfigToggles();
27+
boolean debugSetting = false;
2628
private static Logger logger;
27-
private static final byte f = 0;
2829
private static final byte t = 1;
2930

3031
@EventHandler
3132
public void onMobDamage(EntityDamageEvent damageEvent) {
32-
debugSetting = ConfigParser.isDebug();
33+
debugSetting = configToggles.get(ConfigParser.ConfigToggles.DEBUG);
3334
logger = NerfFarms.plugin.getLogger();
3435
Entity damagedEntity = damageEvent.getEntity();
35-
PersistentDataContainer entityPDC = damagedEntity.getPersistentDataContainer();
36-
3736
// Ignore Event Checks
3837
if (!isMob(damagedEntity)) {
3938
return;
@@ -50,6 +49,7 @@ public void onMobDamage(EntityDamageEvent damageEvent) {
5049
if (isExemptedMob(damagedEntity)) {
5150
return;
5251
}
52+
// Nerfable Damage Checks
5353
if (isNerfableNonPlayerDamage(damageEvent)) {
5454
return;
5555
}
@@ -86,7 +86,7 @@ private boolean isProjectileDamage(EntityDamageEvent event){
8686
Entity entity = event.getEntity();
8787
PersistentDataContainer mobPDC = entity.getPersistentDataContainer();
8888
double hitDamage = event.getFinalDamage();
89-
if (!ConfigParser.isAllowProjectileDamage()){
89+
if (!configToggles.get(ConfigParser.ConfigToggles.ALLOW_PROJECTILE_DAMAGE)){
9090
if (debugSetting) {
9191
logger.info("Arrow damage is not allowed");
9292
}
@@ -127,76 +127,76 @@ private void addPDCDamage(PersistentDataContainer mobPDC, double damage) {
127127
mobPDC.set(disallowedDamage, PersistentDataType.DOUBLE, damageTotal);
128128
}
129129

130-
private boolean isMob(Entity e) {
130+
private boolean isMob(Entity entity) {
131131

132132
if (debugSetting) {
133-
logger.info("Performing isMob on " + e.getName());
133+
logger.info("Performing isMob on " + entity.getName());
134134
}
135135

136-
if (!(e instanceof Mob)) {
136+
if (!(entity instanceof Mob)) {
137137
if (debugSetting) {
138-
logger.info("Ignoring onMobDamage because " + e.getName() + " is not a mob.");
138+
logger.info("Ignoring onMobDamage because " + entity.getName() + " is not a mob.");
139139
}
140140
return false;
141141
}
142142
return true;
143143
}
144144

145-
private boolean isNerfed(Entity e) {
146-
PersistentDataContainer mobPDC = e.getPersistentDataContainer();
145+
private boolean isNerfed(Entity entity) {
146+
PersistentDataContainer mobPDC = entity.getPersistentDataContainer();
147147

148148
if (debugSetting) {
149-
logger.info("Performing isNerfed on " + e.getName());
149+
logger.info("Performing isNerfed on " + entity.getName());
150150
}
151151

152152
if (mobPDC.has(nerfMob)) {
153153
if (debugSetting) {
154-
logger.info(e.getName() + " is already nerfed, ignoring...");
154+
logger.info(entity.getName() + " is already nerfed, ignoring...");
155155
}
156156
return true;
157157
}
158158
return false;
159159
}
160160

161-
private boolean isHostileNerf(Entity e) {
161+
private boolean isHostileNerf(Entity entity) {
162162

163163
if (debugSetting) {
164-
logger.info("Performing isHostileNerf on " + e.getName());
164+
logger.info("Performing isHostileNerf on " + entity.getName());
165165
}
166166

167-
if (ConfigParser.isNerfHostilesOnly() && !(e instanceof Monster)) {
167+
if (configToggles.get(ConfigParser.ConfigToggles.ONLY_NERF_HOSTILES) && !(entity instanceof Monster)) {
168168
if (debugSetting) {
169-
logger.info("Ignoring onMobDamage because " + e.getName() + " is not a Monster and Nerf Hostiles Only is True.");
169+
logger.info("Ignoring onMobDamage because " + entity.getName() + " is not a Monster and Nerf Hostiles Only is True.");
170170
}
171171
return true;
172172
}
173173
return false;
174174
}
175175

176-
private boolean isExemptedSpawnReason(Entity e) {
176+
private boolean isExemptedSpawnReason(Entity entity) {
177177

178178
if (debugSetting) {
179-
logger.info("Performing isExemptedSpawnReason on " + e.getName());
179+
logger.info("Performing isExemptedSpawnReason on " + entity.getName());
180180
}
181181

182-
if (ConfigParser.getSpawnReasonList().contains(e.getEntitySpawnReason())) {
182+
if (ConfigParser.getSpawnReasonList().contains(entity.getEntitySpawnReason())) {
183183
if (debugSetting) {
184-
logger.info("Ignoring onMobDamage because " + e.getName() + " spawned from " + e.getEntitySpawnReason() + " which isn't nerfed.");
184+
logger.info("Ignoring onMobDamage because " + entity.getName() + " spawned from " + entity.getEntitySpawnReason() + " which isn't nerfed.");
185185
}
186186
return true;
187187
}
188188
return false;
189189
}
190190

191-
private boolean isExemptedMob(Entity e) {
191+
private boolean isExemptedMob(Entity entity) {
192192

193193
if (debugSetting) {
194-
logger.info("Performing isExemptedMob on " + e.getName());
194+
logger.info("Performing isExemptedMob on " + entity.getName());
195195
}
196196

197-
if (ConfigParser.getBypassList().contains(e.getType())) {
197+
if (ConfigParser.getBypassList().contains(entity.getType())) {
198198
if (debugSetting) {
199-
logger.info("Ignoring onMobDamage because " + e.getName() + " is on the bypass list as " + e.getType());
199+
logger.info("Ignoring onMobDamage because " + entity.getName() + " is on the bypass list as " + entity.getType());
200200
}
201201
return true;
202202
}
@@ -223,15 +223,15 @@ private boolean isNerfableEnvironmentally(EntityDamageEvent event) {
223223
return false;
224224
}
225225

226-
private void disallowedDamagePercent(PersistentDataContainer mobPDC, Entity e) {
226+
private void disallowedDamagePercent(PersistentDataContainer mobPDC, Entity entity) {
227227
int maxDisallowedDamage = ConfigParser.getMaxDisallowedDamage();
228228
double nerfedDamage = mobPDC.getOrDefault(disallowedDamage, PersistentDataType.DOUBLE, 0.0);
229-
double maxHealth = Objects.requireNonNull(((Mob) e).getAttribute(Attribute.GENERIC_MAX_HEALTH)).getValue();
229+
double maxHealth = Objects.requireNonNull(((Mob) entity).getAttribute(Attribute.GENERIC_MAX_HEALTH)).getValue();
230230
int percentDamage = (int) ((nerfedDamage / maxHealth) * 100);
231231

232232
if (percentDamage >= maxDisallowedDamage) {
233233
if (debugSetting) {
234-
logger.info("Nerfing " + e.getName() + " because they took " + percentDamage + "% total damage from nerfable causes");
234+
logger.info("Nerfing " + entity.getName() + " because they took " + percentDamage + "% total damage from nerfable causes");
235235
}
236236
mobPDC.set(nerfMob, PersistentDataType.BYTE, t);
237237
}
@@ -252,20 +252,20 @@ private boolean isNerfableNonPlayerDamage(EntityDamageEvent event) {
252252
}
253253
if (damager instanceof AbstractSkeleton &&
254254
entity instanceof Creeper &&
255-
ConfigParser.isSkeletonsDamageCreepers()) {
255+
configToggles.get(ConfigParser.ConfigToggles.ALLOW_SKELETON_CREEPER_DAMAGE)) {
256256
if (debugSetting) {
257257
logger.info("Skipping nerf on " + entity.getName() + "because 'Skeletons can damage creepers' is 'true'");
258258
}
259259
return true;
260260
}
261261
if (damager instanceof Wither &&
262-
ConfigParser.isWithersDamageEntities()) {
262+
configToggles.get(ConfigParser.ConfigToggles.ALLOW_WITHER_DAMAGE)) {
263263
if (debugSetting) {
264264
logger.info("Skipping nerf on " + entity.getName() + "because 'Withers can damage entities' is 'true'");
265265
}
266266
return true;
267267
}
268-
if (damager instanceof Projectile && ConfigParser.isAllowProjectileDamage()){
268+
if (damager instanceof Projectile && configToggles.get(ConfigParser.ConfigToggles.ALLOW_PROJECTILE_DAMAGE)){
269269
return false;
270270
}
271271

@@ -329,7 +329,7 @@ private boolean isNerfableInBlock(EntityDamageEvent event) {
329329
private boolean hasBlockedLineofSight(EntityDamageEvent event) {
330330
if (!(event instanceof EntityDamageByEntityEvent)) return false;
331331
if (!(event.getEntity() instanceof LivingEntity entity)) return false;
332-
if (!ConfigParser.isRequireLineOfSight()) return true;
332+
if (!configToggles.get(ConfigParser.ConfigToggles.REQUIRE_LINE_OF_SIGHT)) return true;
333333

334334
Entity damager = ((EntityDamageByEntityEvent) event).getDamager();
335335
PersistentDataContainer mobPDC = entity.getPersistentDataContainer();
@@ -348,7 +348,7 @@ private boolean hasBlockedLineofSight(EntityDamageEvent event) {
348348
}
349349

350350
private boolean canMobMoveToward(EntityDamageEvent event) {
351-
if (!ConfigParser.isRequirePath()) return false;
351+
if (!configToggles.get(ConfigParser.ConfigToggles.REQUIRE_PATH)) return false;
352352
if (!(event instanceof EntityDamageByEntityEvent)) return false;
353353
if (!(event.getEntity() instanceof LivingEntity entity)) return false;
354354
Entity damager = ((EntityDamageByEntityEvent) event).getDamager();

0 commit comments

Comments
 (0)