diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java index e6400ee..05edc67 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java @@ -48,13 +48,30 @@ public class PsHubCore extends JavaPlugin { extractFile("hibernate.cfg.xml"); - sessionFactory = - new Configuration() - .configure(new File(this.getDataFolder().getAbsolutePath() + "/" + "hibernate.cfg.xml")) - .addAnnotatedClass(Server.class) - .addAnnotatedClass(Node.class) - .addAnnotatedClass(Allocation.class) - .buildSessionFactory(); + var configuration = new org.hibernate.cfg.Configuration() + .addAnnotatedClass(Server.class) + .addAnnotatedClass(Node.class) + .addAnnotatedClass(Allocation.class); + + if (getConfig().getBoolean("mysql.get-from-file")) { + configuration + .configure(new File(this.getDataFolder().getAbsolutePath() + "/" + "hibernate.cfg.xml")); + } else { + configuration + .setProperty("hibernate.connection.url", getConfig().getString("mysql.url")) + .setProperty("hibernate.connection.username", getConfig().getString("mysql.username")) + .setProperty("hibernate.connection.password", getConfig().getString("mysql.password")) + .setProperty("hibernate.connection.driver_class", getConfig().getString("mysql.driver")) + .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect") + .setProperty("hibernate.connection.pool_size", "10") + .setProperty("hibernate.show_sql", String.valueOf(getConfig().getBoolean("mysql.debug"))) + .setProperty("hibernate.format_sql", + String.valueOf(getConfig().getBoolean("mysql.debug"))) + .setProperty("hibernate.current_session_context_class", "thread") + .setProperty("hibernate.hbm2ddl.auto", getConfig().getString("mysql.update-policy")); + } + + sessionFactory = configuration.buildSessionFactory(); // Initialize the controllers serversController = new ServersController(sessionFactory); diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Allocation.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Allocation.java index 1d49b72..970f60a 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Allocation.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Allocation.java @@ -19,8 +19,7 @@ import lombok.Setter; public class Allocation { @Id - @Column(name = "id") - @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", unique = true, nullable = false) private long id; @Column(name = "ip", nullable = false) @@ -33,8 +32,8 @@ public class Allocation { @JoinColumn(name = "node") private Node node; - @ManyToOne(optional = true) - @JoinColumn(name = "server_id", nullable = true) + @ManyToOne() + @JoinColumn(name = "server_id") private Server server; public String getFullAddress() { diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Node.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Node.java index 7b8b9c9..a0355c3 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Node.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Node.java @@ -25,23 +25,22 @@ import lombok.Setter; public class Node { @Id - @Column(name = "id") - @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; + @Column(name = "id", unique = true, nullable = false) + private Long id; - @Column(name = "name") + @Column(name = "name", nullable = false) private String name; - @Column(name = "ip") + @Column(name = "ip", nullable = false) private String ip; - @Column(name = "port") + @Column(name = "port", nullable = false) private int port; @Column(name = "max_online") private Integer maxOnline; - @Column(name = "pterodactyl") + @Column(name = "pterodactyl", nullable = false) private boolean pterodactyl = false; @Column(name = "token") diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Server.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Server.java index c03a365..e92ed26 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Server.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/models/Server.java @@ -53,12 +53,11 @@ public class Server { @JoinColumn(name = "node") private Node node; - @Column(name = "allocations") - @OneToMany(mappedBy = "id", fetch = FetchType.EAGER) + @OneToMany(mappedBy = "server", fetch = FetchType.EAGER) private List allocations; - @JoinColumn(name = "default_allocation", referencedColumnName = "id") - @OneToOne + @JoinColumn(name = "default_allocation") + @OneToOne() private Allocation defaultAllocation; /** diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9cc3830..c362d5f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,9 +1,18 @@ +# Please enter your MySQL information below. These need to be the same as for PlayerServers. mysql: - hostname: 127.0.0.1 - username: web - password: webmaster - database: PlayerServers - useSSL: true + # Url of the MySQL server, in format: jdbc:mysql://:/ + # Any additional options can be added at the end of url, such as: + # ?autoReconnect=true&useSSL=false?useUnicode=true&characterEncoding=UTF-8 + url: "jdbc:mysql://localhost:3306/playerservers?useSSL=false&serverTimezone=UTC" + + username: root + password: root + + # Developer options, do not change unless you know what you are doing. + driver: "com.mysql.cj.jdbc.Driver" + update-policy: "update" + debug: false + get-from-file: false # For how long should we keep Server Data cached? cache-time: 20