initial commit
This commit is contained in:
207
src/main/java/ovh/mxg/mxcrafttp/MxCraftTP.java
Normal file
207
src/main/java/ovh/mxg/mxcrafttp/MxCraftTP.java
Normal file
@@ -0,0 +1,207 @@
|
||||
package ovh.mxg.mxcrafttp;
|
||||
|
||||
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.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
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 MxCraftTP extends JavaPlugin implements Listener {
|
||||
|
||||
public static String host;
|
||||
public static String port;
|
||||
public static String database;
|
||||
public static String username;
|
||||
public static String password;
|
||||
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/MxCraftTP/config.yml");
|
||||
if(!f.exists())
|
||||
{
|
||||
saveDefaultConfig();
|
||||
}
|
||||
|
||||
getLogger().info("Loading config...");
|
||||
host = getConfig().getString("MxCraftTP.sqlHost");
|
||||
port = getConfig().getString("MxCraftTP.sqlPort");
|
||||
database = getConfig().getString("MxCraftTP.sqlDB");
|
||||
username = getConfig().getString("MxCraftTP.sqlUser");
|
||||
password = getConfig().getString("MxCraftTP.sqlPass");
|
||||
|
||||
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() == Material.GOLD_BLOCK && 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
if (e.getRawSlot() >= 9){
|
||||
String name = e.getCurrentItem().getItemMeta().getDisplayName();
|
||||
CubeTP(p,name);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerChat(AsyncPlayerChatEvent event){
|
||||
|
||||
}
|
||||
|
||||
public void ShowUI(Player player) throws SQLException {
|
||||
inv = Bukkit.createInventory(null, 45, "MxCraftTP");
|
||||
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.BLACK_STAINED_GLASS_PANE, " "));
|
||||
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, " "));
|
||||
inv.setItem(9,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() {
|
||||
|
||||
}
|
||||
}
|
||||
74
src/main/java/ovh/mxg/mxcrafttp/SQL.java
Normal file
74
src/main/java/ovh/mxg/mxcrafttp/SQL.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package ovh.mxg.mxcrafttp;
|
||||
|
||||
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://" + MxCraftTP.host + ":" + MxCraftTP.port + "/" + MxCraftTP.database, MxCraftTP.username, MxCraftTP.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();
|
||||
}
|
||||
}
|
||||
7
src/main/resources/config.yml
Normal file
7
src/main/resources/config.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
MxCraftTP:
|
||||
# MySQL
|
||||
sqlHost: '127.0.0.1'
|
||||
sqlPort: 3306
|
||||
sqlDB: 'changeme'
|
||||
sqlUser: 'changeme'
|
||||
sqlPass: 'changeme'
|
||||
4
src/main/resources/plugin.yml
Normal file
4
src/main/resources/plugin.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
name: MxCraftTP
|
||||
version: 1.0.0
|
||||
main: ovh.mxg.mxcrafttp.MxCraftTP
|
||||
api-version: '1.20'
|
||||
Reference in New Issue
Block a user