Skip to content

Commit 61253a2

Browse files
committed
🌟 Added reload command, Closes #40. Added buy/claim delay based on permission. Closes #41
Took 29 minutes
1 parent 953ac49 commit 61253a2

File tree

6 files changed

+166
-5
lines changed

6 files changed

+166
-5
lines changed

‎src/main/java/ca/tweetzy/skulls/Skulls.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
import co.aikar.taskchain.BukkitTaskChainFactory;
4545
import co.aikar.taskchain.TaskChain;
4646
import co.aikar.taskchain.TaskChainFactory;
47+
import org.bukkit.NamespacedKey;
48+
49+
import javax.inject.Named;
4750

4851
/**
4952
* Date Created: April 04 2022
@@ -68,6 +71,10 @@ public final class Skulls extends FlightPlugin {
6871
private DatabaseConnector databaseConnector;
6972
private DataManager dataManager;
7073

74+
75+
private final NamespacedKey SKULLS_CLAIM_DELAY = new NamespacedKey(this, "SkullsClaimDelay");
76+
77+
7178
@Override
7279
protected void onFlight() {
7380
// settings and locale setup
@@ -104,7 +111,8 @@ protected void onFlight() {
104111
new SearchCommand(),
105112
new PlayerHeadCommand(),
106113
new GiveCommand(),
107-
new InspectCommand()
114+
new InspectCommand(),
115+
new ReloadCommand()
108116
);
109117

110118
// events
@@ -158,6 +166,10 @@ public static SkullsAPI getAPI() {
158166
return getInstance().api;
159167
}
160168

169+
public static NamespacedKey getClaimDelayKey() {
170+
return getInstance().SKULLS_CLAIM_DELAY;
171+
}
172+
161173
@Override
162174
protected int getBStatsId() {
163175
return 10616;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Skulls
3+
* Copyright 2022 Kiran Hart
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package ca.tweetzy.skulls.commands;
20+
21+
import ca.tweetzy.flight.command.AllowedExecutor;
22+
import ca.tweetzy.flight.command.Command;
23+
import ca.tweetzy.flight.command.ReturnType;
24+
import ca.tweetzy.flight.comp.enums.CompMaterial;
25+
import ca.tweetzy.flight.nbtapi.NBT;
26+
import ca.tweetzy.flight.settings.TranslationManager;
27+
import ca.tweetzy.flight.utils.Common;
28+
import ca.tweetzy.flight.utils.PlayerUtil;
29+
import ca.tweetzy.skulls.Skulls;
30+
import ca.tweetzy.skulls.api.interfaces.PlacedSkull;
31+
import ca.tweetzy.skulls.api.interfaces.Skull;
32+
import ca.tweetzy.skulls.settings.Settings;
33+
import ca.tweetzy.skulls.settings.Translations;
34+
import org.bukkit.block.Block;
35+
import org.bukkit.command.CommandSender;
36+
import org.bukkit.entity.Player;
37+
import org.bukkit.inventory.ItemStack;
38+
39+
import java.util.List;
40+
41+
public final class ReloadCommand extends Command {
42+
43+
public ReloadCommand() {
44+
super(AllowedExecutor.BOTH, "reload");
45+
}
46+
47+
@Override
48+
protected ReturnType execute(CommandSender sender, String... args) {
49+
Settings.setup();
50+
Translations.init();
51+
tell(sender, "&aReloaded configuration files");
52+
return ReturnType.SUCCESS;
53+
}
54+
55+
@Override
56+
protected List<String> tab(CommandSender sender, String... args) {
57+
return null;
58+
}
59+
60+
@Override
61+
public String getPermissionNode() {
62+
return Settings.GENERAL_USAGE_REQUIRES_NO_PERM.getBoolean() ? null : "skulls.command.reload";
63+
}
64+
65+
@Override
66+
public String getSyntax() {
67+
return null;
68+
}
69+
70+
@Override
71+
public String getDescription() {
72+
return "Used to reload the plugin";
73+
}
74+
}

‎src/main/java/ca/tweetzy/skulls/guis/SkullsViewGUI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ protected void onClick(Skull skull, GuiClickEvent event) {
104104
if (!player.isOp() || !player.hasPermission("skulls.buyblocked")) return;
105105
}
106106

107+
if (!Skulls.getPlayerManager().playerCanClaim(player)) {
108+
return;
109+
}
110+
107111
if (!Settings.CHARGE_FOR_HEADS.getBoolean()) {
108112
player.getInventory().addItem(skull.getItemStack());
109113
return;

‎src/main/java/ca/tweetzy/skulls/manager/PlayerManager.java

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@
1818

1919
package ca.tweetzy.skulls.manager;
2020

21+
import ca.tweetzy.flight.settings.TranslationManager;
22+
import ca.tweetzy.flight.utils.Common;
2123
import ca.tweetzy.skulls.Skulls;
2224
import ca.tweetzy.skulls.api.interfaces.SkullUser;
2325
import ca.tweetzy.skulls.impl.SkullPlayer;
26+
import ca.tweetzy.skulls.settings.Settings;
27+
import ca.tweetzy.skulls.settings.Translations;
2428
import lombok.NonNull;
2529
import org.bukkit.entity.Player;
30+
import org.bukkit.persistence.PersistentDataType;
2631

27-
import java.util.ArrayList;
28-
import java.util.Map;
29-
import java.util.UUID;
32+
import java.util.*;
3033
import java.util.concurrent.ConcurrentHashMap;
31-
import java.util.concurrent.atomic.AtomicReference;
3234

3335
/**
3436
* Date Created: April 04 2022
@@ -80,6 +82,66 @@ public SkullUser findOrCreate(@NonNull final UUID uuid) {
8082
return skullUser;
8183
}
8284

85+
public boolean playerCanClaim(@NonNull final Player player) {
86+
if (!Settings.CLAIM_DELAY_ENABLED.getBoolean()) return true;
87+
List<Integer> possibleTimes = new ArrayList<>();
88+
89+
Settings.CLAIM_DELAY_PERMS.getStringList().forEach(line -> {
90+
String[] split = line.split(":");
91+
if (player.hasPermission("skulls.claimdelay." + split[0])) {
92+
possibleTimes.add(Integer.parseInt(split[1]));
93+
}
94+
});
95+
96+
if (possibleTimes.isEmpty()) return true;
97+
int maxSecs = Collections.max(possibleTimes);
98+
long currentMillis = System.currentTimeMillis();
99+
100+
if (!player.getPersistentDataContainer().has(Skulls.getClaimDelayKey())) {
101+
// set the time
102+
final long newMillis = currentMillis + (maxSecs * 1000L);
103+
player.getPersistentDataContainer().set(Skulls.getClaimDelayKey(), PersistentDataType.LONG, newMillis);
104+
return true;
105+
}
106+
107+
final long lastClaimedAt = player.getPersistentDataContainer().get(Skulls.getClaimDelayKey(), PersistentDataType.LONG);
108+
if (lastClaimedAt > currentMillis) {
109+
Common.tell(player, TranslationManager.string(Translations.CLAIM_DELAY, "time_difference", getFriendlyTimeDifference(currentMillis,lastClaimedAt)));
110+
return false;
111+
} else {
112+
final long newMillis = currentMillis + (maxSecs * 1000L);
113+
player.getPersistentDataContainer().set(Skulls.getClaimDelayKey(), PersistentDataType.LONG, newMillis);
114+
}
115+
116+
return true;
117+
}
118+
119+
private String getFriendlyTimeDifference(long startMillis, long endMillis) {
120+
long diffMillis = endMillis - startMillis;
121+
long seconds = diffMillis / 1000;
122+
long minutes = seconds / 60;
123+
long hours = minutes / 60;
124+
long days = hours / 24;
125+
126+
StringBuilder result = new StringBuilder();
127+
128+
if (days > 0) {
129+
result.append(days).append("d ");
130+
hours %= 24;
131+
}
132+
if (hours > 0 || result.length() > 0) {
133+
result.append(hours).append("h ");
134+
minutes %= 60;
135+
}
136+
if (minutes > 0 || result.length() > 0) {
137+
result.append(minutes).append("m ");
138+
seconds %= 60;
139+
}
140+
result.append(seconds).append("s");
141+
142+
return result.toString().trim();
143+
}
144+
83145
public void load() {
84146
this.players.clear();
85147
Skulls.getDataManager().getPlayers((error, loaded) -> {

‎src/main/java/ca/tweetzy/skulls/settings/Settings.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import ca.tweetzy.skulls.Skulls;
2525
import lombok.SneakyThrows;
2626

27+
import java.util.List;
28+
2729
/**
2830
* Date Created: April 04 2022
2931
* Time Created: 9:22 p.m.
@@ -58,6 +60,12 @@ public final class Settings extends FlightSettings {
5860
public static final ConfigEntry PLAYER_HEAD_DROP = create("player head.drop enabled", true);
5961
public static final ConfigEntry PLAYER_HEAD_DROP_CHANCE = create("player head.drop chance", 50);
6062

63+
64+
public static final ConfigEntry CLAIM_DELAY_ENABLED = create("claim delay.enabled", false,"If enabled, players will be forced to wait based on their permission to claim another head.");
65+
public static final ConfigEntry CLAIM_DELAY_PERMS = create("claim delay.permission times", List.of(
66+
"basic:30"
67+
), "Structure -> perm name:seconds to claim again. Ex basic:30 means skulls.claimdelay.basic permission will allow players to get a head every 30 seconds");
68+
6169
/*
6270
==================== GUI END ====================
6371
*/

‎src/main/java/ca/tweetzy/skulls/settings/Translations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public Translations(@NonNull JavaPlugin plugin) {
4040
public static final TranslationEntry SKULL_TITLE = create("skull.name", "&e%skull_name%");
4141
public static final TranslationEntry ID_TAKEN = create("misc.id taken", "&cThat category id is already in use!");
4242
public static final TranslationEntry LOADING = create("misc.loading", "&cPlease wait a bit longer, still loading heads.");
43+
public static final TranslationEntry CLAIM_DELAY = create("misc.claim delay", "&cYou can claim another head in &7(&e%time_difference%&7)");
4344

4445

4546
public static final TranslationEntry ALPHABET = create("categories.alphabet", "Alphabet");

0 commit comments

Comments
 (0)