-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement per-player border type (#69)
* Rename PlayerBorder.barrier to show * Move player event handlers to PlayerListener * Remove PlayerBorder * Implement PerPlayerBorderProxy Currently defaulting to Customer (non-wbapi) border * Add BorderTypeCommand * Create BorderType * Add messages to BorderTypeCommand * Prepare PerPlayerBorderProxy for handling edge cases The user may not have a border type set, then we return with the default. The user's border type may be unavailable now for two reasons: 1. Border type has been disabled after a server/plugin restart 2. Border type is obsolete and does not exist anymore (forward compatibility). * Fix message reference at BorderTypeCommand * Use null for missing wbapiBorder It is fine since it is never used * Avoid wbapi "border show" spam on player move * At command, replace args.isEmpty() with size() != 1 To also show help if more parameters are passed than necessary * Small refactor * Fix set-type permission in addon.yml * Update README Co-authored-by: tastybento <tastybento@users.noreply.github.com>
- Loading branch information
1 parent
620e134
commit 9ded7cc
Showing
12 changed files
with
284 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package world.bentobox.border; | ||
|
||
import java.util.Optional; | ||
|
||
public enum BorderType { | ||
Barrier((byte) 1, "barrier"), | ||
Vanilla((byte) 2, "vanilla"); | ||
// Virtual((byte)3, "virtual"); // not supported yet | ||
|
||
private final byte id; | ||
private final String commandLabel; | ||
|
||
BorderType(byte id, String commandLabel) { | ||
this.id = id; | ||
this.commandLabel = commandLabel; | ||
} | ||
|
||
public byte getId() { | ||
return id; | ||
} | ||
|
||
public String getCommandLabel() { | ||
return commandLabel; | ||
} | ||
|
||
public static Optional<BorderType> fromCommandLabel(String label) { | ||
for (var bt : BorderType.values()) | ||
if (bt.commandLabel.equalsIgnoreCase(label)) | ||
return Optional.of(bt); | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
public static Optional<BorderType> fromId(byte id) { | ||
for (var bt : BorderType.values()) | ||
if (bt.getId() == id) | ||
return Optional.of(bt); | ||
|
||
return Optional.empty(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/world/bentobox/border/PerPlayerBorderProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package world.bentobox.border; | ||
|
||
import org.bukkit.entity.Player; | ||
import world.bentobox.bentobox.api.metadata.MetaDataValue; | ||
import world.bentobox.bentobox.api.user.User; | ||
import world.bentobox.bentobox.database.objects.Island; | ||
import world.bentobox.border.listeners.BorderShower; | ||
|
||
import java.util.Optional; | ||
|
||
public final class PerPlayerBorderProxy implements BorderShower { | ||
|
||
public static final String BORDER_BORDERTYPE_META_DATA = "Border_bordertype"; | ||
|
||
private final Border addon; | ||
private final BorderShower customBorder; | ||
private final BorderShower wbapiBorder; | ||
|
||
public PerPlayerBorderProxy(Border addon, BorderShower customBorder, BorderShower wbapiBorder) { | ||
this.addon = addon; | ||
this.customBorder = customBorder; | ||
this.wbapiBorder = wbapiBorder; | ||
} | ||
|
||
@Override | ||
public void showBorder(Player player, Island island) { | ||
var user = User.getInstance(player); | ||
var border = getBorder(user); | ||
border.showBorder(player, island); | ||
} | ||
|
||
@Override | ||
public void hideBorder(User user) { | ||
var border = getBorder(user); | ||
border.hideBorder(user); | ||
} | ||
|
||
@Override | ||
public void clearUser(User user) { | ||
var border = getBorder(user); | ||
border.clearUser(user); | ||
} | ||
|
||
@Override | ||
public void refreshView(User user, Island island) { | ||
var border = getBorder(user); | ||
border.refreshView(user, island); | ||
} | ||
|
||
private BorderShower getBorder(User user) { | ||
BorderType borderType = getBorderType(user); | ||
return switch (borderType) { | ||
case Barrier -> customBorder; | ||
case Vanilla -> wbapiBorder; | ||
}; | ||
} | ||
|
||
private BorderType getBorderType(User user) { | ||
Optional<Byte> userTypeId = user.getMetaData(BORDER_BORDERTYPE_META_DATA) | ||
.map(MetaDataValue::asByte); | ||
|
||
if (userTypeId.isEmpty()) { | ||
return getDefaultBorderType(); | ||
} | ||
|
||
Optional<BorderType> borderType = BorderType.fromId(userTypeId.get()); | ||
if (borderType.isEmpty() || !addon.getAvailableBorderTypesView().contains(borderType.get())) { | ||
return getDefaultBorderType(); | ||
} | ||
|
||
return borderType.get(); | ||
} | ||
|
||
private static BorderType getDefaultBorderType() { | ||
return BorderType.Barrier; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/world/bentobox/border/commands/BorderTypeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package world.bentobox.border.commands; | ||
|
||
import world.bentobox.bentobox.api.commands.CompositeCommand; | ||
import world.bentobox.bentobox.api.metadata.MetaDataValue; | ||
import world.bentobox.bentobox.api.user.User; | ||
import world.bentobox.bentobox.database.objects.Island; | ||
import world.bentobox.border.Border; | ||
import world.bentobox.border.BorderType; | ||
import world.bentobox.border.PerPlayerBorderProxy; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public final class BorderTypeCommand extends CompositeCommand { | ||
|
||
private final Border addon; | ||
private Island island; | ||
private final List<String> availableTypes; | ||
|
||
public BorderTypeCommand(Border addon, CompositeCommand parent) { | ||
super(addon, parent, "type"); | ||
this.addon = addon; | ||
this.availableTypes = addon.getAvailableBorderTypesView() | ||
.stream() | ||
.map(BorderType::getCommandLabel) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public void setup() { | ||
this.setPermission("border.set-type"); | ||
this.setDescription("border.set-type.description"); | ||
this.setOnlyPlayer(true); | ||
// setConfigurableRankCommand(); // What is this? Should I use this? I guess not. | ||
} | ||
|
||
@Override | ||
public boolean canExecute(User user, String label, List<String> args) { | ||
island = getIslands().getIsland(getWorld(), user); | ||
return island != null; | ||
} | ||
|
||
@Override | ||
public boolean execute(User user, String label, List<String> args) { | ||
if (args.size() != 1) { | ||
this.showHelp(this, user); | ||
return false; | ||
} | ||
|
||
if (availableTypes.stream().anyMatch(args.get(0)::equalsIgnoreCase)) { | ||
changeBorderTypeTo(user, args.get(0)); | ||
return true; | ||
} | ||
|
||
user.sendMessage("border.set-type.error-unavailable-type"); | ||
return false; | ||
} | ||
|
||
private void changeBorderTypeTo(User user, String newBorderType) { | ||
byte newTypeId = BorderType.fromCommandLabel(newBorderType).get().getId(); | ||
|
||
addon.getBorderShower().hideBorder(user); | ||
user.putMetaData(PerPlayerBorderProxy.BORDER_BORDERTYPE_META_DATA, new MetaDataValue(newTypeId)); | ||
addon.getBorderShower().showBorder(user.getPlayer(), island); | ||
|
||
user.sendMessage("border.set-type.changed", | ||
"[type]", newBorderType); | ||
} | ||
|
||
@Override | ||
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { | ||
return Optional.of(availableTypes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.