Skip to content

Commit

Permalink
Use default settings in config if player does not have permissions. (#98
Browse files Browse the repository at this point in the history
)

Redoes the config.yml as well.
Adds test classes.

Relates to #97
  • Loading branch information
tastybento committed Feb 16, 2023
1 parent 9e78610 commit 10f611d
Show file tree
Hide file tree
Showing 12 changed files with 458 additions and 68 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
<java.version>17</java.version>
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.18.2-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.20.1</bentobox.version>
<spigot.version>1.19.3-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.22.1-SNAPSHOT</bentobox.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. -->
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/world/bentobox/border/Border.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ private void registerPlaceholders()
this.getPlugin().getPlaceholdersManager().registerPlaceholder(this,
"type",
user -> BorderType.fromId(user.getMetaData(PerPlayerBorderProxy.BORDER_BORDERTYPE_META_DATA).
orElse(new MetaDataValue(BorderType.VANILLA.getId())).asByte()).
orElse(BorderType.VANILLA).
orElse(new MetaDataValue(getSettings().getType().getId())).asByte()).
orElse(getSettings().getType()).
getCommandLabel());
}
}
4 changes: 2 additions & 2 deletions src/main/java/world/bentobox/border/PerPlayerBorderProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private BorderType getBorderType(User user) {
return borderType.get();
}

private static BorderType getDefaultBorderType() {
return BorderType.VANILLA;
private BorderType getDefaultBorderType() {
return addon.getSettings().getType();
}
}
51 changes: 26 additions & 25 deletions src/main/java/world/bentobox/border/Settings.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package world.bentobox.border;

import java.util.HashSet;
import java.util.Set;

import world.bentobox.bentobox.api.configuration.ConfigComment;
import world.bentobox.bentobox.api.configuration.ConfigEntry;
import world.bentobox.bentobox.api.configuration.ConfigObject;
import world.bentobox.bentobox.api.configuration.StoreAt;

import java.util.HashSet;
import java.util.Set;

@StoreAt(filename = "config.yml", path = "addons/Border")
public class Settings implements ConfigObject {

Expand All @@ -22,9 +22,12 @@ public class Settings implements ConfigObject {
private Set<String> disabledGameModes = new HashSet<>();

@ConfigComment("")
@ConfigComment("Use vanilla world border.")
@ConfigEntry(path = "use-vanilla")
private boolean useVanilla = true;
@ConfigComment("Border type. Options are VANILLA, which uses the vanillia-style board or BARRIER,")
@ConfigComment("which uses particles and barrier blocks. If players have permission to use the barrier type")
@ConfigComment("they may override this option. If they do not have permission or lose the permission")
@ConfigComment("then this setting will be used.")
@ConfigEntry(path = "type")
private BorderType type = BorderType.VANILLA;

@ConfigComment("")
@ConfigComment("Teleport players back inside the border if they somehow get outside.")
Expand All @@ -33,24 +36,25 @@ public class Settings implements ConfigObject {
private boolean returnTeleport = true;

@ConfigComment("")
@ConfigComment("Only applies if vanilla isn't used.")
@ConfigComment("Use barrier blocks. If false, the border is indicated by particles only.")
@ConfigComment("Barrier blocks on/off. Only applies if the border type is BARRIER.")
@ConfigComment("If false, the border is indicated by particles only.")
@ConfigEntry(path = "use-barrier-blocks")
private boolean useBarrierBlocks = true;

@ConfigComment("")
@ConfigComment("Default border behavior")
@ConfigComment("Turn on barrier by default.")
@ConfigEntry(path = "show-by-default")
private boolean showByDefault = true;

@ConfigComment("")
@ConfigComment("Only applies if vanilla isn't used.")
@ConfigComment("Only applies if VANILLA type isn't used.")
@ConfigComment("Show max-protection range border. This is a visual border only and not a barrier.")
@ConfigComment("This setting is useful for game modes where the protection range can move around, like Boxed")
@ConfigEntry(path = "show-max-border")
private boolean showMaxBorder = true;

@ConfigComment("")
@ConfigComment("Only applies if vanilla isn't used.")
@ConfigComment("Only applies if VANILLA type isn't used.")
@ConfigComment("Enables/disables all types of wall particles shown by the addon")
@ConfigEntry(path = "show-particles")
private boolean showParticles = true;
Expand Down Expand Up @@ -111,20 +115,6 @@ public void setShowMaxBorder(boolean showMaxBorder) {
this.showMaxBorder = showMaxBorder;
}

/**
* @return the useVanilla
*/
public boolean isUseVanilla() {
return useVanilla;
}

/**
* @param useVanilla the useVanilla to set
*/
public void setUseVanilla(boolean useVanilla) {
this.useVanilla = useVanilla;
}

/**
* @return the returnTeleport
*/
Expand Down Expand Up @@ -152,4 +142,15 @@ public boolean isShowParticles() {
public void setShowParticles(boolean showParticles) {
this.showParticles = showParticles;
}

public BorderType getType() {
if (type == null) {
type = BorderType.VANILLA;
}
return type;
}

public void setType(BorderType type) {
this.type = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
public final class BorderTypeCommand extends CompositeCommand {

public static final String BORDER_TYPE_COMMAND_PERM = "border.type";
private final Border addon;
private Island island;
private final List<String> availableTypes;
Expand All @@ -34,7 +35,7 @@ public BorderTypeCommand(Border addon, CompositeCommand parent) {

@Override
public void setup() {
this.setPermission("border.type");
this.setPermission(BORDER_TYPE_COMMAND_PERM);
this.setDescription("border.set-type.description");
this.setOnlyPlayer(true);
}
Expand Down Expand Up @@ -80,8 +81,8 @@ public boolean execute(User user, String label, List<String> args) {
private void toggleBorderType(User user)
{
MetaDataValue metaDataValue = user.getMetaData(PerPlayerBorderProxy.BORDER_BORDERTYPE_META_DATA).
orElse(new MetaDataValue(BorderType.VANILLA.getId()));
BorderType borderType = BorderType.fromId(metaDataValue.asByte()).orElse(BorderType.VANILLA);
orElse(new MetaDataValue(addon.getSettings().getType().getId()));
BorderType borderType = BorderType.fromId(metaDataValue.asByte()).orElse(addon.getSettings().getType());

List<BorderType> borderTypes = Arrays.stream(BorderType.values()).toList();
int index = borderTypes.indexOf(borderType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public class IslandBorderCommand extends CompositeCommand {

public static final String BORDER_COMMAND_PERM = "border.toggle";
private Border addon;
private Island island;

Expand All @@ -22,7 +23,7 @@ public IslandBorderCommand(Border addon, CompositeCommand parent, String label)

@Override
public void setup() {
this.setPermission("border.toggle");
this.setPermission(BORDER_COMMAND_PERM);
this.setDescription("border.toggle.description");
this.setOnlyPlayer(true);
setConfigurableRankCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public default void clearUser(User user) {
// Do nothing
}

/**
* Refreshes the barrier view, if required
* @param user user
* @param island island
*/
public default void refreshView(User user, Island island){
// Do nothing
}
Expand Down
49 changes: 28 additions & 21 deletions src/main/java/world/bentobox/border/listeners/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.event.vehicle.VehicleExitEvent;
import org.bukkit.event.vehicle.VehicleMoveEvent;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;

import world.bentobox.bentobox.api.events.island.IslandProtectionRangeChangeEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.border.Border;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.border.PerPlayerBorderProxy;
import world.bentobox.border.commands.BorderTypeCommand;
import world.bentobox.border.commands.IslandBorderCommand;

/**
* @author tastybento
Expand All @@ -52,7 +53,20 @@ public PlayerListener(Border addon) {

@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerJoin(PlayerJoinEvent e) {
show.clearUser(User.getInstance(e.getPlayer()));
User user = User.getInstance(e.getPlayer());
// Check player perms and return to defaults if players don't have them
if (!e.getPlayer().hasPermission(addon.getPermissionPrefix() + IslandBorderCommand.BORDER_COMMAND_PERM)) {

// Restore barrier on/off to default
user.putMetaData(BorderShower.BORDER_STATE_META_DATA, new MetaDataValue(addon.getSettings().isShowByDefault()));
if (!e.getPlayer().hasPermission(addon.getPermissionPrefix() + BorderTypeCommand.BORDER_TYPE_COMMAND_PERM)) {
// Restore default barrier type to player
MetaDataValue metaDataValue = new MetaDataValue(addon.getSettings().getType().getId());
user.putMetaData(PerPlayerBorderProxy.BORDER_BORDERTYPE_META_DATA, metaDataValue);
}
}
// Show the border if required
show.clearUser(user);
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> addon.getIslands().getIslandAt(e.getPlayer().getLocation()).ifPresent(i ->
show.showBorder(e.getPlayer(), i)));
}
Expand Down Expand Up @@ -171,24 +185,9 @@ private boolean outsideCheck(Player player, Location from, Location to) {
}

/**
* Teleports a player back home if they use a vehicle to glitch out of the world border
* @param event - event
* Refreshes the barrier view when the player moves (more than just moving their head)
* @param e event
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onDismount(VehicleExitEvent event) {
if (event.getExited() instanceof Player p && p.hasPermission(addon.getPermissionPrefix() + "border.on")) {
Optional<Island> is = addon.getIslands().getProtectedIslandAt(p.getLocation());
if (is.isPresent()) {
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> {
if (!addon.getIslands().getProtectedIslandAt(p.getLocation()).isPresent()
&& addon.getIslands().getIslandAt(p.getLocation()).equals(is)) {
addon.getIslands().homeTeleportAsync(Util.getWorld(p.getWorld()), p);
}
});
}
}
}

@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerMove(PlayerMoveEvent e) {
// Remove head movement
Expand All @@ -199,6 +198,10 @@ public void onPlayerMove(PlayerMoveEvent e) {
}
}

/**
* Refresh the view when riding in a vehicle
* @param e event
*/
@EventHandler(priority = EventPriority.NORMAL)
public void onVehicleMove(VehicleMoveEvent e) {
// Remove head movement
Expand All @@ -213,6 +216,10 @@ public void onVehicleMove(VehicleMoveEvent e) {
}
}

/**
* Hide and then show the border to react to the change in protection area
* @param e
*/
@EventHandler(priority = EventPriority.NORMAL)
public void onProtectionRangeChange(IslandProtectionRangeChangeEvent e) {
// Hide and show again
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/world/bentobox/border/SettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,21 @@ public void testSetShowMaxBorder() {
}

/**
* Test method for {@link world.bentobox.border.Settings#isUseVanilla()}.
* Test method for {@link world.bentobox.border.Settings#getType()}.
*/
@Test
public void testIsUseVanilla() {
assertTrue(settings.isUseVanilla());
public void testGetType() {
assertEquals(BorderType.VANILLA, settings.getType());
}

/**
* Test method for {@link world.bentobox.border.Settings#setUseVanilla(boolean)}.
* Test method for {@link world.bentobox.border.Settings#setType(BorderType)}.
*/
@Test
public void testSetUseVanilla() {
settings.setUseVanilla(true);
assertTrue(settings.isUseVanilla());
public void testSetType() {
assertEquals(BorderType.VANILLA, settings.getType());
settings.setType(BorderType.BARRIER);
assertEquals(BorderType.BARRIER, settings.getType());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package world.bentobox.border.commands;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -9,7 +11,6 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.awt.color.ICC_ColorSpace;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
Expand Down Expand Up @@ -37,15 +38,14 @@
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.PanelListenerManager;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.border.Border;
import world.bentobox.border.BorderType;
import world.bentobox.border.PerPlayerBorderProxy;
import world.bentobox.border.Settings;
import world.bentobox.border.listeners.BorderShower;

/**
Expand Down Expand Up @@ -132,6 +132,11 @@ public void setUp() throws Exception {

// Shower
when(addon.getBorderShower()).thenReturn(bs);


// Settings
Settings settings = new Settings();
when(addon.getSettings()).thenReturn(settings);


ic = new BorderTypeCommand(addon, ac);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void setUp() throws Exception {

// Settings
Settings settings = new Settings();
when(addon.getSettings()).thenReturn(settings );
when(addon.getSettings()).thenReturn(settings);

ic = new IslandBorderCommand(addon, ac, "");
}
Expand Down

0 comments on commit 10f611d

Please sign in to comment.