Skip to content

Commit 4b45d07

Browse files
committed
Make sure we want to generate the world when we get them
1 parent 0b01bf2 commit 4b45d07

File tree

7 files changed

+30
-22
lines changed

7 files changed

+30
-22
lines changed

API/src/main/java/me/ifydev/dimensify/api/DimensifyConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class DimensifyConstants {
6767
public static final String CANNOT_DELETE_MAIN_WORLD = DIMENSIFY_PREFIX + "&c&lCannot delete the main world!";
6868
public static final String COULD_NOT_CONNECT_TO_DATABASE = DIMENSIFY_PREFIX + "&c&lCannot connect to the database!";
6969
public static final String CANNOT_ENTER_THIS_DIMENSION = DIMENSIFY_PREFIX + "&c&lYou don't have permission to enter this domain!";
70+
public static final String THIS_DIMENSION_DOES_NOT_EXIST_ANYMORE = DIMENSIFY_PREFIX + "&c&lThis dimension does not exist anymore!";
7071

7172
// General success
7273
public static final String CREATING_WORLD = DIMENSIFY_PREFIX + "World '<WORLD>' is being created...";

API/src/main/java/me/ifydev/dimensify/api/backend/impl/SQLHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void initialize(String defaultWorld) {
8686

8787
connection.close();
8888
} catch (SQLException e) {
89+
System.out.println("Encountered an error while trying to setup the database:");
8990
e.printStackTrace();
9091
}
9192

DimensifySpigot/src/main/java/me/ifydev/dimensifyspigot/commands/handlers/BasicHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ public static String createDimension(CommandSender sender, String dimensionType,
9494
public static String goToDimension(Player player, String dimension) {
9595
DimensifyMain plugin = DimensifyMain.get();
9696
// Make sure this dimension actually exists
97-
World world = plugin.getWorldController().getWorld(dimension);
98-
if (world == null)
97+
Optional<World> world = plugin.getWorldController().getWorld(dimension);
98+
if (!world.isPresent())
9999
return DimensifyConstants.INVALID_WORLD.replace("<WORLD>", dimension);
100-
WorldController.enterDimension(player, world);
100+
WorldController.enterDimension(player, world.get());
101101
return "";
102102
}
103103

@@ -121,16 +121,16 @@ public static String sendPlayerToDimension(String playerName, String dimension)
121121
DimensifyMain plugin = DimensifyMain.get();
122122

123123
// Make sure the player and world exist
124-
World world = plugin.getWorldController().getWorld(dimension);
125-
if (world == null)
124+
Optional<World> world = plugin.getWorldController().getWorld(dimension);
125+
if (!world.isPresent())
126126
return DimensifyConstants.INVALID_WORLD.replace("<WORLD>", dimension);
127127
Player player = Bukkit.getPlayerExact(playerName);
128128
if (player == null) return DimensifyConstants.INVALID_PLAYER.replace("<PLAYER>", playerName);
129129

130130
// Send the player to the dimension
131-
player.teleport(world.getSpawnLocation());
131+
player.teleport(world.get().getSpawnLocation());
132132
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ColorUtil.makeReadable(DimensifyConstants.WHOOSH)));
133-
return DimensifyConstants.PLAYER_HAS_BEEN_SENT.replace("<PLAYER>", player.getName());
133+
return DimensifyConstants.PLAYER_HAS_BEEN_SENT.replace("<PLAYER>", player.getName()).replace("<WORLD>", dimension);
134134
}
135135

136136
public static List<String> listWorlds() {

DimensifySpigot/src/main/java/me/ifydev/dimensifyspigot/commands/handlers/PortalHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public static String linkPortal(String portal, String destination) {
6262
if (!db.get().getPortal(portal).isPresent())
6363
return DimensifyConstants.PORTAL_DOES_NOT_EXIST.replace("<PORTAL>", portal);
6464
// And make sure the world exists
65-
World world = plugin.getWorldController().getWorld(destination);
66-
if (world == null)
65+
Optional<World> world = plugin.getWorldController().getWorld(destination);
66+
if (!world.isPresent())
6767
return DimensifyConstants.INVALID_WORLD.replace("<WORLD>", destination);
6868

6969
return db.get().setPortalDestination(portal, destination)

DimensifySpigot/src/main/java/me/ifydev/dimensifyspigot/events/PlayerJoin.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package me.ifydev.dimensifyspigot.events;
22

33
import me.ifydev.dimensifyspigot.DimensifyMain;
4-
import org.bukkit.World;
54
import org.bukkit.entity.Player;
65
import org.bukkit.event.EventHandler;
76
import org.bukkit.event.Listener;
@@ -22,9 +21,7 @@ public void onPlayerJoinEvent(PlayerJoinEvent e) {
2221
if (plugin.isSendPlayersToDefaultWorldOnLogin()) {
2322
plugin.getApi().getDatabaseHandler().ifPresent(db -> {
2423
String dimension = db.getDefaultDimension(false);
25-
26-
World world = plugin.getWorldController().getWorld(dimension);
27-
player.teleport(world.getSpawnLocation());
24+
plugin.getWorldController().getWorld(dimension).ifPresent(dim -> player.teleport(dim.getSpawnLocation()));
2825
});
2926
}
3027
}

DimensifySpigot/src/main/java/me/ifydev/dimensifyspigot/events/PlayerPortal.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package me.ifydev.dimensifyspigot.events;
22

3+
import me.ifydev.dimensify.api.DimensifyConstants;
34
import me.ifydev.dimensifyspigot.DimensifyMain;
5+
import me.ifydev.dimensifyspigot.util.ColorUtil;
46
import me.ifydev.dimensifyspigot.world.WorldController;
57
import org.bukkit.World;
68
import org.bukkit.entity.Player;
79
import org.bukkit.event.EventHandler;
810
import org.bukkit.event.Listener;
911
import org.bukkit.event.player.PlayerPortalEvent;
1012

13+
import java.util.Optional;
14+
1115
/**
1216
* @author Innectic
1317
* @since 06/22/2018
@@ -26,8 +30,12 @@ public void onPlayerAboutToTeleportEvent(PlayerPortalEvent e) {
2630
if (!corners.getDestination().isPresent()) return;
2731

2832
String link = corners.getDestination().get();
29-
World dimension = DimensifyMain.get().getWorldController().getWorld(link);
30-
WorldController.enterDimension(player, dimension);
33+
Optional<World> dimension = DimensifyMain.get().getWorldController().getWorld(link);
34+
if (!dimension.isPresent()) {
35+
player.sendMessage(ColorUtil.makeReadable(DimensifyConstants.THIS_DIMENSION_DOES_NOT_EXIST_ANYMORE));
36+
return;
37+
}
38+
WorldController.enterDimension(player, dimension.get());
3139
});
3240
}
3341
}

DimensifySpigot/src/main/java/me/ifydev/dimensifyspigot/world/WorldController.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,25 @@
1313

1414
import java.io.File;
1515
import java.io.IOException;
16+
import java.util.Optional;
1617

1718
/**
1819
* @author Innectic
1920
* @since 10/1/2017
2021
*/
2122
public class WorldController {
2223

23-
public World getWorld(String name) {
24+
public Optional<World> getWorld(String name) {
25+
return getWorld(name, false);
26+
}
27+
28+
public Optional<World> getWorld(String name, boolean andMake) {
2429
World world = Bukkit.getWorld(name);
25-
if (world == null) {
30+
if (world == null && andMake) {
2631
this.loadWorld(new DimensifyWorld(name, DimensifyMain.get()));
2732
world = Bukkit.getWorld(name);
2833
}
29-
return world;
34+
return Optional.ofNullable(world);
3035
}
3136

3237
public void loadWorld(DimensifyWorld creator) {
@@ -87,8 +92,4 @@ public static void enterDimension(Player player, World dimension) {
8792
player.teleport(dimension.getSpawnLocation());
8893
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ColorUtil.makeReadable(DimensifyConstants.WHOOSH)));
8994
}
90-
91-
public static void enterDimension(Player player, String dimension) {
92-
enterDimension(player, DimensifyMain.get().getWorldController().getWorld(dimension));
93-
}
9495
}

0 commit comments

Comments
 (0)