Skip to content

Commit

Permalink
feat: add multi destination randomisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Galster-dev authored and sekwah41 committed Apr 25, 2023
1 parent 10c009b commit 496396f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ private boolean createPortalRequest(Player player, String[] args) {
boolean isBungeePortal = false;
boolean needsPermission = false;
boolean executesCommand = false;
String destination = null;
String[] destinations = new String[] { };
String portalName = null;
String triggerBlock = null;
String serverName = null;
Expand All @@ -573,12 +573,15 @@ private boolean createPortalRequest(Player player, String[] args) {
}
hasName = true;
portalName = args[i].replaceFirst("name:", "");
} else if (startsWithPortalArg("destinations:", args[i])) {
hasDestination = true;
destinations = args[i].toLowerCase().replaceFirst("destinations:", "").split(",");
} else if (startsWithPortalArg("destination:", args[i])) {
hasDestination = true;
destination = args[i].toLowerCase().replaceFirst("destination:", "");
destinations = args[i].toLowerCase().replaceFirst("destination:", "").split(",");
} else if (startsWithPortalArg("desti:", args[i])) {
hasDestination = true;
destination = args[i].toLowerCase().replaceFirst("desti:", "");
destinations = args[i].toLowerCase().replaceFirst("desti:", "").split(",");
} else if (startsWithPortalArg("triggerblock:", args[i])) {
hasTriggerBlock = true;
triggerBlock = args[i].toLowerCase().replaceFirst("triggerblock:", "");
Expand Down Expand Up @@ -679,23 +682,23 @@ private boolean createPortalRequest(Player player, String[] args) {
player.getMetadata("Pos2Z").get(0).asInt());
}

ConfigAccessor desticonfig = new ConfigAccessor(plugin, "destinations.yml");
String destiPosX = desticonfig.getConfig().getString(destination + ".pos.X");

if (!Portal.portalExists(portalName)) {

player.sendMessage("");
player.sendMessage(PluginMessages.customPrefix
+ "\u00A7e You have created a new portal with the following details:");
player.sendMessage("\u00A7aname: \u00A7e" + portalName);
if (hasDestination) {
if (!isBungeePortal && destiPosX == null) {
player.sendMessage("\u00A7cdestination: \u00A7e" + destination
+ " (destination does not exist)");
return true;
} else {
player.sendMessage("\u00A7adestination: \u00A7e" + destination);
ConfigAccessor desticonfig = new ConfigAccessor(plugin, "destinations.yml");
for (String destination : destinations) {
String destiPosX = desticonfig.getConfig().getString(destination + ".pos.X");
if (!isBungeePortal && destiPosX == null) {
player.sendMessage("\u00A7cdestination: \u00A7e" + destination
+ " (destination does not exist)");
return true;
}
}
player.sendMessage("\u00A7adestinations: \u00A7e" + String.join(", ", destinations));

} else {
player.sendMessage(
Expand Down Expand Up @@ -731,7 +734,7 @@ private boolean createPortalRequest(Player player, String[] args) {
"\u00A7atriggerBlock: \u00A7e" + triggerBlock.toUpperCase());
PortalArg[] portalArgs = new PortalArg[extraData.size()];
portalArgs = extraData.toArray(portalArgs);
player.sendMessage(Portal.create(pos1, pos2, portalName, destination,
player.sendMessage(Portal.create(pos1, pos2, portalName, destinations,
materialSet, serverName, portalArgs));
if(materialSet.contains(Material.END_GATEWAY)) {
AdvancedPortal portal = Portal.getPortal(portalName);
Expand All @@ -748,7 +751,7 @@ private boolean createPortalRequest(Player player, String[] args) {
+ " no valid blocks were listed so the default has been set.");
PortalArg[] portalArgs = new PortalArg[extraData.size()];
portalArgs = extraData.toArray(portalArgs);
player.sendMessage(Portal.create(pos1, pos2, portalName, destination,
player.sendMessage(Portal.create(pos1, pos2, portalName, destinations,
serverName, portalArgs));
}
} else {
Expand All @@ -757,7 +760,7 @@ private boolean createPortalRequest(Player player, String[] args) {
+ Config.getConfig().getString("DefaultPortalTriggerBlock") + ")");
PortalArg[] portalArgs = new PortalArg[extraData.size()];
portalArgs = extraData.toArray(portalArgs);
player.sendMessage(Portal.create(pos1, pos2, portalName, destination,
player.sendMessage(Portal.create(pos1, pos2, portalName, destinations,
serverName, portalArgs));
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class AdvancedPortal {
private String portalName = null;

// TODO store destinations also as variables like portals
private String destiation = null; // Could possibly store the destination name to stop the server having to read the config file
private String[] destinations = new String[] {}; // Could possibly store the destination name to stop the server having to read the config file

private String bungee = null; // Could possibly store the bungee server name to stop the server having to read the config file

Expand All @@ -32,19 +32,27 @@ public class AdvancedPortal {

public HashSet<UUID> inPortal = new HashSet<UUID>();

// TODO think of relaying out the data input to a more logical format.
public AdvancedPortal(String portalName, Material trigger, String destination, Location pos1, Location pos2, PortalArg... portalArgs) {
this(portalName, trigger, new String[] { destination }, pos1, pos2, portalArgs);
}

// TODO think of relaying out the data input to a more logical format.
public AdvancedPortal(String portalName, Material trigger, String[] destinations, Location pos1, Location pos2, PortalArg... portalArgs) {
this(portalName, trigger, pos1, pos2, pos2.getWorld().getName(), portalArgs);
this.destiation = destination;
this.destinations = destinations;
}

public AdvancedPortal(String portalName, Material trigger, Location pos1, Location pos2, PortalArg... portalArgs) {
this(portalName, trigger, pos1, pos2, pos2.getWorld().getName(), portalArgs);
}

public AdvancedPortal(String portalName, Material trigger, String destination, Location pos1, Location pos2, String worldName, PortalArg... portalArgs) {
this(portalName, trigger, new String[] { destination }, pos1, pos2, worldName, portalArgs);
}

public AdvancedPortal(String portalName, Material trigger, String[] destinations, Location pos1, Location pos2, String worldName, PortalArg... portalArgs) {
this(portalName, trigger, pos1, pos2, worldName, portalArgs);
this.destiation = destination;
this.destinations = destinations;
}

public AdvancedPortal(String portalName, Material trigger, Location pos1, Location pos2, String worldName, PortalArg... portalArgs) {
Expand Down Expand Up @@ -97,12 +105,32 @@ public String getName() {
return this.portalName;
}

@Deprecated
public String getDestiation() {
return this.destiation;
if(this.destinations.length == 0) {
// backwards compatibility
return null;
}
return String.join(",", this.destinations);
}

public String[] getDestinations() {
return this.destinations;
}

@Deprecated
public void setDestiation(String destiation) {
this.destiation = destiation;
if(destiation == null) {
// backwards compatibility
this.destinations = new String[] {};
return;
}

this.destinations = destiation.split(",");
}

public void setDestinations(String[] destinations) {
this.destinations = destinations;
}

public String getBungee() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,21 @@ public static Particle getParticle(String name) {

public static String create(Location pos1, Location pos2, String name, String destination,
Set<Material> triggerBlocks, PortalArg... extraData) {
return create(pos1, pos2, name, destination, triggerBlocks, null, extraData);
return create(pos1, pos2, name, new String[] { destination }, triggerBlocks, extraData);
}

public static String create(Location pos1, Location pos2, String name, String[] destinations,
Set<Material> triggerBlocks, PortalArg... extraData) {
return create(pos1, pos2, name, destinations, triggerBlocks, null, extraData);
}

public static String create(Location pos1, Location pos2, String name, String destination,
Set<Material> triggerBlocks, String serverName, PortalArg... portalArgs) {
return create(pos1, pos2, name, new String[] { destination }, triggerBlocks, serverName, portalArgs);
}

public static String create(Location pos1, Location pos2, String name, String[] destinations,
Set<Material> triggerBlocks, String serverName, PortalArg... portalArgs) {

if (!pos1.getWorld().equals(pos2.getWorld())) {
plugin.getLogger().log(Level.WARNING, "pos1 and pos2 must be in the same world!");
Expand Down Expand Up @@ -227,7 +237,8 @@ public static String create(Location pos1, Location pos2, String name, String de
String store = triggerBlocks.stream().map(Enum::name).collect(Collectors.joining(","));
portalData.getConfig().set(name + ".triggerblock", store);

portalData.getConfig().set(name + ".destination", destination);
// not renaming config entry for backwards compatibility
portalData.getConfig().set(name + ".destination", String.join(",", destinations));

portalData.getConfig().set(name + ".bungee", serverName);

Expand Down Expand Up @@ -314,6 +325,11 @@ private static boolean checkOverLapPortal(Location pos1, Location pos2, int posX
}

public static String create(Location pos1, Location pos2, String name, String destination, String serverName,
PortalArg... extraData) {
return create(pos1, pos2, name, new String[] { destination }, serverName, extraData);
}

public static String create(Location pos1, Location pos2, String name, String[] destinations, String serverName,
PortalArg... extraData) { // add stuff for destination names or coordinates
ConfigAccessor config = new ConfigAccessor(plugin, "config.yml");

Expand All @@ -325,7 +341,7 @@ public static String create(Location pos1, Location pos2, String name, String de
triggerBlockType = Material.NETHER_PORTAL;
}

return create(pos1, pos2, name, destination, new HashSet<>(Collections.singletonList(triggerBlockType)),
return create(pos1, pos2, name, destinations, new HashSet<>(Collections.singletonList(triggerBlockType)),
serverName, extraData);
}

Expand Down Expand Up @@ -513,12 +529,13 @@ public static boolean activate(Player player, AdvancedPortal portal, boolean doK
}, 20 * 10);
}

if (portal.getDestiation() != null) {
if (portal.getDestinations().length != 0) {
if(plugin.isProxyPluginEnabled()) {
String randomDest = portal.getDestinations()[random.nextInt(portal.getDestinations().length)];
ByteArrayDataOutput outForList = ByteStreams.newDataOutput();
outForList.writeUTF(BungeeMessages.ENTER_PORTAL);
outForList.writeUTF(bungeeServer);
outForList.writeUTF(portal.getDestiation());
outForList.writeUTF(randomDest);
outForList.writeUTF(player.getUniqueId().toString());

player.sendPluginMessage(plugin, BungeeMessages.CHANNEL_NAME, outForList.toByteArray());
Expand All @@ -539,10 +556,11 @@ public static boolean activate(Player player, AdvancedPortal portal, boolean doK
player.sendPluginMessage(plugin, "BungeeCord", outForSend.toByteArray());
// Down to bungee to sort out the teleporting but yea theoretically they should
// warp.
} else if (portal.getDestiation() != null) {
} else if (portal.getDestinations().length > 0) {
ConfigAccessor configDesti = new ConfigAccessor(plugin, "destinations.yml");
if (configDesti.getConfig().getString(portal.getDestiation() + ".world") != null) {
warped = Destination.warp(player, portal.getDestiation(), portal, hasMessage, false);
String randomDest = portal.getDestinations()[random.nextInt(portal.getDestinations().length)];
if (configDesti.getConfig().getString(randomDest + ".world") != null) {
warped = Destination.warp(player, randomDest, portal, hasMessage, false);
if (!warped) {
if(doKnockback)
throwPlayerBack(player);
Expand Down

0 comments on commit 496396f

Please sign in to comment.