feat: add User class

This commit is contained in:
ColdDev 2022-10-23 22:50:54 +01:00
parent 5a28ececd2
commit a62fca9405
5 changed files with 175 additions and 2 deletions

View File

@ -1,4 +1,4 @@
package darverdevs; package uranium;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -9,8 +9,10 @@ public class Main extends JavaPlugin {
} }
private void setupConfig() { private void setupConfig() {
getLogger().info("Initializing config...");
getConfig().options().copyDefaults(true); getConfig().options().copyDefaults(true);
saveConfig(); saveConfig();
getLogger().info("Done!");
} }
} }

View File

@ -0,0 +1,127 @@
package uranium.user;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import uranium.Main;
import uranium.util.StringUtil;
// To my knowledge, there's no other way around the warnings in `sendBungeeMessage`.
@SuppressWarnings({"UnstableApiUsage"})
public class User {
private final Player player;
private final String name;
private String lastMessage;
private Long lastMessageMS;
private String ip;
private String domain;
private boolean vanished;
private boolean staffChat;
private boolean commandSpy;
public User(Player player) {
this.player = player;
this.name = player.getName();
}
public Player getPlayer() {
return player;
}
public String getName() {
return name;
}
public boolean hasPermission(String permission) {
return player.hasPermission(permission);
}
public String getLastMessage() {
return lastMessage;
}
public void setLastMessage(String lastMessage) {
this.lastMessage = lastMessage;
}
public Long getLastMessageMS() {
return lastMessageMS;
}
public void setLastMessageMS(Long lastMessageMS) {
this.lastMessageMS = lastMessageMS;
}
public String getIP() {
return ip;
}
public void setIP(String ip) {
this.ip = ip;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public boolean isVanished() {
return vanished;
}
public void setVanished(boolean vanished) {
this.vanished = vanished;
if (vanished)
UserManager.getUsers().forEach((user) -> {
if (!user.hasPermission("uranium.vanish"))
user.getPlayer().hidePlayer(player);
});
else UserManager.getUsers().forEach((user) -> user.getPlayer().showPlayer(player));
}
public boolean isStaffChat() {
return staffChat;
}
public void setStaffChat(boolean staffChat) {
this.staffChat = staffChat;
}
public boolean isCommandSpy() {
return commandSpy;
}
public void setCommandSpy(boolean commandSpy) {
this.commandSpy = commandSpy;
}
public void sendBungeeMessage(Player player, String channel, String message) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
out.writeUTF(message);
player.sendPluginMessage(JavaPlugin.getPlugin(Main.class), "BungeeCord", out.toByteArray());
}
public void sendBungeeMessage(Player player, String channel) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
player.sendPluginMessage(JavaPlugin.getPlugin(Main.class), "BungeeCord", out.toByteArray());
}
public void onJoin() {
sendBungeeMessage(player, "IP");
sendBungeeMessage(player, "EAG|GetDomain");
}
public void sendMessage(String message) {
player.sendMessage(StringUtil.cc(message));
}
}

View File

@ -0,0 +1,33 @@
package uranium.user;
import org.bukkit.entity.Player;
import java.util.Collection;
import java.util.HashMap;
public class UserManager {
private static final HashMap<String, User> users;
static {
users = new HashMap<>();
}
public static User addUser(Player player) {
users.put(player.getName().toLowerCase(), new User(player));
return users.get(player.getName().toLowerCase());
}
public static User getUser(Player player) {
return users.get(player.getName().toLowerCase());
}
public static void removeUser(Player player) {
users.remove(player.getName().toLowerCase());
}
public static Collection<User> getUsers() {
return users.values();
}
}

View File

@ -0,0 +1,11 @@
package uranium.util;
import org.bukkit.ChatColor;
public class StringUtil {
public static String cc(String message) {
return ChatColor.translateAlternateColorCodes('&', message);
}
}

View File

@ -1,6 +1,6 @@
name: Uranium name: Uranium
version: 1.0.0-LITE version: 1.0.0-LITE
main: darverdevs.Main main: uranium.Main
description: A moderation utility for Eaglercraft 1.8 servers. description: A moderation utility for Eaglercraft 1.8 servers.
author: Cold author: Cold
softdepend: [PlaceholderAPI] softdepend: [PlaceholderAPI]