Skip to content

Commit

Permalink
Refactor MobKill context checker
Browse files Browse the repository at this point in the history
  • Loading branch information
imDaniX committed Feb 29, 2024
1 parent d8e9174 commit 96a7594
Showing 1 changed file with 15 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;
import java.util.Map;
Expand All @@ -46,18 +47,18 @@ public class MobKillActivator extends Activator {
private final String mobType;
private final String mobName;

private MobKillActivator(Logic base, String type, String name) {
private MobKillActivator(Logic base, @Nullable String type, @Nullable String name) {
super(base);
this.mobType = type;
this.mobName = name;
this.mobName = name == null ? null : ChatColor.translateAlternateColorCodes('&', name);
}

public static MobKillActivator create(Logic base, Parameters param) {
String type = param.originValue();
String name = "";
String name = null;
if (param.contains("type")) {
type = param.getString("type");
name = param.getString("name");
name = param.getString("name", null);
} else if (param.originValue().contains("$")) {
name = type.substring(0, type.indexOf('$'));
type = type.substring(name.length() + 1);
Expand All @@ -66,30 +67,20 @@ public static MobKillActivator create(Logic base, Parameters param) {
}

public static MobKillActivator load(Logic base, ConfigurationSection cfg) {
String type = cfg.getString("mob-type", "");
String name = cfg.getString("mob-name", "");
String type = cfg.getString("mob-type");
String name = cfg.getString("mob-name");
return new MobKillActivator(base, type, name);
}

@Override
public boolean checkContext(@NotNull ActivationContext context) {
Context me = (Context) context;
if (mobType.isEmpty()) return false;
if (me.entity == null) return false;
return isActivatorMob(me.entity);
}

private boolean isActivatorMob(LivingEntity mob) {
if (!mobName.isEmpty()) {
if (!ChatColor.translateAlternateColorCodes('&', mobName.replace("_", " ")).equals(getMobName(mob)))
return false;
} else if (!getMobName(mob).isEmpty()) return false;
return mob.getType().name().equalsIgnoreCase(this.mobType);
}

private String getMobName(LivingEntity mob) {
if (mob.getCustomName() == null) return "";
return mob.getCustomName();
if (mobType != null && !me.entity.getType().name().equals(mobType)) return false;
if (mobName != null) {
if (mobName.isEmpty()) return me.entity.getCustomName() == null;
return mobName.equals(me.entity.getCustomName());
}
return true;
}

@Override
Expand All @@ -106,8 +97,8 @@ public boolean isValid() {
@Override
public String toString() {
String sb = super.toString() + " (" +
"type:" + (mobType.isEmpty() ? "-" : mobType.toUpperCase(Locale.ROOT)) +
" name:" + (mobName.isEmpty() ? "-" : mobName) +
"type:" + (mobType == null ? "-" : mobType.toUpperCase(Locale.ROOT)) +
" name:" + (mobName == null ? "-" : mobName) +
")";
return sb;
}
Expand Down

0 comments on commit 96a7594

Please sign in to comment.