Implement suggested improvements.

* Use singleton pattern for DefaultContentFilter with getInstance method.
* Have a default SendHandler.
* Don't need streams for small dataset.
* Private WorldListContentParser class to improve readability.
This commit is contained in:
Ben Woo 2021-08-17 16:29:31 +08:00
parent 562eed8255
commit 1211dfb057
6 changed files with 79 additions and 40 deletions

View File

@ -22,7 +22,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -51,7 +50,7 @@ public class GamerulesCommand extends MultiverseCommand {
// We NEED a world from the command line // We NEED a world from the command line
Player p; Player p;
World world; World world;
ContentFilter filter = DefaultContentFilter.INSTANCE; ContentFilter filter = DefaultContentFilter.getInstance();
if (sender instanceof Player) { if (sender instanceof Player) {
p = (Player) sender; p = (Player) sender;
@ -103,10 +102,10 @@ public class GamerulesCommand extends MultiverseCommand {
private Map<String, Object> getGameRuleMap(World world) { private Map<String, Object> getGameRuleMap(World world) {
Map<String, Object> gameRuleMap = new HashMap<>(); Map<String, Object> gameRuleMap = new HashMap<>();
Arrays.stream(GameRule.values()).forEach(rule -> { for (GameRule<?> rule : GameRule.values()) {
Object value = world.getGameRuleValue(rule); Object value = world.getGameRuleValue(rule);
gameRuleMap.put(rule.getName(), value); gameRuleMap.put(rule.getName(), value);
}); }
return gameRuleMap; return gameRuleMap;
} }
} }

View File

@ -20,6 +20,7 @@ import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
@ -41,7 +42,7 @@ public class ListCommand extends MultiverseCommand {
@Override @Override
public void runCommand(CommandSender sender, List<String> args) { public void runCommand(CommandSender sender, List<String> args) {
ContentFilter filter = DefaultContentFilter.INSTANCE; ContentFilter filter = DefaultContentFilter.getInstance();
int page = 1; int page = 1;
// Either page or filter. // Either page or filter.
@ -72,27 +73,28 @@ public class ListCommand extends MultiverseCommand {
.send(sender); .send(sender);
} }
private ContentParser newWorldListContentParser() { private class WorldListContentParser implements ContentParser {
return (sender, content) -> {
@Override
public void parse(@NotNull CommandSender sender, @NotNull List<String> content) {
Player player = (sender instanceof Player) ? (Player) sender : null; Player player = (sender instanceof Player) ? (Player) sender : null;
this.plugin.getMVWorldManager().getMVWorlds().stream() plugin.getMVWorldManager().getMVWorlds().stream()
.filter(world -> player == null || plugin.getMVPerms().canEnterWorld(player, world)) .filter(world -> player == null || plugin.getMVPerms().canEnterWorld(player, world))
.filter(world -> canSeeWorld(player, world)) .filter(world -> canSeeWorld(player, world))
.map(world -> hiddenText(world) + world.getColoredWorldString() + " - " + parseColouredEnvironment(world.getEnvironment())) .map(world -> hiddenText(world) + world.getColoredWorldString() + " - " + parseColouredEnvironment(world.getEnvironment()))
.forEach(content::add); .forEach(content::add);
this.plugin.getMVWorldManager().getUnloadedWorlds().stream() plugin.getMVWorldManager().getUnloadedWorlds().stream()
.filter(world -> plugin.getMVPerms().hasPermission(sender, "multiverse.access." + world, true)) .filter(world -> plugin.getMVPerms().hasPermission(sender, "multiverse.access." + world, true))
.map(world -> ChatColor.GRAY + world + " - UNLOADED") .map(world -> ChatColor.GRAY + world + " - UNLOADED")
.forEach(content::add); .forEach(content::add);
};
} }
private boolean canSeeWorld(Player player, MultiverseWorld world) { private boolean canSeeWorld(Player player, MultiverseWorld world) {
return !world.isHidden() return !world.isHidden()
|| player == null || player == null
|| this.plugin.getMVPerms().hasPermission(player, "multiverse.core.modify", true); || plugin.getMVPerms().hasPermission(player, "multiverse.core.modify", true);
} }
private String hiddenText(MultiverseWorld world) { private String hiddenText(MultiverseWorld world) {
@ -115,3 +117,4 @@ public class ListCommand extends MultiverseCommand {
return color + env.toString(); return color + env.toString();
} }
} }
}

View File

@ -1,5 +1,6 @@
package com.onarandombox.MultiverseCore.display; package com.onarandombox.MultiverseCore.display;
import com.onarandombox.MultiverseCore.display.handlers.DefaultSendHandler;
import com.onarandombox.MultiverseCore.display.handlers.SendHandler; import com.onarandombox.MultiverseCore.display.handlers.SendHandler;
import com.onarandombox.MultiverseCore.display.parsers.ContentParser; import com.onarandombox.MultiverseCore.display.parsers.ContentParser;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -25,7 +26,7 @@ public class ContentDisplay {
} }
private final List<ContentParser> contentParsers = new ArrayList<>(); private final List<ContentParser> contentParsers = new ArrayList<>();
private SendHandler sendHandler; private SendHandler sendHandler = DefaultSendHandler.getInstance();
public ContentDisplay() { public ContentDisplay() {
} }

View File

@ -5,9 +5,16 @@ package com.onarandombox.MultiverseCore.display.filters;
*/ */
public class DefaultContentFilter implements ContentFilter { public class DefaultContentFilter implements ContentFilter {
public static DefaultContentFilter INSTANCE = new DefaultContentFilter(); public static DefaultContentFilter instance;
public DefaultContentFilter() { public static DefaultContentFilter getInstance() {
if (instance == null) {
instance = new DefaultContentFilter();
}
return instance;
}
private DefaultContentFilter() {
} }
/** /**

View File

@ -18,7 +18,7 @@ import java.util.stream.Collectors;
public abstract class BaseSendHandler<T extends BaseSendHandler<?>> implements SendHandler { public abstract class BaseSendHandler<T extends BaseSendHandler<?>> implements SendHandler {
protected String header = ""; protected String header = "";
protected ContentFilter filter = DefaultContentFilter.INSTANCE; protected ContentFilter filter = DefaultContentFilter.getInstance();
/** /**
* {@inheritDoc} * {@inheritDoc}

View File

@ -0,0 +1,29 @@
package com.onarandombox.MultiverseCore.display.handlers;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class DefaultSendHandler implements SendHandler {
private static DefaultSendHandler instance;
public static DefaultSendHandler getInstance() {
if (instance == null) {
instance = new DefaultSendHandler();
}
return instance;
}
private DefaultSendHandler() {
}
/**
* {@inheritDoc}
*/
@Override
public void send(@NotNull CommandSender sender, @NotNull List<String> content) {
sender.sendMessage(content.toArray(new String[0]));
}
}