Base rewrite with appliance System

This commit is contained in:
2023-10-24 20:49:44 +02:00
parent a8d37b82db
commit bb54482b5e
42 changed files with 1073 additions and 398 deletions

View File

@@ -0,0 +1,22 @@
package eu.mhsl.craftattack.spawn.util;
import org.bukkit.Chunk;
import org.bukkit.Location;
public class ChunkUtils {
public record ChunkPos(int x, int z) {}
public static Chunk loadChunkAtLocation(Location location) {
ChunkPos chunkPos = locationToChunk(location);
return location.getWorld().getChunkAt(chunkPos.x, chunkPos.z);
}
public static ChunkPos locationToChunk(Location location) {
return new ChunkPos(floor(location.x()) >> 4, floor(location.z()) >> 4);
}
private static int floor(double num) {
int floor = (int) num;
return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63);
}
}

View File

@@ -0,0 +1,18 @@
package eu.mhsl.craftattack.spawn.util;
import net.kyori.adventure.text.format.TextColor;
import java.awt.*;
public class ColorUtil {
public static TextColor mapGreenToRed(double value, double minValue, double maxValue, boolean lowIsGreen) {
float hue = (float) NumberUtil.map(value, minValue, maxValue, 0, 120);
if(lowIsGreen) {
hue = Math.abs(hue - 120);
}
System.out.println("hue " + hue);
return TextColor.color(Color.getHSBColor(hue/360, 1f, 1f).getRGB());
}
}

View File

@@ -0,0 +1,80 @@
package eu.mhsl.craftattack.spawn.util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.awt.*;
import java.util.Arrays;
public class ComponentUtil {
public static Component getFormattedTPS() {
double[] tpsValues = Bukkit.getTPS();
double min1 = Math.min(1.0, Math.max(0.0, tpsValues[0] / 20.0));
double min2 = Math.min(1.0, Math.max(0.0, tpsValues[1] / 20.0));
double min3 = Math.min(1.0, Math.max(0.0, tpsValues[2] / 20.0));
int red1 = (int) (255 * (1.0 - min1));
int green1 = (int) (255 * min1);
int red2 = (int) (255 * (1.0 - min2));
int green2 = (int) (255 * min2);
int red3 = (int) (255 * (1.0 - min3));
int green3 = (int) (255 * min3);
TextColor tpsColor1 = TextColor.color(red1, green1, 0);
TextColor tpsColor2 = TextColor.color(red2, green2, 0);
TextColor tpsColor3 = TextColor.color(red3, green3, 0);
return Component.text()
.append(Component.text("TPS 1, 5, 15m: ", NamedTextColor.GRAY))
.append(Component.text(String.format("%.2f", tpsValues[0]), tpsColor1))
.append(Component.text(", "))
.append(Component.text(String.format("%.2f", tpsValues[1]), tpsColor2))
.append(Component.text(", "))
.append(Component.text(String.format("%.2f", tpsValues[2]), tpsColor3))
.build();
}
public static Component getFormattedMSPT() {
long[] times = Bukkit.getServer().getTickTimes();
double mspt = ((double) Arrays.stream(times).sum() / (double) times.length) * 1.0E-6D;
double roundedMspt = (double) Math.round(mspt * 100d) / 100d;
int percentage = (int) (Math.min(100, (mspt / 50.0) * 100));
TextColor msptColor = ColorUtil.mapGreenToRed(roundedMspt, 0, 50, true);
TextColor percentageColor = ColorUtil.mapGreenToRed(mspt, 90, 100, true);
return Component.text()
.append(Component.text("Serverlast: ", NamedTextColor.GRAY))
.append(Component.text(percentage + "% ", percentageColor))
.appendNewline()
.append(Component.text("(", NamedTextColor.GRAY))
.append(Component.text(roundedMspt + "ms", msptColor))
.append(Component.text(" per tick)", NamedTextColor.GRAY))
.build();
}
public static Component getFormattedPing(Player player) {
return Component.text()
.append(Component.text("Dein Ping: ", NamedTextColor.GRAY))
.append(Component.text(player.getPing() + "ms", ColorUtil.mapGreenToRed(player.getPing(), 100, 500, true)))
.build();
}
public static Component createRainbowText(String text) {
Component builder = Component.empty();
int hue = 0;
for (char c : text.toCharArray()) {
TextColor color = TextColor.color(Color.getHSBColor((float) hue / 360, 1, 1).getRGB());
builder = builder.append(Component.text(c).color(color));
hue += 30;
}
return builder;
}
}

View File

@@ -1,80 +0,0 @@
package eu.mhsl.craftattack.spawn.util;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.List;
import java.util.Objects;
public class ConfigUtil {
private static ConfigUtil instance;
private final File file;
private final FileConfiguration config;
//loaded config variables
private boolean apiSwearWordCheck = false;
private String apiToken;
public static ConfigUtil getConfigUtil() {
if (instance == null) instance = new ConfigUtil("config.yml");
return instance;
}
private ConfigUtil(String path) {
this.file = new File(Objects.requireNonNull(Bukkit.getPluginManager().getPlugin("spawn")).getDataFolder().getAbsolutePath() + "/" + path);
this.config = YamlConfiguration.loadConfiguration(this.file);
loadConfig();
}
public boolean save() {
try {
this.config.save(this.file);
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
private void loadConfig() {
apiSwearWordCheck = (boolean) config.get("enableSwearWordCheck");
apiToken = (String) config.get("apiToken");
}
public FileConfiguration getConfig() {
return this.config;
}
public Location getVillagerLocation() {
List<Double> cordsList = this.config.getDoubleList("villagerLocation");
World world = Bukkit.getWorld("world");
double x = cordsList.get(0);
double y = cordsList.get(1);
double z = cordsList.get(2);
double yaw = cordsList.get(3);
double pitch = cordsList.get(4);
Location location = new Location(world, x, y, z);
location.setYaw((float) yaw);
location.setPitch((float) pitch);
return location;
}
public Location getSpawnLocation() {
List<Double> cordsList = this.config.getDoubleList("spawnLocation");
World world = Bukkit.getWorld("world");
double x = cordsList.get(0);
double y = cordsList.get(1);
double z = cordsList.get(2);
return new Location(world, x, y, z);
}
public boolean isApiSwearWordCheck() {
return apiSwearWordCheck;
}
public String getApiToken() {
return apiToken;
}
}

View File

@@ -0,0 +1,11 @@
package eu.mhsl.craftattack.spawn.util;
public class NumberUtil {
public static double map(double oldValue, double oldMin, double oldMax, double newMin, double newMax) {
double out = (((oldValue - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin;
if(out > newMax) out = newMax;
if(out < newMin) out = newMin;
return out;
}
}

View File

@@ -0,0 +1,15 @@
package eu.mhsl.craftattack.spawn.util;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import eu.mhsl.craftattack.spawn.Main;
import org.bukkit.entity.Player;
public class PluginMessage {
public static void connect(Player player, String server) {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.writeUTF("Connect");
output.writeUTF(server);
player.sendPluginMessage(Main.instance(), "BungeeCord", output.toByteArray());
}
}

View File

@@ -0,0 +1,33 @@
package eu.mhsl.craftattack.spawn.util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import java.awt.*;
public class RainbowComponent {
private int hueOffset = 0;
private final String text;
private final int density;
private final int speed;
public RainbowComponent(String text, int density, int speed) {
this.text = text;
this.density = density;
this.speed = speed;
}
public Component getRainbowState() {
Component builder = Component.empty();
int hue = this.hueOffset;
for (char c : text.toCharArray()) {
TextColor color = TextColor.color(Color.getHSBColor((float) hue / 360, 1, 1).getRGB());
builder = builder.append(Component.text(c).color(color));
hue += density;
}
if(this.hueOffset > Byte.MAX_VALUE - speed) this.hueOffset = 0;
this.hueOffset += (byte) speed;
return builder;
}
}