Skip to content

Commit 82326c7

Browse files
authored
Merge pull request #691 from Multiverse/refractor/cleanup-mvportal
Cleanup MVPortal methods and code style
2 parents 3a5fae8 + 95926ab commit 82326c7

File tree

5 files changed

+93
-59
lines changed

5 files changed

+93
-59
lines changed

src/main/java/org/mvplugins/multiverse/portals/MVPortal.java

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,21 @@
4141

4242
import org.mvplugins.multiverse.portals.utils.MultiverseRegion;
4343

44-
public class MVPortal {
44+
public final class MVPortal {
45+
46+
private static final Collection<Material> INTERIOR_MATERIALS = Arrays.asList(Material.NETHER_PORTAL, Material.GRASS,
47+
Material.VINE, Material.SNOW, Material.AIR, Material.WATER, Material.LAVA);
48+
49+
public static final Pattern PORTAL_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9_-]+");
50+
51+
public static boolean isPortalInterior(Material material) {
52+
return INTERIOR_MATERIALS.contains(material);
53+
}
54+
55+
public static MVPortal loadMVPortalFromConfig(MultiversePortals instance, String name) {
56+
return new MVPortal(instance, name);
57+
}
58+
4559
private final MultiversePortals plugin;
4660
private final PortalsConfig portalsConfig;
4761
private final WorldManager worldManager;
@@ -59,15 +73,6 @@ public class MVPortal {
5973
private Permission fillPermission;
6074
private Permission exempt;
6175

62-
private static final Collection<Material> INTERIOR_MATERIALS = Arrays.asList(Material.NETHER_PORTAL, Material.GRASS,
63-
Material.VINE, Material.SNOW, Material.AIR, Material.WATER, Material.LAVA);
64-
65-
public static final Pattern PORTAL_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9_-]+");
66-
67-
public static boolean isPortalInterior(Material material) {
68-
return INTERIOR_MATERIALS.contains(material);
69-
}
70-
7176
public MVPortal(LoadedMultiverseWorld world, MultiversePortals instance, String name, String owner, String location) {
7277
this(instance, name);
7378
this.setOwner(owner);
@@ -136,9 +141,8 @@ private MVPortal(MultiversePortals plugin, String name) {
136141
}
137142
}
138143

139-
ConfigurationSection save() {
140-
this.configHandle.save();
141-
return this.configHandle.getConfig();
144+
public String getName() {
145+
return this.name;
142146
}
143147

144148
/**
@@ -153,13 +157,41 @@ public StringPropertyHandle getStringPropertyHandle() {
153157
return this.stringPropertyHandle;
154158
}
155159

160+
ConfigurationSection save() {
161+
this.configHandle.save();
162+
return this.configHandle.getConfig();
163+
}
164+
165+
/**
166+
*
167+
* @param owner
168+
* @return
169+
*
170+
* @since 5.1
171+
*/
172+
@ApiStatus.AvailableSince("5.1")
173+
public boolean setOwner(String owner) {
174+
return this.configHandle.set(configNodes.owner, owner).isSuccess();
175+
}
176+
177+
/**
178+
*
179+
* @return
180+
*
181+
* @since 5.1
182+
*/
183+
@ApiStatus.AvailableSince("5.1")
184+
public String getOwner() {
185+
return this.configHandle.get(configNodes.owner);
186+
}
187+
156188
/**
157189
* Set the value of teleportNonPlayers
158190
*
159191
* @param teleportNonPlayers The new value
160192
*/
161193
@ApiStatus.AvailableSince("5.1")
162-
public void setTeleportNonPlayers (boolean teleportNonPlayers) {
194+
public void setTeleportNonPlayers(boolean teleportNonPlayers) {
163195
this.configHandle.set(configNodes.teleportNonPlayers, teleportNonPlayers);
164196
}
165197

@@ -175,32 +207,20 @@ public boolean useSafeTeleporter() {
175207
return this.configHandle.get(configNodes.safeTeleport);
176208
}
177209

178-
public static MVPortal loadMVPortalFromConfig(MultiversePortals instance, String name) {
179-
return new MVPortal(instance, name);
210+
private boolean setCurrency(Material currency) {
211+
return this.configHandle.set(configNodes.currency, currency).isSuccess();
180212
}
181213

182214
public Material getCurrency() {
183215
return this.configHandle.get(configNodes.currency);
184216
}
185217

186-
public double getPrice() {
187-
return this.configHandle.get(configNodes.price);
188-
}
189-
190-
private boolean setCurrency(Material currency) {
191-
return this.configHandle.set(configNodes.currency, currency).isSuccess();
192-
}
193-
194218
private boolean setPrice(double price) {
195219
return this.configHandle.set(configNodes.price, price).isSuccess();
196220
}
197221

198-
public boolean setPortalLocation(String locationString, String worldString) {
199-
LoadedMultiverseWorld world = null;
200-
if (this.worldManager.isWorld(worldString)) {
201-
world = this.worldManager.getLoadedWorld(worldString).getOrNull();
202-
}
203-
return this.setPortalLocation(locationString, world);
222+
public double getPrice() {
223+
return this.configHandle.get(configNodes.price);
204224
}
205225

206226
/**
@@ -215,6 +235,14 @@ public boolean setPortalLocation(String locationString) {
215235
return this.setPortalLocation(PortalLocation.parseLocation(locationString));
216236
}
217237

238+
public boolean setPortalLocation(String locationString, String worldString) {
239+
LoadedMultiverseWorld world = null;
240+
if (this.worldManager.isWorld(worldString)) {
241+
world = this.worldManager.getLoadedWorld(worldString).getOrNull();
242+
}
243+
return this.setPortalLocation(locationString, world);
244+
}
245+
218246
public boolean setPortalLocation(String locationString, LoadedMultiverseWorld world) {
219247
return this.setPortalLocation(PortalLocation.parseLocation(locationString, world, this.name));
220248
}
@@ -235,8 +263,11 @@ boolean setPortalLocationInternal(PortalLocation location) {
235263
return true;
236264
}
237265

238-
private boolean setOwner(String owner) {
239-
return this.configHandle.set(configNodes.owner, owner).isSuccess();
266+
public PortalLocation getPortalLocation() {
267+
if (this.location == null) {
268+
this.location = PortalLocation.parseLocation(this.configHandle.get(configNodes.location));
269+
}
270+
return this.location;
240271
}
241272

242273
public boolean setDestination(String destinationString) {
@@ -253,18 +284,10 @@ public boolean setDestination(DestinationInstance<?, ?> newDestination) {
253284
}
254285

255286
public DestinationInstance<?, ?> getDestination() {
256-
return this.destinationsProvider.parseDestination(this.configHandle.get(configNodes.destination)).getOrNull();
257-
}
258-
259-
public String getName() {
260-
return this.name;
261-
}
262-
263-
public PortalLocation getLocation() {
264-
if (this.location == null) {
265-
this.location = PortalLocation.parseLocation(this.configHandle.get(configNodes.location));
266-
}
267-
return this.location;
287+
return this.destinationsProvider.parseDestination(this.configHandle.get(configNodes.destination))
288+
.onFailure(f ->
289+
Logging.warning("Portal " + this.name + " has an invalid DESTINATION! " + f.getFailureMessage().formatted()))
290+
.getOrNull();
268291
}
269292

270293
public Location getSafePlayerSpawnLocation() {
@@ -386,7 +409,7 @@ public boolean isFrameValid(Location l) {
386409
// Limit the search to the portal's region, extended by 1 block.
387410
boolean frameValid = false;
388411
{
389-
MultiverseRegion r = getLocation().getRegion();
412+
MultiverseRegion r = getPortalLocation().getRegion();
390413
int useX = (r.getWidth() == 1) ? 0 : 1;
391414
int useY = (r.getHeight() == 1) ? 0 : 1;
392415
int useZ = (r.getDepth() == 1) ? 0 : 1;
@@ -519,7 +542,8 @@ public boolean setProperty(String property, String value) {
519542
* @deprecated Busscript feature has been removed.
520543
*/
521544
@Deprecated(since = "5.1" , forRemoval = true)
522-
@ApiStatus.ScheduledForRemoval(inVersion = "6.0") public String getHandlerScript() {
545+
@ApiStatus.ScheduledForRemoval(inVersion = "6.0")
546+
public String getHandlerScript() {
523547
Logging.warning("handle script is deprecated");
524548
return "";
525549
}
@@ -528,7 +552,8 @@ public boolean setProperty(String property, String value) {
528552
* @deprecated Busscript feature has been removed.
529553
*/
530554
@Deprecated(since = "5.1" , forRemoval = true)
531-
@ApiStatus.ScheduledForRemoval(inVersion = "6.0") public void setHandlerScript(String handlerScript) {
555+
@ApiStatus.ScheduledForRemoval(inVersion = "6.0")
556+
public void setHandlerScript(String handlerScript) {
532557
Logging.warning("handle script is deprecated");
533558
}
534559

@@ -540,4 +565,13 @@ public boolean setProperty(String property, String value) {
540565
public boolean isExempt(Player player) {
541566
return false;
542567
}
568+
569+
/**
570+
* @deprecated Use {@link MVPortal#getPortalLocation()} instead.
571+
*/
572+
@Deprecated(forRemoval = true)
573+
@ApiStatus.ScheduledForRemoval(inVersion = "6.0")
574+
public PortalLocation getLocation() {
575+
return getPortalLocation();
576+
}
543577
}

src/main/java/org/mvplugins/multiverse/portals/MultiversePortals.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private void loadPortals() {
183183
if (keys != null) {
184184
for (String pname : keys) {
185185
MVPortal portal = MVPortal.loadMVPortalFromConfig(this, pname);
186-
if (portal.getLocation().isValidLocation()) {
186+
if (portal.getPortalLocation().isValidLocation()) {
187187
this.portalManager.get().addPortal(portal);
188188
} else {
189189
Logging.warning(String.format("Portal '%s' not loaded due to invalid location!", portal.getName()));

src/main/java/org/mvplugins/multiverse/portals/listeners/MVPPlayerListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void playerBucketFill(PlayerBucketFillEvent event) {
111111
} else {
112112
Material fillMaterial = Material.AIR;
113113
Logging.finer("Fill Material: " + fillMaterial);
114-
this.filler.fillRegion(portal.getLocation().getRegion(), event.getBlockClicked().getLocation(), fillMaterial, event.getPlayer());
114+
this.filler.fillRegion(portal.getPortalLocation().getRegion(), event.getBlockClicked().getLocation(), fillMaterial, event.getPlayer());
115115
}
116116
}
117117
}
@@ -150,7 +150,7 @@ public void playerBucketEmpty(PlayerBucketEmptyEvent event) {
150150
}
151151

152152
Logging.finer("Fill Material: " + fillMaterial);
153-
this.filler.fillRegion(portal.getLocation().getRegion(), translatedLocation, fillMaterial, event.getPlayer());
153+
this.filler.fillRegion(portal.getPortalLocation().getRegion(), translatedLocation, fillMaterial, event.getPlayer());
154154
}
155155
}
156156
}
@@ -203,7 +203,7 @@ public void playerInteract(PlayerInteractEvent event) {
203203
fillMaterial = Material.AIR;
204204
}
205205
Logging.finer("Fill Material: " + fillMaterial);
206-
event.setCancelled(this.filler.fillRegion(portal.getLocation().getRegion(), translatedLocation, fillMaterial, event.getPlayer()));
206+
event.setCancelled(this.filler.fillRegion(portal.getPortalLocation().getRegion(), translatedLocation, fillMaterial, event.getPlayer()));
207207
}
208208
}
209209
return;

src/main/java/org/mvplugins/multiverse/portals/utils/DisplayUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DisplayUtils {
2626

2727
public void showStaticInfo(CommandSender sender, MVPortal portal, String message) {
2828
sender.sendMessage(ChatColor.AQUA + "--- " + message + ChatColor.DARK_AQUA + portal.getName() + ChatColor.AQUA + " ---");
29-
String[] locParts = portal.getLocation().toString().split(":");
29+
String[] locParts = portal.getPortalLocation().toString().split(":");
3030
sender.sendMessage("Coords: " + ChatColor.GOLD + locParts[0] + ChatColor.WHITE + " to " + ChatColor.GOLD + locParts[1] + ChatColor.WHITE + " in " + ChatColor.GOLD + portal.getWorld().getName() );
3131
if (portal.getDestination() == null) {
3232
sender.sendMessage("Destination: " + ChatColor.RED + ChatColor.ITALIC + "NOT SET!");

src/main/java/org/mvplugins/multiverse/portals/utils/PortalManager.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public MVPortal getPortal(Player sender, Location l, boolean checkPermission) {
9696

9797
// Ignore portals the player can't use.
9898
if (!checkPermission || !portalsConfig.getEnforcePortalAccess() || portal.playerCanEnterPortal(sender)) {
99-
PortalLocation portalLoc = portal.getLocation();
99+
PortalLocation portalLoc = portal.getPortalLocation();
100100
if (portalLoc.isValidLocation() && portalLoc.getRegion().containsVector(l)) {
101101
return portal;
102102
}
@@ -134,7 +134,7 @@ public boolean isPortal(Location l) {
134134
public MVPortal getPortal(Location l) {
135135
MultiverseWorld world = this.worldManager.getLoadedWorld(l.getWorld().getName()).getOrNull();
136136
for (MVPortal portal : getNearbyPortals(world, l)) {
137-
MultiverseRegion r = portal.getLocation().getRegion();
137+
MultiverseRegion r = portal.getPortalLocation().getRegion();
138138
if (r != null && r.containsVector(l)) {
139139
return portal;
140140
}
@@ -207,7 +207,7 @@ private MVPortal removePortal(String portalName, boolean removeFromConfigs, bool
207207
// leaving behind portal blocks (which would take an unsuspecting
208208
// player to the nether instead of their expected destination).
209209

210-
MultiverseRegion region = removed.getLocation().getRegion();
210+
MultiverseRegion region = removed.getPortalLocation().getRegion();
211211
replaceInRegion(removed.getWorld(), region, Material.NETHER_PORTAL, Material.AIR);
212212
}
213213
this.plugin.getServer().getPluginManager().removePermission(removed.getPermission());
@@ -250,7 +250,7 @@ private List<MVPortal> getPortals(MultiverseWorld world) {
250250
List<MVPortal> all = this.getAllPortals();
251251
List<MVPortal> validItems = new ArrayList<MVPortal>();
252252
for (MVPortal p : all) {
253-
MultiverseWorld portalworld = p.getLocation().getMVWorld();
253+
MultiverseWorld portalworld = p.getPortalLocation().getMVWorld();
254254
if (portalworld != null && portalworld.equals(world)) {
255255
validItems.add(p);
256256
}
@@ -266,7 +266,7 @@ public List<MVPortal> getPortals(CommandSender sender, MultiverseWorld world) {
266266
List<MVPortal> validItems = new ArrayList<MVPortal>();
267267
if (portalsConfig.getEnforcePortalAccess()) {
268268
for (MVPortal p : all) {
269-
if (p.getLocation().isValidLocation() && p.getLocation().getMVWorld().equals(world) &&
269+
if (p.getPortalLocation().isValidLocation() && p.getPortalLocation().getMVWorld().equals(world) &&
270270
p.playerCanEnterPortal((Player) sender)) {
271271
validItems.add(p);
272272
}
@@ -350,7 +350,7 @@ private void addToWorldChunkPortals(MultiverseWorld world, MVPortal portal) {
350350

351351
// If this portal spans multiple chunks, we'll add it to each chunk that
352352
// contains part of it.
353-
PortalLocation location = portal.getLocation();
353+
PortalLocation location = portal.getPortalLocation();
354354
Vector min = location.getMinimum();
355355
Vector max = location.getMaximum();
356356
int c1x = blockToChunk(min.getBlockX()), c1z = blockToChunk(min.getBlockZ());
@@ -381,7 +381,7 @@ private void removeFromWorldChunkPortals(MultiverseWorld world, MVPortal portal)
381381
return;
382382
}
383383

384-
PortalLocation location = portal.getLocation();
384+
PortalLocation location = portal.getPortalLocation();
385385
Vector min = location.getMinimum();
386386
Vector max = location.getMaximum();
387387
int c1x = blockToChunk(min.getBlockX()), c1z = blockToChunk(min.getBlockZ());

0 commit comments

Comments
 (0)