remove stats system
improve operations and make them extendable more easily and let other plugins add operations, like requirements move requirements/operations out of ranks class as they will be used in prestiges
This commit is contained in:
@@ -9,14 +9,16 @@ import org.bukkit.entity.Player;
|
||||
import sh.okx.rankup.Rankup;
|
||||
import sh.okx.rankup.messages.MessageBuilder;
|
||||
import sh.okx.rankup.messages.Variable;
|
||||
import sh.okx.rankup.ranks.requirements.DeductibleRequirement;
|
||||
import sh.okx.rankup.ranks.requirements.Requirement;
|
||||
import sh.okx.rankup.requirements.DeductibleRequirement;
|
||||
import sh.okx.rankup.requirements.Operation;
|
||||
import sh.okx.rankup.requirements.Requirement;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class Rank {
|
||||
@@ -29,14 +31,14 @@ public class Rank {
|
||||
private final String rank;
|
||||
@Getter
|
||||
private final Set<Requirement> requirements;
|
||||
private final BinaryOperator<Boolean> reducer;
|
||||
private final Operation operation;
|
||||
private final List<String> commands;
|
||||
|
||||
public static Rank deserialize(Rankup plugin, ConfigurationSection section) {
|
||||
String rank = section.getString("rank");
|
||||
|
||||
Set<Requirement> requirements = new HashSet<>();
|
||||
BinaryOperator<Boolean> reducer = null;
|
||||
Operation operation = null;
|
||||
ConfigurationSection requirementsSection = section.getConfigurationSection("requirements");
|
||||
if (requirementsSection != null) {
|
||||
for (Map.Entry<String, Object> entry : requirementsSection.getValues(false).entrySet()) {
|
||||
@@ -50,26 +52,8 @@ public class Rank {
|
||||
}
|
||||
}
|
||||
|
||||
String operation = section.getString("operation");
|
||||
if (operation == null) {
|
||||
operation = "and";
|
||||
}
|
||||
switch (operation) {
|
||||
case "and":
|
||||
reducer = (a, b) -> a && b;
|
||||
break;
|
||||
case "or":
|
||||
reducer = (a, b) -> a || b;
|
||||
break;
|
||||
case "xor":
|
||||
reducer = (a, b) -> (a && !b) || (b && !a);
|
||||
break;
|
||||
case "none":
|
||||
reducer = (a, b) -> !a && !b;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid operation type for rank " + rank);
|
||||
}
|
||||
String operationName = Optional.ofNullable(section.getString("operation")).orElse("all");
|
||||
operation = plugin.getOperationRegistry().getOperation(operationName);
|
||||
}
|
||||
|
||||
return new Rank(plugin,
|
||||
@@ -77,15 +61,14 @@ public class Rank {
|
||||
section.getString("next"),
|
||||
rank,
|
||||
requirements,
|
||||
reducer,
|
||||
operation,
|
||||
section.getStringList("commands"));
|
||||
}
|
||||
|
||||
public boolean checkRequirements(Player player) {
|
||||
return requirements.stream()
|
||||
return operation.check(requirements.stream()
|
||||
.map(requirement -> requirement.check(player))
|
||||
.reduce(reducer)
|
||||
.orElse(true);
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public boolean isInRank(Player player) {
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package sh.okx.rankup.ranks.requirements;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import sh.okx.rankup.Rankup;
|
||||
|
||||
public abstract class DeductibleRequirement extends Requirement {
|
||||
public DeductibleRequirement(Rankup plugin, String name) {
|
||||
super(plugin, name);
|
||||
}
|
||||
|
||||
protected DeductibleRequirement(Requirement clone) {
|
||||
super(clone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the effect of this requirement to the player.
|
||||
* For money, this could be taking money away from the player.
|
||||
* You can assume that <code>Requirement#check(Player)</code> has been called,
|
||||
* and has returned true immediately prior to this.
|
||||
*
|
||||
* @param player the player to take from
|
||||
*/
|
||||
public abstract void apply(Player player);
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package sh.okx.rankup.ranks.requirements;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import sh.okx.rankup.Rankup;
|
||||
|
||||
public class GroupRequirement extends Requirement {
|
||||
public GroupRequirement(Rankup plugin, String name) {
|
||||
super(plugin, name);
|
||||
}
|
||||
|
||||
protected GroupRequirement(Requirement clone) {
|
||||
super(clone);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(Player player) {
|
||||
OUTER:
|
||||
for (String requiredGroup : getValueString().split(" ")) {
|
||||
for (String group : plugin.getPermissions().getPlayerGroups(player)) {
|
||||
if (group.equalsIgnoreCase(requiredGroup)) {
|
||||
continue OUTER;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Requirement clone() {
|
||||
return new GroupRequirement(this);
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package sh.okx.rankup.ranks.requirements;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.entity.Player;
|
||||
import sh.okx.rankup.Rankup;
|
||||
|
||||
public class MoneyRequirement extends DeductibleRequirement {
|
||||
public MoneyRequirement(Rankup plugin, String name) {
|
||||
super(plugin, name);
|
||||
}
|
||||
|
||||
protected MoneyRequirement(Requirement clone) {
|
||||
super(clone);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(Player player) {
|
||||
Economy economy = plugin.getEconomy();
|
||||
double balance = economy.getBalance(player);
|
||||
return balance >= getValueDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(Player player) {
|
||||
Economy economy = plugin.getEconomy();
|
||||
economy.withdrawPlayer(player, getValueDouble());
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getRemaining(Player player) {
|
||||
return Math.max(0, getValueDouble() - plugin.getEconomy().getBalance(player));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Requirement clone() {
|
||||
return new MoneyRequirement(this);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package sh.okx.rankup.ranks.requirements;
|
||||
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.entity.Player;
|
||||
import sh.okx.rankup.Rankup;
|
||||
|
||||
public class PlaytimeMinutesRequirement extends Requirement {
|
||||
private static final int TICKS_PER_MINUTE = 20 * 60;
|
||||
private Statistic playOneTick;
|
||||
|
||||
public PlaytimeMinutesRequirement(Rankup plugin, String name) {
|
||||
super(plugin, name);
|
||||
try {
|
||||
playOneTick = Statistic.valueOf("PLAY_ONE_MINUTE");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// statistic was changed in 1.13.
|
||||
playOneTick = Statistic.valueOf("PLAY_ONE_TICK");
|
||||
}
|
||||
}
|
||||
|
||||
protected PlaytimeMinutesRequirement(PlaytimeMinutesRequirement clone) {
|
||||
super(clone);
|
||||
this.playOneTick = clone.playOneTick;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(Player player) {
|
||||
return player.getStatistic(playOneTick) * TICKS_PER_MINUTE >= getValueDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getRemaining(Player player) {
|
||||
return Math.max(0, getValueDouble() - (player.getStatistic(playOneTick) * TICKS_PER_MINUTE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Requirement clone() {
|
||||
return new PlaytimeMinutesRequirement(this);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package sh.okx.rankup.ranks.requirements;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.bukkit.entity.Player;
|
||||
import sh.okx.rankup.Rankup;
|
||||
|
||||
public abstract class Requirement implements Cloneable {
|
||||
protected Rankup plugin;
|
||||
@Getter
|
||||
protected String name;
|
||||
private String value;
|
||||
|
||||
public Requirement(Rankup plugin, String name) {
|
||||
this.plugin = plugin;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
protected Requirement(Requirement clone) {
|
||||
if (clone != null) {
|
||||
this.plugin = clone.plugin;
|
||||
this.name = clone.name;
|
||||
this.value = clone.value;
|
||||
}
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValueString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public double getValueDouble() {
|
||||
return Double.parseDouble(value);
|
||||
}
|
||||
|
||||
public int getValueInt() {
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a player meets this requirement
|
||||
*
|
||||
* @param player the player to check
|
||||
* @return true if they meet the requirement, false otherwise
|
||||
*/
|
||||
public abstract boolean check(Player player);
|
||||
|
||||
/**
|
||||
* Get the remaining amount needed for <code>Requirement#check(Player)</code> to yield true.
|
||||
* This is not required and is only used in placeholders.
|
||||
*
|
||||
* @param player the player to find the remaining amount of
|
||||
* @return the remaining amount needed. Should be non-negative.
|
||||
*/
|
||||
public double getRemaining(Player player) {
|
||||
return getValueDouble();
|
||||
}
|
||||
|
||||
public abstract Requirement clone();
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package sh.okx.rankup.ranks.requirements;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class RequirementRegistry {
|
||||
private Set<Requirement> requirements = new HashSet<>();
|
||||
|
||||
public void addRequirement(Requirement requirement) {
|
||||
requirements.add(requirement);
|
||||
}
|
||||
|
||||
public Requirement newRequirement(String name, String value) {
|
||||
for (Requirement requirement : requirements) {
|
||||
if (requirement.getName().equalsIgnoreCase(name)) {
|
||||
Requirement newRequirement = requirement.clone();
|
||||
newRequirement.setValue(value);
|
||||
return newRequirement;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package sh.okx.rankup.ranks.requirements;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import sh.okx.rankup.Rankup;
|
||||
|
||||
public class XpLevelRequirement extends DeductibleRequirement {
|
||||
public XpLevelRequirement(Rankup plugin, String name) {
|
||||
super(plugin, name);
|
||||
}
|
||||
|
||||
protected XpLevelRequirement(Requirement clone) {
|
||||
super(clone);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(Player player) {
|
||||
return player.getLevel() >= getValueInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(Player player) {
|
||||
player.setLevel(player.getLevel() - getValueInt());
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getRemaining(Player player) {
|
||||
return Math.max(0, getValueInt() - player.getLevel());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Requirement clone() {
|
||||
return new XpLevelRequirement(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user