Create a more reliable way to save and load foxes

This commit is contained in:
SeanOMik 2020-01-20 22:37:10 -06:00
parent 1a97e1991f
commit bf5cb4fdea
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
21 changed files with 189 additions and 12 deletions

View File

@ -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() {

View File

@ -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] ";
}

View File

@ -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;
}
}

View File

@ -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<EntityTamableFox> 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<String> banReasons = new LinkedList<String>(Arrays.asList(banReasonsSTR.substring(1).split(",")));
List<String> banDates = new LinkedList<String>(Arrays.asList(banDatesSTR.substring(1).split(",")));
List<String> banEndDates = new LinkedList<String>(Arrays.asList(banEndDatesSTR.substring(1).split(",")));
List<String> unbanReasons = new LinkedList<String>(Arrays.asList(unbanReasonsSTR.substring(1).split(",")));
List<List<String>> bans = new ArrayList<List<String>>();
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();
}
}
}
}
}