Fix sonar smells in fabric module

This commit is contained in:
Aurora Lahtela 2023-01-20 22:17:40 +02:00
parent 2eba115f6f
commit 52a80622ce
2 changed files with 28 additions and 23 deletions

View File

@ -59,9 +59,9 @@ public class DeathEventListener implements FabricListener {
@Override @Override
public void register() { public void register() {
ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.register((world, killer, killedEntity) -> { ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.register((world, killer, killedEntity) ->
PlanFabricEvents.ON_KILLED.invoker().onKilled(killedEntity, killer); PlanFabricEvents.ON_KILLED.invoker().onKilled(killedEntity, killer)
}); );
PlanFabricEvents.ON_KILLED.register((victim, killer) -> { PlanFabricEvents.ON_KILLED.register((victim, killer) -> {
if (!this.isEnabled) { if (!this.isEnabled) {
return; return;
@ -101,14 +101,14 @@ public class DeathEventListener implements FabricListener {
} }
public Optional<ServerPlayerEntity> getCause(Entity killer) { public Optional<ServerPlayerEntity> getCause(Entity killer) {
if (killer instanceof ServerPlayerEntity) return Optional.of((ServerPlayerEntity) killer); if (killer instanceof ServerPlayerEntity player) return Optional.of(player);
if (killer instanceof TameableEntity) return getOwner((TameableEntity) killer); if (killer instanceof TameableEntity tamed) return getOwner(tamed);
if (killer instanceof ProjectileEntity) return getShooter((ProjectileEntity) killer); if (killer instanceof ProjectileEntity projectile) return getShooter(projectile);
return Optional.empty(); return Optional.empty();
} }
public String findWeapon(Entity killer) { 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 // Projectile, EnderCrystal and all other causes that are not known yet
return new EntityNameFormatter().apply(killer.getType().getName().getString()); return new EntityNameFormatter().apply(killer.getType().getName().getString());
@ -121,8 +121,8 @@ public class DeathEventListener implements FabricListener {
private Optional<ServerPlayerEntity> getShooter(ProjectileEntity projectile) { private Optional<ServerPlayerEntity> getShooter(ProjectileEntity projectile) {
Entity source = projectile.getOwner(); Entity source = projectile.getOwner();
if (source instanceof ServerPlayerEntity) { if (source instanceof ServerPlayerEntity player) {
return Optional.of((ServerPlayerEntity) source); return Optional.of(player);
} }
return Optional.empty(); return Optional.empty();
@ -134,8 +134,8 @@ public class DeathEventListener implements FabricListener {
} }
Entity owner = tameable.getOwner(); Entity owner = tameable.getOwner();
if (owner instanceof ServerPlayerEntity) { if (owner instanceof ServerPlayerEntity player) {
return Optional.of((ServerPlayerEntity) owner); return Optional.of(player);
} }
return Optional.empty(); return Optional.empty();

View File

@ -16,49 +16,54 @@
*/ */
package net.playeranalytics.plugin.server; 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.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
public class FabricPluginLogger implements PluginLogger { 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; private final Logger logger;
public FabricPluginLogger(Logger logger) { public FabricPluginLogger(Logger logger) {
this.logger = logger; this.logger = logger;
} }
@Override @NotNull
public PluginLogger info(String message) { private static String removeChatColors(String message) {
logger.info("[Plan] " + message.replaceAll("§[0-9a-fk-or]", "")); return message.replaceAll(CHAT_COLOR_REGEX, "");
return this;
} }
public void info(String message, Object... args) { @Override
String replacedMsg = message.replaceAll("(?<=\\{).+?(?=})", ""); public PluginLogger info(String message) {
String formattedMsg = "[Plan] " + replacedMsg; logger.info(MESSAGE_FORMAT, StringUtils.contains(message, Component.SECTION) ? removeChatColors(message) : message);
logger.info(formattedMsg.replaceAll("§[0-9a-fk-or]", ""), args); return this;
} }
@Override @Override
public PluginLogger warn(String message) { 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; return this;
} }
@Override @Override
public PluginLogger error(String message) { 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; return this;
} }
@Override @Override
public PluginLogger warn(String message, Throwable throwable) { 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; return this;
} }
@Override @Override
public PluginLogger error(String message, Throwable throwable) { 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; return this;
} }
} }