From 96bf94836a5d3a29ceb622db298731dde7d09f5b Mon Sep 17 00:00:00 2001 From: mikx Date: Mon, 27 Nov 2023 03:43:53 -0500 Subject: [PATCH] mxcrafttp to cubetp --- .gitignore | 51 ++++ pom.xml | 74 ++++++ src/main/java/ovh/mxg/cubetp/CubeTP.java | 320 +++++++++++++++++++++++ src/main/java/ovh/mxg/cubetp/SQL.java | 97 +++++++ src/main/resources/config.yml | 17 ++ src/main/resources/plugin.yml | 4 + target/classes/config.yml | 17 ++ target/classes/plugin.yml | 4 + 8 files changed, 584 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/ovh/mxg/cubetp/CubeTP.java create mode 100644 src/main/java/ovh/mxg/cubetp/SQL.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml create mode 100644 target/classes/config.yml create mode 100644 target/classes/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..12329d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,51 @@ +*.class +*.log +*.ctxt +.mtj.tmp/ +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar +hs_err_pid* +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf +.idea/**/contentModel.xml +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml +.idea/**/gradle.xml +.idea/**/libraries +cmake-build-*/ +.idea/**/mongoSettings.xml +*.iws +out/ +.idea_modules/ +atlassian-ide-plugin.xml +.idea/replstate.xml +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +.idea/httpRequests +.idea/caches/build_file_checksums.ser +.idea +*.iml +out +gen +.gradle +**/build/ +!src/**/build/ +gradle-app.setting +!gradle-wrapper.jar +.gradletasknamecache +run/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4196e16 --- /dev/null +++ b/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + ovh.mxg + CubeTP + 1.0-SNAPSHOT + jar + + CubeTP + + + 1.8 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + + org.spigotmc + spigot-api + 1.20.2-R0.1-SNAPSHOT + provided + + + diff --git a/src/main/java/ovh/mxg/cubetp/CubeTP.java b/src/main/java/ovh/mxg/cubetp/CubeTP.java new file mode 100644 index 0000000..68a7ba2 --- /dev/null +++ b/src/main/java/ovh/mxg/cubetp/CubeTP.java @@ -0,0 +1,320 @@ +package ovh.mxg.cubetp; + +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.Sign; +import org.bukkit.block.sign.Side; +import org.bukkit.block.sign.SignSide; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Item; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.ProjectileHitEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.PlayerEggThrowEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.MainHand; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Random; + +public final class CubeTP extends JavaPlugin implements Listener { + + public static String host; + public static String port; + public static String database; + public static String username; + public static String password; + public static Material cubeTP; + public static Material tomePortalReagent; + Inventory inv = null; + Location clickedCubeLoc; + Block clickedBlock; + String curCubeName = ""; + double curCubeX = 0; + double curCubeY = 0; + double curCubeZ = 0; + @Override + public void onEnable() { + getServer().getPluginManager().registerEvents(this,this); + + //Config + File f = new File("plugins/CubeTP/config.yml"); + if(!f.exists()) + { + saveDefaultConfig(); + } + + getLogger().info("Loading config..."); + host = getConfig().getString("CubeTP.sqlHost"); + port = getConfig().getString("CubeTP.sqlPort"); + database = getConfig().getString("CubeTP.sqlDB"); + username = getConfig().getString("CubeTP.sqlUser"); + password = getConfig().getString("CubeTP.sqlPass"); + cubeTP = Material.valueOf(getConfig().getString("CubeTP.cubeMaterial")); + tomePortalReagent = Material.valueOf(getConfig().getString("CubeTP.tomePortalReagent")); + + getLogger().info("Enabled!"); + } + + @EventHandler + public void PlayerInteractEvent(PlayerInteractEvent event) throws SQLException { + clickedBlock = event.getClickedBlock(); + Player player = event.getPlayer(); + Action action = event.getAction(); + if(event.getHand() == EquipmentSlot.HAND){ + if (clickedBlock == null) return; + World world = getServer().getWorld("world"); + Block block = world.getBlockAt(clickedBlock.getLocation().getBlockX(), clickedBlock.getLocation().getBlockY()+1, clickedBlock.getLocation().getBlockZ()); + if(action == Action.RIGHT_CLICK_BLOCK && clickedBlock.getType() == cubeTP && block.getState() instanceof Sign){ + clickedCubeLoc = clickedBlock.getLocation(); + world = getServer().getWorld("world"); + block = world.getBlockAt(clickedBlock.getLocation().getBlockX(), clickedBlock.getLocation().getBlockY()+1, clickedBlock.getLocation().getBlockZ()); + BlockState state = block.getState(); + Sign sign = (Sign) state; + SignSide sside = sign.getSide(Side.FRONT); + curCubeName = sside.getLine(0); + curCubeX = clickedBlock.getLocation().getBlockX(); + curCubeY = clickedBlock.getLocation().getBlockY(); + curCubeZ = clickedBlock.getLocation().getBlockZ(); + ShowUI(player); + } + if(action == Action.RIGHT_CLICK_BLOCK){ + ItemStack mhi = player.getInventory().getItemInMainHand(); + if(mhi.getItemMeta() != null){ + String mainhand = mhi.getItemMeta().getDisplayName(); + if(mainhand.equals("Tome de portail")){ + ShowUIPortal(player); + mhi.setAmount(0); + } + } + } + } + } + + @EventHandler + public void onInventoryClick(final InventoryClickEvent e) throws SQLException { + if (!e.getInventory().equals(inv)) return; + + e.setCancelled(true); + + final ItemStack clickedItem = e.getCurrentItem(); + + // verify current item is not null + if (clickedItem == null || clickedItem.getType().isAir()) return; + + final Player p = (Player) e.getWhoClicked(); + + // register the cube + if (e.getRawSlot() == 3){ + if(SQL.CheckCubeNameExists(curCubeName)){ + p.sendMessage(MessageFormat.format("§bLe cube {0} existe déjà. Changez le nom.", curCubeName)); + } else { + SQL.AddCube(curCubeName, curCubeX, curCubeY, curCubeZ); + p.sendMessage(MessageFormat.format("§bLe cube {0} a été ajouté.", curCubeName)); + ShowUI(p); + } + } + + // create a tome of portal from a book + if (e.getRawSlot() == 4){ + Inventory pinv = p.getInventory(); + if(HaveBook(p)){ + TakeBook(p); + pinv.addItem(createGuiItem(Material.ENCHANTED_BOOK, "Tome de portail", "Permets d'aller vers un cube, pas de revenir.")); + } else { + p.sendMessage("§bVous devez posséder un livre pour créer cet objet."); + } + } + + // unregister a cube + if (e.getRawSlot() == 5){ + if(SQL.CheckCubeNameExists(curCubeName)){ + SQL.DeleteCubeByName(curCubeName); + p.sendMessage(MessageFormat.format("§bLe cube {0} a été supprimé.", curCubeName)); + ShowUI(p); + } else { + p.sendMessage(MessageFormat.format("§bLe cube {0} n'existe pas.", curCubeName)); + } + } + + // tp to a remote cube + if (e.getRawSlot() >= 9){ + String name = e.getCurrentItem().getItemMeta().getDisplayName(); + CubeTP(p,name); + } + } + + @EventHandler + public void onBlockBreakWithSignOnIt(BlockBreakEvent event){ + + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) throws SQLException { + final Player p = event.getPlayer(); + Block block = event.getBlock(); + if(block.getState() instanceof Sign){ + BlockState state = block.getState(); + Sign sign = (Sign) state; + SignSide sside = sign.getSide(Side.FRONT); + double x = block.getX(); + double y = block.getY()-1; + double z = block.getZ(); + if(SQL.CheckCubeByLocation(x, y, z)){ + String st = sside.getLine(0); + SQL.DeleteCubeByName(st); + Block cb = p.getWorld().getBlockAt(block.getX(),block.getY()-1, block.getZ()); + cb.setType(Material.AIR); + ItemStack item = new ItemStack(cubeTP, 1); + p.getWorld().dropItem(p.getLocation(), item); + } + } + if(block.getType() == Material.GOLD_BLOCK){ + double x = block.getX(); + double y = block.getY(); + double z = block.getZ(); + if(SQL.CheckCubeByLocation(x, y, z)){ + Block sb = p.getWorld().getBlockAt(block.getX(),block.getY()+1, block.getZ()); + BlockState state = sb.getState(); + Sign sign = (Sign) state; + SignSide sside = sign.getSide(Side.FRONT); + String st = sside.getLine(0); + SQL.DeleteCubeByName(st); + sb.setType(Material.AIR); + ItemStack item = new ItemStack(Material.OAK_SIGN, 1); + p.getWorld().dropItem(p.getLocation(), item); + } + } + } + + @EventHandler + public void onPlayerChat(AsyncPlayerChatEvent event){ + + } + + public boolean HaveBook(Player player){ + boolean hb = false; + Inventory pinv = player.getInventory(); + for(ItemStack i: pinv){ + if(i != null){ + if(i.getType() == tomePortalReagent){ + hb = true; + } + } + } + return hb; + } + public void TakeBook(Player player){ + int ba = 0; + Inventory pinv = player.getInventory(); + for(ItemStack i: pinv){ + if(i != null){ + if(i.getType() == tomePortalReagent){ + ba = i.getAmount(); + i.setAmount(ba-1); + } + } + } + } + + public void ShowUI(Player player) throws SQLException { + inv = Bukkit.createInventory(null, 45, "CubeTP"); + World world = getServer().getWorld("world"); + Block block = world.getBlockAt(clickedBlock.getLocation().getBlockX(), clickedBlock.getLocation().getBlockY()+1, clickedBlock.getLocation().getBlockZ()); + BlockState state = block.getState(); + Sign sign = (Sign) state; + SignSide sside = sign.getSide(Side.FRONT); + String lore01 = MessageFormat.format("{0}", sside.getLine(0)); + String lore02 = MessageFormat.format("x{0} y{1} z{2}", clickedBlock.getLocation().getBlockX(), clickedBlock.getLocation().getBlockY(), clickedBlock.getLocation().getBlockZ()); + inv.setItem(0,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(1,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(2,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(3,createGuiItem(Material.BOOK, "§bEnregistrer", lore01, lore02)); + inv.setItem(4,createGuiItem(Material.ENCHANTED_BOOK, "Tome de portail")); + inv.setItem(5,createGuiItem(Material.TNT, "§bRetirer", "§aRetirer de la liste de TP.")); + inv.setItem(6,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(7,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(8,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + AddAllCubes(player); + player.openInventory(inv); + } + + public void ShowUIPortal(Player player) throws SQLException { + inv = Bukkit.createInventory(null, 45, "CubeTP"); + inv.setItem(0,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(1,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(2,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(3,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(4,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(5,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(6,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(7,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + inv.setItem(8,createGuiItem(Material.BLACK_STAINED_GLASS_PANE, " ")); + AddAllCubes(player); + player.openInventory(inv); + } + + public void AddAllCubes(Player player) throws SQLException { + SQL.connect(); + PreparedStatement ps = SQL.getConnection().prepareStatement("SELECT * FROM cubetp"); + ResultSet rs = ps.executeQuery(); + int slot = 9; + while(rs != null && rs.next()) { + String lore01 = MessageFormat.format("x{0} y{1} z{2}", rs.getDouble("x"), rs.getDouble("y"), rs.getDouble("z")); + inv.setItem(slot,createGuiItem(Material.SPYGLASS, rs.getString("name"), lore01)); + slot++; + } + } + + public void CubeTP(Player player, String name) throws SQLException { + SQL.connect(); + PreparedStatement ps = SQL.getConnection().prepareStatement("SELECT * FROM cubetp WHERE name = ?"); + ps.setString(1, name); + ResultSet rs = ps.executeQuery(); + int slot = 9; + if(rs.next()) { + Location loc = new Location(player.getWorld(), rs.getDouble("x"),rs.getDouble("y"),rs.getDouble("z"), 0, 0); + player.teleport(loc); + } + } + + protected ItemStack createGuiItem(final Material material, final String name, final String... lore) { + final ItemStack item = new ItemStack(material, 1); + final ItemMeta meta = item.getItemMeta(); + + // Set the name of the item + meta.setDisplayName(name); + + // Set the lore of the item + meta.setLore(Arrays.asList(lore)); + + item.setItemMeta(meta); + + return item; + } + + @Override + public void onDisable() { + + } +} diff --git a/src/main/java/ovh/mxg/cubetp/SQL.java b/src/main/java/ovh/mxg/cubetp/SQL.java new file mode 100644 index 0000000..22b6e13 --- /dev/null +++ b/src/main/java/ovh/mxg/cubetp/SQL.java @@ -0,0 +1,97 @@ +package ovh.mxg.cubetp; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import java.sql.*; +import java.text.MessageFormat; + +public class SQL { + public static Connection con; + + static ConsoleCommandSender console = Bukkit.getConsoleSender(); + + // connect + public static void connect() { + if (!isConnected()) { + try { + con = DriverManager.getConnection("jdbc:mysql://" + CubeTP.host + ":" + CubeTP.port + "/" + CubeTP.database, CubeTP.username, CubeTP.password); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + // disconnect + public static void disconnect() { + if (isConnected()) { + try { + con.close(); + console.sendMessage("\247c[\2476Minepedia-System\247c]\247bMySQL-Verbindung wurde geschlossen!"); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + // isConnected + public static boolean isConnected() { + return (con == null ? false : true); + } + + // getConnection + public static Connection getConnection() { + return con; + } + + public static void AddCube(String name, double x, double y, double z) throws SQLException { + connect(); + PreparedStatement ps = getConnection().prepareStatement("INSERT IGNORE INTO cubetp (name,x,y,z) VALUES (?,?,?,?)"); + ps.setString(1, name); + ps.setDouble(2, x); + ps.setDouble(3, y); + ps.setDouble(4, z); + ps.executeUpdate(); + } + + public static boolean CheckCubeNameExists(String name) throws SQLException { + SQL.connect(); + PreparedStatement ps = SQL.getConnection().prepareStatement("SELECT * FROM cubetp WHERE name = ?"); + ps.setString(1, name); + ResultSet rs = ps.executeQuery(); + return rs.next(); + } + + public static void DeleteCubeByName(String name) throws SQLException { + SQL.connect(); + PreparedStatement ps = SQL.getConnection().prepareStatement("DELETE FROM cubetp WHERE name = ?"); + ps.setString(1, name); + ps.executeUpdate(); + } + + public static boolean CheckCubeByLocation(double x, double y, double z) throws SQLException { + SQL.connect(); + PreparedStatement ps = SQL.getConnection().prepareStatement("SELECT * FROM cubetp WHERE x = ? AND y = ? AND z = ?"); + ps.setDouble(1, x); + ps.setDouble(2, y); + ps.setDouble(3, z); + ResultSet rs = ps.executeQuery(); + return rs.next(); + } + + public static String GetCubeNameByLocation(double x, double y, double z) throws SQLException { + SQL.connect(); + PreparedStatement ps = SQL.getConnection().prepareStatement("SELECT * FROM cubetp WHERE x = ? AND y = ? AND z = ?"); + ps.setDouble(1, x); + ps.setDouble(2, y); + ps.setDouble(3, z); + ResultSet rs = ps.executeQuery(); + if(rs.next()){ + return rs.getString("name"); + } + return null; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..909ebe4 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,17 @@ +CubeTP: + ## MySQL + sqlHost: '127.0.0.1' + sqlPort: 3306 + sqlDB: 'changeme' + sqlUser: 'changeme' + sqlPass: 'changeme' + ## CubeTP + # Material used by the plugin to create a TP cube. + # Must be a block. Exemple: 'IRON_BLOCK' 'REDSTONE_BLOCK' + # Don't change it if you added tp cube in your world, they will stop working. + # I might add an update command in the future, until then, don't change it if it's not a new world. + cubeMaterial: 'GOLD_BLOCK' + ## TomeOfPortal Reagent + # Material of the reagent used to create a Tome of Portal. + # Any item can be used. You will need one in your inventory to create a Tome of Portal at a tp cube. + tomePortalReagent: 'BOOK' \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..6ab4e28 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,4 @@ +name: CubeTP +version: '${project.version}' +main: ovh.mxg.cubetp.CubeTP +api-version: '1.20' diff --git a/target/classes/config.yml b/target/classes/config.yml new file mode 100644 index 0000000..909ebe4 --- /dev/null +++ b/target/classes/config.yml @@ -0,0 +1,17 @@ +CubeTP: + ## MySQL + sqlHost: '127.0.0.1' + sqlPort: 3306 + sqlDB: 'changeme' + sqlUser: 'changeme' + sqlPass: 'changeme' + ## CubeTP + # Material used by the plugin to create a TP cube. + # Must be a block. Exemple: 'IRON_BLOCK' 'REDSTONE_BLOCK' + # Don't change it if you added tp cube in your world, they will stop working. + # I might add an update command in the future, until then, don't change it if it's not a new world. + cubeMaterial: 'GOLD_BLOCK' + ## TomeOfPortal Reagent + # Material of the reagent used to create a Tome of Portal. + # Any item can be used. You will need one in your inventory to create a Tome of Portal at a tp cube. + tomePortalReagent: 'BOOK' \ No newline at end of file diff --git a/target/classes/plugin.yml b/target/classes/plugin.yml new file mode 100644 index 0000000..0aca871 --- /dev/null +++ b/target/classes/plugin.yml @@ -0,0 +1,4 @@ +name: CubeTP +version: '1.0-SNAPSHOT' +main: ovh.mxg.cubetp.CubeTP +api-version: '1.20'