Skip to content

Commit

Permalink
misc: Improved ChatRegulator Expansion registration
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed May 11, 2023
1 parent ea2aff5 commit 8b8d36a
Showing 1 changed file with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
package io.github._4drian3d.chatregulator.plugin.placeholders;

import com.velocitypowered.api.proxy.Player;
import io.github._4drian3d.chatregulator.api.InfractionPlayer;
import io.github._4drian3d.chatregulator.api.PlayerManager;
import io.github._4drian3d.chatregulator.api.enums.InfractionType;
import io.github._4drian3d.chatregulator.plugin.impl.InfractionPlayerImpl;
import io.github._4drian3d.chatregulator.plugin.impl.PlayerManagerImpl;
import io.github.miniplaceholders.api.Expansion;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.minimessage.tag.Tag;

import java.util.Locale;
import java.util.UUID;

public final class RegulatorExpansion {
private RegulatorExpansion(){}
public static Expansion getExpansion(PlayerManagerImpl manager){
public static Expansion getExpansion(PlayerManager manager) {
return Expansion.builder("chatregulator")
.filter(Player.class)
.audiencePlaceholder("infractions_count", (aud, queue, ctx) -> {
InfractionPlayerImpl player = manager.getPlayer((Player)aud);
String type = queue.popOr(() -> "you need to introduce the infraction type").value();
return Tag.selfClosingInserting(
Component.text(
player.getInfractions().getCount(
switch(type.toLowerCase(Locale.ROOT)){
case "flood" -> InfractionType.FLOOD;
case "spam" -> InfractionType.SPAM;
case "regular" -> InfractionType.REGEX;
case "command" -> InfractionType.BLOCKED_COMMAND;
case "unicode" -> InfractionType.UNICODE;
case "syntax" -> InfractionType.SYNTAX;
case "global" -> InfractionType.GLOBAL;
default -> throw ctx.newException("Invalid Infraction Type");
}
)
)
);
final UUID uuid = aud.get(Identity.UUID).orElse(null);
if (uuid == null) {
return null;
}

final InfractionPlayer player = manager.getPlayer(uuid);
final String type = queue.popOr("you need to introduce the infraction type")
.value().toUpperCase(Locale.ROOT);
final InfractionType infractionType = InfractionType.INDEX.valueOrThrow(type);

return Tag.preProcessParsed(Integer.toString(player.getInfractions().getCount(infractionType)));
})
.build();
}
Expand Down

0 comments on commit 8b8d36a

Please sign in to comment.