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
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<ServerPlayerEntity> 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<ServerPlayerEntity> 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();

View File

@ -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;
}
}