diff --git a/NachoSpigot-API/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackProfile.java b/NachoSpigot-API/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackProfile.java new file mode 100644 index 000000000..fe4266e12 --- /dev/null +++ b/NachoSpigot-API/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackProfile.java @@ -0,0 +1,91 @@ +package dev.cobblesword.nachospigot.knockback; + +public interface KnockbackProfile { + + void save(); + void save(boolean projectiles); + + String getName(); + + void setName(String name); + + double getHorizontal(); + + void setHorizontal(double horizontal); + + double getVertical(); + + void setVertical(double vertical); + + double getVerticalMin(); + + void setVerticalMin(double verticalMin); + + double getVerticalMax(); + + void setVerticalMax(double verticalMax); + + double getExtraHorizontal(); + + void setExtraHorizontal(double extraHorizontal); + + double getExtraVertical(); + + void setExtraVertical(double extraVertical); + + double getFrictionHorizontal(); + + void setFrictionHorizontal(double frictionHorizontal); + + double getFrictionVertical(); + + void setFrictionVertical(double frictionVertical); + + boolean isStopSprint(); + + void setStopSprint(boolean stopSprint); + + double getRodHorizontal(); + + void setRodHorizontal(double rodHorizontal); + + double getRodVertical(); + + void setRodVertical(double rodVertical); + + double getArrowHorizontal(); + + void setArrowHorizontal(double arrowHorizontal); + + double getArrowVertical(); + + void setArrowVertical(double arrowVertical); + + double getPearlHorizontal(); + + void setPearlHorizontal(double pearlHorizontal) ; + + double getPearlVertical(); + + void setPearlVertical(double pearlVertical); + + double getSnowballHorizontal(); + + void setSnowballHorizontal(double snowballHorizontal); + + double getSnowballVertical(); + + void setSnowballVertical(double snowballVertical); + + double getEggHorizontal(); + + void setEggHorizontal(double eggHorizontal); + + double getEggVertical(); + + void setEggVertical(double eggVertical); + + String[] getKnockbackValues(); + + String[] getProjectilesValues(); +} diff --git a/NachoSpigot-API/src/main/java/org/bukkit/Bukkit.java b/NachoSpigot-API/src/main/java/org/bukkit/Bukkit.java index b034cc4f1..3e8074a2e 100644 --- a/NachoSpigot-API/src/main/java/org/bukkit/Bukkit.java +++ b/NachoSpigot-API/src/main/java/org/bukkit/Bukkit.java @@ -565,43 +565,6 @@ public static void reload() { server.reload(); } - /** - * Reloads the knockback config. - */ - public static void reloadKB() { - server.reloadKB(); - } - - /** - * Checks if custom knockback is enabled. - */ - public static boolean customKnockback() { - return server.customKnockback(); - } - - public static void toggleKnockback(Boolean b) { - server.toggleKnockback(b); - } - - public static void setKnockbackFriction(double d) { - server.setKnockbackFriction(d); - } - public static void setKnockbackHorizontal(double d) { - server.setKnockbackHorizontal(d); - } - public static void setKnockbackVertical(double d) { - server.setKnockbackVertical(d); - } - public static void setKnockbackVerticalLimit(double d) { - server.setKnockbackVerticalLimit(d); - } - public static void setKnockbackExtraHorizontal(double d) { - server.setKnockbackExtraHorizontal(d); - } - public static void setKnockbackExtraVertical(double d) { - server.setKnockbackExtraVertical(d); - } - /** * Returns the primary logger associated with this server instance. * diff --git a/NachoSpigot-API/src/main/java/org/bukkit/Server.java b/NachoSpigot-API/src/main/java/org/bukkit/Server.java index b535f5b10..fc047a072 100644 --- a/NachoSpigot-API/src/main/java/org/bukkit/Server.java +++ b/NachoSpigot-API/src/main/java/org/bukkit/Server.java @@ -458,19 +458,6 @@ public interface Server extends PluginMessageRecipient { */ public void reload(); - /** - * Reloads the knockback config. - */ - public void reloadKB(); - public void toggleKnockback(Boolean b); - - public void setKnockbackFriction(double d); - public void setKnockbackHorizontal(double d); - public void setKnockbackVertical(double d); - public void setKnockbackVerticalLimit(double d); - public void setKnockbackExtraHorizontal(double d); - public void setKnockbackExtraVertical(double d); - /** * Returns the primary logger associated with this server instance. * @@ -976,10 +963,6 @@ default boolean pluginsCommandEnabled() { */ CommandMap getCommandMap(); - /** - * Checks if custom knockback is enabled. - */ - boolean customKnockback(); // Paper end public class Spigot diff --git a/NachoSpigot-API/src/main/java/org/bukkit/command/defaults/nacho/KnockbackCommand.java b/NachoSpigot-API/src/main/java/org/bukkit/command/defaults/nacho/KnockbackCommand.java deleted file mode 100644 index 777e5f24b..000000000 --- a/NachoSpigot-API/src/main/java/org/bukkit/command/defaults/nacho/KnockbackCommand.java +++ /dev/null @@ -1,117 +0,0 @@ -package org.bukkit.command.defaults.nacho; - -import dev.cobblesword.nachospigot.CC; -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.List; - -public class KnockbackCommand extends Command { - public KnockbackCommand(String name, List aliases) { - super(name, "", "/" + name, aliases); - setPermission("ns.knockback"); - } - - @Override - public boolean execute(CommandSender sender, String commandLabel, String[] str) { - if (!testPermission(sender)) return true; // by GreenMC - thank you! - if(str.length == 0) { - sendHelp(sender); - return false; - } - switch (str[0].toLowerCase()) { - case "reload": { - sender.sendMessage(CC.gray + "Reloading KB config.."); - Bukkit.reloadKB(); - sender.sendMessage(CC.gray + "Reloaded KB config!"); - break; - } - case "toggle": { - Bukkit.toggleKnockback(!Bukkit.customKnockback()); - sender.sendMessage(CC.yellow + "Custom Knockback value has been changed to " + Bukkit.customKnockback()); - break; - } - - case "set": { - if(!Bukkit.customKnockback()) { - sender.sendMessage(CC.red + "Custom knockback is disabled."); - return false; - } - String[] args = Arrays.copyOfRange(str, 1, str.length); - if (args.length < 2) { - sendHelpKB(sender); - break; - } - double value; - try { - value = Double.parseDouble(args[1]); - } catch (Exception ignored) { - sender.sendMessage(CC.red + "Invalid value."); - break; - } - boolean success = true; - switch (args[0].toLowerCase()) { - case "f": { - Bukkit.setKnockbackFriction(value); - break; - } - case "h": { - Bukkit.setKnockbackHorizontal(value); - break; - } - case "v": { - Bukkit.setKnockbackVertical(value); - break; - } - case "vl": { - Bukkit.setKnockbackVerticalLimit(value); - break; - } - case "eh": { - Bukkit.setKnockbackExtraHorizontal(value); - break; - } - case "ev": { - Bukkit.setKnockbackExtraVertical(value); - break; - } - default: { - sendHelpKB(sender); - success = false; - break; - } - } - if (success) { - sender.sendMessage(CC.gray + args[0].toLowerCase() + CC.aqua + "set to " + CC.gray + value); - } - break; - } - default: { - sendHelp(sender); - break; - } - } - return true; - } - - private void sendHelp(CommandSender sender) { - sender.sendMessage(CC.red + "Please specify a subcommand. Possible subcommands:"); - sender.sendMessage(CC.gray + " | "); - sender.sendMessage(CC.red + "reload | reload the config"); - sender.sendMessage(CC.red + "toggle | toggle the custom kb"); - sender.sendMessage(CC.red + "set | set a kb value to something and save to config"); - } - - private void sendHelpKB(CommandSender sender) { - sender.sendMessage(CC.red + "Please specify what to set. Possible values:"); - sender.sendMessage(CC.gray + " | "); - sender.sendMessage(CC.red + "f | friction"); - sender.sendMessage(CC.red + "h | horizontal"); - sender.sendMessage(CC.red + "v | vertical"); - sender.sendMessage(CC.red + "vl | vertical limit"); - sender.sendMessage(CC.red + "eh | extra horizontal"); - sender.sendMessage(CC.red + "ev | extra vertical"); - } -} diff --git a/NachoSpigot-API/src/main/java/org/bukkit/entity/LivingEntity.java b/NachoSpigot-API/src/main/java/org/bukkit/entity/LivingEntity.java index 81644a9db..c6cf70ca7 100644 --- a/NachoSpigot-API/src/main/java/org/bukkit/entity/LivingEntity.java +++ b/NachoSpigot-API/src/main/java/org/bukkit/entity/LivingEntity.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Set; +import dev.cobblesword.nachospigot.knockback.KnockbackProfile; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -252,6 +253,17 @@ public interface LivingEntity extends Entity, Damageable, ProjectileSource { */ public void setNoDamageTicks(int ticks); + /** + * Gets the knockback profile of this player. + */ + KnockbackProfile getKnockbackProfile(); + + /** + * Sets the knockback profile of this player. The player's knockback + * profile overrides the global knockback profile. + */ + void setKnockbackProfile(KnockbackProfile profile); + /** * Gets the player identified as the killer of the living entity. *

diff --git a/NachoSpigot-Server/pom.xml b/NachoSpigot-Server/pom.xml index ab580a66e..f780b6678 100644 --- a/NachoSpigot-Server/pom.xml +++ b/NachoSpigot-Server/pom.xml @@ -67,6 +67,11 @@ jar compile + + net.jafama + jafama + 2.3.2 + net.sf.jopt-simple jopt-simple diff --git a/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/Nacho.java b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/Nacho.java index 517bc4f81..c257b974d 100644 --- a/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/Nacho.java +++ b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/Nacho.java @@ -1,5 +1,7 @@ package dev.cobblesword.nachospigot; +import dev.cobblesword.nachospigot.knockback.KnockbackCommand; +import dev.cobblesword.nachospigot.knockback.KnockbackConfig; import me.elier.nachospigot.config.NachoConfig; import xyz.sculas.nacho.anticrash.AntiCrash; import xyz.sculas.nacho.async.AsyncExplosions; @@ -39,8 +41,10 @@ public static Nacho get() { public void registerCommands() { SetMaxSlotCommand setMaxSlotCommand = new SetMaxSlotCommand("sms"); //[Nacho-0021] Add setMaxPlayers within Bukkit.getServer() and SetMaxSlot Command SpawnMobCommand spawnMobCommand = new SpawnMobCommand("spawnmob"); + KnockbackCommand knockbackCommand = new KnockbackCommand("kb"); MinecraftServer.getServer().server.getCommandMap().register(setMaxSlotCommand.getName(), "ns", setMaxSlotCommand); MinecraftServer.getServer().server.getCommandMap().register(spawnMobCommand.getName(), "ns", spawnMobCommand); + MinecraftServer.getServer().server.getCommandMap().register(knockbackCommand.getName(), "ns", knockbackCommand); } public void registerPacketListener(PacketListener packetListener) { diff --git a/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/commons/ClickableBuilder.java b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/commons/ClickableBuilder.java new file mode 100644 index 000000000..4fea62aac --- /dev/null +++ b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/commons/ClickableBuilder.java @@ -0,0 +1,32 @@ +package dev.cobblesword.nachospigot.commons; + +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; + +public class ClickableBuilder { + private TextComponent textComponent; + + public ClickableBuilder() { } + + public ClickableBuilder(String message) { + this.textComponent = new TextComponent(message); + } + + public ClickableBuilder setHover(String hover) { + this.textComponent.setHoverEvent( + new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(hover).create()) + ); + return this; + } + + public ClickableBuilder setClick(String command, ClickEvent.Action mode) { + this.textComponent.setClickEvent(new ClickEvent(mode, command)); + return this; + } + + public TextComponent build() { + return this.textComponent; + } +} diff --git a/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/CraftKnockbackProfile.java b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/CraftKnockbackProfile.java new file mode 100644 index 000000000..9ca44fe9f --- /dev/null +++ b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/CraftKnockbackProfile.java @@ -0,0 +1,255 @@ +package dev.cobblesword.nachospigot.knockback; + +import dev.cobblesword.nachospigot.Nacho; + +public class CraftKnockbackProfile implements KnockbackProfile { + + private String name; + + private double horizontal = 0.4D; + private double vertical = 0.4D; + private double verticalMin = -1.0D; + private double verticalMax = 0.4D; + private double extraHorizontal = 0.5D; + private double extraVertical = 0.1D; + private double frictionHorizontal = 0.5D; + private double frictionVertical = 0.5D; + + private boolean stopSprint = true; + + private double rodHorizontal = 0.4D; + private double rodVertical = 0.4D; + private double arrowHorizontal = 0.4D; + private double arrowVertical = 0.4D; + private double pearlHorizontal = 0.4D; + private double pearlVertical = 0.4D; + private double snowballHorizontal = 0.4D; + private double snowballVertical = 0.4D; + private double eggHorizontal = 0.4D; + private double eggVertical = 0.4D; + + public CraftKnockbackProfile(String name) { + this.name = name; + } + + public void save() { + save(false); + } + public void save(boolean projectiles) { + final String path = "knockback.profiles." + this.name; + + KnockbackConfig.set(path + ".stop-sprint", this.stopSprint); + KnockbackConfig.set(path + ".friction-horizontal", this.frictionHorizontal); + KnockbackConfig.set(path + ".friction-vertical", this.frictionVertical); + KnockbackConfig.set(path + ".horizontal", this.horizontal); + KnockbackConfig.set(path + ".vertical", this.vertical); + KnockbackConfig.set(path + ".vertical-max", this.verticalMax); + KnockbackConfig.set(path + ".vertical-min", this.verticalMin); + KnockbackConfig.set(path + ".extra-horizontal", this.extraHorizontal); + KnockbackConfig.set(path + ".extra-vertical", this.extraVertical); + + if (projectiles) { + KnockbackConfig.set(path + ".projectiles.rod.horizontal", this.rodHorizontal); + KnockbackConfig.set(path + ".projectiles.rod.vertical", this.rodVertical); + KnockbackConfig.set(path + ".projectiles.arrow.horizontal", this.arrowHorizontal); + KnockbackConfig.set(path + ".projectiles.arrow.vertical", this.arrowVertical); + KnockbackConfig.set(path + ".projectiles.pearl.horizontal", this.pearlHorizontal); + KnockbackConfig.set(path + ".projectiles.pearl.vertical", this.pearlVertical); + KnockbackConfig.set(path + ".projectiles.snowball.horizontal", this.snowballHorizontal); + KnockbackConfig.set(path + ".projectiles.snowball.vertical", this.snowballVertical); + KnockbackConfig.set(path + ".projectiles.egg.horizontal", this.eggHorizontal); + KnockbackConfig.set(path + ".projectiles.egg.vertical", this.eggVertical); + } + + KnockbackConfig.save(); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getHorizontal() { + return horizontal; + } + + public void setHorizontal(double horizontal) { + this.horizontal = horizontal; + } + + public double getVertical() { + return vertical; + } + + public void setVertical(double vertical) { + this.vertical = vertical; + } + + public double getVerticalMin() { + return verticalMin; + } + + public void setVerticalMin(double verticalMin) { + this.verticalMin = verticalMin; + } + + public double getVerticalMax() { + return verticalMax; + } + + public void setVerticalMax(double verticalMax) { + this.verticalMax = verticalMax; + } + + public double getExtraHorizontal() { + return extraHorizontal; + } + + public void setExtraHorizontal(double extraHorizontal) { + this.extraHorizontal = extraHorizontal; + } + + public double getExtraVertical() { + return extraVertical; + } + + public void setExtraVertical(double extraVertical) { + this.extraVertical = extraVertical; + } + + public double getFrictionHorizontal() { + return frictionHorizontal; + } + + public void setFrictionHorizontal(double frictionHorizontal) { + this.frictionHorizontal = frictionHorizontal; + } + + public double getFrictionVertical() { + return frictionVertical; + } + + public void setFrictionVertical(double frictionVertical) { + this.frictionVertical = frictionVertical; + } + + public boolean isStopSprint() { + return stopSprint; + } + + public void setStopSprint(boolean stopSprint) { + this.stopSprint = stopSprint; + } + + public double getRodHorizontal() { + return rodHorizontal; + } + + public void setRodHorizontal(double rodHorizontal) { + this.rodHorizontal = rodHorizontal; + } + + public double getRodVertical() { + return rodVertical; + } + + public void setRodVertical(double rodVertical) { + this.rodVertical = rodVertical; + } + + public double getArrowHorizontal() { + return arrowHorizontal; + } + + public void setArrowHorizontal(double arrowHorizontal) { + this.arrowHorizontal = arrowHorizontal; + } + + public double getArrowVertical() { + return arrowVertical; + } + + public void setArrowVertical(double arrowVertical) { + this.arrowVertical = arrowVertical; + } + + public double getPearlHorizontal() { + return pearlHorizontal; + } + + public void setPearlHorizontal(double pearlHorizontal) { + this.pearlHorizontal = pearlHorizontal; + } + + public double getPearlVertical() { + return pearlVertical; + } + + public void setPearlVertical(double pearlVertical) { + this.pearlVertical = pearlVertical; + } + + public double getSnowballHorizontal() { + return snowballHorizontal; + } + + public void setSnowballHorizontal(double snowballHorizontal) { + this.snowballHorizontal = snowballHorizontal; + } + + public double getSnowballVertical() { + return snowballVertical; + } + + public void setSnowballVertical(double snowballVertical) { + this.snowballVertical = snowballVertical; + } + + public double getEggHorizontal() { + return eggHorizontal; + } + + public void setEggHorizontal(double eggHorizontal) { + this.eggHorizontal = eggHorizontal; + } + + public double getEggVertical() { + return eggVertical; + } + + public void setEggVertical(double eggVertical) { + this.eggVertical = eggVertical; + } + + public String[] getKnockbackValues() { + return new String[] { + "Horizontal§7: " + this.horizontal, + "Vertical§7: " + this.vertical, + "Vertical-Max§7: " + this.verticalMax, + "Vertical-Min§7: " + this.verticalMin, + "Extra-Horizontal§7: " + this.extraHorizontal, + "Extra-Vertical§7: " + this.extraVertical, + "Friction-Horizontal§7: " + this.frictionHorizontal, + "Friction-Vertical§7: " + this.frictionVertical, + "Stop-Sprint§7: " + this.stopSprint, + }; + } + + public String[] getProjectilesValues() { + return new String[] { + "Rod-Horizontal§7: " + this.rodHorizontal, + "Rod-Vertical§7: " + this.rodVertical, + "Arrow-Horizontal§7: " + this.arrowHorizontal, + "Arrow-Vertical§7: " + this.arrowVertical, + "Pearl-Horizontal§7: " + this.pearlHorizontal, + "Pearl-Vertical§7: " + this.pearlVertical, + "Snowball-Horizontal§7: " + this.snowballHorizontal, + "Snowball-Vertical§7: " + this.snowballVertical, + "Egg-Horizontal§7: " + this.eggHorizontal, + "Egg-Vertical§7: " + this.eggVertical, + }; + } +} diff --git a/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/Knockback.java b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/Knockback.java deleted file mode 100644 index a2217d4a0..000000000 --- a/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/Knockback.java +++ /dev/null @@ -1,46 +0,0 @@ -package dev.cobblesword.nachospigot.knockback; - -import dev.cobblesword.nachospigot.commons.FileUtils; -import net.minecraft.server.MinecraftServer; -import org.bukkit.command.defaults.nacho.KnockbackCommand; - -import java.io.File; -import java.util.Collections; - -public class Knockback { - - private static Knockback INSTANCE; - - private static final File CONFIG_FILE = new File("knockback.json"); - private KnockbackConfig config; - - public Knockback() { - INSTANCE = this; - this.config = new KnockbackConfig(); - while (!CONFIG_FILE.exists()) FileUtils.toFile(this.config, CONFIG_FILE); - this.config = FileUtils.toObject(CONFIG_FILE, KnockbackConfig.class); - this.saveConfig(); - } - - public void registerCommands() { - KnockbackCommand knockbackCommand = new KnockbackCommand("knockback", Collections.singletonList("kb")); - MinecraftServer.getServer().server.getCommandMap().register(knockbackCommand.getName(), "ns", knockbackCommand); - } - - public void reloadConfig() { - this.config = FileUtils.toObject(CONFIG_FILE, KnockbackConfig.class); - } - - public void saveConfig() { - FileUtils.toFile(this.config, CONFIG_FILE); - } - - public static Knockback get() { - return INSTANCE == null ? new Knockback() : INSTANCE; - } - - public KnockbackConfig getConfig() - { - return config; - } -} \ No newline at end of file diff --git a/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackCommand.java b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackCommand.java new file mode 100644 index 000000000..804391e6d --- /dev/null +++ b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackCommand.java @@ -0,0 +1,451 @@ +package dev.cobblesword.nachospigot.knockback; + +import dev.cobblesword.nachospigot.Nacho; +import dev.cobblesword.nachospigot.commons.ClickableBuilder; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.TextComponent; +import org.apache.commons.lang3.math.NumberUtils; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class KnockbackCommand extends Command { + + private final String separator = "§8§m-=-------------------------=-"; + + public KnockbackCommand(String name) { + super(name); + } + + @Override + public boolean execute(CommandSender sender, String commandLabel, String[] args) { + if (!sender.isOp() || !(sender instanceof Player)) { return false; } + Player player = (Player)sender; + + switch (args.length) { + case 2: { + switch (args[0].toLowerCase()) { + case "create": { + if (!isProfileName(args[1])) { + CraftKnockbackProfile profile = new CraftKnockbackProfile(args[1]); + KnockbackConfig.getKbProfiles().add(profile); + profile.save(); + knockbackCommandMain(player); + player.sendMessage("§aThe profile §e" + args[1] + " §ahas been created."); + return true; + } else { + player.sendMessage("§cA knockback profile with that name already exists."); + } + break; + } + case "delete": { + if (KnockbackConfig.getCurrentKb().getName().equalsIgnoreCase(args[1])) { + knockbackCommandMain(player); + player.sendMessage("§cYou cannot delete the profile that is being used."); + return false; + } + if (KnockbackConfig.getKbProfiles().removeIf(profile -> profile.getName().equalsIgnoreCase(args[1]))) { + KnockbackConfig.set("knockback.profiles." + args[1], null); + knockbackCommandMain(player); + player.sendMessage("§aThe profile §e" + args[1] + " §ahas been removed."); + return true; + } else { + player.sendMessage("§cThis profile doesn't exist."); + } + break; + } + case "load": { + KnockbackProfile profile = KnockbackConfig.getKbProfileByName(args[1]); + if (profile != null) { + if (KnockbackConfig.getCurrentKb().getName().equalsIgnoreCase(args[1])) { + player.sendMessage("§cThis profile is loaded."); + return false; + } + KnockbackConfig.setCurrentKb(profile); + KnockbackConfig.set("knockback.current", profile.getName()); + KnockbackConfig.save(); + knockbackCommandMain(player); + player.sendMessage("§aThe profile §e" + args[1] + " §ahas been loaded."); + return true; + } else { + player.sendMessage("§cThis profile doesn't exist."); + } + break; + } + case "view": { + KnockbackProfile profile = KnockbackConfig.getKbProfileByName(args[1]); + if (profile != null) { + knockbackCommandView(player, profile); + return true; + } + player.sendMessage("§cThis profile doesn't exist."); + break; + } + case "projectile": { + KnockbackProfile profile = KnockbackConfig.getKbProfileByName(args[1]); + if (profile != null) { + knockbackCommandViewProjectiles(player, profile); + return true; + } + player.sendMessage("§cThis profile doesn't exist."); + break; + } + default: { + knockbackCommandMain(player); + } + } + break; + } + case 3: { + switch (args[0].toLowerCase()) { + case "set": { + KnockbackProfile profile = KnockbackConfig.getKbProfileByName(args[1]); + if (profile == null) { + sender.sendMessage("§cA profile with that name could not be found."); + return false; + } + Player target = Bukkit.getPlayer(args[2]); + if (target == null) { + sender.sendMessage("§cThat player is not online."); + return false; + } + target.setKnockbackProfile(profile); + break; + } + } + break; + } + case 4: { + if (args[0].equalsIgnoreCase("edit")) { + KnockbackProfile profile = KnockbackConfig.getKbProfileByName(args[1].toLowerCase()); + if (profile == null) { + player.sendMessage("§cThis profile doesn't exist."); + return false; + } + switch (args[2].toLowerCase()) { + case "friction-horizontal": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setFrictionHorizontal(value); + profile.save(); + knockbackCommandView(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "friction-vertical": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setFrictionVertical(value); + profile.save(); + knockbackCommandView(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "horizontal": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setHorizontal(value); + profile.save(); + knockbackCommandView(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "vertical": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setVertical(value); + profile.save(); + knockbackCommandView(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "extra-horizontal": { + if (!NumberUtils.isNumber(args[3])) { + sender.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setExtraHorizontal(value); + profile.save(); + knockbackCommandView(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "extra-vertical": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setExtraVertical(value); + profile.save(); + knockbackCommandView(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "vertical-max": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setVerticalMax(value); + profile.save(); + knockbackCommandView(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "vertical-min": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setVerticalMin(value); + profile.save(); + knockbackCommandView(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "stop-sprint": { + if ((args[3]).equalsIgnoreCase("true") || (args[3]).equalsIgnoreCase("false")) { + profile.setStopSprint(Boolean.parseBoolean(args[3])); + profile.save(); + knockbackCommandView(player, profile); + player.sendMessage("§aValue edited and saved."); + return true; + } else { + player.sendMessage("§4" + args[3] + " §cis not a boolean."); + } + break; + } + case "rod-horizontal": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setRodHorizontal(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "rod-vertical": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setRodVertical(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "arrow-horizontal": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setArrowHorizontal(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "arrow-vertical": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setArrowVertical(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "pearl-horizontal": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setPearlHorizontal(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "pearl-vertical": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setPearlVertical(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "snowball-horizontal": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setSnowballHorizontal(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "snowball-vertical": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setSnowballVertical(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "egg-horizontal": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setEggHorizontal(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + case "egg-vertical": { + if (!NumberUtils.isNumber(args[3])) { + player.sendMessage("§4" + args[3] + " §c is not a number."); + return false; + } + double value = Double.parseDouble(args[3]); + profile.setEggVertical(value); + profile.save(); + knockbackCommandViewProjectiles(player, profile); + player.sendMessage("§aValue edited and saved."); + break; + } + } + } + break; + } + default: { + knockbackCommandMain(player); + } + } + return false; + } + + private void knockbackCommandMain(Player player) { + player.sendMessage(separator + "\n" + "§a§lKnockback profile list:\n"); + + for (KnockbackProfile profile : KnockbackConfig.getKbProfiles()) { + boolean current = KnockbackConfig.getCurrentKb().getName().equals(profile.getName()); + + TextComponent line = new ClickableBuilder("§8§l(§e§l➜§8§l) ") + .setHover("§c[Click here to apply this profile to a player] ") + .setClick("/kb set " + profile.getName() + " ", ClickEvent.Action.SUGGEST_COMMAND) + .build(); + TextComponent load = new ClickableBuilder("§8§l(" + (current ? "§a" : "§7") + "✔§8§l) ") + .setHover(current ? "§c[This profile is loaded] " : "§a[Click here to load this profile]") + .setClick("/kb load " + profile.getName(), ClickEvent.Action.RUN_COMMAND) + .build(); + TextComponent delete = new ClickableBuilder("§8§l(§c§l✖§8§l) ") + .setHover("§c[Click here to delete this profile] ") + .setClick("/kb delete " + profile.getName(), ClickEvent.Action.RUN_COMMAND) + .build(); + TextComponent edit = new ClickableBuilder("§7 " + profile.getName() + " §8§l[§6§l✎§8§l]") + .setHover("§e[Click here to edit this profile]") + .setClick("/kb view " + profile.getName(), ClickEvent.Action.RUN_COMMAND) + .build(); + player.spigot().sendMessage(line, load, delete, edit); + } + + player.spigot().sendMessage( + new ClickableBuilder("\n§8§l[§a§lCreate new profile§8]") + .setHover("§c[Click here to create a new profile]") + .setClick("/kb create ", ClickEvent.Action.SUGGEST_COMMAND) + .build() + ); + player.sendMessage(separator); + } + private void knockbackCommandView(Player player, KnockbackProfile profile) { + player.sendMessage(separator + "\n" + "§a§lKnockback values:\n"); + for (String values : profile.getKnockbackValues()) { + TextComponent value = new TextComponent("§6» §e" + values); + TextComponent edit = new ClickableBuilder(" §8§l[§e§l✎§8§l]") + .setHover("§e[Click to edit " + values + " value]") + .setClick("/kb edit " + profile.getName() + " " + values.replace("§7: ", " "), ClickEvent.Action.SUGGEST_COMMAND) + .build(); + player.spigot().sendMessage(value, edit); + } + TextComponent page = new ClickableBuilder("\n§8§l[§c§l⬑§8§l] ") + .setHover("§e[Click to back]") + .setClick("/kb", ClickEvent.Action.RUN_COMMAND) + .build(); + TextComponent projectiles = new ClickableBuilder(" §8§l[§a§lEdit projectiles§8§l]") + .setClick("/kb projectile " + profile.getName(), ClickEvent.Action.RUN_COMMAND) + .setHover("§e[Click to edit projectiles]") + .build(); + player.spigot().sendMessage(page, projectiles); + player.sendMessage(separator); + } + private void knockbackCommandViewProjectiles(Player player, KnockbackProfile profile) { + player.sendMessage(separator + "\n§a§lProjectiles values: \n"); + for (String values : profile.getProjectilesValues()) { + TextComponent value = new TextComponent("§6» §e" + values); + TextComponent edit = new ClickableBuilder(" §8§l[§e§l✎§8§l]") + .setHover("§e[Click here to edit " + values + " value]") + .setClick("/kb edit " + profile.getName() + " " + values.replace("§7: ", " "), ClickEvent.Action.SUGGEST_COMMAND) + .build(); + player.sendMessage(value, edit); + } + TextComponent page = new ClickableBuilder("\n§8§l[§c§l⬑§8§l] ") + .setHover("§e[Click to back]") + .setClick("/kb", ClickEvent.Action.RUN_COMMAND) + .build(); + TextComponent knockback = new ClickableBuilder("§8§l[§a§lEdit knockback§8§l]") + .setHover("§e[Click here to edit this knockback]") + .setClick("/kb view " + profile.getName(), ClickEvent.Action.RUN_COMMAND) + .build(); + player.spigot().sendMessage(page, knockback); + player.sendMessage(separator); + + } + + private boolean isProfileName(String name) { + for (KnockbackProfile profile : KnockbackConfig.getKbProfiles()) { + if (profile.getName().equalsIgnoreCase(name)) { + return true; + } + } + return false; + } +} diff --git a/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackConfig.java b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackConfig.java index 0a6e21818..0cf49ba3d 100644 --- a/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackConfig.java +++ b/NachoSpigot-Server/src/main/java/dev/cobblesword/nachospigot/knockback/KnockbackConfig.java @@ -1,11 +1,161 @@ package dev.cobblesword.nachospigot.knockback; +import com.google.common.base.Throwables; +import me.elier.nachospigot.config.NachoConfig; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bukkit.Bukkit; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; +import org.sugarcanemc.sugarcane.util.yaml.YamlCommenter; + +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.util.HashSet; +import java.util.List; +import java.util.Set; + public class KnockbackConfig { - public boolean customKnockback = false; - public double knockbackFriction = 2.0D; - public double knockbackHorizontal = 0.4D; - public double knockbackVertical = 0.4D; - public double knockbackVerticalLimit = 0.4D; - public double knockbackExtraHorizontal = 0.5D; - public double knockbackExtraVertical = 0.1D; + private static final Logger LOGGER = LogManager.getLogger(KnockbackConfig.class); + private static File CONFIG_FILE; + protected static final YamlCommenter c = new YamlCommenter(); + private static final String HEADER = "This is the knockback configuration file for NachoSpigot.\n"; + static YamlConfiguration config; + + private static KnockbackProfile currentKb; + private static Set kbProfiles = new HashSet<>(); + + public static void init(File configFile) { + CONFIG_FILE = configFile; + config = new YamlConfiguration(); + try { + System.out.println("Loading NachoSpigot knockback config from " + configFile.getName()); + config.load(CONFIG_FILE); + } catch (IOException ignored) { + } catch (InvalidConfigurationException ex) { + LOGGER.log(Level.ERROR, "Could not load knockback.yml, please correct your syntax errors", ex); + throw Throwables.propagate(ex); + } + config.options().copyDefaults(true); + c.setHeader(HEADER); + + save(); + + final KnockbackProfile defaultProfile = new CraftKnockbackProfile("Default"); + + kbProfiles = new HashSet<>(); + kbProfiles.add(defaultProfile); + + for (String key : getKeys("knockback.profiles")) { + final String path = "knockback.profiles." + key; + CraftKnockbackProfile profile = (CraftKnockbackProfile) getKbProfileByName(key); + if (profile == null) { + profile = new CraftKnockbackProfile(key); + kbProfiles.add(profile); + } + profile.setStopSprint(getBoolean(path + ".stop-sprint", true)); + profile.setFrictionHorizontal(getDouble(path + ".friction-horizontal", 0.5D)); + profile.setFrictionVertical(getDouble(path + ".friction-vertical", 0.5D)); + profile.setHorizontal(getDouble(path + ".horizontal", 0.4D)); + profile.setVertical(getDouble(path + ".vertical", 0.4D)); + profile.setVerticalMax(getDouble(path + ".vertical-max", 0.4D)); + profile.setVerticalMin(getDouble(path + ".vertical-min", -1.0D)); + profile.setExtraHorizontal(getDouble(path + ".extra-horizontal", 0.5D)); + profile.setExtraVertical(getDouble(path + ".extra-vertical", 0.05D)); + + + profile.setRodHorizontal(getDouble(path + ".projectiles.rod.horizontal", 0.4D)); + profile.setRodVertical(getDouble(path + ".projectiles.rod.vertical", 0.4D)); + profile.setArrowHorizontal(getDouble(path + ".projectiles.arrow.horizontal", 0.4D)); + profile.setArrowVertical(getDouble(path + ".projectiles.arrow.vertical", 0.4D)); + profile.setPearlHorizontal(getDouble(path + ".projectiles.pearl.horizontal", 0.4D)); + profile.setPearlVertical(getDouble(path + ".projectiles.pearl.vertical", 0.4D)); + profile.setSnowballHorizontal(getDouble(path + ".projectiles.snowball.horizontal", 0.4D)); + profile.setSnowballVertical(getDouble(path + ".projectiles.snowball.vertical", 0.4D)); + profile.setEggHorizontal(getDouble(path + ".projectiles.egg.horizontal", 0.4D)); + profile.setEggHorizontal(getDouble(path + ".projectiles.egg.vertical", 0.4D)); + } + currentKb = getKbProfileByName(getString("knockback.current", "default")); + if (currentKb == null) { + currentKb = defaultProfile; + } + } + + public static KnockbackProfile getCurrentKb() { + return currentKb; + } + + public static void setCurrentKb(KnockbackProfile kb) { + currentKb = kb; + } + + public static KnockbackProfile getKbProfileByName(String name) { + for (KnockbackProfile profile : kbProfiles) { + if (profile.getName().equalsIgnoreCase(name)) { + return profile; + } + } + return null; + } + + public static Set getKbProfiles() { + return kbProfiles; + } + + public static void save() { + try { + config.save(CONFIG_FILE); + } catch (IOException ex) { + LOGGER.log(Level.ERROR, "Could not save " + CONFIG_FILE, ex); + } + } + + public static void set(String path, Object val) { + config.set(path, val); + + save(); + } + + public static Set getKeys(String path) { + if (!config.isConfigurationSection(path)) { + config.createSection(path); + return new HashSet<>(); + } + + return config.getConfigurationSection(path).getKeys(false); + } + + 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) { + config.addDefault(path, def); + return config.getFloat(path, config.getFloat(path)); + } + + private static int getInt(String path, int def) { + config.addDefault(path, def); + return config.getInt(path, config.getInt(path)); + } + + private static List getList(String path, T def) { + config.addDefault(path, def); + return 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)); + } } diff --git a/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoConfig.java b/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoConfig.java index 550f45228..683b0a7b3 100644 --- a/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoConfig.java +++ b/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoConfig.java @@ -101,6 +101,7 @@ private static void migrate(File old_config) { set("world-settings.default.disable-sponge-absorption", nachoJson.disableSpongeAbsorption); set("settings.fix-eat-while-running", nachoJson.fixEatWhileRunning); set("settings.hide-projectiles-from-hidden-players", nachoJson.hideProjectilesFromHiddenPlayers); + set("settings.instant-use-entity", nachoJson.hideProjectilesFromHiddenPlayers); old_config.delete(); } @@ -329,4 +330,10 @@ private static void enableProtocolLibShim() { c.addComment("settings.enable-protocollib-shim", "Enable ProtocolLib network shim. Allows ProtocolLib to work, but requires extra memory. Disable this if you don't use ProtocolLib!"); } + public static boolean instantPlayInUseEntity; + + private static void instantPlayInUseEntity() { + instantPlayInUseEntity = getBoolean("settings.instant-interaction", false); + c.addComment("settings.instant-interaction", "Disables delay of all interactions"); + } } diff --git a/NachoSpigot-Server/src/main/java/net/minecraft/server/DedicatedServer.java b/NachoSpigot-Server/src/main/java/net/minecraft/server/DedicatedServer.java index 5de5dfa7f..a04267dd8 100644 --- a/NachoSpigot-Server/src/main/java/net/minecraft/server/DedicatedServer.java +++ b/NachoSpigot-Server/src/main/java/net/minecraft/server/DedicatedServer.java @@ -10,7 +10,7 @@ import dev.cobblesword.nachospigot.Nacho; import dev.cobblesword.nachospigot.commons.IPUtils; -import dev.cobblesword.nachospigot.knockback.Knockback; +import dev.cobblesword.nachospigot.knockback.KnockbackConfig; import me.elier.nachospigot.config.NachoConfig; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -174,6 +174,7 @@ public void run() { } // Spigot start NachoConfig.init((File) options.valueOf("nacho-settings")); // NachoSpigot - Load config before PlayerList + KnockbackConfig.init((File) options.valueOf("knockback-settings")); this.a(new DedicatedPlayerList(this)); org.spigotmc.SpigotConfig.init((File) options.valueOf("spigot-settings")); org.spigotmc.SpigotConfig.registerCommands(); @@ -183,7 +184,6 @@ public void run() { org.github.paperspigot.PaperSpigotConfig.registerCommands(); // PaperSpigot end Nacho.get().registerCommands(); // NachoSpigot :: Commands - Knockback.get().registerCommands(); // NS Knockback :: Commands DedicatedServer.LOGGER.info("Generating keypair"); this.a(MinecraftEncryption.b()); diff --git a/NachoSpigot-Server/src/main/java/net/minecraft/server/Entity.java b/NachoSpigot-Server/src/main/java/net/minecraft/server/Entity.java index 70bf42697..d8da298c5 100644 --- a/NachoSpigot-Server/src/main/java/net/minecraft/server/Entity.java +++ b/NachoSpigot-Server/src/main/java/net/minecraft/server/Entity.java @@ -9,6 +9,7 @@ // CraftBukkit start import com.eatthepath.uuid.FastUUID; import dev.cobblesword.nachospigot.commons.Constants; +import dev.cobblesword.nachospigot.knockback.KnockbackProfile; import org.apache.logging.log4j.LogManager; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -1814,6 +1815,15 @@ public boolean isSprinting() { return this.g(3); } + public boolean extraKnockback; + + public void setExtraKnockback(boolean flag) { + this.extraKnockback = flag; + } + public boolean isExtraKnockback() { + return this.extraKnockback; + } + public void setSprinting(boolean flag) { this.b(3, flag); } @@ -2359,4 +2369,14 @@ protected void a(EntityLiving entityliving, Entity entity) { EnchantmentManager.b(entityliving, entity); } + + private KnockbackProfile knockbackProfile; + + public KnockbackProfile getKnockbackProfile() { + return knockbackProfile; + } + + public void setKnockbackProfile(KnockbackProfile profile) { + this.knockbackProfile = profile; + } } diff --git a/NachoSpigot-Server/src/main/java/net/minecraft/server/EntityHuman.java b/NachoSpigot-Server/src/main/java/net/minecraft/server/EntityHuman.java index 7a2c84a3f..c6e894c6e 100644 --- a/NachoSpigot-Server/src/main/java/net/minecraft/server/EntityHuman.java +++ b/NachoSpigot-Server/src/main/java/net/minecraft/server/EntityHuman.java @@ -10,9 +10,10 @@ // CraftBukkit start import dev.cobblesword.nachospigot.Nacho; -import dev.cobblesword.nachospigot.knockback.Knockback; import dev.cobblesword.nachospigot.knockback.KnockbackConfig; +import dev.cobblesword.nachospigot.knockback.KnockbackProfile; import me.elier.nachospigot.config.NachoConfig; +import net.jafama.FastMath; import org.bukkit.craftbukkit.entity.CraftHumanEntity; import org.bukkit.craftbukkit.entity.CraftItem; import org.bukkit.craftbukkit.inventory.CraftItemStack; @@ -965,22 +966,21 @@ public double am() { return -0.35D; } + + public void attack(Entity entity) { if (entity.aD()) { if (!entity.l(this)) { float f = (float) this.getAttributeInstance(GenericAttributes.ATTACK_DAMAGE).getValue(); - byte b0 = 0; - float f1 = 0.0F; + float f1 = EnchantmentManager.a(this.bA(), EnumMonsterType.UNDEFINED); if (entity instanceof EntityLiving) { f1 = EnchantmentManager.a(this.bA(), ((EntityLiving) entity).getMonsterType()); - } else { - f1 = EnchantmentManager.a(this.bA(), EnumMonsterType.UNDEFINED); } - int i = b0 + EnchantmentManager.a((EntityLiving) this); + int i = EnchantmentManager.a((EntityLiving) this); - if (this.isSprinting()) { + if (this.isExtraKnockback()) { ++i; } @@ -1014,34 +1014,28 @@ public void attack(Entity entity) { if (flag2) { if (i > 0) { - KnockbackConfig config = Knockback.get().getConfig(); - if(!config.customKnockback) entity.g(-MathHelper.sin(this.yaw * 3.1415927F / 180.0F) * (float) i * 0.5F, 0.1D, (double) (MathHelper.cos(this.yaw * 3.1415927F / 180.0F) * (float) i * 0.5F)); else + KnockbackProfile profile = (entity.getKnockbackProfile() == null) ? + KnockbackConfig.getCurrentKb() : entity.getKnockbackProfile(); entity.g( - (-MathHelper.sin(this.yaw * 3.1415927F / 180.0F) * (float) i * config.knockbackExtraHorizontal), - config.knockbackExtraVertical, - (MathHelper.cos(this.yaw * 3.1415927F / 180.0F) * (float) i * config.knockbackExtraHorizontal)); + (-MathHelper.sin((float) (this.yaw * Math.PI / 180.0D)) * i * profile.getExtraHorizontal()), + profile.getExtraVertical(), + (MathHelper.cos((float) (this.yaw * Math.PI / 180.0D)) * i * profile.getExtraHorizontal())); this.motX *= 0.6D; this.motZ *= 0.6D; - this.setSprinting(false); + if (profile.isStopSprint()) this.setExtraKnockback(false); //Nacho - Prevent desync player sprinting } if (entity instanceof EntityPlayer && entity.velocityChanged) { - // CraftBukkit start - Add Velocity Event - boolean cancelled = false; Player player = (Player) entity.getBukkitEntity(); - org.bukkit.util.Vector velocity = new Vector( d0, d1, d2 ); - - PlayerVelocityEvent event = new PlayerVelocityEvent(player, velocity.clone()); + final Vector velocity = new Vector(entity.motX, entity.motY, entity.motZ); + PlayerVelocityEvent event = new PlayerVelocityEvent(player, velocity); world.getServer().getPluginManager().callEvent(event); - if (event.isCancelled()) { - cancelled = true; - } else if (!velocity.equals(event.getVelocity())) { - player.setVelocity(event.getVelocity()); - } - - if (!cancelled) { - ( (EntityPlayer) entity ).playerConnection.sendPacket( new PacketPlayOutEntityVelocity( entity ) ); + if (!event.isCancelled()) { + if (!velocity.equals(event.getVelocity())) { + player.setVelocity(event.getVelocity()); + } + ((EntityPlayer) entity).playerConnection.sendPacket(new PacketPlayOutEntityVelocity(entity)); entity.velocityChanged = false; entity.motX = d0; entity.motY = d1; diff --git a/NachoSpigot-Server/src/main/java/net/minecraft/server/EntityLiving.java b/NachoSpigot-Server/src/main/java/net/minecraft/server/EntityLiving.java index 46cf5108a..82cabf285 100644 --- a/NachoSpigot-Server/src/main/java/net/minecraft/server/EntityLiving.java +++ b/NachoSpigot-Server/src/main/java/net/minecraft/server/EntityLiving.java @@ -1,22 +1,14 @@ package net.minecraft.server; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.common.collect.Maps; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.UUID; - -// CraftBukkit start -import java.util.ArrayList; import com.google.common.base.Function; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import dev.cobblesword.nachospigot.Nacho; import dev.cobblesword.nachospigot.commons.Constants; -import dev.cobblesword.nachospigot.knockback.Knockback; import dev.cobblesword.nachospigot.knockback.KnockbackConfig; +import dev.cobblesword.nachospigot.knockback.KnockbackProfile; +import net.jafama.FastMath; +import org.bukkit.Bukkit; import org.bukkit.craftbukkit.event.CraftEventFactory; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Vehicle; @@ -24,13 +16,9 @@ import org.bukkit.event.entity.EntityDamageEvent.DamageModifier; import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.vehicle.VehicleExitEvent; -// CraftBukkit end - -import co.aikar.timings.SpigotTimings; // Spigot - -// PaperSpigot start -import org.bukkit.Bukkit; import org.spigotmc.event.entity.EntityDismountEvent; + +import java.util.*; // PaperSpigot end public abstract class EntityLiving extends Entity { @@ -798,16 +786,16 @@ public boolean damageEntity(DamageSource damagesource, float f) { } if (entity != null) { - double d0 = entity.locX - this.locX; + double distanceX = entity.locX - this.locX; - double d1; + double distanceZ; - for (d1 = entity.locZ - this.locZ; d0 * d0 + d1 * d1 < 1.0E-4D; d1 = (Math.random() - Math.random()) * 0.01D) { - d0 = (Math.random() - Math.random()) * 0.01D; + for (distanceZ = entity.locZ - this.locZ; distanceX * distanceX + distanceZ * distanceZ < 1.0E-4D; distanceZ = (Math.random() - Math.random()) * 0.01D) { + distanceX = (Math.random() - Math.random()) * 0.01D; } - this.aw = (float) (MathHelper.b(d1, d0) * 180.0D / 3.1415927410125732D - (double) this.yaw); - this.a(entity, f, d0, d1); + this.aw = (float) (MathHelper.b(distanceZ, distanceX) * 180.0D / 3.1415927410125732D - (double) this.yaw); + this.a(distanceX, distanceZ, damagesource); } else { this.aw = (float) ((int) (Math.random() * 2.0D) * 180); } @@ -898,32 +886,52 @@ public void die(DamageSource damagesource) { protected void dropEquipment(boolean flag, int i) {} - public void a(Entity entity, float f, double d0, double d1) { + public void a(double x, double z, DamageSource source) { if (this.random.nextDouble() >= this.getAttributeInstance(GenericAttributes.c).getValue()) { this.ai = true; - float magnitude = MathHelper.sqrt(d0 * d0 + d1 * d1); - - final KnockbackConfig config = Knockback.get().getConfig(); - if(config.customKnockback) { - this.motX /= config.knockbackFriction; - this.motY /= config.knockbackFriction; - this.motZ /= config.knockbackFriction; - this.motX -= d0 / (double) magnitude * config.knockbackHorizontal; - this.motY += config.knockbackVertical; - this.motZ -= d1 / (double) magnitude * config.knockbackHorizontal; - if (this.motY > config.knockbackVerticalLimit) { - this.motY = config.knockbackVerticalLimit; + + double magnitude = FastMath.sqrt(FastMath.pow(x, 2) + FastMath.pow(z, 2)); + double horizontal = 0.4D; + double vertical = 0.4D; + + KnockbackProfile kb = (this.getKnockbackProfile() == null) ? KnockbackConfig.getCurrentKb() : this.getKnockbackProfile(); + + if (source instanceof EntityDamageSourceIndirect) { + if (((EntityDamageSourceIndirect) source).getProximateDamageSource() instanceof EntityFishingHook) { + horizontal = kb.getRodHorizontal(); + vertical = kb.getRodVertical(); + } + if (((EntityDamageSourceIndirect) source).getProximateDamageSource() instanceof EntityArrow) { + horizontal = kb.getArrowHorizontal(); + vertical = kb.getArrowVertical(); + } + if (((EntityDamageSourceIndirect) source).getProximateDamageSource() instanceof EntitySnowball) { + horizontal = kb.getSnowballHorizontal(); + vertical = kb.getSnowballVertical(); + } + if (((EntityDamageSourceIndirect) source).getProximateDamageSource() instanceof EntityEgg) { + horizontal = kb.getEggHorizontal(); + vertical = kb.getEggVertical(); + } + if (((EntityDamageSourceIndirect) source).getProximateDamageSource() instanceof EntityEnderPearl) { + horizontal = kb.getPearlHorizontal(); + vertical = kb.getPearlVertical(); } } else { - float f2 = 0.4F; - this.motX /= 2.0D; - this.motY /= 2.0D; - this.motZ /= 2.0D; - this.motX -= d0 / (double) magnitude * (double) f2; - this.motY += f2; - this.motZ -= d1 / (double) magnitude * (double) f2; - if (this.motY > 0.4000000059604645D) this.motY = 0.4000000059604645D; + horizontal = kb.getHorizontal(); + vertical = kb.getVertical(); } + + // + this.motX *= kb.getFrictionHorizontal(); + this.motY *= kb.getFrictionVertical(); + this.motZ *= kb.getFrictionHorizontal(); + + this.motX -= x / magnitude * horizontal; + this.motY += vertical; + this.motZ -= z / magnitude * horizontal; + if (this.motY > kb.getVerticalMax()) this.motY = kb.getVerticalMax(); + if (this.motY < kb.getVerticalMin()) this.motY = kb.getVerticalMin(); } } diff --git a/NachoSpigot-Server/src/main/java/net/minecraft/server/Packet.java b/NachoSpigot-Server/src/main/java/net/minecraft/server/Packet.java new file mode 100644 index 000000000..18c1bf84d --- /dev/null +++ b/NachoSpigot-Server/src/main/java/net/minecraft/server/Packet.java @@ -0,0 +1,14 @@ +package net.minecraft.server; + +import java.io.IOException; + +public interface Packet { + void a(PacketDataSerializer var1) throws IOException; + + void b(PacketDataSerializer var1) throws IOException; + + void a(T var1); + + default boolean instant() { return false;} +} + diff --git a/NachoSpigot-Server/src/main/java/net/minecraft/server/PacketPlayInUseEntity.java b/NachoSpigot-Server/src/main/java/net/minecraft/server/PacketPlayInUseEntity.java new file mode 100644 index 000000000..d4feb0a5f --- /dev/null +++ b/NachoSpigot-Server/src/main/java/net/minecraft/server/PacketPlayInUseEntity.java @@ -0,0 +1,64 @@ +package net.minecraft.server; + +import me.elier.nachospigot.config.NachoConfig; + +import java.io.IOException; + +public class PacketPlayInUseEntity implements Packet { + private int a; + private PacketPlayInUseEntity.EnumEntityUseAction action; + private Vec3D c; + + public PacketPlayInUseEntity() { + } + + public void a(PacketDataSerializer var1) throws IOException { + this.a = var1.e(); + this.action = var1.a(PacketPlayInUseEntity.EnumEntityUseAction.class); + if (this.action == PacketPlayInUseEntity.EnumEntityUseAction.INTERACT_AT) { + this.c = new Vec3D(var1.readFloat(), var1.readFloat(), var1.readFloat()); + } + + } + + public void b(PacketDataSerializer var1) throws IOException { + var1.b(this.a); + var1.a(this.action); + if (this.action == PacketPlayInUseEntity.EnumEntityUseAction.INTERACT_AT) { + var1.writeFloat((float)this.c.a); + var1.writeFloat((float)this.c.b); + var1.writeFloat((float)this.c.c); + } + + } + + public void a(PacketListenerPlayIn var1) { + var1.a(this); + } + + public Entity a(World var1) { + return var1.a(this.a); + } + + public PacketPlayInUseEntity.EnumEntityUseAction a() { + return this.action; + } + + public Vec3D b() { + return this.c; + } + + public enum EnumEntityUseAction { + INTERACT, + ATTACK, + INTERACT_AT; + + EnumEntityUseAction() { + } + } + + @Override + public boolean instant() { + return NachoConfig.instantPlayInUseEntity; + } +} diff --git a/NachoSpigot-Server/src/main/java/net/minecraft/server/PlayerConnection.java b/NachoSpigot-Server/src/main/java/net/minecraft/server/PlayerConnection.java index dec84ac0f..add5ac816 100644 --- a/NachoSpigot-Server/src/main/java/net/minecraft/server/PlayerConnection.java +++ b/NachoSpigot-Server/src/main/java/net/minecraft/server/PlayerConnection.java @@ -1450,10 +1450,12 @@ public void a(PacketPlayInEntityAction packetplayinentityaction) { case 3: this.player.setSprinting(true); + this.player.setExtraKnockback(true); break; case 4: this.player.setSprinting(false); + this.player.setExtraKnockback(false); break; case 5: diff --git a/NachoSpigot-Server/src/main/java/net/minecraft/server/PlayerConnectionUtils.java b/NachoSpigot-Server/src/main/java/net/minecraft/server/PlayerConnectionUtils.java new file mode 100644 index 000000000..16060b1b2 --- /dev/null +++ b/NachoSpigot-Server/src/main/java/net/minecraft/server/PlayerConnectionUtils.java @@ -0,0 +1,14 @@ +package net.minecraft.server; + +public class PlayerConnectionUtils { + public static void ensureMainThread(final Packet packet, final T listener, IAsyncTaskHandler iasynctaskhandler) throws CancelledPacketHandleException { + if (!packet.instant() && !iasynctaskhandler.isMainThread()) { + iasynctaskhandler.postToMainThread(new Runnable() { + public void run() { + packet.a(listener); + } + }); + throw CancelledPacketHandleException.INSTANCE; + } + } +} diff --git a/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/CraftServer.java index df8068275..2ed44bbc9 100644 --- a/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -21,7 +21,7 @@ import com.eatthepath.uuid.FastUUID; import dev.cobblesword.nachospigot.Nacho; -import dev.cobblesword.nachospigot.knockback.Knockback; +import dev.cobblesword.nachospigot.knockback.KnockbackConfig; import me.elier.nachospigot.config.NachoConfig; import org.bukkit.craftbukkit.inventory.*; import xyz.sculas.nacho.malware.AntiMalware; @@ -737,6 +737,7 @@ public void reload() { org.github.paperspigot.PaperSpigotConfig.init((File) console.options.valueOf("paper-settings")); // PaperSpigot net.techcable.tacospigot.TacoSpigotConfig.init((File) console.options.valueOf("taco-settings")); // TacoSpigot NachoConfig.init((File) console.options.valueOf("nacho-settings")); // NachoSpigot + KnockbackConfig.init((File) console.options.valueOf("knockback-settings")); Nacho.get(); // NachoSpigot for (WorldServer world : console.worlds) { world.worldData.setDifficulty(difficulty); @@ -763,7 +764,6 @@ public void reload() { org.spigotmc.SpigotConfig.registerCommands(); // Spigot org.github.paperspigot.PaperSpigotConfig.registerCommands(); // PaperSpigot Nacho.get().registerCommands(); // NachoSpigot :: Commands - Knockback.get().registerCommands(); // NS Knockback :: Commands overrideAllCommandBlockCommands = commandsConfiguration.getStringList("command-block-overrides").contains("*"); @@ -796,57 +796,6 @@ public void reload() { enablePlugins(PluginLoadOrder.POSTWORLD); } - @Override - public void reloadKB() { - Knockback.get().reloadConfig(); - } - - @Override - public boolean customKnockback() { - return Knockback.get().getConfig().customKnockback; - }; - - @Override - public void toggleKnockback(Boolean b) { - Knockback.get().getConfig().customKnockback = b; - Knockback.get().saveConfig(); - } - - @Override - public void setKnockbackFriction(double d) { - Knockback.get().getConfig().knockbackFriction = d; - Knockback.get().saveConfig(); - } - - @Override - public void setKnockbackHorizontal(double d) { - Knockback.get().getConfig().knockbackHorizontal = d; - Knockback.get().saveConfig(); - } - - @Override - public void setKnockbackVertical(double d) { - Knockback.get().getConfig().knockbackVertical = d; - Knockback.get().saveConfig(); - } - - @Override - public void setKnockbackVerticalLimit(double d) { - Knockback.get().getConfig().knockbackVerticalLimit = d; - Knockback.get().saveConfig(); - } - - @Override - public void setKnockbackExtraHorizontal(double d) { - Knockback.get().getConfig().knockbackExtraHorizontal = d; - Knockback.get().saveConfig(); - } - - @Override - public void setKnockbackExtraVertical(double d) { - Knockback.get().getConfig().knockbackExtraVertical = d; - Knockback.get().saveConfig(); - } private void loadIcon() { icon = new CraftIconCache(null); diff --git a/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 7814dfc2d..05af61ff8 100644 --- a/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -13,6 +13,7 @@ import java.util.UUID; import dev.cobblesword.nachospigot.commons.Constants; +import me.elier.nachospigot.config.NachoConfig; import net.minecraft.server.*; import org.apache.commons.lang.Validate; @@ -335,7 +336,13 @@ public org.bukkit.entity.Item dropItem(Location loc, ItemStack item) { Validate.isTrue(item.getTypeId() != 0, "Cannot drop AIR."); EntityItem entity = new EntityItem(world, loc.getX(), loc.getY(), loc.getZ(), CraftItemStack.asNMSCopy(item)); entity.pickupDelay = 10; - world.addEntity(entity); + + if (!world.isMainThread()) { + world.postToMainThread(()-> world.addEntity(entity)); + } else { + world.addEntity(entity); + } + // TODO this is inconsistent with how Entity.getBukkitEntity() works. // However, this entity is not at the moment backed by a server entity class so it may be left. return new CraftItem(world.getServer(), entity); diff --git a/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/Main.java b/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/Main.java index 5e1fba31e..4ebb72766 100644 --- a/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/Main.java +++ b/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/Main.java @@ -154,6 +154,11 @@ public static void main(String[] args) { .ofType(File.class) .defaultsTo(new File("nacho.yml")) .describedAs("Yml file"); + acceptsAll(asList("knockback", "knockback-settings"), "File for nachospigot knockback settings") + .withRequiredArg() + .ofType(File.class) + .defaultsTo(new File("knockback.yml")) + .describedAs("Yml file"); // NachoSpigot end } }; diff --git a/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java index 395e644d5..b38b5f5b2 100644 --- a/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java +++ b/NachoSpigot-Server/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Set; +import dev.cobblesword.nachospigot.knockback.KnockbackProfile; import net.minecraft.server.DamageSource; import net.minecraft.server.EntityArmorStand; import net.minecraft.server.EntityArrow; @@ -539,4 +540,14 @@ public void setPullWhileLeashed(boolean pullWhileLeashed) { ((EntityInsentient) getHandle()).setPullWhileLeashed(pullWhileLeashed); } // TacoSpigot end + + @Override + public KnockbackProfile getKnockbackProfile() { + return getHandle().getKnockbackProfile(); + } + + @Override + public void setKnockbackProfile(KnockbackProfile profile) { + getHandle().setKnockbackProfile(profile); + } }