Create 0043-Add-config-yapfa-command-and-basic-settings.patch

This commit is contained in:
tr7zw 2020-03-05 22:32:51 +01:00
parent ae0c3c0c84
commit 2062c5e74c
1 changed files with 442 additions and 0 deletions

View File

@ -0,0 +1,442 @@
From b5e399c0d54549bd36220f5cd54cc126e17d7aaf Mon Sep 17 00:00:00 2001
From: tr7zw <tr7zw@live.de>
Date: Thu, 5 Mar 2020 22:31:50 +0100
Subject: [PATCH] Add config, yapfa command and basic settings
---
.../java/de/tr7zw/yapfa/YapfaCommand.java | 129 +++++++++++
src/main/java/de/tr7zw/yapfa/YapfaConfig.java | 209 ++++++++++++++++++
.../net/minecraft/server/DedicatedServer.java | 9 +
.../net/minecraft/server/EntityLiving.java | 9 +-
4 files changed, 354 insertions(+), 2 deletions(-)
create mode 100644 src/main/java/de/tr7zw/yapfa/YapfaCommand.java
create mode 100644 src/main/java/de/tr7zw/yapfa/YapfaConfig.java
diff --git a/src/main/java/de/tr7zw/yapfa/YapfaCommand.java b/src/main/java/de/tr7zw/yapfa/YapfaCommand.java
new file mode 100644
index 000000000..58ce1f826
--- /dev/null
+++ b/src/main/java/de/tr7zw/yapfa/YapfaCommand.java
@@ -0,0 +1,129 @@
+package de.tr7zw.yapfa;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+
+import org.bukkit.Bukkit;
+import org.bukkit.ChatColor;
+import org.bukkit.Location;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+
+import com.google.common.base.Functions;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+
+import net.minecraft.server.MinecraftKey;
+import net.minecraft.server.MinecraftServer;
+
+public class YapfaCommand extends Command {
+
+ public YapfaCommand(String name) {
+ super(name);
+ this.description = "YAPFA related commands";
+ this.usageMessage = "/yapfa [help | reload | info | version]";
+ this.setPermission("bukkit.command.yapfa");
+ }
+
+ @Override
+ public List<String> tabComplete(CommandSender sender, String alias, String[] args, Location location) throws IllegalArgumentException {
+ if (args.length <= 1)
+ return getListMatchingLast(args, "help", "info", "reload", "version");
+ return Collections.emptyList();
+ }
+
+ // Code from Mojang - copyright them
+ public static List<String> getListMatchingLast(String[] args, String... matches) {
+ return getListMatchingLast(args, (Collection) Arrays.asList(matches));
+ }
+
+ public static boolean matches(String s, String s1) {
+ return s1.regionMatches(true, 0, s, 0, s.length());
+ }
+
+ public static List<String> getListMatchingLast(String[] strings, Collection<?> collection) {
+ String last = strings[strings.length - 1];
+ ArrayList<String> results = Lists.newArrayList();
+
+ if (!collection.isEmpty()) {
+ Iterator iterator = Iterables.transform(collection, Functions.toStringFunction()).iterator();
+
+ while (iterator.hasNext()) {
+ String s1 = (String) iterator.next();
+
+ if (matches(last, s1)) {
+ results.add(s1);
+ }
+ }
+
+ if (results.isEmpty()) {
+ iterator = collection.iterator();
+
+ while (iterator.hasNext()) {
+ Object object = iterator.next();
+
+ if (object instanceof MinecraftKey && matches(last, ((MinecraftKey) object).getKey())) {
+ results.add(String.valueOf(object));
+ }
+ }
+ }
+ }
+
+ return results;
+ }
+ // end copy stuff
+
+ @Override
+ public boolean execute(CommandSender sender, String commandLabel, String[] args) {
+ if (!testPermission(sender)) return true;
+
+ if (args.length == 0) {
+ sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
+ return false;
+ }
+
+ switch (args[0].toLowerCase(Locale.ENGLISH)) {
+ case "info":
+ doInfo(sender);
+ break;
+ case "reload":
+ doReload(sender);
+ break;
+ case "ver":
+ case "version":
+ Command ver = org.bukkit.Bukkit.getServer().getCommandMap().getCommand("version");
+ if (ver != null) {
+ ver.execute(sender, commandLabel, new String[0]);
+ break;
+ }
+ // else - fall through to default
+ default:
+ sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
+ return false;
+ }
+
+ return true;
+ }
+
+ private void doInfo(CommandSender sender) {
+ Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Last tick took " + Bukkit.getLastTickMs() + "ms");
+ //TODO
+ }
+
+ private void doReload(CommandSender sender) {
+ Command.broadcastCommandMessage(sender, ChatColor.RED + "Please note that this command is not supported and may cause issues.");
+ Command.broadcastCommandMessage(sender, ChatColor.RED + "If you encounter any issues please use the /stop command to restart your server.");
+
+ MinecraftServer console = MinecraftServer.getServer();
+ de.tr7zw.yapfa.YapfaConfig.init(new File("yapfa.yml"));
+ console.server.reloadCount++;
+
+ Command.broadcastCommandMessage(sender, ChatColor.GREEN + "YAPFA config reload complete.");
+ }
+}
diff --git a/src/main/java/de/tr7zw/yapfa/YapfaConfig.java b/src/main/java/de/tr7zw/yapfa/YapfaConfig.java
new file mode 100644
index 000000000..5207ece43
--- /dev/null
+++ b/src/main/java/de/tr7zw/yapfa/YapfaConfig.java
@@ -0,0 +1,209 @@
+package de.tr7zw.yapfa;
+
+import com.destroystokyo.paper.io.chunk.ChunkTaskManager;
+import com.google.common.base.Strings;
+import com.google.common.base.Throwables;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.regex.Pattern;
+
+import com.google.common.collect.Lists;
+import net.minecraft.server.MinecraftServer;
+import org.bukkit.Bukkit;
+import org.bukkit.ChatColor;
+import org.bukkit.command.Command;
+import org.bukkit.configuration.ConfigurationSection;
+import org.bukkit.configuration.InvalidConfigurationException;
+import org.bukkit.configuration.file.YamlConfiguration;
+import co.aikar.timings.Timings;
+import co.aikar.timings.TimingsManager;
+import org.spigotmc.SpigotConfig;
+import org.spigotmc.WatchdogThread;
+
+public class YapfaConfig {
+
+ private static File CONFIG_FILE;
+ private static final String HEADER = "This is the main configuration file for YAPFA.\n"
+ + "YAPFA contains many breaking changes and settings, so know what you are doing!\n"
+ + "You have been warned!\n";
+ /*========================================================================*/
+ public static YamlConfiguration config;
+ static int version;
+ static Map<String, Command> commands;
+ private static boolean verbose;
+ private static boolean fatalError;
+ /*========================================================================*/
+ private static boolean metricsStarted;
+
+ public static void init(File configFile) {
+ CONFIG_FILE = configFile;
+ config = new YamlConfiguration();
+ try {
+ config.load(CONFIG_FILE);
+ } catch (IOException ex) {
+ } catch (InvalidConfigurationException ex) {
+ Bukkit.getLogger().log(Level.SEVERE, "Could not load yapfa.yml, please correct your syntax errors", ex);
+ throw Throwables.propagate(ex);
+ }
+ config.options().header(HEADER);
+ config.options().copyDefaults(true);
+ verbose = getBoolean("verbose", false);
+
+ commands = new HashMap<String, Command>();
+ commands.put("yapfa", new YapfaCommand("yapfa"));
+
+ version = getInt("config-version", 1);
+ set("config-version", 1);
+ readConfig(YapfaConfig.class, null);
+ }
+
+ protected static void logError(String s) {
+ Bukkit.getLogger().severe(s);
+ }
+
+ protected static void fatal(String s) {
+ fatalError = true;
+ throw new RuntimeException("Fatal yapfa.yml config error: " + s);
+ }
+
+ protected static void log(String s) {
+ if (verbose) {
+ Bukkit.getLogger().info(s);
+ }
+ }
+
+ public static void registerCommands() {
+ for (Map.Entry<String, Command> entry : commands.entrySet()) {
+ MinecraftServer.getServer().server.getCommandMap().register(entry.getKey(), "Yapfa", entry.getValue());
+ }
+ }
+
+ static void readConfig(Class<?> clazz, Object instance) {
+ for (Method method : clazz.getDeclaredMethods()) {
+ if (Modifier.isPrivate(method.getModifiers())) {
+ if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
+ try {
+ method.setAccessible(true);
+ method.invoke(instance);
+ } catch (InvocationTargetException ex) {
+ throw Throwables.propagate(ex.getCause());
+ } catch (Exception ex) {
+ Bukkit.getLogger().log(Level.SEVERE, "Error invoking " + method, ex);
+ }
+ }
+ }
+ }
+
+ try {
+ config.save(CONFIG_FILE);
+ } catch (IOException ex) {
+ Bukkit.getLogger().log(Level.SEVERE, "Could not save " + CONFIG_FILE, ex);
+ }
+ }
+
+ private static final Pattern SPACE = Pattern.compile(" ");
+ private static final Pattern NOT_NUMERIC = Pattern.compile("[^-\\d.]");
+ public static int getSeconds(String str) {
+ str = SPACE.matcher(str).replaceAll("");
+ final char unit = str.charAt(str.length() - 1);
+ str = NOT_NUMERIC.matcher(str).replaceAll("");
+ double num;
+ try {
+ num = Double.parseDouble(str);
+ } catch (Exception e) {
+ num = 0D;
+ }
+ switch (unit) {
+ case 'd': num *= (double) 60*60*24; break;
+ case 'h': num *= (double) 60*60; break;
+ case 'm': num *= (double) 60; break;
+ default: case 's': break;
+ }
+ return (int) num;
+ }
+
+ protected static String timeSummary(int seconds) {
+ String time = "";
+
+ if (seconds > 60 * 60 * 24) {
+ time += TimeUnit.SECONDS.toDays(seconds) + "d";
+ seconds %= 60 * 60 * 24;
+ }
+
+ if (seconds > 60 * 60) {
+ time += TimeUnit.SECONDS.toHours(seconds) + "h";
+ seconds %= 60 * 60;
+ }
+
+ if (seconds > 0) {
+ time += TimeUnit.SECONDS.toMinutes(seconds) + "m";
+ }
+ return time;
+ }
+
+ private static void set(String path, Object val) {
+ config.set(path, val);
+ }
+
+ private static boolean getBoolean(String path, boolean def) {
+ config.addDefault(path, def);
+ return config.getBoolean(path, config.getBoolean(path));
+ }
+
+ private static double getDouble(String path, double def) {
+ config.addDefault(path, def);
+ return config.getDouble(path, config.getDouble(path));
+ }
+
+ private static float getFloat(String path, float def) {
+ // TODO: Figure out why getFloat() always returns the default value.
+ return (float) getDouble(path, (double) def);
+ }
+
+ private static int getInt(String path, int def) {
+ config.addDefault(path, def);
+ return config.getInt(path, config.getInt(path));
+ }
+
+ private static <T> List getList(String path, T def) {
+ config.addDefault(path, def);
+ return (List<T>) config.getList(path, config.getList(path));
+ }
+
+ private static String getString(String path, String def) {
+ config.addDefault(path, def);
+ return config.getString(path, config.getString(path));
+ }
+
+ public static boolean disableEntityWaterChecks = false;
+ private static void disableEntityWaterChecks() {
+ disableEntityWaterChecks = getBoolean("settings.disableEntityWaterChecks", false);
+ }
+
+ public static boolean disableEntityStuckChecks = false;
+ private static void disableEntityStuckChecks() {
+ disableEntityStuckChecks = getBoolean("settings.disableEntityStuckChecks", false);
+ }
+
+ public static boolean disablePlayerOutOfWorldBorderCheck = false;
+ private static void disablePlayerOutOfWorldBorderCheck() {
+ disablePlayerOutOfWorldBorderCheck = getBoolean("settings.disablePlayerOutOfWorldBorderCheck", false);
+ }
+
+ public static boolean disableEntityCollisions = false;
+ private static void disableEntityCollisions() {
+ disableEntityCollisions = getBoolean("settings.disableEntityCollisions", false);
+ }
+
+}
diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java
index 8a241c28a..c4006ff01 100644
--- a/src/main/java/net/minecraft/server/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/DedicatedServer.java
@@ -194,6 +194,15 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer
com.destroystokyo.paper.PaperConfig.registerCommands();
com.destroystokyo.paper.VersionHistoryManager.INSTANCE.getClass(); // load version history now
// Paper end
+ // YAPFA start
+ try {
+ de.tr7zw.yapfa.YapfaConfig.init(new File("yapfa.yml"));
+ } catch (Exception e) {
+ DedicatedServer.LOGGER.error("Unable to load server configuration", e);
+ return false;
+ }
+ de.tr7zw.yapfa.YapfaConfig.registerCommands();
+ // YAPFA end
this.setSpawnAnimals(dedicatedserverproperties.spawnAnimals);
this.setSpawnNPCs(dedicatedserverproperties.spawnNpcs);
diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java
index 5b402690d..fa3affb54 100644
--- a/src/main/java/net/minecraft/server/EntityLiving.java
+++ b/src/main/java/net/minecraft/server/EntityLiving.java
@@ -246,10 +246,11 @@ public abstract class EntityLiving extends Entity {
this.world.getMethodProfiler().enter("livingEntityBaseTick");
boolean flag = this instanceof EntityHuman;
+ if(!de.tr7zw.yapfa.YapfaConfig.disableEntityStuckChecks) { // YAPFA start
if (this.isAlive()) {
if (this.inBlock()) {
this.damageEntity(DamageSource.STUCK, 1.0F);
- } else if (flag && !this.world.getWorldBorder().a(this.getBoundingBox())) {
+ } else if (flag && !de.tr7zw.yapfa.YapfaConfig.disablePlayerOutOfWorldBorderCheck && !this.world.getWorldBorder().a(this.getBoundingBox())) { // YAPFA
double d0 = this.world.getWorldBorder().a((Entity) this) + this.world.getWorldBorder().getDamageBuffer();
if (d0 < 0.0D) {
@@ -261,6 +262,7 @@ public abstract class EntityLiving extends Entity {
}
}
}
+ } // YAPFA end
if (this.isFireProof() || this.world.isClientSide) {
this.extinguish();
@@ -269,6 +271,7 @@ public abstract class EntityLiving extends Entity {
boolean flag1 = flag && ((EntityHuman) this).abilities.isInvulnerable;
if (this.isAlive()) {
+ if(!de.tr7zw.yapfa.YapfaConfig.disableEntityWaterChecks) { // YAPFA start
if (this.a(TagsFluid.WATER) && this.world.getType(new BlockPosition(this.locX(), this.getHeadY(), this.locZ())).getBlock() != Blocks.BUBBLE_COLUMN) {
if (!this.canBreatheUnderwater() && !MobEffectUtil.c(this) && !flag1) { // Paper - use OBFHELPER so it can be overridden
this.setAirTicks(this.l(this.getAirTicks()));
@@ -294,6 +297,7 @@ public abstract class EntityLiving extends Entity {
} else if (this.getAirTicks() < this.bw()) {
this.setAirTicks(this.m(this.getAirTicks()));
}
+ } // YAPFA end
if (!this.world.isClientSide) {
BlockPosition blockposition = new BlockPosition(this);
@@ -2640,11 +2644,12 @@ public abstract class EntityLiving extends Entity {
this.e(new Vec3D((double) this.aZ, (double) this.ba, (double) this.bb));
this.world.getMethodProfiler().exit();
this.world.getMethodProfiler().enter("push");
- if (this.bn > 0) {
+ if (!de.tr7zw.yapfa.YapfaConfig.disableEntityCollisions && this.bn > 0) { // YAPFA
--this.bn;
this.a(axisalignedbb, this.getBoundingBox());
}
+ if(!de.tr7zw.yapfa.YapfaConfig.disableEntityCollisions) // YAPFA
this.collideNearby();
this.world.getMethodProfiler().exit();
}
--
2.25.1.windows.1