-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBetterShardsAPI.java
160 lines (142 loc) · 6.97 KB
/
BetterShardsAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package vg.civcraft.mc.bettershards;
import java.util.UUID;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import vg.civcraft.mc.bettershards.events.PlayerChangeServerReason;
import vg.civcraft.mc.bettershards.external.MercuryManager;
import vg.civcraft.mc.bettershards.misc.BedLocation;
import vg.civcraft.mc.bettershards.misc.CustomWorldNBTStorage;
import vg.civcraft.mc.bettershards.misc.PlayerStillDeadException;
import vg.civcraft.mc.bettershards.misc.TeleportInfo;
import vg.civcraft.mc.bettershards.portal.Portal;
public class BetterShardsAPI {
/**
* Teleports a player to a different shard.
* @param p The Player that you wish to connect.
* @param serverName The name of the server that you wish the player to be sent to.
* @param reason The reason to be identified by the PlayerChangeServerEvent triggered by this method.
* @throws PlayerStillDeadException If the player is dead than this exception is thrown. There are issues with trying
* to teleport a dead player. If calling from PlayerRespawnEvent just schedule a sync method to occur after the event.
*/
public static boolean connectPlayer(Player p, String serverName, PlayerChangeServerReason reason) throws PlayerStillDeadException {
return BetterShardsPlugin.getConnectionManager().teleportPlayerToServer(p, serverName, reason);
}
/**
* Teleports a player to a different shard.
* @param p The UUID of the Player that you wish to connect.
* @param serverName The name of the server that you wish the player to be sent to.
* @param reason The reason to be identified by the PlayerChangeServerEvent triggered by this method.
* @throws PlayerStillDeadException If the player is dead than this exception is thrown. There are issues with trying
* to teleport a dead player. If calling from PlayerRespawnEvent just schedule a sync method to occur after the event.
*/
public static boolean connectPlayer(UUID p, String serverName, PlayerChangeServerReason reason) throws PlayerStillDeadException {
return BetterShardsPlugin.getConnectionManager().teleportPlayerToServer(p, serverName, reason);
}
/**
* Teleports the specified player to the current server.
* @param uuid The name of said player.
*/
public static void requestPlayerTeleport(String name) {
BetterShardsPlugin.getConnectionManager().teleportOtherServerPlayer(name);
}
/**
* Teleports a player to a different shard.
* @param p The Player that you wish to connect.
* @param portal The portal that the player should be teleported to.
* @param reason The reason to be identified by the PlayerChangeServerEvent triggered by this method.
* @param data Additional data provided for the teleport
* @throws PlayerStillDeadException If the player is dead than this exception is thrown. There are issues with trying
* to teleport a dead player. If calling from PlayerRespawnEvent just schedule a sync method to occur after the event.
*/
public static boolean connectPlayer(Player p, Portal portal, PlayerChangeServerReason reason, Object ... data) throws PlayerStillDeadException {
if (BetterShardsPlugin.getConnectionManager().teleportPlayerToServer(p, portal.getServerName(), reason)) {
MercuryManager.teleportPlayer(p.getUniqueId(), portal, data); // We want to do this after because we don't know if a player was teleported yet.
return true;
}
return false;
}
public static String getServerName() {
return BetterShardsPlugin.getCurrentServerName();
}
/**
* Checks if a player has a bed.
* @param uuid
* @return Returns true if player has one, false otherwise.
*/
public static boolean hasBed(UUID uuid) {
return BetterShardsPlugin.getBedManager().getBed(uuid) != null;
}
public static BedLocation getBedLocation(UUID uuid) {
return BetterShardsPlugin.getBedManager().getBed(uuid);
}
/**
* Adds the BedLocation to both the db and to localcaching as well as sending it
* to other servers. This method will also remove any prexisting beds.
* @param uuid The uuid of the player getting the bed.
* @param bed The BedLocation object.
*/
public static void addBedLocation(UUID uuid, BedLocation bed) {
removeBedLocation(bed);
BetterShardsPlugin.getBedManager().addBedLocation(uuid, bed);
BetterShardsPlugin.getDatabaseManager().addBedLocation(bed);
MercuryManager.sendBedLocation(bed);
}
public static void removeBedLocation(BedLocation bed) {
BetterShardsPlugin.getBedManager().removeBed(bed.getUUID());
BetterShardsPlugin.getDatabaseManager().removeBed(bed.getUUID());
MercuryManager.removeBedLocation(bed);
}
/**
* Sends the info to servers that a player needs to be teleported.
* @param server The server to teleport the player to.
* @param uuid The uuid of the player to teleport.
* @param info Create a TeleportInfo object to pass.
* world can be either the world name or world uuid.
*/
public static void teleportPlayer(String server, UUID uuid, TeleportInfo info) {
MercuryManager.teleportPlayer(server, uuid, info);
}
/**
* RandomSpawns a player on the server
* @param server the server to randomspawn the player on
* @param player the player to randomspawn
*/
public static void randomSpawnPlayer(String server, UUID player) {
try {
connectPlayer(player, server, PlayerChangeServerReason.RANDOMSPAWN);
} catch (PlayerStillDeadException e) {
e.printStackTrace();
}
MercuryManager.notifyRandomSpawn(server, player);
}
/**
* To make cross shard integration of other plugins easier, BetterShards allows those to store data in the form of yaml configuration
* sections together with player data. It will automatically get saved when the player switches to another shard and get loaded on there.
*
* @param plugin Plugin for which data should be saved
* @param uuid UUID of the player for whom data should be saved
*
* @return ConfigurationSection object, which can be edited and read as needed
*/
public static ConfigurationSection getConfigurationSection(JavaPlugin plugin, UUID uuid) {
return CustomWorldNBTStorage.getWorldNBTStorage().getConfigurationSection(uuid, plugin);
}
/**
* Registers a portal with BetterShards.
* This should be called for every custom portal you have.
* @param plugin_id The plugin specific id that your portal uses.
* @param plugin The plugin that is registering this portal.
* @param portal The portal Class.
* @param name The name of the portalType that will show up to players.
*/
public static <E extends Portal> void registerPortal(int plugin_id, String plugin, Class<E> portal, String name) {
BetterShardsPlugin.getDatabaseManager().addPortalType(plugin_id, plugin);
// Now we get the generated id from bettershards.
int real_id = BetterShardsPlugin.getDatabaseManager().getPortalID(plugin, plugin_id);
BetterShardsPlugin.getPortalManager().getPortalFactory().registerPortal(real_id, portal, name);
}
public static int getPortalID(int pluginId, String plugin) {
return BetterShardsPlugin.getDatabaseManager().getPortalID(plugin, pluginId);
}
}