Skip to content

Commit

Permalink
Implement BUY mode in BiomesPanel.
Browse files Browse the repository at this point in the history
Switch from strict single-click-type mode to "array" of modes. Allows to have both: buy and choose buttons with the same click type.
  • Loading branch information
BONNe committed Jan 21, 2022
1 parent 48518ac commit 9620811
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<powermock.version>2.0.2</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.18-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.17.3</bentobox.version>
<bentobox.version>1.20.0-SNAPSHOT</bentobox.version>
<level.version>2.5.0</level.version>
<greenhouses.version>1.4.0-SNAPSHOT</greenhouses.version>
<vault.version>1.7</vault.version>
Expand Down
52 changes: 43 additions & 9 deletions src/main/java/world/bentobox/biomes/panels/user/BiomesPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import org.eclipse.jdt.annotation.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.TemplatedPanel;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
Expand Down Expand Up @@ -207,26 +209,47 @@ private PanelItem createBiomeButton(ItemTemplateRecord template, BiomesObject bi
builder.description(this.generateBiomesDescription(biomesObject, this.user));
}

// Get only possible actions, by removing all inactive ones.
List<ItemTemplateRecord.ActionRecords> activeActions = new ArrayList<>(template.actions());

activeActions.removeIf(action -> {
switch (action.actionType().toUpperCase())
{
case "BUY" -> {
return this.islandData.isPurchased(biomesObject);
}
case "CHANGE", "ADVANCED_PANEL" -> {
return !this.islandData.isUnlocked(biomesObject) ||
!this.islandData.isPurchased(biomesObject);
}
default -> {
return false;
}
}
});

// Add Click handler
builder.clickHandler((panel, user, clickType, i) -> {
for (ItemTemplateRecord.ActionRecords action : template.actions())
for (ItemTemplateRecord.ActionRecords action : activeActions)
{
if (clickType == action.clickType())
{
switch (action.actionType().toUpperCase())
{
case "CHANGE":
case "BUY" -> {
this.buyBiome(biomesObject);
}
case "CHANGE" -> {
// Biome change is done via commands, because that is the only way how
// to apply timeouts reliably.

String[] split = action.content().split(":");

this.changeBiome(biomesObject, split[0], split.length > 1 ? split[1] : "1");

break;
case "ADVANCED_PANEL":
AdvancedPanel.open(this, biomesObject, this.user);
break;
}
case "ADVANCED_PANEL" -> {
AdvancedPanel.open(this, biomesObject, null);
}
}
}
}
Expand All @@ -235,9 +258,8 @@ private PanelItem createBiomeButton(ItemTemplateRecord template, BiomesObject bi
});

// Collect tooltips.
List<String> tooltips = template.actions().stream().
List<String> tooltips = activeActions.stream().
filter(action -> action.tooltip() != null).
filter(action -> this.manager.canApplyBiome(this.user, this.islandData, biomesObject) || "ADVANCED_PANEL".equalsIgnoreCase(action.actionType())).
map(action -> this.user.getTranslation(this.world, action.tooltip())).
filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
Expand Down Expand Up @@ -443,6 +465,18 @@ private void changeBiome(BiomesObject biomesObject, String mode, String range)
}


/**
* Buy biome.
*
* @param biomesObject the biomes object
*/
private void buyBiome(BiomesObject biomesObject)
{
this.callCommand(this.addon.getSettings().getPlayerBuyCommand().split(" ")[0],
Collections.singletonList(biomesObject.getUniqueId()));
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
Expand Down
12 changes: 10 additions & 2 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ biomes:
confirmation: "This will remove existing biomes from [gamemode] and import new biomes from the template file - please confirm"
migrate:
description: "migrates biomes stored data to new format"
set:
description: "changes biome for the player"
parameters: "<player> <biome-id> [mode] [size]"
# This section contains only player commands translations.
player:
main:
Expand Down Expand Up @@ -72,7 +75,7 @@ biomes:
&7 Changes biome to [biome]
&7 Update mode: [mode]
&7 Update range: [range]
&7 Update range: [number]
# Button that is used to return to previous GUI or exit it completely.
return:
name: "&f&l Return"
Expand Down Expand Up @@ -535,7 +538,7 @@ biomes:
click-to-select: "&e Click &7 to select."
click-to-choose: "&e Click &7 to choose."
click-to-complete: "&e Click &7 to complete."
left-click-to-accept: "&e Left Click &7 to complete."
left-click-to-accept: "&e Left Click &7 to apply."
right-click-to-write: "&e Right Click &7 to write."
click-to-reduce: "&e Click &7 to reduce."
click-to-increase: "&e Click &7 to increase."
Expand Down Expand Up @@ -564,6 +567,7 @@ biomes:
right-click-to-select: "&e Right Click &7 to select."
right-click-to-deselect: "&e Right Click &7 to deselect."
left-click-to-choose: "&e Left Click &7 to choose."
click-to-buy: "&e Click &7 to buy."
click-to-cancel: "&e Click &7 to cancel."
click-to-save: "&e Click &7 to save."
click-to-deselect: "&e Click &7 to deselect."
Expand All @@ -579,6 +583,10 @@ biomes:
right-click-to-open: "&e Right Click &7 to open."
click-to-perform: "&e Click &7 to perform."
descriptions:
update-mode:
island: "Island Mode"
chunk: "Chunk Mode"
range: "Range Mode"
selected: "&2 Selected"
island-owner: "[player]"
unknown: "&e Unknown"
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/panels/main_panel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ main_panel:
data:
type: BIOME
actions:
left:
- click-type: left
type: CHANGE
# Supports ISLAND | CHUNK:NUMBER | RANGE:NUMBER
content: ISLAND
tooltip: biomes.gui.tips.left-click-to-choose
right:
- click-type: left
type: BUY
tooltip: biomes.gui.tips.click-to-buy
- click-type: right
type: ADVANCED_PANEL
tooltip: biomes.gui.tips.right-click-to-open

0 comments on commit 9620811

Please sign in to comment.