Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add votes.one-vote-per-day option #140

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ buildNumber.properties
.classpath
.project
.settings/*

# IntelliJ
.idea/
*.iml
56 changes: 35 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

<groupId>io.minimum.minecraft</groupId>
<artifactId>SuperbVote</artifactId>
<version>0.5.5</version>
<version>0.6.1-CORP</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>

<build>
<resources>
Expand All @@ -16,19 +22,10 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<version>3.6.0</version>
<configuration>
<relocations>
<relocation>
Expand All @@ -40,6 +37,13 @@
<shadedPattern>io.minimum.minecraft.superbvote.slf4j</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Built-By>maven</Built-By>
</manifestEntries>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
Expand All @@ -59,7 +63,7 @@
</repository>
<repository>
<id>clip-repo</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>bintray-nuvotifier-repo</id>
Expand All @@ -75,38 +79,48 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14-R0.1-SNAPSHOT</version>
<version>1.19-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<version>1.18.36</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.NuVotifier.NuVotifier</groupId>
<artifactId>nuvotifier-bukkit</artifactId>
<version>v2.6.0</version>
<groupId>com.github.NuVotifier</groupId>
<artifactId>NuVotifier</artifactId>
<version>2.7.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.6</version>
<version>5.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.8.2</version>
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<version>1.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>
</project>
</project>
15 changes: 11 additions & 4 deletions src/main/java/io/minimum/minecraft/superbvote/SuperbVote.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public void onEnable() {
}

getCommand("superbvote").setExecutor(new SuperbVoteCommand());
getCommand("vote").setExecutor(configuration.getVoteCommand());
getCommand("votestreak").setExecutor(configuration.getVoteStreakCommand());
registerCommands();

getServer().getPluginManager().registerEvents(new SuperbVoteListener(), this);
getServer().getPluginManager().registerEvents(new TopPlayerSignListener(), this);
Expand Down Expand Up @@ -124,8 +123,7 @@ public void reloadPlugin() {
scoreboardHandler.reload();
voteServiceCooldown = new VoteServiceCooldown(getConfig().getInt("votes.cooldown-per-service", 3600));
getServer().getScheduler().runTaskAsynchronously(this, getScoreboardHandler()::doPopulate);
getCommand("vote").setExecutor(configuration.getVoteCommand());
getCommand("votestreak").setExecutor(configuration.getVoteStreakCommand());
registerCommands();

if (voteReminderTask != null) {
voteReminderTask.cancel();
Expand All @@ -141,4 +139,13 @@ public void reloadPlugin() {
public ClassLoader _exposeClassLoader() {
return getClassLoader();
}

private void registerCommands() {
if (configuration.getVoteCommand() != null) {
getCommand("vote").setExecutor(configuration.getVoteCommand());
}
if (configuration.getVoteStreakCommand() != null) {
getCommand("votestreak").setExecutor(configuration.getVoteStreakCommand());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ public VoteStorage initializeVoteStorage() throws IOException {
file = "votes.json";
SuperbVote.getPlugin().getLogger().info("No file found in configuration, using 'votes.json'.");
}
if (configuration.getBoolean("votes.one-vote-per-day")) {
SuperbVote.getPlugin().getLogger()
.warning("votes.one-vote-per-day=true is not fully supported with JSON storage! " +
"Use mysql instead if you need this feature to work properly.");
}
return new JsonVoteStorage(new File(SuperbVote.getPlugin().getDataFolder(), file));
case "mysql":
String host = configuration.getString("storage.mysql.host", "localhost");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private VotingFile migrateOldVersion(Map<UUID, Integer> votes) throws IOExceptio
}

@Override
public void addVote(Vote vote) {
public void addVote(Vote vote, PlayerVotes playerVotes) {
Preconditions.checkNotNull(vote, "vote");
rwl.writeLock().lock();
try {
Expand Down Expand Up @@ -143,8 +143,10 @@ public PlayerVotes getVotes(UUID player) {
rwl.readLock().lock();
try {
PlayerRecord pr = voteCounts.get(player);
return new PlayerVotes(player, pr != null ? pr.lastKnownUsername : null, pr == null ? 0 : pr.votes,
PlayerVotes.Type.CURRENT);
if (pr == null) {
return new PlayerVotes(player, null, 0, Collections.emptyMap(), PlayerVotes.Type.CURRENT);
}
return new PlayerVotes(player, pr.lastKnownUsername, pr.votes, Collections.emptyMap(), PlayerVotes.Type.CURRENT);
} finally {
rwl.readLock().unlock();
}
Expand All @@ -160,7 +162,9 @@ public List<PlayerVotes> getTopVoters(int amount, int page) {
.sorted(Collections.reverseOrder(Comparator.comparing(Map.Entry::getValue)))
.skip(skip)
.limit(amount)
.map(e -> new PlayerVotes(e.getKey(), e.getValue().lastKnownUsername, e.getValue().votes, PlayerVotes.Type.CURRENT))
.map(e -> new PlayerVotes(e.getKey(), e.getValue().lastKnownUsername, e.getValue().votes,
Collections.emptyMap(),
PlayerVotes.Type.CURRENT))
.collect(Collectors.toList());
} finally {
rwl.readLock().unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class MysqlVoteStorage implements ExtendedVoteStorage {
private static final int TABLE_VERSION_2 = 2;
private static final int TABLE_VERSION_3 = 3;
private static final int TABLE_VERSION_4 = 4;
private static final int TABLE_VERSION_CURRENT = TABLE_VERSION_4;
private static final int TABLE_VERSION_5 = 5;
private static final int TABLE_VERSION_CURRENT = TABLE_VERSION_5;

private final HikariPool dbPool;
private final String tableName, streaksTableName;
Expand All @@ -47,7 +48,7 @@ public void initialize() {
try (ResultSet t = connection.getMetaData().getTables(null, null, tableName, null)) {
if (!t.next()) {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("CREATE TABLE " + tableName + " (uuid VARCHAR(36) PRIMARY KEY NOT NULL, last_name VARCHAR(16), votes INT NOT NULL, last_vote TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)");
statement.executeUpdate("CREATE TABLE " + tableName + " (uuid VARCHAR(36) PRIMARY KEY NOT NULL, last_name VARCHAR(16), votes INT NOT NULL, last_vote TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, last_votes VARCHAR(256))");
// This may speed up leaderboards
statement.executeUpdate("CREATE INDEX uuid_votes_idx ON " + tableName + " (uuid, votes)");
}
Expand All @@ -74,6 +75,12 @@ public void initialize() {
statement.executeUpdate("ALTER TABLE " + tableName + " MODIFY votes int(11) NOT NULL DEFAULT 0");
}
}
if (ver < TABLE_VERSION_5) {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN last_votes VARCHAR(256)");
}
isUpdated = true;
}
}
}
}
Expand Down Expand Up @@ -106,28 +113,34 @@ public void initialize() {
}

@Override
public void addVote(Vote vote) {
public void addVote(Vote vote, PlayerVotes playerVotes) {
if (readOnly)
return;

Preconditions.checkNotNull(vote, "vote");
try (Connection connection = dbPool.getConnection()) {
Timestamp voteReceivedTimestamp = new Timestamp(vote.getReceived()
.getTime());
if (vote.getName() != null) {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + tableName + " (uuid, last_name, votes, last_vote) VALUES (?, ?, 1, ?)" +
" ON DUPLICATE KEY UPDATE votes = votes + 1, last_name = ?, last_vote = ?")) {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + tableName + " (uuid, last_name, votes, last_vote, last_votes) VALUES (?, ?, 1, ?, ?)" +
" ON DUPLICATE KEY UPDATE votes = votes + 1, last_name = ?, last_vote = ?, last_votes = ?")) {
statement.setString(1, vote.getUuid().toString());
statement.setString(2, vote.getName());
statement.setTimestamp(3, new Timestamp(vote.getReceived().getTime()));
statement.setString(4, vote.getName());
statement.setTimestamp(5, new Timestamp(vote.getReceived().getTime()));
statement.setTimestamp(3, voteReceivedTimestamp);
statement.setString(4, playerVotes.getSerializedLastVotes());
statement.setString(5, vote.getName());
statement.setTimestamp(6, voteReceivedTimestamp);
statement.setString(7, playerVotes.getSerializedLastVotes());
statement.executeUpdate();
}
} else {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + tableName + " (uuid, last_name, votes, last_vote) VALUES (?, NULL, 1, ?)" +
" ON DUPLICATE KEY UPDATE votes = votes + 1, last_vote = ?")) {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + tableName + " (uuid, last_name, votes, last_vote, last_votes) VALUES (?, NULL, 1, ?, ?)" +
" ON DUPLICATE KEY UPDATE votes = votes + 1, last_vote = ?, last_votes = ?")) {
statement.setString(1, vote.getUuid().toString());
statement.setTimestamp(2, new Timestamp(vote.getReceived().getTime()));
statement.setTimestamp(3, new Timestamp(vote.getReceived().getTime()));
statement.setTimestamp(2, voteReceivedTimestamp);
statement.setString(3, playerVotes.getSerializedLastVotes());
statement.setTimestamp(4, voteReceivedTimestamp);
statement.setString(5, playerVotes.getSerializedLastVotes());
statement.executeUpdate();
}
}
Expand Down Expand Up @@ -184,7 +197,7 @@ public void setVotes(UUID player, int votes, long ts) {

Preconditions.checkNotNull(player, "player");
try (Connection connection = dbPool.getConnection()) {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + tableName + " (uuid, votes, last_vote) VALUES (?, ?, ?)" +
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + tableName + " (uuid, votes, last_vote, last_votes) VALUES (?, ?, ?, NULL)" +
" ON DUPLICATE KEY UPDATE votes = ?, last_vote = ?")) {
statement.setString(1, player.toString());
statement.setInt(2, votes);
Expand Down Expand Up @@ -216,34 +229,41 @@ public void clearVotes() {
public PlayerVotes getVotes(UUID player) {
Preconditions.checkNotNull(player, "player");
try (Connection connection = dbPool.getConnection()) {
try (PreparedStatement statement = connection.prepareStatement("SELECT last_name, votes FROM " + tableName + " WHERE uuid = ?")) {
try (PreparedStatement statement = connection.prepareStatement("SELECT last_name, votes, last_votes FROM " + tableName + " WHERE uuid = ?")) {
statement.setString(1, player.toString());
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return new PlayerVotes(player, resultSet.getString(1), resultSet.getInt(2), PlayerVotes.Type.CURRENT);
return new PlayerVotes(player,
resultSet.getString(1),
resultSet.getInt(2),
PlayerVotes.deserializeLastVotes(resultSet.getString(3)),
PlayerVotes.Type.CURRENT);
} else {
return new PlayerVotes(player, null, 0, PlayerVotes.Type.CURRENT);
return new PlayerVotes(player, null, 0, Collections.emptyMap(), PlayerVotes.Type.CURRENT);
}
}
}
} catch (SQLException e) {
SuperbVote.getPlugin().getLogger().log(Level.SEVERE, "Unable to get votes for " + player.toString(), e);
return new PlayerVotes(player, null, 0, PlayerVotes.Type.CURRENT);
return new PlayerVotes(player, null, 0, Collections.emptyMap(), PlayerVotes.Type.CURRENT);
}
}

@Override
public List<PlayerVotes> getTopVoters(int amount, int page) {
int offset = page * amount;
try (Connection connection = dbPool.getConnection()) {
try (PreparedStatement statement = connection.prepareStatement("SELECT uuid, last_name, votes FROM " + tableName + " WHERE votes > 0 ORDER BY votes DESC " +
try (PreparedStatement statement = connection.prepareStatement("SELECT uuid, last_name, votes, last_votes FROM " + tableName + " WHERE votes > 0 ORDER BY votes DESC " +
"LIMIT " + amount + " OFFSET " + offset)) {
try (ResultSet resultSet = statement.executeQuery()) {
List<PlayerVotes> records = new ArrayList<>();
while (resultSet.next()) {
UUID uuid = UUID.fromString(resultSet.getString(1));
String name = resultSet.getString(2);
records.add(new PlayerVotes(uuid, name, resultSet.getInt(3), PlayerVotes.Type.CURRENT));
records.add(new PlayerVotes(uuid, name,
resultSet.getInt(3),
PlayerVotes.deserializeLastVotes(resultSet.getString(4)),
PlayerVotes.Type.CURRENT));
}
return records;
}
Expand Down Expand Up @@ -293,7 +313,7 @@ public List<PlayerVotes> getAllPlayersWithNoVotesToday(List<UUID> onlinePlayers)
List<PlayerVotes> votes = new ArrayList<>();
try (Connection connection = dbPool.getConnection()) {
String valueStatement = Joiner.on(", ").join(Collections.nCopies(onlinePlayers.size(), "?"));
try (PreparedStatement statement = connection.prepareStatement("SELECT uuid, last_name, votes, (DATE(last_vote) = CURRENT_DATE()) AS has_voted_today FROM " + tableName + " WHERE uuid IN (" + valueStatement + ")")) {
try (PreparedStatement statement = connection.prepareStatement("SELECT uuid, last_name, votes, last_votes, (DATE(last_vote) = CURRENT_DATE()) AS has_voted_today FROM " + tableName + " WHERE uuid IN (" + valueStatement + ")")) {
for (int i = 0; i < onlinePlayers.size(); i++) {
statement.setString(i + 1, onlinePlayers.get(i).toString());
}
Expand All @@ -302,12 +322,13 @@ public List<PlayerVotes> getAllPlayersWithNoVotesToday(List<UUID> onlinePlayers)
while (resultSet.next()) {
UUID uuid = UUID.fromString(resultSet.getString(1));
found.add(uuid);
if (resultSet.getBoolean(4)) {
if (resultSet.getBoolean(5)) {
continue; // already voted today
}
PlayerVotes pv = new PlayerVotes(UUID.fromString(resultSet.getString(1)),
resultSet.getString(2),
resultSet.getInt(3),
PlayerVotes.deserializeLastVotes(resultSet.getString(4)),
PlayerVotes.Type.CURRENT);
votes.add(pv);
}
Expand All @@ -317,7 +338,7 @@ public List<PlayerVotes> getAllPlayersWithNoVotesToday(List<UUID> onlinePlayers)
List<UUID> missing = new ArrayList<>(onlinePlayers);
missing.removeAll(found);
for (UUID uuid : missing) {
votes.add(new PlayerVotes(uuid, null, 0, PlayerVotes.Type.CURRENT));
votes.add(new PlayerVotes(uuid, null, 0, Collections.emptyMap(), PlayerVotes.Type.CURRENT));
}
}
return votes;
Expand Down
Loading