Skip to content

Commit

Permalink
Added redeemable amount.
Browse files Browse the repository at this point in the history
- Fixed remove treasure not removing treasure from the world.
- Added redeemable amount to editor.
  • Loading branch information
Smudgge committed Jul 31, 2023
1 parent 98b29d3 commit 4b19b54
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.cozyplugins</groupId>
<artifactId>CozyTreasureHunt</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>
<name>CozyTreasureHunt</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public class Treasure implements ConfigurationConvertable<Treasure>, Savable, Re

private int limit;
private @Nullable String limitMessage;

private @NotNull RewardBundle rewardBundle;
private int redeemable;
private @Nullable String redeemableMessage;

/**
* Used to create a treasure type.
Expand All @@ -82,6 +83,8 @@ public Treasure(@NotNull UUID identifier) {
this.particleSize = 1f;
this.limit = -1;
this.rewardBundle = new RewardBundle();
this.redeemable = 1;
this.redeemableMessage = "&7You have already redeemed this treasure.";
}

/**
Expand Down Expand Up @@ -243,7 +246,28 @@ public int getLimit() {
* @return The reward bundle.
*/
public @NotNull RewardBundle getRewardBundle() {
return rewardBundle;
return this.rewardBundle;
}

/**
* Used to get the amount of times the treasure is redeemable
* by different players before it disappears.
*
* @return The amount of times this treasure is redeemable.
*/
public int getRedeemable() {
return this.redeemable;
}

/**
* Used to get the redeemable message.
* This message is sent when a player trys to click
* a treasure they have already redeemed.
*
* @return The redeemable message.
*/
public @Nullable String getRedeemableMessage() {
return redeemableMessage;
}

/**
Expand Down Expand Up @@ -465,6 +489,30 @@ public int getLimit() {
return this;
}

/**
* Used to set the amount of times the treasure is redeemable
* by different players before it disappears.
*
* @param amount The amount of times it can be redeemed.
* @return This instance.
*/
public @NotNull Treasure setRedeemable(int amount) {
this.redeemable = amount;
return this;
}

/**
* Used to set the redeemable message.
* This message is sent to the player if they
* try and redeem a treasure they have already redeemed.
*
* @param message The redeemable message.
* @return This instance.
*/
public @NotNull Treasure setRedeemableMessage(@Nullable String message) {
this.redeemableMessage = message;
return this;
}

/**
* Used to spawn the treasure block.
Expand Down Expand Up @@ -574,6 +622,8 @@ public void spawnParticlesPlayerRight(@NotNull PlayerUser playerUser) {
section.set("limit", this.limit);
section.set("limit_message", this.limitMessage);
section.set("reward", this.rewardBundle.convert().getMap());
section.set("redeemable", this.redeemable);
section.set("redeemable_message", this.redeemableMessage);

return section;
}
Expand Down Expand Up @@ -614,6 +664,8 @@ public Treasure convert(ConfigurationSection section) {
this.limit = section.getInteger("limit", -1);
this.limitMessage = section.getString("limit_message", "&7You have reached the maximum amount of &f{name}&7.");
this.rewardBundle = new RewardBundle().convert(section.getSection("reward"));
this.redeemable = section.getInteger("redeemable", 1);
this.redeemableMessage = section.getString("redeemable_message", "&7You have already redeemed this treasure.");

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,5 +756,85 @@ protected void onBundleUpdate(@NotNull RewardBundle bundle) {
editorInventory.open(user.getPlayer());
})
);

// Multiple redeeming.
this.setItem(new InventoryItem()
.setMaterial(Material.COBWEB)
.setName("&6&lSet Redeemable Amount")
.setLore("&7Click to set the redeemable amount.",
"&7This defines the amount of times it can be",
"&7clicked by unique players before it disappears.",
"&7Setting this to -1 will ensure that the treasure",
"&7never disappears when a player clicks it.",
"&aCurrent &e" + treasure.getRedeemable(),
"&f+20 &7Shift Left Click",
"&f+1 &7Left Click",
"&f-1 &7Right Click",
"&f-20 &7Shift Right Click")
.addSlot(31)
.addAction((ClickAction) (user, type, inventory) -> {
if (type == ClickType.SHIFT_LEFT) treasure.setRedeemable(treasure.getRedeemable() + 20);
if (type == ClickType.LEFT) treasure.setRedeemable(treasure.getRedeemable() + 1);
if (type == ClickType.RIGHT) treasure.setRedeemable(treasure.getRedeemable() - 1);
if (type == ClickType.SHIFT_RIGHT) treasure.setRedeemable(treasure.getRedeemable() - 20);

// Boundary's.
if (treasure.getRedeemable() < -1) treasure.setRedeemable(-1);

treasure.save();

// Reset the item.
this.setItem(new CozyItem()
.setMaterial(Material.COBWEB)
.setName("&6&lSet Redeemable Amount")
.setLore("&7Click to set the redeemable amount.",
"&7This defines the amount of times it can be",
"&7clicked by unique players before it disappears.",
"&7Setting this to -1 will ensure that the treasure",
"&7never disappears when a player clicks it.",
"&aCurrent &e" + treasure.getRedeemable(),
"&f+20 &7Shift Left Click",
"&f+1 &7Left Click",
"&f-1 &7Right Click",
"&f-20 &7Shift Right Click")
, 31);
})
);

// TODO Multiple redeeming message.
this.setItem(new InventoryItem()
.setMaterial(Material.COBWEB)
.setName("&6&lNot Redeemable Message")
.setLore("&7Click to set the not redeemable message.",
"&7This message is sent to the player when",
"&7the player attempts to click anouther treasure",
"&7when they have already clicked it.",
"&aCurrently &e" + treasure.getRedeemableMessage())
.addSlot(32)
.addAction(new AnvilValueAction()
.setAnvilTitle("&8&lNot Redeemable Message")
.setAction((value, user) -> {
if (value != null) {
if (value.equals("")) {
treasure.setRedeemableMessage(null);
} else {
treasure.setRedeemableMessage(value);
}
}

treasure.save();

this.setItem(new CozyItem()
.setMaterial(Material.COBWEB)
.setName("&6&lNot Redeemable Message")
.setLore("&7Click to set the not redeemable message.",
"&7This message is sent to the player when",
"&7the player attempts to click anouther treasure",
"&7when they have already clicked it.",
"&aCurrently &e" + treasure.getRedeemableMessage())
, 29);
})
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.cozyplugins.cozytreasurehunt.event.TreasurePostClickEvent;
import com.github.cozyplugins.cozytreasurehunt.event.TreasurePreClickEvent;
import com.github.cozyplugins.cozytreasurehunt.storage.ConfigFile;
import com.github.cozyplugins.cozytreasurehunt.storage.DataStorage;
import com.github.cozyplugins.cozytreasurehunt.storage.PlayerData;
import com.github.smuddgge.squishyconfiguration.interfaces.ConfigurationSection;
import org.bukkit.event.EventHandler;
Expand All @@ -22,6 +23,14 @@ public void onTreasurePreClick(TreasurePreClickEvent event) {
PlayerData playerData = event.getPlayerData();
Treasure treasure = event.getTreasure();

// Check if they have already redeemed this treasure.
if (playerData.hasRedeemed(event.getTreasureLocation())) {

event.getPlayer().sendMessage("&7You have already redeemed this treasure.");
event.setCancelled(true);
return;
}

// Check if they have reached the global limit.
int treasureFound = playerData.getAmountFound();
int globalLimit = ConfigFile.getGlobalLimit();
Expand Down Expand Up @@ -58,13 +67,28 @@ public void onTreasurePreClick(TreasurePreClickEvent event) {
public void onTreasurePostClick(TreasurePostClickEvent event) {
Treasure treasure = event.getTreasure();
TreasureLocation treasureLocation = event.getTreasureLocation();
PlayerData playerData = event.getPlayerData();

// Remove the treasure.
treasureLocation.removeSilently();
// Check if the treasure can be redeemed again by a different pearson.
int maxRedeemableAmount = treasure.getRedeemable();
int redeemedAmount = DataStorage.getAmountRedeemed(treasureLocation);

// If the treasure will be fully redeemed.
// If the max is -1 that means the treasure will always be spawned.
if ((redeemedAmount + 1) >= maxRedeemableAmount && maxRedeemableAmount != -1) {
// Remove the treasure.
treasureLocation.removeSilently();

// Reset the location player data.
DataStorage.resetLocationData(treasureLocation);
} else {

// Add redeemed location for the player.
playerData.addRedeemedLocation(treasureLocation);
}

// Add treasure to player data.
PlayerData playerData = event.getPlayerData();
playerData.increaseTreasureFound(event.getTreasure().getName(), 1);
playerData.increaseTreasureFound(treasureLocation);

// Player information.
ConfigurationSection section = playerData.getInformation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.github.cozyplugins.cozylibrary.ConsoleManager;
import com.github.cozyplugins.cozytreasurehunt.Leaderboard;
import com.github.cozyplugins.cozytreasurehunt.TreasureLocation;
import com.github.cozyplugins.cozytreasurehunt.storage.configuration.DataConfigurationDirectory;
import com.github.smuddgge.squishyconfiguration.implementation.yaml.YamlConfiguration;
import com.github.smuddgge.squishyconfiguration.interfaces.ConfigurationSection;
Expand Down Expand Up @@ -188,4 +189,35 @@ public static void removeAll() {
configuration.save();

}

/**
* Used to get the amount of times the same treasure has been redeemed.
*
* @param treasureLocation The instance of a treasure location.
* @return The amount of times it has been redeemed.
*/
public static int getAmountRedeemed(TreasureLocation treasureLocation) {
int amountRedeemed = 0;

for (PlayerData playerData : DataStorage.getAll()) {
if (playerData.hasRedeemed(treasureLocation)) amountRedeemed++;
}

return amountRedeemed;
}

/**
* Loops though every player and removes the treasure
* location if they have it in there info section.
*
* @param treasureLocation The instance of the treasure location.
*/
public static void resetLocationData(TreasureLocation treasureLocation) {
for (PlayerData playerData : DataStorage.getAll()) {
if (!playerData.hasRedeemed(treasureLocation)) continue;

playerData.removeRedeemedLocation(treasureLocation);
playerData.save();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ public static boolean contains(Location location) {
* @param identifier The treasure's identifier.
*/
public static void remove(@NotNull String identifier) {
// Get the treasure location.
TreasureLocation location = LocationStorage.get(identifier);
if (location == null) return;

// Remove the location from the world.
location.removeSilently();

// Remove the location from configuration.
for (File file : LocationStorage.storage.getFiles()) {
YamlConfiguration configuration = new YamlConfiguration(file);
configuration.load();
Expand All @@ -273,6 +281,12 @@ public static void remove(@NotNull String identifier) {
* Used to remove all locations.
*/
public static void removeAll() {
// Remove the treasures from the worlds.
for (TreasureLocation location : LocationStorage.getAll()) {
location.removeSilently();
}

// Remove the treasures from the configuration file.
for (File file : LocationStorage.storage.getFiles()) {
YamlConfiguration configuration = new YamlConfiguration(file);
configuration.set(null);
Expand Down
Loading

0 comments on commit 4b19b54

Please sign in to comment.