11package me .ifydev .dimensify .api .backend .impl ;
22
3+ import me .ifydev .dimensify .api .DimensifyAPI ;
34import me .ifydev .dimensify .api .backend .AbstractDataHandler ;
5+ import me .ifydev .dimensify .api .backend .ConnectionError ;
46import me .ifydev .dimensify .api .backend .ConnectionInformation ;
57import me .ifydev .dimensify .api .dimensions .Dimension ;
68import me .ifydev .dimensify .api .portal .PortalMeta ;
@@ -44,8 +46,7 @@ private Optional<Connection> getConnection() {
4446 String connectionURL = baseConnectionUrl + "/" + connectionInformation .getDatabase ();
4547 return Optional .ofNullable (DriverManager .getConnection (connectionURL , connectionInformation .getUsername (), connectionInformation .getPassword ()));
4648 } catch (SQLException e ) {
47- // TODO
48- // PermissifyAPI.get().ifPresent(api -> api.getDisplayUtil().displayError(ConnectionError.REJECTED, Optional.of(e)));
49+ DimensifyAPI .get ().ifPresent (api -> api .getDisplayUtil ().displayError (ConnectionError .REJECTED , Optional .of (e )));
4950 }
5051 return Optional .empty ();
5152 }
@@ -55,30 +56,35 @@ public void initialize(String defaultWorld) {
5556 this .dimensions = new ArrayList <>();
5657
5758 try {
58- Optional <Connection > connection = getConnection ();
59- if (!connection .isPresent ()) return ;
59+ Connection connection ;
60+ if (isUsingSQLite ) connection = DriverManager .getConnection (baseConnectionUrl );
61+ else connection = DriverManager .getConnection (baseConnectionUrl , connectionInformation .getUsername (), connectionInformation .getPassword ());
62+ if (connection == null ) {
63+ DimensifyAPI .get ().ifPresent (api -> api .getDisplayUtil ().displayError (ConnectionError .REJECTED , Optional .empty ()));
64+ return ;
65+ }
6066
6167 String database = connectionInformation .getDatabase ();
6268
6369 if (!isUsingSQLite ) {
64- PreparedStatement statement = connection .get (). prepareStatement ("CREATE DATABASE IF NOT EXISTS " + database );
70+ PreparedStatement statement = connection .prepareStatement ("CREATE DATABASE IF NOT EXISTS " + database );
6571 statement .execute ();
6672 statement .close ();
6773 database += "." ;
6874 } else database = "" ;
6975
70- PreparedStatement dimensionsStatement = connection .get (). prepareStatement ("CREATE TABLE IF NOT EXISTS " + database +
76+ PreparedStatement dimensionsStatement = connection .prepareStatement ("CREATE TABLE IF NOT EXISTS " + database +
7177 "dimensions (`name` VARCHAR(50) NOT NULL, `type` VARCHAR(20) NOT NULL, meta VARCHAR(767), `default` TINYINT NOT NULL)" );
7278 dimensionsStatement .execute ();
7379 dimensionsStatement .close ();
7480
75- PreparedStatement portalsStatement = connection .get (). prepareStatement ("CREATE TABLE IF NOT EXISTS " + database +
81+ PreparedStatement portalsStatement = connection .prepareStatement ("CREATE TABLE IF NOT EXISTS " + database +
7682 "portals (`world` VARCHAR(50) NOT NULL, x1 INTEGER NOT NULL, x2 INTEGER NOT NULL, y1 INTEGER NOT NULL, y2 INTEGER NOT NULL" +
7783 ", z1 INTEGER NOT NULL, z2 INTEGER NOT NULL, destination VARCHAR(60), `type` VARCHAR(60) NOT NULL, `name` VARCHAR(60) NOT NULL)" );
7884 portalsStatement .execute ();
7985 portalsStatement .close ();
8086
81- connection .get (). close ();
87+ connection .close ();
8288 } catch (SQLException e ) {
8389 e .printStackTrace ();
8490 }
@@ -106,7 +112,7 @@ public void reload() {
106112 this .drop ();
107113
108114 this .portals = this .getPortals ();
109- this .dimensions = this .getDimensions ();
115+ this .dimensions = this .getDimensions (true );
110116 }
111117
112118 @ Override
@@ -118,7 +124,11 @@ public void drop() {
118124 @ Override
119125 public boolean createPortal (PortalMeta meta ) {
120126 Optional <Connection > connection = getConnection ();
121- if (!connection .isPresent ()) return false ;
127+ if (!connection .isPresent ()) {
128+ DimensifyAPI .get ().ifPresent (api -> api .getDisplayUtil ().displayError (ConnectionError .REJECTED , Optional .empty ()));
129+ return false ;
130+ }
131+ this .portals .add (meta );
122132
123133 try {
124134 PreparedStatement statement = connection .get ().prepareStatement ("INSERT INTO portals " +
@@ -150,7 +160,10 @@ public boolean createPortal(PortalMeta meta) {
150160 @ Override
151161 public boolean removePortal (String name ) {
152162 Optional <Connection > connection = getConnection ();
153- if (!connection .isPresent ()) return false ;
163+ if (!connection .isPresent ()) {
164+ DimensifyAPI .get ().ifPresent (api -> api .getDisplayUtil ().displayError (ConnectionError .REJECTED , Optional .empty ()));
165+ return false ;
166+ }
154167
155168 try {
156169 PreparedStatement statement = connection .get ().prepareStatement ("DELETE FROM portals WHERE `name`=?" );
@@ -193,7 +206,10 @@ public List<PortalMeta> getPortals() {
193206 @ Override
194207 public boolean setPortalDestination (String portal , String destination ) {
195208 Optional <Connection > connection = getConnection ();
196- if (!connection .isPresent ()) return false ;
209+ if (!connection .isPresent ()) {
210+ DimensifyAPI .get ().ifPresent (api -> api .getDisplayUtil ().displayError (ConnectionError .REJECTED , Optional .empty ()));
211+ return false ;
212+ }
197213
198214 this .destinations .put (portal , destination );
199215
@@ -213,7 +229,9 @@ public boolean setPortalDestination(String portal, String destination) {
213229 }
214230
215231 @ Override
216- public List <Dimension > getDimensions () {
232+ public List <Dimension > getDimensions (boolean skipCache ) {
233+ if (!skipCache ) return this .dimensions ;
234+
217235 Optional <Connection > connection = getConnection ();
218236 if (!connection .isPresent ()) return Collections .emptyList ();
219237
@@ -255,7 +273,10 @@ public boolean setDefaultDimension(String name) {
255273 this .defaultWorld = dimension .get ().getName ();
256274
257275 Optional <Connection > connection = getConnection ();
258- if (!connection .isPresent ()) return false ;
276+ if (!connection .isPresent ()) {
277+ DimensifyAPI .get ().ifPresent (api -> api .getDisplayUtil ().displayError (ConnectionError .REJECTED , Optional .empty ()));
278+ return false ;
279+ }
259280 try {
260281 PreparedStatement removeCurrent = connection .get ().prepareStatement ("UPDATE dimensions SET `default`=0 WHERE `default`=1" );
261282 removeCurrent .execute ();
@@ -275,14 +296,17 @@ public boolean setDefaultDimension(String name) {
275296 @ Override
276297 public String getDefaultDimension (boolean skipCache ) {
277298 if (skipCache )
278- for (Dimension dimension : this .getDimensions ()) if (dimension .isDefault ()) return dimension .getName ();
299+ for (Dimension dimension : this .getDimensions (false )) if (dimension .isDefault ()) return dimension .getName ();
279300 return defaultWorld ;
280301 }
281302
282303 @ Override
283304 public boolean createDimension (Dimension dimension ) {
284305 Optional <Connection > connection = getConnection ();
285- if (!connection .isPresent ()) return false ;
306+ if (!connection .isPresent ()) {
307+ DimensifyAPI .get ().ifPresent (api -> api .getDisplayUtil ().displayError (ConnectionError .REJECTED , Optional .empty ()));
308+ return false ;
309+ }
286310
287311 if (dimensions .stream ().anyMatch (d -> d .getName ().equalsIgnoreCase (dimension .getName ()))) return false ;
288312 this .dimensions .add (dimension );
@@ -309,7 +333,10 @@ public boolean createDimension(Dimension dimension) {
309333 @ Override
310334 public boolean removeDimension (String name ) {
311335 Optional <Connection > connection = getConnection ();
312- if (!connection .isPresent ()) return false ;
336+ if (!connection .isPresent ()) {
337+ DimensifyAPI .get ().ifPresent (api -> api .getDisplayUtil ().displayError (ConnectionError .REJECTED , Optional .empty ()));
338+ return false ;
339+ }
313340
314341 Optional <Dimension > dimension = this .dimensions .stream ().filter (d -> d .getName ().equalsIgnoreCase (name )).findFirst ();
315342 if (!dimension .isPresent ()) return false ;
0 commit comments