mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-07 17:07:36 +01:00
Fixes Invincible Visitor settings panel
https://github.com/BentoBoxWorld/BentoBox/issues/1762
This commit is contained in:
parent
a1a78de96f
commit
e472e07c34
@ -2,7 +2,6 @@ package world.bentobox.bentobox.listeners.flags.worldsettings;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
@ -50,8 +49,14 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
||||
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 {
|
||||
@ -68,6 +73,10 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
||||
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();
|
||||
@ -81,10 +90,11 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
||||
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);
|
||||
@ -96,6 +106,17 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
||||
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
|
||||
|
@ -16,7 +16,9 @@ import static org.mockito.Mockito.when;
|
||||
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;
|
||||
@ -99,6 +101,16 @@ public class InvincibleVisitorsListenerTest {
|
||||
|
||||
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));
|
||||
@ -193,8 +205,10 @@ public class InvincibleVisitorsListenerTest {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user