Skip to content

Commit

Permalink
Implement customizable Values GUI.
Browse files Browse the repository at this point in the history
This GUI shows value to all items in game. It also shows max limit of blocks, if it is set.

Fixes of #192
  • Loading branch information
BONNe committed Jun 17, 2022
1 parent cc90579 commit 4ea208b
Show file tree
Hide file tree
Showing 9 changed files with 1,262 additions and 127 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<spigot.version>1.16.5-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.20.0</bentobox.version>
<!-- Panel Utils version -->
<panelutils.version>1.0.0</panelutils.version>
<panelutils.version>1.1.0</panelutils.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- Do not change unless you want different name for local builds. -->
Expand Down
1 change: 1 addition & 0 deletions src/main/java/world/bentobox/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void onLoad() {
// Save existing panels.
this.saveResource("panels/top_panel.yml", false);
this.saveResource("panels/detail_panel.yml", false);
this.saveResource("panels/value_panel.yml", false);
}

private boolean loadSettings() {
Expand Down
134 changes: 113 additions & 21 deletions src/main/java/world/bentobox/level/commands/IslandValueCommand.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,142 @@
package world.bentobox.level.commands;

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

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.PlayerInventory;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.level.Level;
import world.bentobox.level.panels.ValuePanel;
import world.bentobox.level.util.Utils;

public class IslandValueCommand extends CompositeCommand {

public class IslandValueCommand extends CompositeCommand
{
private final Level addon;

public IslandValueCommand(Level addon, CompositeCommand parent) {

public IslandValueCommand(Level addon, CompositeCommand parent)
{
super(parent, "value");
this.addon = addon;
}


@Override
public void setup() {
public void setup()
{
this.setPermission("island.value");
this.setDescription("island.value.description");
this.setParametersHelp("level.commands.value.parameters");
this.setDescription("level.commands.value.description");
this.setOnlyPlayer(true);
}


@Override
public boolean execute(User user, String label, List<String> args) {
Player player = user.getPlayer();
PlayerInventory inventory = player.getInventory();
if (!inventory.getItemInMainHand().getType().equals(Material.AIR)) {
Material material = inventory.getItemInMainHand().getType();
Integer value = addon.getBlockConfig().getValue(getWorld(), material);
if (value != null) {
user.sendMessage("island.value.success", "[value]", String.valueOf(value));
double underWater = addon.getSettings().getUnderWaterMultiplier();
if (underWater > 1.0) {
user.sendMessage("island.value.success-underwater", "[value]", (underWater * value) + "");
}
} else {
user.sendMessage("island.value.no-value");
public boolean execute(User user, String label, List<String> args)
{
if (args.size() > 1)
{
this.showHelp(this, user);
return true;
}

if (args.isEmpty())
{
ValuePanel.openPanel(this.addon, this.getWorld(), user);
}
else if (args.get(0).equalsIgnoreCase("HAND"))
{
Player player = user.getPlayer();
PlayerInventory inventory = player.getInventory();

if (!inventory.getItemInMainHand().getType().equals(Material.AIR))
{
this.printValue(user, inventory.getItemInMainHand().getType());
}
else
{
Utils.sendMessage(user, user.getTranslation("level.conversations.empty-hand"));
}
}
else
{
Material material = Material.matchMaterial(args.get(0));

if (material == null)
{
Utils.sendMessage(user,
user.getTranslation(this.getWorld(), "level.conversations.unknown-item",
"[material]", args.get(0)));
}
else
{
this.printValue(user, material);
}
} else {
user.sendMessage("island.value.empty-hand");
}

return true;
}

}

/**
* This method prints value of the given material in chat.
* @param user User who receives the message.
* @param material Material value.
*/
private void printValue(User user, Material material)
{
Integer value = this.addon.getBlockConfig().getValue(getWorld(), material);

if (value != null)
{
Utils.sendMessage(user,
user.getTranslation(this.getWorld(), "level.conversations.value",
"[value]", String.valueOf(value),
"[material]", Utils.prettifyObject(material, user)));

double underWater = this.addon.getSettings().getUnderWaterMultiplier();

if (underWater > 1.0)
{
Utils.sendMessage(user,
user.getTranslation(this.getWorld(),"level.conversations.success-underwater",
"[value]", (underWater * value) + ""),
"[material]", Utils.prettifyObject(material, user));
}
}
else
{
Utils.sendMessage(user,
user.getTranslation(this.getWorld(),"level.conversations.no-value"));
}
}


@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args)
{
String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : "";

if (args.isEmpty())
{
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}

List<String> options = new ArrayList<>(Arrays.stream(Material.values()).
filter(Material::isBlock).
map(Material::name).toList());

options.add("HAND");

return Optional.of(Util.tabLimit(options, lastArg));
}
}
105 changes: 8 additions & 97 deletions src/main/java/world/bentobox/level/panels/DetailsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ private void updateFilters()
{
if (o1.getValue().equals(o2.getValue()))
{
String o1Name = DetailsPanel.prettifyObject(o1.getKey(), this.user);
String o2Name = DetailsPanel.prettifyObject(o2.getKey(), this.user);
String o1Name = Utils.prettifyObject(o1.getKey(), this.user);
String o2Name = Utils.prettifyObject(o2.getKey(), this.user);

return String.CASE_INSENSITIVE_ORDER.compare(o1Name, o2Name);
}
Expand All @@ -193,8 +193,8 @@ private void updateFilters()

if (o1Value == o2Value)
{
String o1Name = DetailsPanel.prettifyObject(o1.getKey(), this.user);
String o2Name = DetailsPanel.prettifyObject(o2.getKey(), this.user);
String o1Name = Utils.prettifyObject(o1.getKey(), this.user);
String o2Name = Utils.prettifyObject(o2.getKey(), this.user);

return String.CASE_INSENSITIVE_ORDER.compare(o1Name, o2Name);
}
Expand All @@ -208,8 +208,8 @@ private void updateFilters()
{
sorter = (o1, o2) ->
{
String o1Name = DetailsPanel.prettifyObject(o1.getKey(), this.user);
String o2Name = DetailsPanel.prettifyObject(o2.getKey(), this.user);
String o1Name = Utils.prettifyObject(o1.getKey(), this.user);
String o2Name = Utils.prettifyObject(o2.getKey(), this.user);

return String.CASE_INSENSITIVE_ORDER.compare(o1Name, o2Name);
};
Expand Down Expand Up @@ -647,10 +647,10 @@ private PanelItem createMaterialButton(ItemTemplateRecord template,
{
builder.name(this.user.getTranslation(this.world, template.title(),
"[number]", String.valueOf(materialCount.getValue()),
"[material]", DetailsPanel.prettifyObject(materialCount.getKey(), this.user)));
"[material]", Utils.prettifyObject(materialCount.getKey(), this.user)));
}

String description = DetailsPanel.prettifyDescription(materialCount.getKey(), this.user);
String description = Utils.prettifyDescription(materialCount.getKey(), this.user);

final String reference = "level.gui.buttons.material.";
String blockId = this.user.getTranslationOrNothing(reference + "id",
Expand Down Expand Up @@ -710,95 +710,6 @@ public static void openPanel(Level addon,
}


/**
* Prettify Material object for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified string for Material.
*/
private static String prettifyObject(Material object, User user)
{
// Nothing to translate
if (object == null)
{
return "";
}

// Find addon structure with:
// [addon]:
// materials:
// [material]:
// name: [name]
String translation = user.getTranslationOrNothing("level.materials." + object.name().toLowerCase() + ".name");

if (!translation.isEmpty())
{
// We found our translation.
return translation;
}

// Find addon structure with:
// [addon]:
// materials:
// [material]: [name]

translation = user.getTranslationOrNothing("level.materials." + object.name().toLowerCase());

if (!translation.isEmpty())
{
// We found our translation.
return translation;
}

// Find general structure with:
// materials:
// [material]: [name]

translation = user.getTranslationOrNothing("materials." + object.name().toLowerCase());

if (!translation.isEmpty())
{
// We found our translation.
return translation;
}

// Use Lang Utils Hook to translate material
return LangUtilsHook.getMaterialName(object, user);
}


/**
* Prettify Material object description for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified description string for Material.
*/
public static String prettifyDescription(Material object, User user)
{
// Nothing to translate
if (object == null)
{
return "";
}

// Find addon structure with:
// [addon]:
// materials:
// [material]:
// description: [text]
String translation = user.getTranslationOrNothing("level.materials." + object.name().toLowerCase() + ".description");

if (!translation.isEmpty())
{
// We found our translation.
return translation;
}

// No text to return.
return "";
}


// ---------------------------------------------------------------------
// Section: Enums
// ---------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 4ea208b

Please sign in to comment.