Skip to content

Commit

Permalink
🌟 Added reload command, Closes #40. Added buy/claim delay based on pe…
Browse files Browse the repository at this point in the history
…rmission. Closes #41

Took 29 minutes
  • Loading branch information
kiranhart committed Sep 9, 2024
1 parent 953ac49 commit 61253a2
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/main/java/ca/tweetzy/skulls/Skulls.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import co.aikar.taskchain.BukkitTaskChainFactory;
import co.aikar.taskchain.TaskChain;
import co.aikar.taskchain.TaskChainFactory;
import org.bukkit.NamespacedKey;

import javax.inject.Named;

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


private final NamespacedKey SKULLS_CLAIM_DELAY = new NamespacedKey(this, "SkullsClaimDelay");


@Override
protected void onFlight() {
// settings and locale setup
Expand Down Expand Up @@ -104,7 +111,8 @@ protected void onFlight() {
new SearchCommand(),
new PlayerHeadCommand(),
new GiveCommand(),
new InspectCommand()
new InspectCommand(),
new ReloadCommand()
);

// events
Expand Down Expand Up @@ -158,6 +166,10 @@ public static SkullsAPI getAPI() {
return getInstance().api;
}

public static NamespacedKey getClaimDelayKey() {
return getInstance().SKULLS_CLAIM_DELAY;
}

@Override
protected int getBStatsId() {
return 10616;
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/ca/tweetzy/skulls/commands/ReloadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Skulls
* Copyright 2022 Kiran Hart
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package ca.tweetzy.skulls.commands;

import ca.tweetzy.flight.command.AllowedExecutor;
import ca.tweetzy.flight.command.Command;
import ca.tweetzy.flight.command.ReturnType;
import ca.tweetzy.flight.comp.enums.CompMaterial;
import ca.tweetzy.flight.nbtapi.NBT;
import ca.tweetzy.flight.settings.TranslationManager;
import ca.tweetzy.flight.utils.Common;
import ca.tweetzy.flight.utils.PlayerUtil;
import ca.tweetzy.skulls.Skulls;
import ca.tweetzy.skulls.api.interfaces.PlacedSkull;
import ca.tweetzy.skulls.api.interfaces.Skull;
import ca.tweetzy.skulls.settings.Settings;
import ca.tweetzy.skulls.settings.Translations;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.List;

public final class ReloadCommand extends Command {

public ReloadCommand() {
super(AllowedExecutor.BOTH, "reload");
}

@Override
protected ReturnType execute(CommandSender sender, String... args) {
Settings.setup();
Translations.init();
tell(sender, "&aReloaded configuration files");
return ReturnType.SUCCESS;
}

@Override
protected List<String> tab(CommandSender sender, String... args) {
return null;
}

@Override
public String getPermissionNode() {
return Settings.GENERAL_USAGE_REQUIRES_NO_PERM.getBoolean() ? null : "skulls.command.reload";
}

@Override
public String getSyntax() {
return null;
}

@Override
public String getDescription() {
return "Used to reload the plugin";
}
}
4 changes: 4 additions & 0 deletions src/main/java/ca/tweetzy/skulls/guis/SkullsViewGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ protected void onClick(Skull skull, GuiClickEvent event) {
if (!player.isOp() || !player.hasPermission("skulls.buyblocked")) return;
}

if (!Skulls.getPlayerManager().playerCanClaim(player)) {
return;
}

if (!Settings.CHARGE_FOR_HEADS.getBoolean()) {
player.getInventory().addItem(skull.getItemStack());
return;
Expand Down
70 changes: 66 additions & 4 deletions src/main/java/ca/tweetzy/skulls/manager/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@

package ca.tweetzy.skulls.manager;

import ca.tweetzy.flight.settings.TranslationManager;
import ca.tweetzy.flight.utils.Common;
import ca.tweetzy.skulls.Skulls;
import ca.tweetzy.skulls.api.interfaces.SkullUser;
import ca.tweetzy.skulls.impl.SkullPlayer;
import ca.tweetzy.skulls.settings.Settings;
import ca.tweetzy.skulls.settings.Translations;
import lombok.NonNull;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;

import java.util.ArrayList;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

/**
* Date Created: April 04 2022
Expand Down Expand Up @@ -80,6 +82,66 @@ public SkullUser findOrCreate(@NonNull final UUID uuid) {
return skullUser;
}

public boolean playerCanClaim(@NonNull final Player player) {
if (!Settings.CLAIM_DELAY_ENABLED.getBoolean()) return true;
List<Integer> possibleTimes = new ArrayList<>();

Settings.CLAIM_DELAY_PERMS.getStringList().forEach(line -> {
String[] split = line.split(":");
if (player.hasPermission("skulls.claimdelay." + split[0])) {
possibleTimes.add(Integer.parseInt(split[1]));
}
});

if (possibleTimes.isEmpty()) return true;
int maxSecs = Collections.max(possibleTimes);
long currentMillis = System.currentTimeMillis();

if (!player.getPersistentDataContainer().has(Skulls.getClaimDelayKey())) {
// set the time
final long newMillis = currentMillis + (maxSecs * 1000L);
player.getPersistentDataContainer().set(Skulls.getClaimDelayKey(), PersistentDataType.LONG, newMillis);
return true;
}

final long lastClaimedAt = player.getPersistentDataContainer().get(Skulls.getClaimDelayKey(), PersistentDataType.LONG);
if (lastClaimedAt > currentMillis) {
Common.tell(player, TranslationManager.string(Translations.CLAIM_DELAY, "time_difference", getFriendlyTimeDifference(currentMillis,lastClaimedAt)));
return false;
} else {
final long newMillis = currentMillis + (maxSecs * 1000L);
player.getPersistentDataContainer().set(Skulls.getClaimDelayKey(), PersistentDataType.LONG, newMillis);
}

return true;
}

private String getFriendlyTimeDifference(long startMillis, long endMillis) {
long diffMillis = endMillis - startMillis;
long seconds = diffMillis / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;

StringBuilder result = new StringBuilder();

if (days > 0) {
result.append(days).append("d ");
hours %= 24;
}
if (hours > 0 || result.length() > 0) {
result.append(hours).append("h ");
minutes %= 60;
}
if (minutes > 0 || result.length() > 0) {
result.append(minutes).append("m ");
seconds %= 60;
}
result.append(seconds).append("s");

return result.toString().trim();
}

public void load() {
this.players.clear();
Skulls.getDataManager().getPlayers((error, loaded) -> {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ca/tweetzy/skulls/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import ca.tweetzy.skulls.Skulls;
import lombok.SneakyThrows;

import java.util.List;

/**
* Date Created: April 04 2022
* Time Created: 9:22 p.m.
Expand Down Expand Up @@ -58,6 +60,12 @@ public final class Settings extends FlightSettings {
public static final ConfigEntry PLAYER_HEAD_DROP = create("player head.drop enabled", true);
public static final ConfigEntry PLAYER_HEAD_DROP_CHANCE = create("player head.drop chance", 50);


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.");
public static final ConfigEntry CLAIM_DELAY_PERMS = create("claim delay.permission times", List.of(
"basic:30"
), "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");

/*
==================== GUI END ====================
*/
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ca/tweetzy/skulls/settings/Translations.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Translations(@NonNull JavaPlugin plugin) {
public static final TranslationEntry SKULL_TITLE = create("skull.name", "&e%skull_name%");
public static final TranslationEntry ID_TAKEN = create("misc.id taken", "&cThat category id is already in use!");
public static final TranslationEntry LOADING = create("misc.loading", "&cPlease wait a bit longer, still loading heads.");
public static final TranslationEntry CLAIM_DELAY = create("misc.claim delay", "&cYou can claim another head in &7(&e%time_difference%&7)");


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

0 comments on commit 61253a2

Please sign in to comment.