Skip to content

Commit

Permalink
fix: reward multipliers not showing
Browse files Browse the repository at this point in the history
  • Loading branch information
Thatsmusic99 committed Oct 25, 2023
1 parent e0a4065 commit 46be996
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 70 deletions.
Expand Up @@ -103,7 +103,7 @@ public String getMainName() {
return mainName;
}

public Reward getReward() {
public Reward<?> getReward() {
return RewardsManager.get().getReward(reward);
}

Expand All @@ -112,7 +112,7 @@ public String getHeadType() {
}

public long getGainedXP() {
return getReward().getXp();
return getReward().getXp(getDifficulty());
}

public abstract CompletableFuture<Boolean> canComplete(Player p);
Expand Down
36 changes: 25 additions & 11 deletions src/main/java/io/github/thatsmusic99/headsplus/api/Reward.java
Expand Up @@ -3,24 +3,28 @@
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
import io.github.thatsmusic99.headsplus.api.rewards.*;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class Reward {
public abstract class Reward<T> {

protected @NotNull T reward;
private @Nullable String message;
private final long xp;
private String message;
private boolean useMultiplier;

public Reward(long xp) {
public Reward(@NotNull T reward, long xp) {
this.xp = xp;
this.reward = reward;
}

public static Reward fromConfigSection(String id, ConfigSection section) {
public static Reward<?> fromConfigSection(String id, ConfigSection section) {
String type = section.getString("type");
if (type == null) type = section.getString("reward-type");
if (type == null)
throw new IllegalStateException("There is no reward type for " + id + "!");

Reward reward;
Reward<?> reward;
switch (type.toLowerCase()) {
case "eco":
reward = EconomyReward.fromConfigSection(id, section);
Expand Down Expand Up @@ -50,25 +54,35 @@ public static Reward fromConfigSection(String id, ConfigSection section) {
return reward;
}

public void rewardPlayer(Challenge challenge, Player player) {
HPPlayer.getHPPlayer(player.getUniqueId()).addXp(useMultiplier ? xp * challenge.getDifficulty() : xp);
public void rewardPlayer(@Nullable Challenge challenge, @NotNull Player player) {
HPPlayer.getHPPlayer(player.getUniqueId())
.addXp(useMultiplier ? xp * (challenge == null ? 1 : challenge.getDifficulty()) : xp);
}

public String getRewardString(Player player) {
return getRewardString(player, 1);
}

public String getRewardString(Player player, int difficulty) {
if (message != null) return message;
return getDefaultRewardString(player);
return getDefaultRewardString(player, isUsingMultiplier() ? difficulty : 1);
}

public boolean isUsingMultiplier() {
return useMultiplier;
}

public abstract String getDefaultRewardString(Player player);
public abstract String getDefaultRewardString(Player player, int difficulty);

public void multiplyRewardValues(int multiplier) {
public T multiplyRewardValues(int multiplier) {
return this.reward;
}

public long getXp() {
return xp;
return getXp(1);
}

public long getXp(int multiplier) {
return useMultiplier ? multiplier * xp : xp;
}
}
Expand Up @@ -7,14 +7,12 @@
import io.github.thatsmusic99.headsplus.config.MessagesManager;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class AddGroupReward extends Reward {
public class AddGroupReward extends Reward<String> {

private final String group;

public AddGroupReward(String group, long xp) {
super(xp);
this.group = group;
public AddGroupReward(@NotNull String group, long xp) {
super(group, xp);
}

public static AddGroupReward fromConfigSection(String id, ConfigSection section) {
Expand All @@ -26,17 +24,17 @@ public static AddGroupReward fromConfigSection(String id, ConfigSection section)
}

@Override
public void rewardPlayer(Challenge challenge, Player player) {
public void rewardPlayer(Challenge challenge, @NotNull Player player) {
super.rewardPlayer(challenge, player);
Permission permissions = HeadsPlus.get().getPermissions();
if (!permissions.isEnabled()) return;
if (permissions.playerInGroup(player, group)) return;
permissions.playerAddGroup(player, group);
if (permissions.playerInGroup(player, this.reward)) return;
permissions.playerAddGroup(player, this.reward);
}

@Override
public String getDefaultRewardString(Player player) {
public String getDefaultRewardString(Player player, int difficulty) {
return MessagesManager.get().getString("inventory.icon.reward.group-add", player)
.replace("{group}", group);
.replace("{group}", this.reward);
}
}
Expand Up @@ -6,14 +6,13 @@
import io.github.thatsmusic99.headsplus.api.Reward;
import io.github.thatsmusic99.headsplus.config.MessagesManager;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class EconomyReward extends Reward {

private final double money;
public class EconomyReward extends Reward<Double> {

public EconomyReward(double money, long xp) {
super(xp);
this.money = money;
super(money, xp);
}

public static EconomyReward fromConfigSection(String id, ConfigSection section) {
Expand All @@ -24,20 +23,25 @@ public static EconomyReward fromConfigSection(String id, ConfigSection section)
}

@Override
public void rewardPlayer(Challenge challenge, Player player) {
public void rewardPlayer(@Nullable Challenge challenge, @NotNull Player player) {
super.rewardPlayer(challenge, player);
if (!HeadsPlus.get().isVaultEnabled()) return;
if (!HeadsPlus.get().getEconomy().isEnabled()) return;
int difficulty = 1;
if (isUsingMultiplier()) {
if (isUsingMultiplier() && challenge != null) {
difficulty = challenge.getDifficulty();
}
HeadsPlus.get().getEconomy().depositPlayer(player, money * difficulty);
HeadsPlus.get().getEconomy().depositPlayer(player, this.reward * difficulty);
}

@Override
public String getDefaultRewardString(Player player) {
public String getDefaultRewardString(Player player, int difficulty) {
return MessagesManager.get().getString("inventory.icon.reward.currency", player)
.replace("{amount}", String.valueOf(money));
.replace("{amount}", String.valueOf(multiplyRewardValues(difficulty)));
}

@Override
public Double multiplyRewardValues(int multiplier) {
return this.reward * multiplier;
}
}
Expand Up @@ -9,14 +9,13 @@
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ItemReward extends Reward {

private final ItemStack item;
public class ItemReward extends Reward<ItemStack> {

public ItemReward(ItemStack item, long xp) {
super(xp);
this.item = item;
super(item, xp);
}

@Deprecated
Expand Down Expand Up @@ -48,29 +47,33 @@ public static ItemReward fromConfigSection(String id, ConfigSection section) {
}

@Override
public String getDefaultRewardString(Player player) {
public String getDefaultRewardString(Player player, int difficulty) {
return MessagesManager.get().getString("inventory.icon.reward.item-give", player)
.replace("{amount}", String.valueOf(item.getAmount()))
.replace("{item}", HeadsPlus.capitalize(item.getType().name().replaceAll("_", " ")));
.replace("{amount}", String.valueOf(multiplyRewardValues(difficulty).getAmount()))
.replace("{item}", HeadsPlus.capitalize(reward.getType().name().replaceAll("_", " ")));
}

@Override
public void rewardPlayer(Challenge challenge, Player player) {
public void rewardPlayer(@Nullable Challenge challenge, @NotNull Player player) {
super.rewardPlayer(challenge, player);

// If their inventory is full, drop the item so it's not entirely lost
if (player.getInventory().firstEmpty() == -1) {
player.getWorld().dropItem(player.getLocation(), item);
player.getWorld().dropItem(player.getLocation(), reward);
return;
}
ItemStack item = this.item;
if (isUsingMultiplier()) {
item = this.item.clone();
item.setAmount(this.item.getAmount() * challenge.getDifficulty());
ItemStack item = this.reward;
if (isUsingMultiplier() && challenge != null) {
item = this.reward.clone();
item.setAmount(this.reward.getAmount() * challenge.getDifficulty());
}
player.getInventory().addItem(item);
}

@Override
public void multiplyRewardValues(int multiplier) {
item.setAmount(item.getAmount() * multiplier);
public ItemStack multiplyRewardValues(int multiplier) {
ItemStack item = this.reward.clone();
item.setAmount(this.reward.getAmount() * multiplier);
return item;
}
}
Expand Up @@ -7,14 +7,12 @@
import io.github.thatsmusic99.headsplus.config.MessagesManager;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class RemoveGroupReward extends Reward {
public class RemoveGroupReward extends Reward<String> {

private final String group;

public RemoveGroupReward(String group, long xp) {
super(xp);
this.group = group;
public RemoveGroupReward(@NotNull String group, long xp) {
super(group, xp);
}

public static RemoveGroupReward fromConfigSection(String id, ConfigSection section) {
Expand All @@ -26,17 +24,17 @@ public static RemoveGroupReward fromConfigSection(String id, ConfigSection secti
}

@Override
public void rewardPlayer(Challenge challenge, Player player) {
public void rewardPlayer(Challenge challenge, @NotNull Player player) {
super.rewardPlayer(challenge, player);
Permission permissions = HeadsPlus.get().getPermissions();
if (!permissions.isEnabled()) return;
if (!permissions.playerInGroup(player, group)) return;
permissions.playerRemoveGroup(player, group);
if (!permissions.playerInGroup(player, this.reward)) return;
permissions.playerRemoveGroup(player, this.reward);
}

@Override
public String getDefaultRewardString(Player player) {
public String getDefaultRewardString(Player player, int difficulty) {
return MessagesManager.get().getString("inventory.icon.reward.group-remove", player)
.replace("{group}", group);
.replace("{group}", this.reward);
}
}
Expand Up @@ -5,16 +5,14 @@
import io.github.thatsmusic99.headsplus.api.Reward;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class RunCommandReward extends Reward {

private final List<String> commands;
public class RunCommandReward extends Reward<List<String>> {

public RunCommandReward(long xp, List<String> commands) {
super(xp);
this.commands = commands;
super(commands, xp);
}

public static RunCommandReward fromConfigSection(String id, ConfigSection section) {
Expand All @@ -26,9 +24,9 @@ public static RunCommandReward fromConfigSection(String id, ConfigSection sectio
}

@Override
public void rewardPlayer(Challenge challenge, Player player) {
public void rewardPlayer(Challenge challenge, @NotNull Player player) {
super.rewardPlayer(challenge, player);
for (String command : commands) {
for (String command : this.reward) {
if (command.startsWith("player>")) {
Bukkit.dispatchCommand(player, command.substring(8).replaceAll("\\{player}", player.getName()));
return;
Expand All @@ -38,7 +36,7 @@ public void rewardPlayer(Challenge challenge, Player player) {
}

@Override
public String getDefaultRewardString(Player player) {
public String getDefaultRewardString(Player player, int difficulty) {
return null;
}
}
Expand Up @@ -22,7 +22,7 @@ public class Challenge extends Content {
public Challenge(io.github.thatsmusic99.headsplus.api.Challenge challenge, Player player) {
super(challenge.isComplete(player) ? challenge.getCompleteIcon().clone() : challenge.getIcon().clone());
this.challenge = challenge;
this.reward = challenge.getReward().getRewardString(player);
this.reward = challenge.getReward().getRewardString(player, challenge.getDifficulty());
initNameAndLore("challenge", player);
}

Expand Down

0 comments on commit 46be996

Please sign in to comment.