Skip to content

Commit

Permalink
Fixes Invincible Visitor settings panel
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed May 23, 2021
1 parent a1a78de commit e472e07
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

import org.bukkit.Material;
import org.bukkit.Sound;
Expand Down Expand Up @@ -50,8 +49,14 @@ public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
String ivPanelName = user.getTranslation("protection.flags.INVINCIBLE_VISITORS.name");
if (panel.getName().equals(ivPanelName)) {
// This is a click on the IV panel
// Slot relates to the sorted enum
DamageCause c = Arrays.stream(EntityDamageEvent.DamageCause.values()).sorted(Comparator.comparing(DamageCause::name)).collect(Collectors.toList()).get(slot);
String itemName = panel.getItems().get(slot).getName();
DamageCause c = getEnum(user, itemName);
if (c == null) {
user.sendMessage("general.errors.general");
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F);
BentoBox.getInstance().logError("Inv Visitor click did not match translated name: " + itemName);
return true;
}
if (getIWM().getIvSettings(user.getWorld()).contains(c.name())) {
getIWM().getIvSettings(user.getWorld()).remove(c.name());
} else {
Expand All @@ -68,6 +73,10 @@ public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
return true;
}

private DamageCause getEnum(User user, String itemName) {
return Arrays.stream(EntityDamageEvent.DamageCause.values()).filter(dc -> getTranslation(user, dc.name()).equals(itemName)).findFirst().orElse(null);
}

private void openPanel(User user, String ivPanelName) {
// Close the current panel
user.closeInventory();
Expand All @@ -81,10 +90,11 @@ private void openPanel(User user, String ivPanelName) {
pb.build();
}



private PanelItem getPanelItem(DamageCause c, User user) {
PanelItemBuilder pib = new PanelItemBuilder();
String translation = user.getTranslationOrNothing("enums.DamageCause." + c.name());
pib.name(translation.isEmpty() ? Util.prettifyText(c.toString()) : translation);
pib.name(getTranslation(user, c.name()));
pib.clickHandler(this);
if (getIWM().getIvSettings(user.getWorld()).contains(c.name())) {
pib.icon(Material.GREEN_SHULKER_BOX);
Expand All @@ -96,6 +106,17 @@ private PanelItem getPanelItem(DamageCause c, User user) {
return pib.build();
}

/**
* Get the translation of the DamageCause enum
* @param user user seeing text
* @param name enum name
* @return translation or a prettified version of name
*/
private String getTranslation(User user, String name) {
String translation = user.getTranslationOrNothing("enums.DamageCause." + name);
return translation.isEmpty() ? Util.prettifyText(name) : translation;
}

/**
* Prevents visitors from getting damage if a particular damage type is listed in the config
* @param e - event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -99,6 +101,16 @@ public void setUp() throws Exception {

when(panel.getInventory()).thenReturn(mock(Inventory.class));
when(panel.getName()).thenReturn("panel");
Map<Integer, PanelItem> map = new HashMap<>();
List<String> sortedNames = Arrays.stream(EntityDamageEvent.DamageCause.values()).map(DamageCause::name)
.map(Util::prettifyText).sorted().collect(Collectors.toList());
int i = 0;
for (String name : sortedNames) {
PanelItem pi = mock(PanelItem.class);
when(pi.getName()).thenReturn(name);
map.put(i++, pi);
}
when(panel.getItems()).thenReturn(map);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.inWorld()).thenReturn(true);
when(user.getWorld()).thenReturn(mock(World.class));
Expand Down Expand Up @@ -193,8 +205,10 @@ public void testOnClickIVPanel() {
ClickType clickType = ClickType.LEFT;
ivSettings.clear();
when(panel.getName()).thenReturn("panel");
// Make the panel

// Test all damage causes to make sure they can be clicked on and off
for (int slot = 0; slot < DamageCause.values().length; slot ++) {
for (int slot = 0; slot < DamageCause.values().length; slot++) {
// Get the damage type
DamageCause dc = Arrays.stream(EntityDamageEvent.DamageCause.values()).sorted(Comparator.comparing(DamageCause::name)).collect(Collectors.toList()).get(slot);
// IV settings should be empty
Expand Down

0 comments on commit e472e07

Please sign in to comment.