feat,refactor: team joiner plugin and some refactoring
This commit is contained in:
@@ -81,6 +81,7 @@ public class Bot {
|
||||
public MailPlugin mail;
|
||||
public PacketSnifferPlugin packetSniffer;
|
||||
public VoiceChatPlugin voiceChat;
|
||||
public TeamJoinerPlugin teamJoiner;
|
||||
public TagPlugin tag;
|
||||
public WorldPlugin world;
|
||||
public AuthPlugin auth;
|
||||
@@ -129,6 +130,7 @@ public class Bot {
|
||||
this.mail = new MailPlugin(this);
|
||||
this.packetSniffer = new PacketSnifferPlugin(this);
|
||||
this.voiceChat = new VoiceChatPlugin(this);
|
||||
this.teamJoiner = new TeamJoinerPlugin(this);
|
||||
this.tag = new TagPlugin(this);
|
||||
this.world = new WorldPlugin(this);
|
||||
this.auth = new AuthPlugin(this);
|
||||
|
||||
@@ -17,6 +17,7 @@ public class Configuration {
|
||||
public String weatherApiKey;
|
||||
|
||||
public String bossBarNamespace = "chomens_bot";
|
||||
public String teamName = "chomens_bot";
|
||||
|
||||
public Core core = new Core();
|
||||
public Discord discord = new Discord();
|
||||
|
||||
@@ -8,8 +8,11 @@ import org.geysermc.mcprotocollib.protocol.data.game.scoreboard.NameTagVisibilit
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.scoreboard.TeamColor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Team {
|
||||
public String teamName;
|
||||
public List<String> players;
|
||||
public Component displayName;
|
||||
public boolean friendlyFire;
|
||||
public boolean seeFriendlyInvisibles;
|
||||
@@ -21,6 +24,7 @@ public class Team {
|
||||
|
||||
public Team (
|
||||
String teamName,
|
||||
List<String> players,
|
||||
Component displayName,
|
||||
boolean friendlyFire,
|
||||
boolean seeFriendlyInvisibles,
|
||||
@@ -31,6 +35,7 @@ public class Team {
|
||||
Component suffix
|
||||
) {
|
||||
this.teamName = teamName;
|
||||
this.players = players;
|
||||
this.displayName = displayName;
|
||||
this.friendlyFire = friendlyFire;
|
||||
this.seeFriendlyInvisibles = seeFriendlyInvisibles;
|
||||
|
||||
@@ -120,7 +120,10 @@ public class ChatPlugin extends Bot.Listener {
|
||||
case 4 -> Component.translatable("%s", name, message); // paper chat type thingy
|
||||
case 5 -> Component.translatable("chat.type.announcement", name, message); // /say
|
||||
case 6, 7 -> {
|
||||
final Team botTeam = bot.team.teamsByPlayer.get(bot.profile.getName());
|
||||
final Team botTeam = bot.team.findTeamByMember(bot.profile.getName());
|
||||
|
||||
if (botTeam == null) yield null;
|
||||
|
||||
final Component botTeamDisplayName = Component
|
||||
.translatable("chat.square_brackets")
|
||||
.arguments(botTeam.displayName)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package me.chayapak1.chomens_bot.plugins;
|
||||
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.data.Team;
|
||||
import me.chayapak1.chomens_bot.util.UUIDUtilities;
|
||||
|
||||
// the name might sound confusing but it just adds the bot into its own team
|
||||
public class TeamJoinerPlugin extends TickPlugin.Listener {
|
||||
public final String teamName;
|
||||
|
||||
private final Bot bot;
|
||||
|
||||
public TeamJoinerPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
this.teamName = bot.config.teamName;
|
||||
|
||||
bot.tick.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTick () {
|
||||
final Team team = bot.team.findTeamByName(teamName);
|
||||
|
||||
if (team == null) {
|
||||
addTeam();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!team.players.contains(bot.username)) joinTeam();
|
||||
|
||||
for (String player : team.players) {
|
||||
if (!player.equals(bot.username)) {
|
||||
excludeOthers();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addTeam () {
|
||||
bot.core.run("minecraft:team add " + teamName);
|
||||
joinTeam();
|
||||
}
|
||||
|
||||
private void joinTeam () {
|
||||
bot.core.run("minecraft:team join " + teamName + " " + UUIDUtilities.selector(bot.profile.getId()));
|
||||
}
|
||||
|
||||
private void excludeOthers () {
|
||||
bot.core.run(
|
||||
String.format(
|
||||
"minecraft:team leave %s,team=%s]",
|
||||
UUIDUtilities.exclusiveSelector(bot.profile.getId(), false),
|
||||
teamName
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,38 @@
|
||||
package me.chayapak1.chomens_bot.plugins;
|
||||
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.data.Team;
|
||||
import org.geysermc.mcprotocollib.network.Session;
|
||||
import org.geysermc.mcprotocollib.network.packet.Packet;
|
||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.scoreboard.ClientboundSetPlayerTeamPacket;
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.data.Team;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class TeamPlugin extends Bot.Listener {
|
||||
public final Map<String, Team> teams = new HashMap<>();
|
||||
public final Map<String, Team> teamsByPlayer = new HashMap<>();
|
||||
public final List<Team> teams = new ArrayList<>();
|
||||
|
||||
public TeamPlugin (Bot bot) {
|
||||
bot.addListener(this);
|
||||
}
|
||||
|
||||
public Team findTeamByName (String name) {
|
||||
for (Team team : teams) {
|
||||
if (team.teamName.equals(name)) return team;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Team findTeamByMember (String member) {
|
||||
for (Team team : teams) {
|
||||
if (team.players.contains(member)) return team;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetReceived(Session session, Packet packet) {
|
||||
if (packet instanceof ClientboundSetPlayerTeamPacket) packetReceived((ClientboundSetPlayerTeamPacket) packet);
|
||||
@@ -28,6 +44,7 @@ public class TeamPlugin extends Bot.Listener {
|
||||
case CREATE -> {
|
||||
final Team team = new Team(
|
||||
packet.getTeamName(),
|
||||
new ArrayList<>(),
|
||||
packet.getDisplayName(),
|
||||
packet.isFriendlyFire(),
|
||||
packet.isSeeFriendlyInvisibles(),
|
||||
@@ -38,11 +55,17 @@ public class TeamPlugin extends Bot.Listener {
|
||||
packet.getSuffix()
|
||||
);
|
||||
|
||||
teams.put(packet.getTeamName(), team);
|
||||
teams.add(team);
|
||||
}
|
||||
case REMOVE -> {
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
teams.remove(team);
|
||||
}
|
||||
case REMOVE -> teams.remove(packet.getTeamName());
|
||||
case UPDATE -> {
|
||||
final Team team = teams.get(packet.getTeamName());
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
@@ -57,20 +80,22 @@ public class TeamPlugin extends Bot.Listener {
|
||||
team.suffix = packet.getSuffix();
|
||||
}
|
||||
case ADD_PLAYER -> {
|
||||
final Team team = teams.get(packet.getTeamName());
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
for (String player : packet.getPlayers()) teamsByPlayer.put(player, team);
|
||||
team.players.addAll(Arrays.asList(packet.getPlayers()));
|
||||
}
|
||||
case REMOVE_PLAYER -> {
|
||||
final Team team = teams.get(packet.getTeamName());
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
for (String player : packet.getPlayers()) teamsByPlayer.remove(player);
|
||||
team.players.removeAll(Arrays.asList(packet.getPlayers()));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) { e.printStackTrace(); }
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,9 @@ public class UUIDUtilities {
|
||||
return "[I;" + array[0] + "," + array[1] + "," + array[2] + "," + array[3] + "]"; // TODO: improve lol
|
||||
}
|
||||
|
||||
public static String selector (UUID uuid) { return "@a[limit=1,nbt={UUID:" + snbt(uuid) + "}]"; }
|
||||
public static String exclusiveSelector (UUID uuid) { return "@a[nbt=!{UUID:" + snbt(uuid) + "}]"; }
|
||||
public static String selector (UUID uuid) { return selector(uuid, true); }
|
||||
public static String selector (UUID uuid, boolean end) { return "@a[limit=1,nbt={UUID:" + snbt(uuid) + "}" + (end ? "]" : ""); }
|
||||
|
||||
public static String exclusiveSelector (UUID uuid) { return exclusiveSelector(uuid, true); }
|
||||
public static String exclusiveSelector (UUID uuid, boolean end) { return "@a[nbt=!{UUID:" + snbt(uuid) + "}" + (end ? "]" : ""); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user