Skip to content

Commit

Permalink
Added error when trying to sell in /emf shop with no sellable items.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oheers committed Apr 16, 2021
1 parent ef814c6 commit 1c4ba54
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 24 deletions.
17 changes: 17 additions & 0 deletions main/java/com/oheers/fish/config/messages/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,24 @@ public String getConfirmName() {
return config.getString("confirm-gui-name");
}

public String getNoValueName() {
String s = config.getString("error-gui-name");
if (s != null) return s;
else return "&c&lCan't Sell";
}

public List<String> sellLore() {
return config.getStringList("sell-gui-lore");
}

public List<String> noValueLore() {
List<String> l = config.getStringList("error-gui-lore");
if (!l.isEmpty()) return l;
else {
l.add("&c&lValue: &c$0");
l.add("&cAdd your caught fish to this.");
l.add("&cGUI to sell them.");
return l;
}
}
}
25 changes: 21 additions & 4 deletions main/java/com/oheers/fish/selling/InteractHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,25 @@ public void interact(InventoryClickEvent event) {
}

// makes the player confirm their choice
gui.createConfirmIcon();
gui.setConfirmIcon();
gui.createIcon();
gui.setIcon();
gui.setFiller();

gui.setModified(false);
event.setCancelled(true);

} else if (clickedItem.isSimilar(gui.getErrorIcon())) {
// cancels on right click
if (event.getAction().equals(InventoryAction.PICKUP_HALF)) {
event.setCancelled(true);
gui.close(false);
return;
}

// makes the player confirm their choice
gui.createIcon();
gui.setIcon();
gui.setFiller();

gui.setModified(false);
event.setCancelled(true);
Expand All @@ -56,8 +73,8 @@ public void interact(InventoryClickEvent event) {
if (gui.getModified()) {

// the menu has been modified since we last gave the confirmation button, so it sends it again
gui.createConfirmIcon();
gui.setConfirmIcon();
gui.createIcon();
gui.setIcon();

gui.setModified(false);
} else {
Expand Down
89 changes: 70 additions & 19 deletions main/java/com/oheers/fish/selling/SellGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ public class SellGUI {

public double value;

private boolean error;

public int fishCount;

private ItemStack sellIcon, filler, confirmIcon;
private ItemStack sellIcon, filler, errorFiller, confirmIcon, noValueIcon;

public SellGUI(Player p) {
this.player = p;
this.modified = false;
makeMenu();
addFiller();
setFiller();
addFiller(filler);
setSellItem();
this.player.openInventory(menu);
}
Expand All @@ -47,16 +50,23 @@ public Player getPlayer() {
return player;
}

public void addFiller() {
public void setFiller() {
// the gray glass panes at the bottom
ItemStack fill = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
ItemStack error = new ItemStack(Material.RED_STAINED_GLASS_PANE);
ItemMeta fillMeta = fill.getItemMeta();
ItemMeta errMeta = error.getItemMeta();
fillMeta.setDisplayName(ChatColor.RESET + "");
errMeta.setDisplayName(ChatColor.RESET + "");
fill.setItemMeta(fillMeta);
error.setItemMeta(errMeta);

// sets it as a default menu item that won't be dropped in a .close() request
this.filler = WorthNBT.attributeDefault(fill);
this.errorFiller = WorthNBT.attributeDefault(error);
}

public void addFiller(ItemStack fill) {
menu.setItem(27, fill);
menu.setItem(28, fill);
menu.setItem(29, fill);
Expand All @@ -68,6 +78,19 @@ public void addFiller() {
menu.setItem(35, fill);
}

public void errorFiller() {
// the gray glass panes at the bottom
ItemStack fill = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
ItemMeta fillMeta = fill.getItemMeta();
fillMeta.setDisplayName(ChatColor.RESET + "");
fill.setItemMeta(fillMeta);

// sets it as a default menu item that won't be dropped in a .close() request
this.errorFiller = WorthNBT.attributeDefault(fill);

addFiller(this.filler);
}

public void setSellItem() {
ItemStack sIcon = new ItemStack(Material.GOLD_INGOT);
ItemMeta sellMeta = sIcon.getItemMeta();
Expand All @@ -94,26 +117,54 @@ public ItemStack getConfirmIcon() {
return this.confirmIcon;
}

public void createConfirmIcon() {
ItemStack confirm = new ItemStack(Material.GOLD_BLOCK);
ItemMeta cMeta = confirm.getItemMeta();
cMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', EvenMoreFish.msgs.getConfirmName()));
// Generates the lore, looping through each line in messages.yml lore thingy, and generating it
List<String> lore = new ArrayList<>();
for (String line : EvenMoreFish.msgs.sellLore()) {
lore.add(new Message().setMSG(line).setSellPrice(getTotalWorth()).toString());
}
cMeta.setLore(lore);
public ItemStack getErrorIcon() {
return this.noValueIcon;
}

confirm.setItemMeta(cMeta);
glowify(confirm);
public void createIcon() {
String totalWorth = getTotalWorth();
if (totalWorth.equals("0.0")) {
ItemStack error = new ItemStack(Material.REDSTONE_BLOCK);
ItemMeta errorMeta = error.getItemMeta();
errorMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', EvenMoreFish.msgs.getNoValueName()));
List<String> lore = new ArrayList<>();
for (String line : EvenMoreFish.msgs.noValueLore()) {
lore.add(ChatColor.translateAlternateColorCodes('&', line));
}
errorMeta.setLore(lore);
error.setItemMeta(errorMeta);
glowify(error);
this.noValueIcon = WorthNBT.attributeDefault(error);
this.error = true;
} else {
ItemStack confirm = new ItemStack(Material.GOLD_BLOCK);
ItemMeta cMeta = confirm.getItemMeta();
cMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', EvenMoreFish.msgs.getConfirmName()));
// Generates the lore, looping through each line in messages.yml lore thingy, and generating it
List<String> lore = new ArrayList<>();
for (String line : EvenMoreFish.msgs.sellLore()) {
lore.add(new Message().setMSG(line).setSellPrice(totalWorth).toString());
}
cMeta.setLore(lore);

this.confirmIcon = WorthNBT.attributeDefault(confirm);
confirm.setItemMeta(cMeta);
glowify(confirm);
this.confirmIcon = WorthNBT.attributeDefault(confirm);
this.error = false;
}
}

public void setConfirmIcon() {
this.menu.setItem(31, null);
this.menu.setItem(31, this.confirmIcon);
public void setIcon() {
if (this.error) {
this.menu.setItem(31, null);
this.menu.setItem(31, this.noValueIcon);
this.addFiller(errorFiller);
} else {
this.menu.setItem(31, null);
this.menu.setItem(31, this.confirmIcon);
this.addFiller(filler);
}

}

public String getTotalWorth() {
Expand Down
9 changes: 8 additions & 1 deletion main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,21 @@ worth-gui-name: "&1&lSell Fish"
sell-gui-name: "&6&lSELL"
# The name found on the confirming item in /emf shop
confirm-gui-name: "&6&lCONFIRM"
# The name found on the error item in /emf shop when the gui contains no items of value.
error-gui-name: "&c&lCan't Sell"
# The lore found on the selling/confirmation item in /emf shop
sell-gui-lore:
- "&e&lValue: &e${sell-price}"
- "&7LEFT CLICK to sell the fish."
- "&7RIGHT CLICK to cancel."
# The lore below the error item in /emf shop when the gui contains no items of value.
error-gui-lore:
- "&c&lValue: &c$0"
- "&cAdd your caught fish to this."
- "&cGUI to sell them."
# The message sent to players when they've sold their fish in the /emf shop
fish-sale: "&fYou've sold &a{amount} &ffish for &a${sell-price}&f."

# ATTENTION ATTENTION ATTENTION
# DO NOT EDIT THIS VALUE.
config-version: 2
config-version: 3

0 comments on commit 1c4ba54

Please sign in to comment.