Skip to content
This repository has been 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 01173c4
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 57 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);
}

}
2 changes: 1 addition & 1 deletion PGM/src/main/java/tc/oc/pgm/polls/PollListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import tc.oc.commons.core.plugin.PluginFacet;
import tc.oc.pgm.polls.event.PollEndEvent;

public class PollListener implements PluginFacet, Listener {
public class PollListener implements Listener {

private final Audiences audiences;

Expand Down
8 changes: 6 additions & 2 deletions PGM/src/main/java/tc/oc/pgm/polls/PollManifest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tc.oc.pgm.polls;

import com.google.inject.assistedinject.FactoryModuleBuilder;
import tc.oc.commons.core.commands.CommandBinder;
import tc.oc.commons.core.inject.HybridManifest;
import tc.oc.commons.core.plugin.PluginFacetBinder;
import tc.oc.minecraft.api.event.ListenerBinder;
import tc.oc.pgm.polls.commands.PollCommands;
import tc.oc.pgm.polls.types.PollCustom;
import tc.oc.pgm.polls.types.PollKick;
Expand All @@ -21,9 +23,11 @@ protected void configure() {
install(fmb.build(PollMutation.Factory.class));

final PluginFacetBinder facets = new PluginFacetBinder(binder());
facets.register(PollCommands.class);
facets.register(PollManager.class);
facets.register(PollListener.class);
facets.register(PollBlacklist.class);

new CommandBinder(binder()).register(PollCommands.class);

new ListenerBinder(binder()).bindListener().to(PollListener.class);
}
}
6 changes: 2 additions & 4 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,13 +7,11 @@
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;
import tc.oc.commons.core.chat.Audiences;
import tc.oc.commons.core.chat.Component;
import tc.oc.commons.core.commands.Commands;
import tc.oc.commons.core.commands.TranslatableCommandException;
import tc.oc.pgm.commands.CommandUtils;
import tc.oc.pgm.polls.Poll;
Expand All @@ -22,7 +20,7 @@

import javax.inject.Inject;

public class PollCommands implements Commands {
public class PollCommands {

private final PollManager pollManager;
private final IdentityProvider identityProvider;
Expand Down Expand Up @@ -63,7 +61,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
14 changes: 6 additions & 8 deletions PGM/src/main/java/tc/oc/pgm/polls/types/PollKick.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,30 @@ public interface Factory {
PollKick create(PlayerId initiator, Player player);
}

private final PlayerId player;
private final PlayerId playerId;
private final IdentityProvider identityProvider;
private final PunishmentCreator punishmentCreator;
private final UserStore userStore;

@AssistedInject PollKick(@Assisted PlayerId initiator, @Assisted Player player, PollManager pollManager, Audiences audiences, IdentityProvider identityProvider, PunishmentCreator punishmentCreator, UserStore userStore) {
super(pollManager, initiator, audiences);
this.identityProvider = identityProvider;
this.punishmentCreator = punishmentCreator;
this.userStore = userStore;
this.player = userStore.playerId(player);
this.playerId = userStore.playerId(player);
}

@Override
public void executeAction() {
punishmentCreator.create(null, player, "The poll to kick " + player.username() + " succeeded", PunishmentDoc.Type.KICK, null, false, false, true);
audiences.localServer().sendMessage(new Component(ChatColor.DARK_AQUA).translate("poll.kick.success", new PlayerComponent(identityProvider.currentIdentity(player))));
punishmentCreator.create(null, playerId, "The poll to kick " + playerId.username() + " succeeded", PunishmentDoc.Type.KICK, null, false, false, true);
audiences.localServer().sendMessage(new Component(ChatColor.DARK_AQUA).translate("poll.kick.success", new PlayerComponent(identityProvider.currentIdentity(playerId))));
}

@Override
public String getActionString() {
return normalize + "Kick: " + boldAqua + this.player.username();
return normalize + "Kick: " + boldAqua + this.playerId.username();
}

@Override
public String getDescriptionMessage() {
return "to kick " + boldAqua + this.player.username();
return "to kick " + boldAqua + this.playerId.username();
}
}

0 comments on commit 01173c4

Please sign in to comment.