mirror of
https://code.chipmunk.land/ChomeNS/chipmunkmod.git
synced 2025-11-13 22:16:14 +00:00
refactor: misc rainbow name cleanups
This commit is contained in:
parent
1f8a05f189
commit
754f981390
2 changed files with 56 additions and 66 deletions
|
|
@ -1,8 +1,11 @@
|
||||||
package land.chipmunk.chipmunkmod.modules;
|
package land.chipmunk.chipmunkmod.modules;
|
||||||
|
|
||||||
|
import com.mojang.authlib.GameProfile;
|
||||||
import land.chipmunk.chipmunkmod.util.ColorUtilities;
|
import land.chipmunk.chipmunkmod.util.ColorUtilities;
|
||||||
|
|
||||||
|
|
||||||
|
import land.chipmunk.chipmunkmod.util.RandomUtilities;
|
||||||
|
import land.chipmunk.chipmunkmod.util.UUIDUtilities;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.TextColor;
|
import net.kyori.adventure.text.format.TextColor;
|
||||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
|
|
@ -18,17 +21,13 @@ public class RainbowName {
|
||||||
|
|
||||||
public static final RainbowName INSTANCE = new RainbowName(MinecraftClient.getInstance());
|
public static final RainbowName INSTANCE = new RainbowName(MinecraftClient.getInstance());
|
||||||
|
|
||||||
private static final String BUKKIT_COLOR_CODES = "123456789abcdefklmorx";
|
private final Random random = new Random();
|
||||||
private static final String TEAM_NAME_CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-.+";
|
|
||||||
|
|
||||||
private Timer timer = null;
|
|
||||||
|
|
||||||
public boolean enabled = false;
|
public boolean enabled = false;
|
||||||
|
private Timer timer = null;
|
||||||
|
|
||||||
private String[] team;
|
private String team;
|
||||||
|
|
||||||
public String displayName;
|
public String displayName;
|
||||||
|
|
||||||
private int startHue = 0;
|
private int startHue = 0;
|
||||||
|
|
||||||
public void init () {
|
public void init () {
|
||||||
|
|
@ -44,72 +43,28 @@ public class RainbowName {
|
||||||
timer.schedule(task, 0, 50);
|
timer.schedule(task, 0, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String[] generateColorCodes(int length) {
|
|
||||||
String SALTCHARS = BUKKIT_COLOR_CODES;
|
|
||||||
StringBuilder salt = new StringBuilder();
|
|
||||||
Random rnd = new Random();
|
|
||||||
while (salt.length() < length) { // length of the random string.
|
|
||||||
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
|
|
||||||
salt.append(SALTCHARS.charAt(index));
|
|
||||||
}
|
|
||||||
String saltStr = salt.toString();
|
|
||||||
return saltStr.split("");
|
|
||||||
}
|
|
||||||
|
|
||||||
private String generateUsername (String[] codes) {
|
|
||||||
StringBuilder string = new StringBuilder();
|
|
||||||
for (String code : codes) string.append("&").append(code);
|
|
||||||
return string.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String generateUsername (int _codes) {
|
|
||||||
StringBuilder string = new StringBuilder();
|
|
||||||
|
|
||||||
final String[] codes = generateColorCodes(_codes);
|
|
||||||
|
|
||||||
for (String code : codes) string.append("&").append(code);
|
|
||||||
return string.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String generateUsername (char[] codes, char character) {
|
|
||||||
StringBuilder string = new StringBuilder();
|
|
||||||
for (char code : codes) string.append(character + code);
|
|
||||||
return string.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String[] generateTeamName () {
|
|
||||||
String SALTCHARS = TEAM_NAME_CHARACTERS;
|
|
||||||
StringBuilder salt = new StringBuilder();
|
|
||||||
Random rnd = new Random();
|
|
||||||
while (salt.length() < TEAM_NAME_CHARACTERS.length()) { // length of the random string.
|
|
||||||
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
|
|
||||||
salt.append(SALTCHARS.charAt(index));
|
|
||||||
}
|
|
||||||
String saltStr = salt.toString();
|
|
||||||
return saltStr.split("");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void enable () {
|
public void enable () {
|
||||||
final String[] colorCodes = generateColorCodes(8);
|
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
|
||||||
client.getNetworkHandler().sendChatCommand("extras:username " + generateUsername(colorCodes));
|
final GameProfile profile = networkHandler.getProfile();
|
||||||
|
|
||||||
team = generateTeamName();
|
final String username = RandomUtilities.emptyUsername(random);
|
||||||
|
team = RandomUtilities.randomString(random, RandomUtilities.TEAM_ALLOWED_CHARS, 16);
|
||||||
|
final String selfSelector = UUIDUtilities.selector(profile.getId());
|
||||||
|
|
||||||
CommandCore.INSTANCE.run("minecraft:team add " + String.join("", team));
|
networkHandler.sendChatCommand("extras:username " + username);
|
||||||
|
CommandCore.INSTANCE.run("minecraft:team add " + team);
|
||||||
CommandCore.INSTANCE.run("minecraft:execute as " + client.getNetworkHandler().getProfile().getId() + " run team join " + String.join("", team));
|
CommandCore.INSTANCE.run("minecraft:team join " + team + " " + selfSelector);
|
||||||
|
|
||||||
enabled = true;
|
enabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disable () {
|
public void disable () {
|
||||||
client.getNetworkHandler().sendChatCommand("extras:username " + client.getSession().getUsername());
|
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
|
||||||
|
final GameProfile profile = networkHandler.getProfile();
|
||||||
CommandCore.INSTANCE.run("minecraft:team remove " + String.join("", team));
|
|
||||||
team = null;
|
|
||||||
|
|
||||||
CommandCore.INSTANCE.run("essentials:nick " + client.getSession().getUsername() + " off");
|
|
||||||
|
|
||||||
|
CommandCore.INSTANCE.run("essentials:nick " + profile.getId() + " off");
|
||||||
|
CommandCore.INSTANCE.run("minecraft:team remove " + team);
|
||||||
|
networkHandler.sendChatCommand("extras:username " + profile.getName());
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,6 +84,7 @@ public class RainbowName {
|
||||||
|
|
||||||
if (!enabled) return;
|
if (!enabled) return;
|
||||||
|
|
||||||
|
final GameProfile profile = networkHandler.getProfile();
|
||||||
int hue = startHue;
|
int hue = startHue;
|
||||||
int increment = (int) (360.0 / Math.max(displayName.length(), 20));
|
int increment = (int) (360.0 / Math.max(displayName.length(), 20));
|
||||||
|
|
||||||
|
|
@ -142,8 +98,8 @@ public class RainbowName {
|
||||||
hue = (hue + increment) % 360;
|
hue = (hue + increment) % 360;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandCore.INSTANCE.run("minecraft:team modify " + String.join("", team) + " prefix " + GsonComponentSerializer.gson().serialize(component));
|
CommandCore.INSTANCE.run("minecraft:team modify " + team + " prefix " + GsonComponentSerializer.gson().serialize(component));
|
||||||
CommandCore.INSTANCE.run("essentials:nick " + client.getSession().getUsername() + " " + essentialsNickname);
|
CommandCore.INSTANCE.run("essentials:nick " + profile.getId() + " " + essentialsNickname);
|
||||||
|
|
||||||
startHue = (startHue + increment) % 360;
|
startHue = (startHue + increment) % 360;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
package land.chipmunk.chipmunkmod.util;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public final class RandomUtilities {
|
||||||
|
// https://github.com/kaboomserver/extras/blob/master/src/main/java/pw/kaboom/extras/util/Utility.java#L32
|
||||||
|
public static final char[] LEGACY_STYLE_CODES = "0123456789abcdefklmnorx".toCharArray();
|
||||||
|
public static final char[] TEAM_ALLOWED_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-.+"
|
||||||
|
.toCharArray();
|
||||||
|
|
||||||
|
public static String emptyUsername(final Random random) {
|
||||||
|
final char[] buf = new char[16];
|
||||||
|
|
||||||
|
for (int i = 0; i < 16; i += 2) {
|
||||||
|
final int j = random.nextInt(LEGACY_STYLE_CODES.length);
|
||||||
|
|
||||||
|
buf[i] = '&';
|
||||||
|
buf[i + 1] = LEGACY_STYLE_CODES[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String randomString(final Random random, final char[] charset, final int length) {
|
||||||
|
final char[] buf = new char[length];
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
final int j = random.nextInt(charset.length);
|
||||||
|
buf[i] = charset[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new String(buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue