From bf5cb4fdea3a87fbf58b95feab7ecf92c0accc39 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Mon, 20 Jan 2020 22:37:10 -0600 Subject: [PATCH] Create a more reliable way to save and load foxes --- .../tamablefoxes/EntityTamableFox.java | 12 +- .../tamablefoxes/TamableFoxes.java | 18 +-- .../command/CommandSpawnTamableFox.java | 0 .../tamablefoxes/io/FileManager.java | 0 .../pathfinding/FoxPathfindGoalBeg.java | 0 .../pathfinding/FoxPathfindGoalBreed.java | 0 .../pathfinding/FoxPathfindGoalFleeSun.java | 0 .../pathfinding/FoxPathfindGoalFloat.java | 0 .../FoxPathfindGoalFollowOwner.java | 0 .../FoxPathfindGoalHurtByTarget.java | 0 .../pathfinding/FoxPathfindGoalLunge.java | 0 .../FoxPathfindGoalLungeUNKNOWN_USE.java | 0 .../FoxPathfindGoalMeleeAttack.java | 0 .../FoxPathfindGoalOwnerHurtByTarget.java | 0 .../FoxPathfindGoalOwnerHurtTarget.java | 0 .../FoxPathfindGoalPickBushes.java | 0 .../FoxPathfindGoalRandomStrollLand.java | 0 .../FoxPathfindGoalRandomTargetNonTamed.java | 0 .../pathfinding/FoxPathfindGoalSit.java | 0 .../tamablefoxes/sqlite/SQLiteHandler.java | 51 ++++++++ .../sqlite/SQLiteSetterGetter.java | 120 ++++++++++++++++++ 21 files changed, 189 insertions(+), 12 deletions(-) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/EntityTamableFox.java (97%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/TamableFoxes.java (97%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/command/CommandSpawnTamableFox.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/io/FileManager.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalBeg.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalBreed.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalFleeSun.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalFloat.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalFollowOwner.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalHurtByTarget.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalLunge.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalLungeUNKNOWN_USE.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalMeleeAttack.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtByTarget.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtTarget.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalPickBushes.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalRandomStrollLand.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalRandomTargetNonTamed.java (100%) rename src/main/java/net/{seanomilk => seanomik}/tamablefoxes/pathfinding/FoxPathfindGoalSit.java (100%) create mode 100644 src/main/java/net/seanomik/tamablefoxes/sqlite/SQLiteHandler.java create mode 100644 src/main/java/net/seanomik/tamablefoxes/sqlite/SQLiteSetterGetter.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/EntityTamableFox.java b/src/main/java/net/seanomik/tamablefoxes/EntityTamableFox.java similarity index 97% rename from src/main/java/net/seanomilk/tamablefoxes/EntityTamableFox.java rename to src/main/java/net/seanomik/tamablefoxes/EntityTamableFox.java index 221af46..6e784dc 100644 --- a/src/main/java/net/seanomilk/tamablefoxes/EntityTamableFox.java +++ b/src/main/java/net/seanomik/tamablefoxes/EntityTamableFox.java @@ -30,14 +30,15 @@ public class EntityTamableFox extends EntityFox { private FoxPathfindGoalSit goalSit; private PathfinderGoalNearestAttackableTarget goalAttack; - public EntityTamableFox(TamableFoxes plugin, EntityTypes entitytypes, World world) { super(EntityTypes.FOX, world); this.plugin = plugin; thisFox = (Fox) this.getBukkitEntity(); plugin.getFoxUUIDs().put(this.getBukkitEntity().getUniqueId(), null); + this.setPersistent(); } + @Override protected void initPathfinder() { this.goalSit = new FoxPathfindGoalSit(this); this.goalSelector.a(1, new FoxPathfindGoalFloat(this)); @@ -70,6 +71,7 @@ public class EntityTamableFox extends EntityFox { this.targetSelector.a(5, new FoxPathfindGoalHurtByTarget(this).a(new Class[0])); } + @Override protected void initAttributes() { this.getAttributeMap().b(GenericAttributes.MAX_HEALTH); this.getAttributeMap().b(GenericAttributes.KNOCKBACK_RESISTANCE); @@ -137,10 +139,12 @@ public class EntityTamableFox extends EntityFox { public void setTamed(boolean tamed) { this.isTamed = tamed; - // remove attack goal if now tamed - if (isTamed && plugin.isTamedAttackRabbitChicken()) + // Remove attack goal if tamed + if (isTamed && plugin.isTamedAttackRabbitChicken()) { this.targetSelector.a(goalAttack); - else this.targetSelector.a(4, goalAttack); + } else { + this.targetSelector.a(4, goalAttack); + } } public String getChosenName() { diff --git a/src/main/java/net/seanomilk/tamablefoxes/TamableFoxes.java b/src/main/java/net/seanomik/tamablefoxes/TamableFoxes.java similarity index 97% rename from src/main/java/net/seanomilk/tamablefoxes/TamableFoxes.java rename to src/main/java/net/seanomik/tamablefoxes/TamableFoxes.java index a469d2d..a2b11f9 100644 --- a/src/main/java/net/seanomilk/tamablefoxes/TamableFoxes.java +++ b/src/main/java/net/seanomik/tamablefoxes/TamableFoxes.java @@ -20,6 +20,7 @@ import org.bukkit.craftbukkit.v1_15_R1.entity.CraftItem; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Fox; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -33,6 +34,8 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.java.JavaPlugin; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -178,6 +181,7 @@ public class TamableFoxes extends JavaPlugin implements Listener { configFoxes.get().set("Foxes." + spawnedEntity.getUniqueID() + ".owner", "none"); fileManager.saveConfig("foxes.yml"); + return fox; } @@ -352,12 +356,10 @@ public class TamableFoxes extends JavaPlugin implements Listener { // Name process player.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "You just tamed a wild fox!"); player.sendMessage(ChatColor.RED + "What do you want to call it?"); - //player.sendMessage(ChatColor.GRAY + "Type a name in chat"); - //waitingName.put(player, tamableFox.getUniqueID()); tamableFox.setChosenName("???"); new AnvilGUI.Builder() - .onComplete((plr, text) -> { //called when the inventory output slot is clicked + .onComplete((plr, text) -> { // Called when the inventory output slot is clicked if(!text.equals("")) { tamableFox.setChosenName(text); plr.sendMessage(getPrefix() + ChatColor.GREEN + text + " is perfect!"); @@ -366,10 +368,10 @@ public class TamableFoxes extends JavaPlugin implements Listener { return AnvilGUI.Response.text("Insert a name for your fox!"); } }) - .preventClose() // prevents the inventory from being closed - .text("Fox name") // sets the text the GUI should start with - .plugin(this) // set the plugin instance - .open(player); // opens the GUI for the player provided + .preventClose() // Prevents the inventory from being closed + .text("Fox name") // Sets the text the GUI should start with + .plugin(this) // Set the plugin instance + .open(player); // Opens the GUI for the player provided } else { player.getWorld().spawnParticle(Particle.SMOKE_NORMAL, entity.getLocation(), 10, 0.3D, 0.3D, 0.3D, 0.15D); @@ -493,7 +495,7 @@ public class TamableFoxes extends JavaPlugin implements Listener { return foxUUIDs; } - public String getPrefix() { + public static String getPrefix() { //return ChatColor.translateAlternateColorCodes('&', (String) config.get("prefix")); return ChatColor.RED + "[Tamable Foxes] "; } diff --git a/src/main/java/net/seanomilk/tamablefoxes/command/CommandSpawnTamableFox.java b/src/main/java/net/seanomik/tamablefoxes/command/CommandSpawnTamableFox.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/command/CommandSpawnTamableFox.java rename to src/main/java/net/seanomik/tamablefoxes/command/CommandSpawnTamableFox.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/io/FileManager.java b/src/main/java/net/seanomik/tamablefoxes/io/FileManager.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/io/FileManager.java rename to src/main/java/net/seanomik/tamablefoxes/io/FileManager.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalBeg.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalBeg.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalBeg.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalBeg.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalBreed.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalBreed.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalBreed.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalBreed.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalFleeSun.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalFleeSun.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalFleeSun.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalFleeSun.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalFloat.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalFloat.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalFloat.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalFloat.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalFollowOwner.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalFollowOwner.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalFollowOwner.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalFollowOwner.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalHurtByTarget.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalHurtByTarget.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalHurtByTarget.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalHurtByTarget.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalLunge.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalLunge.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalLunge.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalLunge.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalLungeUNKNOWN_USE.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalLungeUNKNOWN_USE.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalLungeUNKNOWN_USE.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalLungeUNKNOWN_USE.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalMeleeAttack.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalMeleeAttack.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalMeleeAttack.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalMeleeAttack.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtByTarget.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtByTarget.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtByTarget.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtByTarget.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtTarget.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtTarget.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtTarget.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalOwnerHurtTarget.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalPickBushes.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalPickBushes.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalPickBushes.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalPickBushes.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalRandomStrollLand.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalRandomStrollLand.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalRandomStrollLand.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalRandomStrollLand.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalRandomTargetNonTamed.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalRandomTargetNonTamed.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalRandomTargetNonTamed.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalRandomTargetNonTamed.java diff --git a/src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalSit.java b/src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalSit.java similarity index 100% rename from src/main/java/net/seanomilk/tamablefoxes/pathfinding/FoxPathfindGoalSit.java rename to src/main/java/net/seanomik/tamablefoxes/pathfinding/FoxPathfindGoalSit.java diff --git a/src/main/java/net/seanomik/tamablefoxes/sqlite/SQLiteHandler.java b/src/main/java/net/seanomik/tamablefoxes/sqlite/SQLiteHandler.java new file mode 100644 index 0000000..3937fde --- /dev/null +++ b/src/main/java/net/seanomik/tamablefoxes/sqlite/SQLiteHandler.java @@ -0,0 +1,51 @@ +package net.seanomik.tamablefoxes.sqllite; + +import net.seanomik.tamablefoxes.TamableFoxes; +import org.bukkit.Bukkit; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class SQLiteHandler { + private Connection connection; + + public void connect() { + try { + String baseLoc = Bukkit.getWorldContainer().toURI().toString().substring(6); + baseLoc = baseLoc.substring(0,baseLoc.length()-2); + + String url = "jdbc:sqlite:" + baseLoc + "plugins/TamableFoxes/foxes.db"; + connection = DriverManager.getConnection(url); + + Bukkit.getConsoleSender().sendMessage(TamableFoxes.getPrefix() + "Connection to SQLite has been established."); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public Connection getConnection() { + return connection; + } + + public void closeConnection() { + try { + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void newConnection() { + try { + connection.close(); + connect(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void setConnection(Connection connection) { + this.connection = connection; + } +} diff --git a/src/main/java/net/seanomik/tamablefoxes/sqlite/SQLiteSetterGetter.java b/src/main/java/net/seanomik/tamablefoxes/sqlite/SQLiteSetterGetter.java new file mode 100644 index 0000000..f099499 --- /dev/null +++ b/src/main/java/net/seanomik/tamablefoxes/sqlite/SQLiteSetterGetter.java @@ -0,0 +1,120 @@ +package net.seanomik.tamablefoxes.sqllite; + +import net.minecraft.server.v1_15_R1.EnumItemSlot; +import net.seanomik.tamablefoxes.EntityTamableFox; +import net.seanomik.tamablefoxes.TamableFoxes; +import org.bukkit.plugin.Plugin; + +import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; + +public class SQLiteSetterGetter { + public static Plugin plugin; + public static SQLiteHandler sqLiteHandler; + + public void createTablesIfNotExist() { + plugin = TamableFoxes.getPlugin(TamableFoxes.class); + sqLiteHandler = TamableFoxes.sqLiteHandler; + //String pluginDatabase = Reference.SQLiteDatabase; + + String foxesTable = + "CREATE TABLE IF NOT EXISTS `foxes` ( " + + "`OWNER_UUID` TEXT PRIMARY KEY , " + + "`NAME` TEXT NOT NULL , " + + "`LOCATION` TEXT NOT NULL , " + + "`MOUTH_ITEM` TEXT NOT NULL);"; + + try { + sqLiteHandler.connect(); + // Create previous bans table + DatabaseMetaData dbm = sqLiteHandler.getConnection().getMetaData(); + ResultSet tables = dbm.getTables(null, null, "foxes", null); + if (!tables.next()) { + PreparedStatement statement = sqLiteHandler.getConnection().prepareStatement(foxesTable); + statement.executeUpdate(); + + plugin.getServer().getConsoleSender().sendMessage(TamableFoxes.getPrefix() + "Created foxes table!"); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + if (sqLiteHandler.getConnection() != null) { + try { + sqLiteHandler.getConnection().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } + + public void saveFox(EntityTamableFox fox) { + plugin = TamableFoxes.getPlugin(TamableFoxes.class); + try { + sqLiteHandler.connect(); + PreparedStatement statement = sqLiteHandler.getConnection() + .prepareStatement("INSERT INTO foxes (OWNER_UUID,NAME,LOCATION,MOUTH_ITEM) VALUES (?,?,?,?)"); + + statement.setString(1, (fox.getOwner().getUniqueID() == null) ? "none" : fox.getOwner().getUniqueID().toString()); + statement.setString(2, fox.getChosenName()); + + statement.setString(3, fox.locX() + "," + fox.locY() + "," + fox.locY()); + statement.setString(4, fox.getEquipment(EnumItemSlot.MAINHAND).toString()); + statement.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + if (sqLiteHandler.getConnection() != null) { + try { + sqLiteHandler.getConnection().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } + + public List spawnFoxes() { + plugin = Dexun.getPlugin(Dexun.class); + try { + sqLiteHandler.connect(); + PreparedStatement statement = sqLiteHandler.getConnection() + .prepareStatement("SELECT * FROM foxes"); + ResultSet results = statement.executeQuery(); + results.next(); + + String banReasonsSTR = results.getString("REASONS"); + String banDatesSTR = results.getString("ON_DATES"); + String banEndDatesSTR = results.getString("END_DATES"); + String unbanReasonsSTR = results.getString("UNBAN_REASONS"); + + List banReasons = new LinkedList(Arrays.asList(banReasonsSTR.substring(1).split(","))); + List banDates = new LinkedList(Arrays.asList(banDatesSTR.substring(1).split(","))); + List banEndDates = new LinkedList(Arrays.asList(banEndDatesSTR.substring(1).split(","))); + List unbanReasons = new LinkedList(Arrays.asList(unbanReasonsSTR.substring(1).split(","))); + + List> bans = new ArrayList>(); + bans.add(banReasons); + bans.add(banDates); + bans.add(banEndDates); + bans.add(unbanReasons); + + return bans; + } catch (SQLException e) { + e.printStackTrace(); + } finally { + if (sqLiteHandler.getConnection() != null) { + try { + sqLiteHandler.getConnection().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } +}