Skip to content
This repository was archived by the owner on Nov 25, 2019. It is now read-only.

Commit

Permalink
Improve polls some more
Browse files Browse the repository at this point in the history
  • Loading branch information
cswhite2000 committed Jan 25, 2018
1 parent 3c74a30 commit f5c8438
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ rating.sameRating = You have already rated this map a {0}.

huddle.globalChatDisabled = Global chat is disabled during team huddle

poll.vote.value.invalid = Accepted values: yes|no
poll.vote.invalidValue = Accepted values: yes|no
poll.noPollRunning = There is currently no poll running!
poll.kick.exempt = You cannot poll kick this player!
poll.disabled = Polls are disabled on this server!
Expand Down
12 changes: 0 additions & 12 deletions PGM/src/main/java/tc/oc/pgm/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ public static double setNextTokenChange() {

}

public static class Poll {
public static Path getPollAbleMapPath() {
Path pollPath = Paths.get(getConfiguration().getString("poll.maps.path", "default.txt"));
if(!pollPath.isAbsolute()) pollPath = PGM.getMatchManager().getPluginDataFolder().resolve(pollPath);
return pollPath;
}

public static boolean enabled() {
return getConfiguration().getBoolean("poll.enabled", true);
}
}

public static class Broadcast {
public static boolean title() {
return getConfiguration().getBoolean("broadcast.title", true);
Expand Down
6 changes: 3 additions & 3 deletions PGM/src/main/java/tc/oc/pgm/polls/Poll.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

public abstract class Poll implements Runnable {

public static String boldAqua = ChatColor.BOLD + "" + ChatColor.AQUA;
public static String normalize = ChatColor.RESET + "" + ChatColor.DARK_AQUA;
public static String separator = ChatColor.RESET + " | ";
public static final String boldAqua = ChatColor.BOLD + "" + ChatColor.AQUA;
public static final String normalize = ChatColor.RESET + "" + ChatColor.DARK_AQUA;
public static final String separator = ChatColor.RESET + " | ";

protected final PollManager pollManager;
protected final PlayerId initiator;
Expand Down
28 changes: 18 additions & 10 deletions PGM/src/main/java/tc/oc/pgm/polls/PollBlacklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import tc.oc.commons.core.logging.Loggers;
import tc.oc.commons.core.plugin.PluginFacet;
import tc.oc.pgm.Config;
import tc.oc.pgm.PGM;
import tc.oc.pgm.map.MapId;
import tc.oc.pgm.map.MapLibrary;
Expand All @@ -17,16 +17,21 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;

@Singleton
public class PollBlacklist implements PluginFacet {

private List<MapId> blacklistedMaps = new ArrayList<>();
private List<PGMMap> blacklistedMaps = new ArrayList<>();

private final MapLibrary mapLibrary;
private final PollConfig pollConfig;
private final Logger logger;

@Inject PollBlacklist(MapLibrary mapLibrary) {
@Inject PollBlacklist(MapLibrary mapLibrary, PollConfig pollConfig, Loggers loggers) {
this.mapLibrary = mapLibrary;
this.pollConfig = pollConfig;
this.logger = loggers.get(getClass());
}

@Override
Expand All @@ -35,34 +40,37 @@ public void enable() {
}

public void loadPollBlacklist() {
Path filepath = Config.Poll.getPollAbleMapPath();
Path filepath = pollConfig.getPollBlacklistPath();
if (filepath == null) return;
List<String> lines = null;
try {
lines = Files.readAllLines(filepath, Charsets.UTF_8);
} catch (IOException e) {
PGM.get().getLogger().severe("Error in reading poll blacklist from file!");
logger.severe("Error in reading poll blacklist from file!");
}
if (lines == null) return;
ImmutableList.Builder<MapId> maps = ImmutableList.builder();
ImmutableList.Builder<PGMMap> maps = ImmutableList.builder();
for(String line : lines) {
if (line.contains("#")) {
line = line.substring(0, line.indexOf("#"));
}

line = line.trim();
if(line.isEmpty()) {
continue;
}

Optional<PGMMap> map = mapLibrary.getMapByNameOrId(line);
if(map.isPresent()) {
maps.add(map.get().getId());
maps.add(map.get());
} else {
mapLibrary.getLogger().severe("Unknown map '" + line
+ "' when parsing " + filepath.toString());
logger.warning("Unknown map '" + line + "' when parsing " + filepath.toString());
}
}
this.blacklistedMaps = maps.build();
}

public boolean isBlacklisted(PGMMap map) {
return blacklistedMaps.contains(map.getId());
return blacklistedMaps.contains(map);
}
}
35 changes: 35 additions & 0 deletions PGM/src/main/java/tc/oc/pgm/polls/PollConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tc.oc.pgm.polls;

import org.bukkit.configuration.ConfigurationSection;
import tc.oc.pgm.PGM;
import tc.oc.pgm.match.MatchManager;

import javax.inject.Inject;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.google.common.base.Preconditions.checkNotNull;

public class PollConfig {

private final ConfigurationSection config;
private final MatchManager matchManager;

@Inject PollConfig(ConfigurationSection config, MatchManager matchManager) {
this.config = checkNotNull(config.getConfigurationSection("poll"));
this.matchManager = matchManager;
}

public Path getPollBlacklistPath() {
Path pollPath = Paths.get(config.getString("maps.path", "default.txt"));
if(!pollPath.isAbsolute()) {
pollPath = matchManager.getPluginDataFolder().resolve(pollPath);
}
return pollPath;
}

public boolean enabled() {
return config.getBoolean("enabled", true);
}

}
3 changes: 1 addition & 2 deletions PGM/src/main/java/tc/oc/pgm/polls/commands/PollCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.sk89q.minecraft.util.commands.NestedCommand;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import tc.oc.api.docs.PlayerId;
import tc.oc.commons.bukkit.chat.PlayerComponent;
import tc.oc.commons.bukkit.nick.IdentityProvider;
Expand Down Expand Up @@ -63,7 +62,7 @@ public void vote(CommandContext args, CommandSender sender) throws CommandExcept
currentPoll.voteAgainst(voter);
sender.sendMessage(new Component(ChatColor.RED).translate("poll.vote.against"));
} else {
throw new TranslatableCommandException("poll.vote.value.invalid");
throw new TranslatableCommandException("poll.vote.invalidValue");
}
} else {
throw new TranslatableCommandException("poll.noPollRunning");
Expand Down
33 changes: 17 additions & 16 deletions PGM/src/main/java/tc/oc/pgm/polls/commands/PollSubCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@
import tc.oc.commons.core.commands.TranslatableCommandException;
import tc.oc.commons.core.formatting.StringUtils;
import tc.oc.commons.core.restart.RestartManager;
import tc.oc.pgm.Config;
import tc.oc.pgm.commands.CommandUtils;
import tc.oc.pgm.map.PGMMap;
import tc.oc.pgm.match.MatchManager;
import tc.oc.pgm.mutation.Mutation;
import tc.oc.pgm.mutation.MutationQueue;
import tc.oc.pgm.mutation.command.MutationCommands;
import tc.oc.pgm.polls.PollBlacklist;
import tc.oc.pgm.polls.PollConfig;
import tc.oc.pgm.polls.types.PollCustom;
import tc.oc.pgm.polls.types.PollKick;
import tc.oc.pgm.polls.PollManager;
import tc.oc.pgm.polls.types.PollMutation;
import tc.oc.pgm.polls.types.PollNextMap;

import javax.inject.Inject;
import java.util.Collection;
import java.util.List;

public class PollSubCommands {
Expand All @@ -44,19 +42,21 @@ public class PollSubCommands {
private final UserStore userStore;
private final OnlinePlayers onlinePlayers;
private final MatchManager matchManager;
private final PollConfig pollConfig;

@Inject
PollSubCommands(RestartManager restartManager,
MutationQueue mutationQueue,
PollManager pollManager,
PollCustom.Factory pollCustomFactory,
PollNextMap.Factory pollMapFactory,
PollMutation.Factory pollMutationFactory,
PollKick.Factory pollKickFactory,
PollBlacklist pollBlacklist,
UserStore userStore,
OnlinePlayers onlinePlayers,
MatchManager matchManager) {
MutationQueue mutationQueue,
PollManager pollManager,
PollCustom.Factory pollCustomFactory,
PollNextMap.Factory pollMapFactory,
PollMutation.Factory pollMutationFactory,
PollKick.Factory pollKickFactory,
PollBlacklist pollBlacklist,
UserStore userStore,
OnlinePlayers onlinePlayers,
MatchManager matchManager,
PollConfig pollConfig) {
this.restartManager = restartManager;
this.mutationQueue = mutationQueue;
this.pollManager = pollManager;
Expand All @@ -68,6 +68,7 @@ public class PollSubCommands {
this.userStore = userStore;
this.onlinePlayers = onlinePlayers;
this.matchManager = matchManager;
this.pollConfig = pollConfig;
}


Expand Down Expand Up @@ -105,7 +106,7 @@ public List<String> pollNext(CommandContext args, CommandSender sender) throws C
return CommandUtils.completeMapName(mapName);
}

if(!Config.Poll.enabled()) {
if(!pollConfig.enabled()) {
throw new TranslatableCommandException("poll.disabled");
}

Expand Down Expand Up @@ -147,7 +148,7 @@ public List<String> pollMutation(CommandContext args, CommandSender sender) thro
return StringUtils.complete(args.getSuggestionContext().getPrefix(), mutationQueue.mutationsAvailable().stream().map(mutation -> mutation.name().toLowerCase()));
}

if(!Config.Poll.enabled()) {
if(!pollConfig.enabled()) {
throw new TranslatableCommandException("poll.disabled");
}

Expand All @@ -161,7 +162,7 @@ public List<String> pollMutation(CommandContext args, CommandSender sender) thro
if(mutation == null) {
throw new TranslatableCommandException("command.mutation.error.find", mutationString);
} else if(mutationQueue.mutations().contains(mutation)) {
throw new TranslatableCommandException(true ? "command.mutation.error.enabled" : "command.mutation.error.disabled", mutation.getComponent(net.md_5.bungee.api.ChatColor.RED));
throw new TranslatableCommandException("command.mutation.error.enabled", mutation.getComponent(net.md_5.bungee.api.ChatColor.RED));
} else if(!mutation.isPollable() && !sender.hasPermission("poll.mutation.override")) {
throw new TranslatableCommandException("command.mutation.error.illegal", mutationString);
}
Expand Down

0 comments on commit f5c8438

Please sign in to comment.