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

View File

@ -20,6 +20,7 @@ import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionDefault;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@ -41,7 +42,7 @@ public class ListCommand extends MultiverseCommand {
@Override
public void runCommand(CommandSender sender, List<String> args) {
ContentFilter filter = DefaultContentFilter.INSTANCE;
ContentFilter filter = DefaultContentFilter.getInstance();
int page = 1;
// Either page or filter.
@ -64,7 +65,7 @@ public class ListCommand extends MultiverseCommand {
}
ContentDisplay.create()
.addContentParser(newWorldListContentParser())
.addContentParser(new WorldListContentParser())
.withSendHandler(PagedSendHandler.create()
.withHeader("%s====[ Multiverse World List ]====", ChatColor.GOLD)
.withFilter(filter)
@ -72,46 +73,48 @@ public class ListCommand extends MultiverseCommand {
.send(sender);
}
private ContentParser newWorldListContentParser() {
return (sender, content) -> {
private class WorldListContentParser implements ContentParser {
@Override
public void parse(@NotNull CommandSender sender, @NotNull List<String> content) {
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 -> canSeeWorld(player, world))
.map(world -> hiddenText(world) + world.getColoredWorldString() + " - " + parseColouredEnvironment(world.getEnvironment()))
.forEach(content::add);
this.plugin.getMVWorldManager().getUnloadedWorlds().stream()
plugin.getMVWorldManager().getUnloadedWorlds().stream()
.filter(world -> plugin.getMVPerms().hasPermission(sender, "multiverse.access." + world, true))
.map(world -> ChatColor.GRAY + world + " - UNLOADED")
.forEach(content::add);
};
}
private boolean canSeeWorld(Player player, MultiverseWorld world) {
return !world.isHidden()
|| player == null
|| this.plugin.getMVPerms().hasPermission(player, "multiverse.core.modify", true);
}
private String hiddenText(MultiverseWorld world) {
return (world.isHidden()) ? String.format("%s[H] ", ChatColor.GRAY) : "";
}
private String parseColouredEnvironment(World.Environment env) {
ChatColor color = ChatColor.GOLD;
switch (env) {
case NETHER:
color = ChatColor.RED;
break;
case NORMAL:
color = ChatColor.GREEN;
break;
case THE_END:
color = ChatColor.AQUA;
break;
}
return color + env.toString();
private boolean canSeeWorld(Player player, MultiverseWorld world) {
return !world.isHidden()
|| player == null
|| plugin.getMVPerms().hasPermission(player, "multiverse.core.modify", true);
}
private String hiddenText(MultiverseWorld world) {
return (world.isHidden()) ? String.format("%s[H] ", ChatColor.GRAY) : "";
}
private String parseColouredEnvironment(World.Environment env) {
ChatColor color = ChatColor.GOLD;
switch (env) {
case NETHER:
color = ChatColor.RED;
break;
case NORMAL:
color = ChatColor.GREEN;
break;
case THE_END:
color = ChatColor.AQUA;
break;
}
return color + env.toString();
}
}
}

View File

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

View File

@ -5,9 +5,16 @@ package com.onarandombox.MultiverseCore.display.filters;
*/
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 {
protected String header = "";
protected ContentFilter filter = DefaultContentFilter.INSTANCE;
protected ContentFilter filter = DefaultContentFilter.getInstance();
/**
* {@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]));
}
}