Skip to content

Commit

Permalink
Implement a toggle between border types just by writing border type c…
Browse files Browse the repository at this point in the history
…ommand.

Requested by #87
  • Loading branch information
BONNe committed Jul 14, 2022
1 parent 097e17e commit fe152df
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package world.bentobox.border.commands;

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

Expand Down Expand Up @@ -41,6 +42,11 @@ public boolean canExecute(User user, String label, List<String> args) {

@Override
public boolean execute(User user, String label, List<String> args) {
if (args.size() == 0) {
this.toggleBorderType(user);
return false;
}

if (args.size() != 1) {
this.showHelp(this, user);
return false;
Expand All @@ -55,6 +61,30 @@ public boolean execute(User user, String label, List<String> args) {
return false;
}


/**
* This method toggles from one island border type to another border type.
* @param user User whos border must be changed.
*/
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);

List<BorderType> borderTypes = Arrays.stream(BorderType.values()).toList();
int index = borderTypes.indexOf(borderType);

if (index + 1 >= borderTypes.size())
{
this.changeBorderTypeTo(user, borderTypes.get(0).getCommandLabel());
}
else
{
this.changeBorderTypeTo(user, borderTypes.get(index + 1).getCommandLabel());
}
}

private void changeBorderTypeTo(User user, String newBorderType) {
BorderType.fromCommandLabel(newBorderType).map(BorderType::getId).map(MetaDataValue::new)
.ifPresentOrElse(newTypeId -> {
Expand Down

0 comments on commit fe152df

Please sign in to comment.