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

Limit the claim blocks that players can buy #163

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ jobs:
distro-descriptions: |
Paper
files: |
target/HuskClaims-Paper-${{ env.version_name }}.jar
target/HuskClaims-Paper-${{ env.version_name }}.jar
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
package net.william278.huskclaims.command;

import net.william278.huskclaims.HuskClaims;
import net.william278.huskclaims.config.Settings;
import net.william278.huskclaims.hook.EconomyHook;
import net.william278.huskclaims.user.ClaimBlocksManager;
import net.william278.huskclaims.user.OnlineUser;
import net.william278.huskclaims.user.SavedUser;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand Down Expand Up @@ -53,6 +55,8 @@ public void execute(@NotNull OnlineUser executor, @NotNull String[] args) {
.ifPresent(executor::sendMessage);
return;
}


buyClaimBlocks(executor, amount.get(), hook.get());
}

Expand All @@ -63,14 +67,21 @@ private void buyClaimBlocks(@NotNull OnlineUser executor, long amount, @NotNull
.ifPresent(executor::sendMessage);
return;
}
plugin.editClaimBlocks(
executor,
ClaimBlocksManager.ClaimBlockSource.PURCHASE,
(blocks) -> blocks + amount,
(newBalance) -> plugin.getLocales().getLocale("claim_blocks_purchased",
Long.toString(amount), hook.format(cost), Long.toString(newBalance))
.ifPresent(executor::sendMessage)
);
final long userBought = plugin.getBoughtClaimBlocks(executor)+ amount;
final long limit = plugin.getSettings().getHooks().getEconomy().getBlockLimit();
if(userBought > limit) {
plugin.getLocales().getLocale("error_over_claim_blocks")
.ifPresent(executor::sendMessage);
} else {
plugin.editClaimBlocks(
executor,
ClaimBlocksManager.ClaimBlockSource.PURCHASE,
amount,
(newBalance) -> plugin.getLocales().getLocale("claim_blocks_purchased",
Long.toString(amount), hook.format(cost), Long.toString(newBalance))
.ifPresent(executor::sendMessage)
);
}
}

private double getBlockPrice(long amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@ public static class EconomyHookSettings {

@Comment("The cost of buying 1 claim block")
private double costPerBlock = 1.0;

@Comment("Limit on the number of claim cubes a player can purchase (-1 means no limit)")
private int blockLimit = -1;
}

private PlaceholderSettings placeholders = new PlaceholderSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void setSchemaVersion(int version) {
public Optional<SavedUser> getUser(@NotNull UUID uuid) {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(format("""
SELECT `uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`
SELECT `uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`, `bought_claim_blocks`
FROM `%user_data%`
WHERE uuid = ?"""))) {
statement.setString(1, uuid.toString());
Expand All @@ -230,7 +230,8 @@ public Optional<SavedUser> getUser(@NotNull UUID uuid) {
resultSet.getTimestamp("last_login").toLocalDateTime()
.atOffset(OffsetDateTime.now().getOffset()),
resultSet.getLong("claim_blocks"),
resultSet.getInt("hours_played")
resultSet.getInt("hours_played"),
resultSet.getLong("bought_claim_blocks")
));
}
}
Expand All @@ -244,7 +245,7 @@ public Optional<SavedUser> getUser(@NotNull UUID uuid) {
public Optional<SavedUser> getUser(@NotNull String username) {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(format("""
SELECT `uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`
SELECT `uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`, `bought_claim_blocks`
FROM `%user_data%`
WHERE `username` = ?"""))) {
statement.setString(1, username);
Expand All @@ -259,7 +260,8 @@ public Optional<SavedUser> getUser(@NotNull String username) {
resultSet.getTimestamp("last_login").toLocalDateTime()
.atOffset(OffsetDateTime.now().getOffset()),
resultSet.getLong("claim_blocks"),
resultSet.getInt("hours_played")
resultSet.getInt("hours_played"),
resultSet.getLong("bought_claim_blocks")
));
}
}
Expand All @@ -274,7 +276,7 @@ public List<SavedUser> getInactiveUsers(long daysInactive) {
final List<SavedUser> inactiveUsers = Lists.newArrayList();
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(format("""
SELECT `uuid`, `username`, `last_login`, `preferences`, `claim_blocks`, `hours_played`
SELECT `uuid`, `username`, `last_login`, `preferences`, `claim_blocks`, `hours_played`, `bought_claim_blocks`
FROM `%user_data%`
WHERE `last_login` < DATE_SUB(NOW(), INTERVAL ? DAY);"""))) {
statement.setLong(1, daysInactive);
Expand All @@ -289,7 +291,8 @@ public List<SavedUser> getInactiveUsers(long daysInactive) {
resultSet.getTimestamp("last_login").toLocalDateTime()
.atOffset(OffsetDateTime.now().getOffset()),
resultSet.getLong("claim_blocks"),
resultSet.getInt("hours_played")
resultSet.getInt("hours_played"),
resultSet.getLong("bought_claim_blocks")
));
}
}
Expand All @@ -304,7 +307,7 @@ public List<SavedUser> getInactiveUsers(long daysInactive) {
public void createUser(@NotNull SavedUser saved) {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(format("""
INSERT INTO `%user_data%` (`uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`)
INSERT INTO `%user_data%` (`uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`, `bought_claim_blocks`)
VALUES (?, ?, ?, ?, ?, ?)"""))) {
statement.setString(1, saved.getUser().getUuid().toString());
statement.setString(2, saved.getUser().getName());
Expand All @@ -313,6 +316,7 @@ public void createUser(@NotNull SavedUser saved) {
statement.setInt(5, saved.getHoursPlayed());
statement.setBytes(6, plugin.getGson().toJson(saved.getPreferences())
.getBytes(StandardCharsets.UTF_8));
statement.setLong(7, saved.getBoughtClaimBlocks());
statement.executeUpdate();
}
} catch (SQLException e) {
Expand All @@ -325,13 +329,14 @@ public void updateUser(@NotNull SavedUser user) {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(format("""
UPDATE `%user_data%`
SET `claim_blocks` = ?, `hours_played` = ?, `preferences` = ?
SET `claim_blocks` = ?, `hours_played` = ?, `preferences` = ?, `bought_claim_blocks` = ?
WHERE `uuid` = ?"""))) {
statement.setLong(1, user.getClaimBlocks());
statement.setInt(2, user.getHoursPlayed());
statement.setBytes(3, plugin.getGson().toJson(user.getPreferences())
.getBytes(StandardCharsets.UTF_8));
statement.setString(4, user.getUser().getUuid().toString());
statement.setLong(4, user.getBoughtClaimBlocks());
statement.setString(5, user.getUser().getUuid().toString());
statement.executeUpdate();
}
} catch (SQLException e) {
Expand All @@ -343,19 +348,21 @@ public void updateUser(@NotNull SavedUser user) {
public void createOrUpdateUser(@NotNull SavedUser data) {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(format("""
INSERT INTO `%user_data%` (`uuid`, `username`, `last_login`, `claim_blocks`, `preferences`)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE `username` = ?, `last_login` = ?, `claim_blocks` = ?, `preferences` = ?;"""))) {
INSERT INTO `%user_data%` (`uuid`, `username`, `last_login`, `claim_blocks`, `preferences`, `bought_claim_blocks`)
VALUES (?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE `username` = ?, `last_login` = ?, `claim_blocks` = ?, `preferences` = ?, `bought_claim_blocks` = ?;"""))) {
final byte[] prefs = plugin.getGson().toJson(data.getPreferences()).getBytes(StandardCharsets.UTF_8);
statement.setString(1, data.getUser().getUuid().toString());
statement.setString(2, data.getUser().getName());
statement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()));
statement.setLong(4, data.getClaimBlocks());
statement.setBytes(5, prefs);
statement.setString(6, data.getUser().getName());
statement.setTimestamp(7, Timestamp.valueOf(LocalDateTime.now()));
statement.setLong(8, data.getClaimBlocks());
statement.setBytes(9, prefs);
statement.setLong(6, data.getBoughtClaimBlocks());
statement.setString(7, data.getUser().getName());
statement.setTimestamp(8, Timestamp.valueOf(LocalDateTime.now()));
statement.setLong(9, data.getClaimBlocks());
statement.setBytes(10, prefs);
statement.setLong(11, data.getBoughtClaimBlocks());
statement.executeUpdate();
}
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void setSchemaVersion(int version) {
@Override
public Optional<SavedUser> getUser(@NotNull UUID uuid) {
try (PreparedStatement statement = getConnection().prepareStatement(format("""
SELECT `uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`
SELECT `uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`, `bought_claim_blocks`
FROM `%user_data%`
WHERE uuid = ?"""))) {
statement.setString(1, uuid.toString());
Expand All @@ -220,7 +220,8 @@ public Optional<SavedUser> getUser(@NotNull UUID uuid) {
resultSet.getTimestamp("last_login").toLocalDateTime()
.atOffset(OffsetDateTime.now().getOffset()),
resultSet.getLong("claim_blocks"),
resultSet.getInt("hours_played")
resultSet.getInt("hours_played"),
resultSet.getLong("bought_claim_blocks")
));
}
} catch (SQLException e) {
Expand All @@ -232,7 +233,7 @@ public Optional<SavedUser> getUser(@NotNull UUID uuid) {
@Override
public Optional<SavedUser> getUser(@NotNull String username) {
try (PreparedStatement statement = getConnection().prepareStatement(format("""
SELECT `uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`
SELECT `uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`, `bought_claim_blocks`
FROM `%user_data%`
WHERE `username` = ?"""))) {
statement.setString(1, username);
Expand All @@ -247,7 +248,8 @@ public Optional<SavedUser> getUser(@NotNull String username) {
resultSet.getTimestamp("last_login").toLocalDateTime()
.atOffset(OffsetDateTime.now().getOffset()),
resultSet.getLong("claim_blocks"),
resultSet.getInt("hours_played")
resultSet.getInt("hours_played"),
resultSet.getLong("bought_claim_blocks")
));
}
} catch (SQLException e) {
Expand All @@ -260,7 +262,7 @@ public Optional<SavedUser> getUser(@NotNull String username) {
public List<SavedUser> getInactiveUsers(long daysInactive) {
final List<SavedUser> inactiveUsers = Lists.newArrayList();
try (PreparedStatement statement = getConnection().prepareStatement(format("""
SELECT `uuid`, `username`, `last_login`, `preferences`, `claim_blocks`, `hours_played`
SELECT `uuid`, `username`, `last_login`, `preferences`, `claim_blocks`, `hours_played`, `bought_claim_blocks`
FROM `%user_data%`
WHERE datetime(`last_login` / 1000, 'unixepoch') < datetime('now', ?);"""))) {
statement.setString(1, String.format("-%d days", daysInactive));
Expand All @@ -275,7 +277,8 @@ public List<SavedUser> getInactiveUsers(long daysInactive) {
resultSet.getTimestamp("last_login").toLocalDateTime()
.atOffset(OffsetDateTime.now().getOffset()),
resultSet.getLong("claim_blocks"),
resultSet.getInt("hours_played")
resultSet.getInt("hours_played"),
resultSet.getLong("bought_claim_blocks")
));
}
} catch (SQLException e) {
Expand All @@ -288,15 +291,16 @@ public List<SavedUser> getInactiveUsers(long daysInactive) {
@Override
public void createUser(@NotNull SavedUser saved) {
try (PreparedStatement statement = getConnection().prepareStatement(format("""
INSERT INTO `%user_data%` (`uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`)
VALUES (?, ?, ?, ?, ?, ?)"""))) {
INSERT INTO `%user_data%` (`uuid`, `username`, `last_login`, `claim_blocks`, `hours_played`, `preferences`, `bought_claim_blocks`)
VALUES (?, ?, ?, ?, ?, ?, ?)"""))) {
statement.setString(1, saved.getUser().getUuid().toString());
statement.setString(2, saved.getUser().getName());
statement.setTimestamp(3, Timestamp.valueOf(saved.getLastLogin().toLocalDateTime()));
statement.setLong(4, saved.getClaimBlocks());
statement.setInt(5, saved.getHoursPlayed());
statement.setBytes(6, plugin.getGson().toJson(saved.getPreferences())
.getBytes(StandardCharsets.UTF_8));
statement.setLong(7, saved.getBoughtClaimBlocks());
statement.executeUpdate();
} catch (SQLException e) {
plugin.log(Level.SEVERE, "Failed to create user in table", e);
Expand All @@ -307,13 +311,14 @@ public void createUser(@NotNull SavedUser saved) {
public void updateUser(@NotNull SavedUser user) {
try (PreparedStatement statement = getConnection().prepareStatement(format("""
UPDATE `%user_data%`
SET `claim_blocks` = ?, `hours_played` = ?, `preferences` = ?
SET `claim_blocks` = ?, `hours_played` = ?, `preferences` = ?, `bought_claim_blocks` = ?
WHERE `uuid` = ?"""))) {
statement.setLong(1, user.getClaimBlocks());
statement.setInt(2, user.getHoursPlayed());
statement.setBytes(3, plugin.getGson().toJson(user.getPreferences())
.getBytes(StandardCharsets.UTF_8));
statement.setString(4, user.getUser().getUuid().toString());
statement.setLong(4, user.getBoughtClaimBlocks());
statement.setString(5, user.getUser().getUuid().toString());
statement.executeUpdate();
} catch (SQLException e) {
plugin.log(Level.SEVERE, "Failed to update Saved User data in table", e);
Expand All @@ -323,19 +328,21 @@ public void updateUser(@NotNull SavedUser user) {
@Override
public void createOrUpdateUser(@NotNull SavedUser saved) {
try (PreparedStatement statement = getConnection().prepareStatement(format("""
INSERT INTO `%user_data%` (`uuid`, `username`, `last_login`, `claim_blocks`, `preferences`)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(`uuid`) DO UPDATE SET `username` = ?, `last_login` = ?, `claim_blocks` = ?, `preferences` = ?;"""))) {
INSERT INTO `%user_data%` (`uuid`, `username`, `last_login`, `claim_blocks`, `preferences`, `bought_claim_blocks`)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(`uuid`) DO UPDATE SET `username` = ?, `last_login` = ?, `claim_blocks` = ?, `preferences` = ?, `bought_claim_blocks` = ?;"""))) {
final byte[] prefs = plugin.getGson().toJson(saved.getPreferences()).getBytes(StandardCharsets.UTF_8);
statement.setString(1, saved.getUser().getUuid().toString());
statement.setString(2, saved.getUser().getName());
statement.setTimestamp(3, Timestamp.valueOf(saved.getLastLogin().toLocalDateTime()));
statement.setLong(4, saved.getClaimBlocks());
statement.setBytes(5, prefs);
statement.setString(6, saved.getUser().getName());
statement.setTimestamp(7, Timestamp.valueOf(saved.getLastLogin().toLocalDateTime()));
statement.setLong(8, saved.getClaimBlocks());
statement.setBytes(9, prefs);
statement.setLong(6, saved.getBoughtClaimBlocks());
statement.setString(7, saved.getUser().getName());
statement.setTimestamp(8, Timestamp.valueOf(saved.getLastLogin().toLocalDateTime()));
statement.setLong(9, saved.getClaimBlocks());
statement.setBytes(10, prefs);
statement.setLong(11, saved.getBoughtClaimBlocks());
statement.executeUpdate();
} catch (SQLException e) {
plugin.log(Level.SEVERE, "Failed to create or update user in table", e);
Expand Down
Loading