Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple island location from island center. #1659

Merged
merged 15 commits into from
Feb 13, 2021
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void deletePlayer(User user, UUID targetUUID) {
if (vector == null) {
user.sendMessage("general.success");
} else {
user.sendMessage("commands.admin.delete.deleted-island", "[xyz]", Util.xyz(vector));
user.sendMessage("commands.admin.delete.deleted-island", TextVariables.XYZ, Util.xyz(vector));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private boolean register(User user, String targetName, UUID targetUUID, Optional
if (i.isSpawn()) {
getIslands().clearSpawn(i.getWorld());
}
user.sendMessage("commands.admin.register.registered-island", "[xyz]", Util.xyz(i.getCenter().toVector()),
user.sendMessage("commands.admin.register.registered-island", TextVariables.XYZ, Util.xyz(i.getCenter().toVector()),
TextVariables.NAME, targetName);
user.sendMessage("general.success");
// Build and call event
Expand Down Expand Up @@ -114,7 +114,7 @@ private boolean register(User user, String targetName, UUID targetUUID, Optional
getIslands().setOwner(user, targetUUID, i);
i.setReserved(true);
i.getCenter().getBlock().setType(Material.BEDROCK);
user.sendMessage("commands.admin.register.reserved-island", "[xyz]", Util.xyz(i.getCenter().toVector()),
user.sendMessage("commands.admin.register.reserved-island", TextVariables.XYZ, Util.xyz(i.getCenter().toVector()),
TextVariables.NAME, targetName);
// Build and fire event
IslandEvent.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package world.bentobox.bentobox.api.commands.admin;


import java.util.List;
import java.util.Optional;

import org.bukkit.Location;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;


/**
* This command sets the center of the protected area.
* The location can be anywhere inside the island range area. Therefore the protected
* range can be up to 2x the island range.
* The location will change for all environments.
* @author tastybento
* @since 1.16.0
*/
public class AdminSetProtectionCenterCommand extends ConfirmableCommand
{
private Location targetLoc;
private Island island;


/**
* Sub-command constructor
*
* @param parent - the parent composite command
*/
public AdminSetProtectionCenterCommand(CompositeCommand parent) {
super(parent, "setprotectionlocation");
}


@Override
public void setup()
{
this.setPermission("admin.setprotectionlocation");
this.setParametersHelp("commands.admin.setprotectionlocation.parameters");
this.setDescription("commands.admin.setprotectionlocation.description");
}

@Override
public boolean canExecute(User user, String label, List<String> args) {
if (args.size() == 3) {
// Get location
targetLoc = getLocation(user, args);
} else {
targetLoc = new Location(getWorld(), user.getLocation().getBlockX(), user.getLocation().getBlockY(), user.getLocation().getBlockZ());
}
if (targetLoc == null) {
user.sendMessage("commands.admin.setprotectionlocation.xyz-error");
return false;
}
Optional<Island> optionalIsland = getIslands().getIslandAt(targetLoc);
if (!optionalIsland.isPresent()) {
user.sendMessage("commands.admin.setspawnpoint.no-island-here");
return false;
}
island = optionalIsland.get();
return true;
}

private Location getLocation(User user, List<String> args) {
try {
int x = Integer.parseInt(args.get(0));
int y = Integer.parseInt(args.get(1));
int z = Integer.parseInt(args.get(2));
return new Location(getWorld(), x, y, z);
} catch (Exception e) {
return null;
}
}

@Override
public boolean execute(User user, String label, List<String> args) {
String name = getPlayers().getName(island.getOwner());
user.sendMessage("commands.admin.setprotectionlocation.island", TextVariables.XYZ, Util.xyz(island.getCenter().toVector()), TextVariables.NAME, name);
// Confirm
this.askConfirmation(user, user.getTranslation("commands.admin.setprotectionlocation.confirmation", TextVariables.XYZ, Util.xyz(targetLoc.toVector())),
() -> this.setLocation(user));
return true;
}


/**
* Set the island location to the user's location.
* @param user User who initiate change.
*/
private void setLocation(User user) {
try {
// Set
island.setProtectionCenter(targetLoc);
user.sendMessage("commands.admin.setprotectionlocation.success", TextVariables.XYZ, Util.xyz(targetLoc.toVector()));
} catch (Exception e) {
user.sendMessage("commands.admin.setprotectionlocation.failure", TextVariables.XYZ, Util.xyz(targetLoc.toVector()));
getAddon().logError("Protection location could not be changed because the island does not exist");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void setSpawn(User user, Island i) {
getIslands().setSpawn(i);
i.setSpawnPoint(World.Environment.NORMAL, user.getLocation());
// Set the island's range to the full island space because it is spawn
i.setProtectionRange(i.getRange());
i.setProtectionRange(i.getRange() * 2);
user.sendMessage("commands.admin.setspawn.success");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void unregisterPlayer(User user, String targetName, UUID targetUUID) {
// Remove all island players that reference this island
oldIsland.getMembers().clear();
getIslands().save(oldIsland);
user.sendMessage("commands.admin.unregister.unregistered-island", "[xyz]", Util.xyz(oldIsland.getCenter().toVector()),
user.sendMessage("commands.admin.unregister.unregistered-island", TextVariables.XYZ, Util.xyz(oldIsland.getCenter().toVector()),
TextVariables.NAME, targetName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public void setup() {
new AdminPurgeCommand(this);
// Settings
new AdminSettingsCommand(this);
// Location
new AdminSetProtectionCenterCommand(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public boolean execute(User user, String label, @NonNull List<String> args) {
Island island = getIslands().getIsland(getWorld(), targetUUID);
int newRange = island.getProtectionRange() + Integer.parseInt(args.get(1));

if (newRange > island.getRange()) {
if (newRange > island.getRange() * 2) {
user.sendMessage("commands.admin.range.invalid-value.too-high", TextVariables.NUMBER, String.valueOf(island.getRange()));
return false;
} else if (newRange == island.getProtectionRange()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;

/**
* @author Poslovitch
Expand Down Expand Up @@ -84,15 +85,15 @@ private void showZones(User user) {

getIslands().getIslandAt(user.getLocation()).ifPresent(island -> {
// Draw the island protected area
drawZone(user, Particle.BARRIER, null, island.getCenter(), island.getProtectionRange());
drawZone(user, Particle.BARRIER, null, island, island.getProtectionRange());

// Draw the default protected area if island protected zone is different
if (island.getProtectionRange() != getPlugin().getIWM().getIslandProtectionRange(getWorld())) {
drawZone(user, Particle.VILLAGER_HAPPY, null, island.getCenter(), getPlugin().getIWM().getIslandProtectionRange(getWorld()));
drawZone(user, Particle.VILLAGER_HAPPY, null, island, getPlugin().getIWM().getIslandProtectionRange(getWorld()));
}

// Draw the island area
drawZone(user, Particle.REDSTONE, new Particle.DustOptions(Color.GRAY, 1.0F), island.getCenter(), island.getRange());
drawZone(user, Particle.REDSTONE, new Particle.DustOptions(Color.GRAY, 1.0F), island, island.getRange());
});
}, 20, 30));
}
Expand All @@ -103,7 +104,8 @@ private void hideZones(User user) {
displayRanges.remove(user);
}

private void drawZone(User user, Particle particle, Particle.DustOptions dustOptions, Location center, int range) {
private void drawZone(User user, Particle particle, Particle.DustOptions dustOptions, Island island, int range) {
Location center = island.getProtectionCenter();
// Get player Y coordinate
int playerY = user.getPlayer().getLocation().getBlockY() + 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public boolean execute(User user, String label, List<String> args) {
Island island = getIslands().getIsland(getWorld(), targetUUID);

// Do some sanity checks to make sure the new protection range won't cause problems
if (range <= 1) {
if (range < 1) {
user.sendMessage("commands.admin.range.invalid-value.too-low", TextVariables.NUMBER, args.get(1));
return false;
}
if (range > island.getRange()) {
user.sendMessage("commands.admin.range.invalid-value.too-high", TextVariables.NUMBER, String.valueOf(island.getRange()));
if (range > island.getRange() * 2) {
user.sendMessage("commands.admin.range.invalid-value.too-high", TextVariables.NUMBER, String.valueOf(2 * island.getRange()));
return false;
}
if (range == island.getProtectionRange()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ private TextVariables() {}
* @since 1.15.0
*/
public static final String GAMEMODE = "[gamemode]";
/**
* Used for coordinates
* @since 1.16.0
*/
public static final String XYZ = "[xyz]";
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public BlueprintPaster(@NonNull BentoBox plugin, @NonNull Blueprint bp, World wo
// Offset due to bedrock
Vector off = bp.getBedrock() != null ? bp.getBedrock() : new Vector(0,0,0);
// Calculate location for pasting
this.location = island.getCenter().toVector().subtract(off).toLocation(world);
this.location = island.getProtectionCenter().toVector().subtract(off).toLocation(world);
}

/**
Expand Down
Loading