i think im done now?

This commit is contained in:
5vl 2022-04-08 17:00:57 +02:00
parent 82b08ff1d5
commit 266921adb4
No known key found for this signature in database
GPG Key ID: DA8938F22548E4D5
8 changed files with 146 additions and 13 deletions

View File

@ -6,21 +6,26 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.sql.SQLException;
public class JumpCommand implements CommandExecutor { public class JumpCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)) { if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use this command!"); sender.sendMessage(Main.color("&cOnly players can use this command!"));
return true; return true;
} }
Player p = (Player) sender; Player p = (Player) sender;
if (!p.hasPermission("doublejump.gui")) { if (!p.hasPermission("doublejump.gui")) {
p.sendMessage("You don't have permission to use this command!"); p.sendMessage(Main.color("You don't have permission to use this command!"));
return true; return true;
} }
p.openInventory(JumpGui.getGui(p)); try {
Main.inGui.add(p); p.openInventory(JumpGui.getGui(p));
Main.inGui.add(p);
} catch (SQLException e) {
e.printStackTrace();
}
return true; return true;
} }
} }

View File

@ -1,18 +1,22 @@
package me.fivevl.doublejump; package me.fivevl.doublejump;
import me.fivevl.doublejump.events.OnFly; import me.fivevl.doublejump.events.OnFly;
import me.fivevl.doublejump.events.OnInventoryClick;
import me.fivevl.doublejump.events.OnJoin;
import me.fivevl.doublejump.events.OnJump; import me.fivevl.doublejump.events.OnJump;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
public class Main extends JavaPlugin { public class Main extends JavaPlugin {
public static List<Player> doDoubleJump = new ArrayList<>(); public static HashMap<Player, Integer> doDoubleJump = new HashMap<>();
public static List<Player> inGui = new ArrayList<>(); public static List<Player> inGui = new ArrayList<>();
private static Main instance; private static Main instance;
public static Main getInstance() { public static Main getInstance() {
@ -34,6 +38,8 @@ public class Main extends JavaPlugin {
} }
Bukkit.getPluginManager().registerEvents(new OnFly(), this); Bukkit.getPluginManager().registerEvents(new OnFly(), this);
Bukkit.getPluginManager().registerEvents(new OnJump(), this); Bukkit.getPluginManager().registerEvents(new OnJump(), this);
Bukkit.getPluginManager().registerEvents(new OnJoin(), this);
Bukkit.getPluginManager().registerEvents(new OnInventoryClick(), this);
Objects.requireNonNull(getCommand("jump")).setExecutor(new JumpCommand()); Objects.requireNonNull(getCommand("jump")).setExecutor(new JumpCommand());
getLogger().info("DoubleJump enabled!"); getLogger().info("DoubleJump enabled!");
} }
@ -73,4 +79,8 @@ public class Main extends JavaPlugin {
sql.connect(); sql.connect();
sql.execute("CREATE TABLE IF NOT EXISTS `settings` (`uuid` VARCHAR(36) NOT NULL, `strength` DOUBLE NOT NULL DEFAULT '1', PRIMARY KEY (`uuid`))"); sql.execute("CREATE TABLE IF NOT EXISTS `settings` (`uuid` VARCHAR(36) NOT NULL, `strength` DOUBLE NOT NULL DEFAULT '1', PRIMARY KEY (`uuid`))");
} }
public static String color(String s) {
return ChatColor.translateAlternateColorCodes('&', s);
}
} }

View File

@ -1,6 +1,7 @@
package me.fivevl.doublejump; package me.fivevl.doublejump;
import javax.sql.rowset.CachedRowSet; import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetProvider;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
@ -56,7 +57,7 @@ public class MySQL implements Database {
@Override @Override
public CachedRowSet query(String query) throws SQLException{ public CachedRowSet query(String query) throws SQLException{
CachedRowSet cachedRowSet = new com.sun.rowset.CachedRowSetImpl(); CachedRowSet cachedRowSet = RowSetProvider.newFactory().createCachedRowSet();
cachedRowSet.populate(connection.prepareStatement(query).executeQuery()); cachedRowSet.populate(connection.prepareStatement(query).executeQuery());
return cachedRowSet; return cachedRowSet;
} }

View File

@ -1,20 +1,28 @@
package me.fivevl.doublejump.events; package me.fivevl.doublejump.events;
import me.fivevl.doublejump.Main; import me.fivevl.doublejump.Main;
import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerToggleFlightEvent; import org.bukkit.event.player.PlayerToggleFlightEvent;
import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
public class OnFly implements Listener { public class OnFly implements Listener {
@EventHandler @EventHandler
public void onFly(PlayerToggleFlightEvent e) { public void onFly(PlayerToggleFlightEvent e) throws SQLException {
Player p = e.getPlayer(); Player p = e.getPlayer();
if (p.hasPermission("doublejump.use") && Main.doDoubleJump.contains(p) && (p.getGameMode() == GameMode.ADVENTURE || p.getGameMode() == GameMode.SURVIVAL)) { if (p.hasPermission("doublejump.use") && Main.doDoubleJump.containsKey(p) && (p.getGameMode() == GameMode.ADVENTURE || p.getGameMode() == GameMode.SURVIVAL)) {
e.setCancelled(true); e.setCancelled(true);
p.setAllowFlight(false); p.setAllowFlight(false);
p.setVelocity(p.getLocation().getDirection().multiply(2.0D)); Bukkit.getScheduler().cancelTask(Main.doDoubleJump.get(p));
Main.doDoubleJump.remove(p);
CachedRowSet rs = Main.getMySQL().query("SELECT * FROM `settings` WHERE `uuid` = '" + p.getUniqueId() + "'");
rs.next();
p.setVelocity(p.getLocation().getDirection().multiply(rs.getDouble("strength")));
p.setFallDistance(0); p.setFallDistance(0);
} }
} }

View File

@ -0,0 +1,51 @@
package me.fivevl.doublejump.events;
import me.fivevl.doublejump.Main;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import javax.sql.rowset.CachedRowSet;
import java.math.RoundingMode;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Collections;
public class OnInventoryClick implements Listener {
@SuppressWarnings("deprecation")
@EventHandler
public void onClick(InventoryClickEvent e) throws SQLException {
Player p = (Player) e.getWhoClicked();
if (!Main.inGui.contains(p)) return;
e.setCancelled(true);
CachedRowSet rs = Main.getMySQL().query("SELECT * FROM `settings` WHERE `uuid` = '" + p.getUniqueId() + "'");
rs.next();
double strength = rs.getDouble("strength");
double newStrength = strength;
if (e.getRawSlot() == 12) newStrength = strength - 0.2;
if (e.getRawSlot() == 14) newStrength = strength + 0.2;
if (newStrength < 1) newStrength = 1;
if (newStrength > 5) newStrength = 5;
if (strength == newStrength) return;
DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.CEILING);
newStrength = Double.parseDouble(df.format(newStrength).replace(",", "."));
Inventory inv = e.getClickedInventory();
ItemStack item = inv.getItem(13);
ItemMeta currentStrengthMeta = item.getItemMeta();
currentStrengthMeta.setLore(Collections.singletonList(Main.color("&e" + newStrength)));
item.setItemMeta(currentStrengthMeta);
inv.setItem(13, item);
Main.getMySQL().execute("UPDATE `settings` SET `strength` = '" + newStrength + "' WHERE `uuid` = '" + p.getUniqueId() + "'");
}
@EventHandler
public void onClose(InventoryCloseEvent e) {
Main.inGui.remove((Player) e.getPlayer());
}
}

View File

@ -0,0 +1,20 @@
package me.fivevl.doublejump.events;
import me.fivevl.doublejump.Main;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
public class OnJoin implements Listener {
@EventHandler
public void onJoin(PlayerJoinEvent e) throws SQLException {
Player p = e.getPlayer();
CachedRowSet rs = Main.getMySQL().query("SELECT * FROM `settings` WHERE `uuid` = '" + p.getUniqueId() + "'");
if (!rs.next()) {
Main.getMySQL().execute("INSERT INTO `settings` (`uuid`, `strength`) VALUES ('" + p.getUniqueId() + "', default)");
}
}
}

View File

@ -14,11 +14,11 @@ public class OnJump implements Listener {
Player p = e.getPlayer(); Player p = e.getPlayer();
if (p.hasPermission("doublejump.use") && (p.getGameMode() == GameMode.ADVENTURE || p.getGameMode() == GameMode.SURVIVAL)) { if (p.hasPermission("doublejump.use") && (p.getGameMode() == GameMode.ADVENTURE || p.getGameMode() == GameMode.SURVIVAL)) {
p.setAllowFlight(true); p.setAllowFlight(true);
Main.doDoubleJump.add(p); int id = Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> {
Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> {
p.setAllowFlight(false); p.setAllowFlight(false);
Main.doDoubleJump.remove(p); Main.doDoubleJump.remove(p);
}, 20L); }, 20L);
Main.doDoubleJump.put(p, id);
} }
} }
} }

View File

@ -1,13 +1,51 @@
package me.fivevl.doublejump.guis; package me.fivevl.doublejump.guis;
import me.fivevl.doublejump.Main;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
import java.util.Collections;
public class JumpGui { public class JumpGui {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static Inventory getGui(Player p) { public static Inventory getGui(Player p) throws SQLException {
Inventory inv = Bukkit.createInventory(null, 27, "Jump Strength"); Inventory inv = Bukkit.createInventory(null, 27, Main.color("&8Jump Strength Menu"));
ItemStack glass = new ItemStack(Material.GRAY_STAINED_GLASS_PANE, 1);
ItemMeta glassMeta = glass.getItemMeta();
glassMeta.setDisplayName(" ");
glass.setItemMeta(glassMeta);
for (int i = 0; i < inv.getSize(); i++) {
inv.setItem(i, glass);
}
ItemStack decreaseStrength = new ItemStack(Material.ARROW, 1);
ItemMeta decreaseStrengthMeta = decreaseStrength.getItemMeta();
decreaseStrengthMeta.setDisplayName(Main.color("&9Decrease Strength"));
decreaseStrength.setItemMeta(decreaseStrengthMeta);
inv.setItem(12, decreaseStrength);
ItemStack increaseStrength = new ItemStack(Material.ARROW, 1);
ItemMeta increaseStrengthMeta = increaseStrength.getItemMeta();
increaseStrengthMeta.setDisplayName(Main.color("&9Increase Strength"));
increaseStrength.setItemMeta(increaseStrengthMeta);
inv.setItem(14, increaseStrength);
CachedRowSet rs = Main.getMySQL().query("SELECT * FROM `settings` WHERE `uuid` = '" + p.getUniqueId() + "'");
if (rs.next()) {
ItemStack currentStrength = new ItemStack(Material.FEATHER, 1);
ItemMeta currentStrengthMeta = currentStrength.getItemMeta();
currentStrengthMeta.setDisplayName(Main.color("&2Current Jump Strength"));
currentStrengthMeta.setLore(Collections.singletonList(Main.color("&e" + rs.getDouble("strength"))));
currentStrength.setItemMeta(currentStrengthMeta);
inv.setItem(13, currentStrength);
}
return inv; return inv;
} }