Code clean up. Java updates.

This commit is contained in:
tastybento 2021-12-22 14:37:10 -08:00
parent 0248c398f6
commit 51408751a6
13 changed files with 71 additions and 102 deletions

View File

@ -17,6 +17,7 @@ import world.bentobox.limits.commands.PlayerCommand;
import world.bentobox.limits.listeners.BlockLimitsListener;
import world.bentobox.limits.listeners.EntityLimitListener;
import world.bentobox.limits.listeners.JoinListener;
import world.bentobox.limits.objects.IslandBlockCount;
/**
@ -134,7 +135,7 @@ public class Limits extends Addon {
private void registerPlaceholders(GameModeAddon gm) {
if (getPlugin().getPlaceholdersManager() == null) return;
Arrays.stream(Material.values())
.filter(m -> m.isBlock())
.filter(Material::isBlock)
.forEach(m -> registerCountAndLimitPlaceholders(m, gm));
}
@ -148,8 +149,8 @@ public class Limits extends Addon {
* "Limits_bskyblock_island_hopper_count"
* "Limits_bskyblock_island_hopper_limit"
*
* @param m
* @param gm
* @param m material
* @param gm game mode
*/
private void registerCountAndLimitPlaceholders(Material m, GameModeAddon gm) {
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
@ -163,7 +164,7 @@ public class Limits extends Addon {
/**
* @param user - Used to identify the island the user belongs to
* @param m - The material we are trying to count on the island
* @param gm
* @param gm Game Mode Addon
* @return Number of blocks of the specified material on the given user's island
*/
private int getCount(@Nullable User user, Material m, GameModeAddon gm) {
@ -171,13 +172,17 @@ public class Limits extends Addon {
if (is == null) {
return 0;
}
return getBlockLimitListener().getIsland(gm.getIslands().getIsland(gm.getOverWorld(), user).getUniqueId()).getBlockCount(m);
@Nullable IslandBlockCount ibc = getBlockLimitListener().getIsland(is.getUniqueId());
if (ibc == null) {
return 0;
}
return ibc.getBlockCount(m);
}
/**
* @param user - Used to identify the island the user belongs to
* @param m - The material whose limit we are querying
* @param gm
* @param gm Game Mode Addon
* @return The limit of the specified material on the given user's island
*/
private String getLimit(@Nullable User user, Material m, GameModeAddon gm) {
@ -185,8 +190,11 @@ public class Limits extends Addon {
if (is == null) {
return "Limit not set";
}
int limit = getBlockLimitListener().getIsland(gm.getIslands().getIsland(gm.getOverWorld(), user).getUniqueId()).
getBlockLimit(m);
@Nullable IslandBlockCount ibc = getBlockLimitListener().getIsland(is.getUniqueId());
if (ibc == null) {
return "Limit not set";
}
int limit = ibc.getBlockLimit(m);
return limit == -1 ? "Limit not set" : String.valueOf(limit);
}

View File

@ -1,14 +1,6 @@
package world.bentobox.limits;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import org.bukkit.configuration.ConfigurationSection;
@ -89,7 +81,7 @@ public class Settings {
addon.logError("Unknown entity type: " + s + " - skipping...");
}
return null;
}).filter(e -> e != null).collect(Collectors.toCollection(LinkedHashSet::new));
}).filter(Objects::nonNull).collect(Collectors.toCollection(LinkedHashSet::new));
if (entities.isEmpty())
continue;
EntityGroup group = new EntityGroup(name, entities, limit);
@ -102,7 +94,7 @@ public class Settings {
}
addon.log("Entity group limits:");
getGroupLimitDefinitions().stream().map(e -> "Limit " + e.getName() + " (" + e.getTypes().stream().map(x -> x.name()).collect(Collectors.joining(", ")) + ") to " + e.getLimit()).forEach(addon::log);
getGroupLimitDefinitions().stream().map(e -> "Limit " + e.getName() + " (" + e.getTypes().stream().map(Enum::name).collect(Collectors.joining(", ")) + ") to " + e.getLimit()).forEach(addon::log);
}
private EntityType getType(String key) {
@ -127,7 +119,7 @@ public class Settings {
* @return the group limit definitions
*/
public List<EntityGroup> getGroupLimitDefinitions() {
return groupLimits.values().stream().flatMap(e -> e.stream()).distinct().collect(Collectors.toList());
return groupLimits.values().stream().flatMap(Collection::stream).distinct().collect(Collectors.toList());
}
/**
@ -186,9 +178,7 @@ public class Settings {
if (getClass() != obj.getClass())
return false;
final EntityGroup other = (EntityGroup) obj;
if (!Objects.equals(this.name, other.name))
return false;
return true;
return Objects.equals(this.name, other.name);
}
@Override

View File

@ -1,13 +1,7 @@
package world.bentobox.limits.commands;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.bukkit.Material;
@ -88,21 +82,18 @@ public class LimitTab implements Tab {
addEntityLimits(ibc, island);
addEntityGroupLimits(ibc, island);
// Sort
switch (sortBy) {
default:
Collections.sort(result, (o1, o2) -> o1.getName().compareTo(o2.getName()));
break;
case Z2A:
Collections.sort(result, (o1, o2) -> o2.getName().compareTo(o1.getName()));
break;
if (sortBy == SORT_BY.Z2A) {
result.sort((o1, o2) -> o2.getName().compareTo(o1.getName()));
} else {
result.sort(Comparator.comparing(PanelItem::getName));
}
}
private void addEntityGroupLimits(IslandBlockCount ibc, Island island) {
// Entity group limits
Map<EntityGroup, Integer> groupmap = addon.getSettings().getGroupLimitDefinitions().stream().collect(Collectors.toMap(e -> e, e -> e.getLimit()));
Map<String, EntityGroup> groupbyname = groupmap.keySet().stream().collect(Collectors.toMap(e -> e.getName(), e -> e));
Map<EntityGroup, Integer> groupmap = addon.getSettings().getGroupLimitDefinitions().stream().collect(Collectors.toMap(e -> e, EntityGroup::getLimit));
Map<String, EntityGroup> groupbyname = groupmap.keySet().stream().collect(Collectors.toMap(EntityGroup::getName, e -> e));
// Merge in any permission-based limits
if (ibc == null) {
return;
@ -111,8 +102,7 @@ public class LimitTab implements Tab {
.filter(e -> groupbyname.containsKey(e.getKey()))
.forEach(e -> groupmap.put(groupbyname.get(e.getKey()), e.getValue()));
ibc.getEntityGroupLimitsOffset().entrySet().forEach(o ->
groupmap.put(groupbyname.get(o.getKey()), (groupmap.getOrDefault(o.getKey(), 0) + o.getValue())));
ibc.getEntityGroupLimitsOffset().forEach((key, value) -> groupmap.put(groupbyname.get(key), (groupmap.getOrDefault(key, 0) + value)));
groupmap.forEach((v, limit) -> {
PanelItemBuilder pib = new PanelItemBuilder();
EntityType k = v.getTypes().iterator().next();
@ -124,7 +114,7 @@ public class LimitTab implements Tab {
if (E2M.containsKey(k)) {
m = E2M.get(k);
} else if (k.isAlive()) {
m = Material.valueOf(k.toString() + "_SPAWN_EGG");
m = Material.valueOf(k + "_SPAWN_EGG");
} else {
// Regular material
m = Material.valueOf(k.toString());
@ -149,7 +139,7 @@ public class LimitTab implements Tab {
Map<EntityType, Integer> map = new HashMap<>(addon.getSettings().getLimits());
// Merge in any permission-based limits
if (ibc != null) {
ibc.getEntityLimits().forEach(map::put);
map.putAll(ibc.getEntityLimits());
ibc.getEntityLimitsOffset().forEach((k,v) -> map.put(k, map.getOrDefault(k, 0) + v));
}
@ -161,7 +151,7 @@ public class LimitTab implements Tab {
if (E2M.containsKey(k)) {
m = E2M.get(k);
} else if (k.isAlive()) {
m = Material.valueOf(k.toString() + "_SPAWN_EGG");
m = Material.valueOf(k + "_SPAWN_EGG");
} else {
// Regular material
m = Material.valueOf(k.toString());

View File

@ -37,10 +37,9 @@ public class LimitsCalc {
private IslandBlockCount ibc;
private final Map<Material, AtomicInteger> blockCount;
private final User sender;
private final Set<Pair<Integer, Integer>> chunksToScan;
private int count;
private int chunksToScanCount;
private BentoBox plugin;
private final int chunksToScanCount;
private final BentoBox plugin;
/**
@ -56,13 +55,13 @@ public class LimitsCalc {
this.addon = addon;
this.island = instance.getIslands().getIsland(world, targetPlayer);
this.bll = addon.getBlockLimitListener();
this.ibc = bll.getIsland(island.getUniqueId());
this.ibc = bll.getIsland(Objects.requireNonNull(island).getUniqueId());
blockCount = new EnumMap<>(Material.class);
this.sender = sender;
this.world = world;
// Get chunks to scan
chunksToScan = getChunksToScan(island);
Set<Pair<Integer, Integer>> chunksToScan = getChunksToScan(island);
count = 0;
boolean isNether = plugin.getIWM().isNetherGenerate(world) && plugin.getIWM().isNetherIslands(world);

View File

@ -110,7 +110,7 @@ public class LimitsJoinPermCheckEvent extends BentoBoxEvent implements Cancellab
/**
* Ignore player's perms. This differs to canceling the event in that the IslandBlockCount will be used if given via
* {@link setIbc(IslandBlockCount ibc)}
* {@link #setIbc(IslandBlockCount ibc)}
* @param ignorePerms the ignorePerms to set
*/
public void setIgnorePerms(boolean ignorePerms) {

View File

@ -52,7 +52,7 @@ public class LimitsPermCheckEvent extends LimitsJoinPermCheckEvent {
/**
* @return the entityGroup
*/
public EntityGroup getEntityGroup() {
public @Nullable EntityGroup getEntityGroup() {
return entityGroup;
}
@ -60,7 +60,7 @@ public class LimitsPermCheckEvent extends LimitsJoinPermCheckEvent {
/**
* @param entityGroup the entityGroup to set
*/
public void setEntityGroup(EntityGroup entityGroup) {
public void setEntityGroup(@Nullable EntityGroup entityGroup) {
this.entityGroup = entityGroup;
}
@ -68,7 +68,7 @@ public class LimitsPermCheckEvent extends LimitsJoinPermCheckEvent {
/**
* @return the entityType
*/
public EntityType getEntityType() {
public @Nullable EntityType getEntityType() {
return entityType;
}
@ -76,7 +76,7 @@ public class LimitsPermCheckEvent extends LimitsJoinPermCheckEvent {
/**
* @param entityType the entityType to set
*/
public void setEntityType(EntityType entityType) {
public void setEntityType(@Nullable EntityType entityType) {
this.entityType = entityType;
}
@ -84,7 +84,7 @@ public class LimitsPermCheckEvent extends LimitsJoinPermCheckEvent {
/**
* @return the material
*/
public Material getMaterial() {
public @Nullable Material getMaterial() {
return material;
}
@ -92,7 +92,7 @@ public class LimitsPermCheckEvent extends LimitsJoinPermCheckEvent {
/**
* @param material the material to set
*/
public void setMaterial(Material material) {
public void setMaterial(@Nullable Material material) {
this.material = material;
}

View File

@ -164,7 +164,7 @@ public class BlockLimitsListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onTurtleEggBreak(PlayerInteractEvent e) {
if (e.getAction().equals(Action.PHYSICAL) && e.getClickedBlock().getType().equals(Material.TURTLE_EGG)) {
if (e.getAction().equals(Action.PHYSICAL) && e.getClickedBlock() != null && e.getClickedBlock().getType().equals(Material.TURTLE_EGG)) {
handleBreak(e, e.getClickedBlock());
}
}
@ -296,8 +296,8 @@ public class BlockLimitsListener implements Listener {
return Material.PLAYER_HEAD;
} else if (mat == Material.DRAGON_WALL_HEAD) {
return Material.DRAGON_HEAD;
} else if (mat != null && mat == Material.getMaterial("BAMBOO_SAPLING")) {
return Material.getMaterial("BAMBOO");
} else if (mat == Material.BAMBOO_SAPLING) {
return Material.BAMBOO;
} else if (mat == Material.PISTON_HEAD || mat == Material.MOVING_PISTON) {
TechnicalPiston tp = (TechnicalPiston) b;
if (tp.getType() == TechnicalPiston.Type.NORMAL) {
@ -314,7 +314,6 @@ public class BlockLimitsListener implements Listener {
*
* @param b - block
* @param add - true to add a block, false to remove
* @param changeTo - material this block will become
* @return limit amount if over limit, or -1 if no limitation
*/
private int process(Block b, boolean add) {
@ -415,14 +414,14 @@ public class BlockLimitsListener implements Listener {
// Merge limits
Map<Material, Integer> result = new EnumMap<>(Material.class);
// Default
defaultLimitMap.forEach(result::put);
result.putAll(defaultLimitMap);
// World
if (worldLimitMap.containsKey(w)) {
worldLimitMap.get(w).forEach(result::put);
result.putAll(worldLimitMap.get(w));
}
// Island
if (islandCountMap.containsKey(id)) {
islandCountMap.get(id).getBlockLimits().forEach(result::put);
result.putAll(islandCountMap.get(id).getBlockLimits());
}
return result;
}

View File

@ -52,7 +52,6 @@ public class EntityLimitListener implements Listener {
*/
public EntityLimitListener(Limits addon) {
this.addon = addon;
justSpawned.clear();
}
/**
@ -178,9 +177,7 @@ public class EntityLimitListener implements Listener {
if (island.isSpawn() || !res.hit()) {
// Allowed
if (async) {
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> {
l.getWorld().spawn(l, e.getClass(), entity -> preSpawn(entity, reason, l));
});
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> l.getWorld().spawn(l, e.getClass(), entity -> preSpawn(entity, reason, l)));
} // else do nothing
} else {
if (async) {
@ -199,21 +196,15 @@ public class EntityLimitListener implements Listener {
justSpawned.add(entity.getUniqueId());
// Check for entities that need cleanup
switch (reason) {
case BUILD_IRONGOLEM:
detectIronGolem(l);
break;
case BUILD_SNOWMAN:
detectSnowman(l);
break;
case BUILD_WITHER:
detectWither(l);
// Create explosion
l.getWorld().createExplosion(l, 7F, true, true, entity);
break;
default:
break;
case BUILD_IRONGOLEM -> detectIronGolem(l);
case BUILD_SNOWMAN -> detectSnowman(l);
case BUILD_WITHER -> {
detectWither(l);
// Create explosion
l.getWorld().createExplosion(l, 7F, true, true, entity);
}
default -> {
}
}
}
@ -410,7 +401,7 @@ public class EntityLimitListener implements Listener {
}
// Merge in any permission-based limits
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
Map<String, EntityGroup> groupbyname = groupsLimits.keySet().stream().collect(Collectors.toMap(e -> e.getName(), e -> e));
Map<String, EntityGroup> groupbyname = groupsLimits.keySet().stream().collect(Collectors.toMap(EntityGroup::getName, e -> e));
addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityGroupLimits().entrySet().stream()
.filter(e -> groupbyname.containsKey(e.getKey()))
.forEach(e -> groupsLimits.put(groupbyname.get(e.getKey()), e.getValue()));
@ -428,7 +419,7 @@ public class EntityLimitListener implements Listener {
return new AtLimitResult();
}
class AtLimitResult {
static class AtLimitResult {
private Map.Entry<EntityType, Integer> typelimit;
private Map.Entry<EntityGroup, Integer> grouplimit;

View File

@ -80,10 +80,9 @@ public class EntityLimitsDO implements DataObject {
if (obj == null) {
return false;
}
if (!(obj instanceof EntityLimitsDO)) {
if (!(obj instanceof EntityLimitsDO other)) {
return false;
}
EntityLimitsDO other = (EntityLimitsDO) obj;
if (uniqueId == null) {
return other.uniqueId == null;
} else return uniqueId.equals(other.uniqueId);

View File

@ -20,10 +20,10 @@ import world.bentobox.bentobox.database.objects.Table;
public class IslandBlockCount implements DataObject {
@Expose
private String uniqueId = "";
private String uniqueId;
@Expose
private String gameMode = "";
private String gameMode;
@Expose
private Map<Material, Integer> blockCounts = new EnumMap<>(Material.class);

View File

@ -1,7 +1,7 @@
package bentobox.addon.limits.listeners;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@ -11,12 +11,6 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
@ -86,7 +80,7 @@ public class JoinListenerTest {
when(addon.getGameModePermPrefix(any())).thenReturn("bskyblock.");
when(addon.getSettings()).thenReturn(settings);
// Settings
when(settings.getGroupLimitDefinitions()).thenReturn(new ArrayList<>(Arrays.asList(new Settings.EntityGroup("friendly", new HashSet<>(), -1))));
when(settings.getGroupLimitDefinitions()).thenReturn(new ArrayList<>(List.of(new Settings.EntityGroup("friendly", new HashSet<>(), -1))));
// Island Manager
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(island.getUniqueId()).thenReturn("unique_id");

View File

@ -55,7 +55,7 @@ public class LimitTabTest {
private Settings settings;
@Before
public void setUp() throws Exception {
public void setUp() {
// Island
when(island.getWorld()).thenReturn(world);
// Addon
@ -78,7 +78,7 @@ public class LimitTabTest {
}
@After
public void tearDown() throws Exception {
public void tearDown() {
}
@Test

View File

@ -45,7 +45,6 @@ public class EntityLimitListenerTest {
private LivingEntity ent;
@Mock
private BlockLimitsListener bll;
private Settings settings;
@Mock
private World world;
private List<Entity> collection;
@ -71,7 +70,7 @@ public class EntityLimitListenerTest {
config.load("src/main/resources/config.yml");
// Settings
when(addon.getConfig()).thenReturn(config);
settings = new Settings(addon);
Settings settings = new Settings(addon);
when(addon.getSettings()).thenReturn(settings);
// World
@ -87,7 +86,7 @@ public class EntityLimitListenerTest {
}
@After
public void tearDown() throws Exception {
public void tearDown() {
}
/**