From 52a80622ce225d220a7200fda3e6ff748c6094eb Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Fri, 20 Jan 2023 22:17:40 +0200 Subject: [PATCH] Fix sonar smells in fabric module --- .../listeners/fabric/DeathEventListener.java | 22 +++++++------- .../plugin/server/FabricPluginLogger.java | 29 +++++++++++-------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Plan/fabric/src/main/java/net/playeranalytics/plan/gathering/listeners/fabric/DeathEventListener.java b/Plan/fabric/src/main/java/net/playeranalytics/plan/gathering/listeners/fabric/DeathEventListener.java index 647c77fef..253d2cadf 100644 --- a/Plan/fabric/src/main/java/net/playeranalytics/plan/gathering/listeners/fabric/DeathEventListener.java +++ b/Plan/fabric/src/main/java/net/playeranalytics/plan/gathering/listeners/fabric/DeathEventListener.java @@ -59,9 +59,9 @@ public class DeathEventListener implements FabricListener { @Override public void register() { - ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.register((world, killer, killedEntity) -> { - PlanFabricEvents.ON_KILLED.invoker().onKilled(killedEntity, killer); - }); + ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.register((world, killer, killedEntity) -> + PlanFabricEvents.ON_KILLED.invoker().onKilled(killedEntity, killer) + ); PlanFabricEvents.ON_KILLED.register((victim, killer) -> { if (!this.isEnabled) { return; @@ -101,14 +101,14 @@ public class DeathEventListener implements FabricListener { } public Optional getCause(Entity killer) { - if (killer instanceof ServerPlayerEntity) return Optional.of((ServerPlayerEntity) killer); - if (killer instanceof TameableEntity) return getOwner((TameableEntity) killer); - if (killer instanceof ProjectileEntity) return getShooter((ProjectileEntity) killer); + if (killer instanceof ServerPlayerEntity player) return Optional.of(player); + if (killer instanceof TameableEntity tamed) return getOwner(tamed); + if (killer instanceof ProjectileEntity projectile) return getShooter(projectile); return Optional.empty(); } public String findWeapon(Entity killer) { - if (killer instanceof ServerPlayerEntity) return getItemInHand((ServerPlayerEntity) killer); + if (killer instanceof ServerPlayerEntity player) return getItemInHand(player); // Projectile, EnderCrystal and all other causes that are not known yet return new EntityNameFormatter().apply(killer.getType().getName().getString()); @@ -121,8 +121,8 @@ public class DeathEventListener implements FabricListener { private Optional getShooter(ProjectileEntity projectile) { Entity source = projectile.getOwner(); - if (source instanceof ServerPlayerEntity) { - return Optional.of((ServerPlayerEntity) source); + if (source instanceof ServerPlayerEntity player) { + return Optional.of(player); } return Optional.empty(); @@ -134,8 +134,8 @@ public class DeathEventListener implements FabricListener { } Entity owner = tameable.getOwner(); - if (owner instanceof ServerPlayerEntity) { - return Optional.of((ServerPlayerEntity) owner); + if (owner instanceof ServerPlayerEntity player) { + return Optional.of(player); } return Optional.empty(); diff --git a/Plan/fabric/src/main/java/net/playeranalytics/plugin/server/FabricPluginLogger.java b/Plan/fabric/src/main/java/net/playeranalytics/plugin/server/FabricPluginLogger.java index 972a1bf67..dfb3ecabd 100644 --- a/Plan/fabric/src/main/java/net/playeranalytics/plugin/server/FabricPluginLogger.java +++ b/Plan/fabric/src/main/java/net/playeranalytics/plugin/server/FabricPluginLogger.java @@ -16,49 +16,54 @@ */ package net.playeranalytics.plugin.server; +import com.djrapitops.plan.component.Component; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.NotNull; public class FabricPluginLogger implements PluginLogger { + private static final String CHAT_COLOR_REGEX = "§[0-9a-fk-or]"; + private static final String MESSAGE_FORMAT = "[Plan] {}"; + private final Logger logger; public FabricPluginLogger(Logger logger) { this.logger = logger; } - @Override - public PluginLogger info(String message) { - logger.info("[Plan] " + message.replaceAll("§[0-9a-fk-or]", "")); - return this; + @NotNull + private static String removeChatColors(String message) { + return message.replaceAll(CHAT_COLOR_REGEX, ""); } - public void info(String message, Object... args) { - String replacedMsg = message.replaceAll("(?<=\\{).+?(?=})", ""); - String formattedMsg = "[Plan] " + replacedMsg; - logger.info(formattedMsg.replaceAll("§[0-9a-fk-or]", ""), args); + @Override + public PluginLogger info(String message) { + logger.info(MESSAGE_FORMAT, StringUtils.contains(message, Component.SECTION) ? removeChatColors(message) : message); + return this; } @Override public PluginLogger warn(String message) { - logger.warn("[Plan] " + message.replaceAll("§[0-9a-fk-or]", "")); + logger.warn(MESSAGE_FORMAT, StringUtils.contains(message, Component.SECTION) ? removeChatColors(message) : message); return this; } @Override public PluginLogger error(String message) { - logger.error("[Plan] " + message.replaceAll("§[0-9a-fk-or]", "")); + logger.error(MESSAGE_FORMAT, StringUtils.contains(message, Component.SECTION) ? removeChatColors(message) : message); return this; } @Override public PluginLogger warn(String message, Throwable throwable) { - logger.warn("[Plan] " + message.replaceAll("§[0-9a-fk-or]", ""), throwable); + logger.warn(MESSAGE_FORMAT, StringUtils.contains(message, Component.SECTION) ? removeChatColors(message) : message, throwable); return this; } @Override public PluginLogger error(String message, Throwable throwable) { - logger.error("[Plan] " + message.replaceAll("§[0-9a-fk-or]", ""), throwable); + logger.error(MESSAGE_FORMAT, StringUtils.contains(message, Component.SECTION) ? removeChatColors(message) : message, throwable); return this; } }