/
PortalsLogic.java
154 lines (121 loc) · 6.35 KB
/
PortalsLogic.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
package com.bgsoftware.superiorskyblock.utils.logic;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.schematic.Schematic;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.lang.Message;
import com.bgsoftware.superiorskyblock.lang.PlayerLocales;
import com.bgsoftware.superiorskyblock.player.SuperiorNPCPlayer;
import com.bgsoftware.superiorskyblock.utils.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.math.BigDecimal;
public final class PortalsLogic {
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();
private PortalsLogic() {
}
public static void handlePlayerPortal(Player player, Location portalLocation,
PlayerTeleportEvent.TeleportCause teleportCause,
Cancellable involvedEvent) {
SuperiorPlayer superiorPlayer = plugin.getPlayers().getSuperiorPlayer(player);
if (superiorPlayer instanceof SuperiorNPCPlayer)
return;
Island island = plugin.getGrid().getIslandAt(portalLocation);
if (island == null || !plugin.getGrid().isIslandsWorld(portalLocation.getWorld()))
return;
if (involvedEvent != null)
involvedEvent.setCancelled(true);
if (superiorPlayer.isImmunedToPortals())
return;
World.Environment destinationEnvironment = getTargetWorld(portalLocation, teleportCause);
if (!isIslandWorldEnabled(destinationEnvironment, island)) {
if (!Message.WORLD_NOT_UNLOCKED.isEmpty(superiorPlayer.getUserLocale()))
PlayerLocales.sendSchematicMessage(superiorPlayer, Message.WORLD_NOT_UNLOCKED.getMessage(
superiorPlayer.getUserLocale(), StringUtils.format(destinationEnvironment.name())));
return;
}
try {
// If schematic was already generated, simply teleport player to destination location.
if (island.wasSchematicGenerated(destinationEnvironment)) {
superiorPlayer.teleport(island, destinationEnvironment);
return;
}
String destinationEnvironmentName = destinationEnvironment.name().toLowerCase();
String islandSchematic = island.getSchematicName();
Schematic schematic = plugin.getSchematics().getSchematic(islandSchematic.isEmpty() ?
plugin.getSchematics().getDefaultSchematic(destinationEnvironment) :
islandSchematic + "_" + destinationEnvironmentName);
if (schematic == null) {
PlayerLocales.sendSchematicMessage(superiorPlayer, ChatColor.RED + "The server hasn't added a " +
destinationEnvironmentName + " schematic. Please contact administrator to solve the problem. " +
"The format for " + destinationEnvironmentName + " schematic is \"" +
islandSchematic + "_" + destinationEnvironmentName + "\".");
return;
}
island.setSchematicGenerate(destinationEnvironment);
Location schematicPlacementLocation = island.getCenter(destinationEnvironment).subtract(0, 1, 0);
BigDecimal originalWorth = island.getRawWorth();
BigDecimal originalLevel = island.getRawLevel();
schematic.pasteSchematic(island, schematicPlacementLocation, () -> {
if (shouldOffsetSchematic(destinationEnvironment)) {
BigDecimal schematicWorth = island.getRawWorth().subtract(originalWorth),
schematicLevel = island.getRawLevel().subtract(originalLevel);
island.setBonusWorth(island.getBonusWorth().subtract(schematicWorth));
island.setBonusLevel(island.getBonusLevel().subtract(schematicLevel));
}
Location destinationLocation = island.getIslandHome(destinationEnvironment);
if (destinationEnvironment == World.Environment.THE_END) {
plugin.getNMSDragonFight().awardTheEndAchievement(player);
if (plugin.getSettings().getWorlds().getEnd().isDragonFight())
plugin.getNMSDragonFight().startDragonBattle(island, destinationLocation);
}
superiorPlayer.teleport(schematic.adjustRotation(destinationLocation));
});
} catch (NullPointerException ignored) {
}
}
public static boolean shouldOffsetSchematic(World.Environment environment) {
switch (environment) {
case NORMAL:
return plugin.getSettings().getWorlds().getNormal().isSchematicOffset();
case NETHER:
return plugin.getSettings().getWorlds().getNether().isSchematicOffset();
case THE_END:
return plugin.getSettings().getWorlds().getEnd().isSchematicOffset();
default:
return false;
}
}
public static World.Environment getTargetWorld(Location portalLocation, PlayerTeleportEvent.TeleportCause teleportCause) {
World.Environment portalEnvironment = portalLocation.getWorld().getEnvironment();
World.Environment environment;
switch (teleportCause) {
case END_PORTAL:
environment = World.Environment.THE_END;
break;
case NETHER_PORTAL:
environment = World.Environment.NETHER;
break;
default:
environment = World.Environment.NORMAL;
break;
}
return environment == portalEnvironment ? World.Environment.NORMAL : environment;
}
public static boolean isIslandWorldEnabled(World.Environment environment, Island island) {
switch (environment) {
case NORMAL:
return island.isNormalEnabled();
case NETHER:
return island.isNetherEnabled();
case THE_END:
return island.isEndEnabled();
default:
return true;
}
}
}