mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-24 00:51:40 +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.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
@ -50,8 +49,14 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
|||||||
String ivPanelName = user.getTranslation("protection.flags.INVINCIBLE_VISITORS.name");
|
String ivPanelName = user.getTranslation("protection.flags.INVINCIBLE_VISITORS.name");
|
||||||
if (panel.getName().equals(ivPanelName)) {
|
if (panel.getName().equals(ivPanelName)) {
|
||||||
// This is a click on the IV panel
|
// This is a click on the IV panel
|
||||||
// Slot relates to the sorted enum
|
String itemName = panel.getItems().get(slot).getName();
|
||||||
DamageCause c = Arrays.stream(EntityDamageEvent.DamageCause.values()).sorted(Comparator.comparing(DamageCause::name)).collect(Collectors.toList()).get(slot);
|
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())) {
|
if (getIWM().getIvSettings(user.getWorld()).contains(c.name())) {
|
||||||
getIWM().getIvSettings(user.getWorld()).remove(c.name());
|
getIWM().getIvSettings(user.getWorld()).remove(c.name());
|
||||||
} else {
|
} else {
|
||||||
@ -68,6 +73,10 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
|||||||
return true;
|
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) {
|
private void openPanel(User user, String ivPanelName) {
|
||||||
// Close the current panel
|
// Close the current panel
|
||||||
user.closeInventory();
|
user.closeInventory();
|
||||||
@ -81,10 +90,11 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
|||||||
pb.build();
|
pb.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private PanelItem getPanelItem(DamageCause c, User user) {
|
private PanelItem getPanelItem(DamageCause c, User user) {
|
||||||
PanelItemBuilder pib = new PanelItemBuilder();
|
PanelItemBuilder pib = new PanelItemBuilder();
|
||||||
String translation = user.getTranslationOrNothing("enums.DamageCause." + c.name());
|
pib.name(getTranslation(user, c.name()));
|
||||||
pib.name(translation.isEmpty() ? Util.prettifyText(c.toString()) : translation);
|
|
||||||
pib.clickHandler(this);
|
pib.clickHandler(this);
|
||||||
if (getIWM().getIvSettings(user.getWorld()).contains(c.name())) {
|
if (getIWM().getIvSettings(user.getWorld()).contains(c.name())) {
|
||||||
pib.icon(Material.GREEN_SHULKER_BOX);
|
pib.icon(Material.GREEN_SHULKER_BOX);
|
||||||
@ -96,6 +106,17 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
|||||||
return pib.build();
|
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
|
* Prevents visitors from getting damage if a particular damage type is listed in the config
|
||||||
* @param e - event
|
* @param e - event
|
||||||
|
@ -16,7 +16,9 @@ import static org.mockito.Mockito.when;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -99,6 +101,16 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
|
|
||||||
when(panel.getInventory()).thenReturn(mock(Inventory.class));
|
when(panel.getInventory()).thenReturn(mock(Inventory.class));
|
||||||
when(panel.getName()).thenReturn("panel");
|
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()
|
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||||
when(user.inWorld()).thenReturn(true);
|
when(user.inWorld()).thenReturn(true);
|
||||||
when(user.getWorld()).thenReturn(mock(World.class));
|
when(user.getWorld()).thenReturn(mock(World.class));
|
||||||
@ -193,8 +205,10 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
ClickType clickType = ClickType.LEFT;
|
ClickType clickType = ClickType.LEFT;
|
||||||
ivSettings.clear();
|
ivSettings.clear();
|
||||||
when(panel.getName()).thenReturn("panel");
|
when(panel.getName()).thenReturn("panel");
|
||||||
|
// Make the panel
|
||||||
|
|
||||||
// Test all damage causes to make sure they can be clicked on and off
|
// 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
|
// Get the damage type
|
||||||
DamageCause dc = Arrays.stream(EntityDamageEvent.DamageCause.values()).sorted(Comparator.comparing(DamageCause::name)).collect(Collectors.toList()).get(slot);
|
DamageCause dc = Arrays.stream(EntityDamageEvent.DamageCause.values()).sorted(Comparator.comparing(DamageCause::name)).collect(Collectors.toList()).get(slot);
|
||||||
// IV settings should be empty
|
// IV settings should be empty
|
||||||
|
Loading…
Reference in New Issue
Block a user