Added ECO and BANK to challenges

This commit is contained in:
Fabrizio La Rosa 2020-06-26 19:51:41 +02:00
parent 898dd64d7a
commit 6730c53fa1
3 changed files with 114 additions and 36 deletions

View File

@ -1,12 +1,9 @@
package com.songoda.skyblock.challenge.challenge;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.hooks.EconomyManager;
import com.songoda.skyblock.SkyBlock;
import com.songoda.skyblock.bank.BankManager;
import com.songoda.skyblock.island.Island;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -19,17 +16,20 @@ import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import com.songoda.skyblock.api.SkyBlockAPI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
public class Challenge {
private ChallengeCategory category;
private int id;
private String name;
private int maxTimes;
private boolean showInChat;
private final ChallengeCategory category;
private final int id;
private final String name;
private final int maxTimes;
private final boolean showInChat;
private List<Peer<Type, Object>> requires;
private List<Peer<Type, Object>> rewards;
private ItemChallenge item;
private final ItemChallenge item;
public Challenge(ChallengeCategory category, int id, String name, int maxTimes, boolean showInChat,
List<String> requires, List<String> rewards, ItemChallenge item) {
@ -55,7 +55,7 @@ public class Challenge {
String arg1 = str.substring(idx + 1);
try {
Type t = Type.valueOf(arg0);
this.requires.add(new Peer<Type, Object>(t, t.convert(arg1)));
this.requires.add(new Peer<>(t, t.convert(arg1)));
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Invalid line : " + str + " : " + ex.getMessage());
} catch (Exception ex) {
@ -71,7 +71,7 @@ public class Challenge {
String arg1 = str.substring(idx + 1);
try {
Type t = Type.valueOf(arg0);
this.rewards.add(new Peer<Type, Object>(t, t.convert(arg1)));
this.rewards.add(new Peer<>(t, t.convert(arg1)));
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Invalid line : " + str + ": " + ex.getMessage());
} catch (Exception ex) {
@ -114,7 +114,7 @@ public class Challenge {
return item;
}
public static enum Type {
public enum Type {
ITEM {
// An item
@ -122,7 +122,7 @@ public class Challenge {
* Convert the value to a useable ItemStack
*/
@Override
public Object convert(String value) throws IllegalArgumentException {
public ItemStack convert(String value) throws IllegalArgumentException {
if (value == null || "".equalsIgnoreCase(value.trim()))
throw new IllegalArgumentException("Value is empty or null");
int index = value.indexOf(' ');
@ -195,7 +195,7 @@ public class Challenge {
// A command to execute
@Override
public Object convert(String value) throws IllegalArgumentException {
public String convert(String value) throws IllegalArgumentException {
// Here we don't have to convert the value because the value is the command
if (value == null || "".equalsIgnoreCase(value))
throw new IllegalArgumentException("Value is empty or null");
@ -224,7 +224,7 @@ public class Challenge {
// The level of island of a player
@Override
public Object convert(String value) throws IllegalArgumentException {
public Integer convert(String value) throws IllegalArgumentException {
// Convert the value to an Integer representing the minimum level of island
// required
if (value == null || "".equalsIgnoreCase(value))
@ -262,7 +262,7 @@ public class Challenge {
NEAR {
@Override
public Object convert(String value) throws IllegalArgumentException {
public Peer<EntityType, Integer> convert(String value) throws IllegalArgumentException {
// We returns the entity type and the number of entity required
if (value == null || "".equalsIgnoreCase(value))
throw new IllegalArgumentException("Value is empty or null");
@ -320,7 +320,7 @@ public class Challenge {
private Pattern space = Pattern.compile(" ");
@Override
public Object convert(String value) throws IllegalArgumentException {
public Peer<PotionType, Peer<Integer, Integer>> convert(String value) throws IllegalArgumentException {
// We returns the potion required
if (value == null || "".equalsIgnoreCase(value))
throw new IllegalArgumentException("Value is empty or null");
@ -336,9 +336,6 @@ public class Challenge {
throw new IllegalArgumentException(
"\"" + split[0] + "\" isn't a correct Minecraft PotionType (value = \"" + value + "\")");
}
if (pt == null)
throw new IllegalArgumentException(
"\"" + split[0] + "\" isn't a correct Minecraft PotionType (value = \"" + value + "\")");
// The data
int data;
try {
@ -356,7 +353,7 @@ public class Challenge {
throw new IllegalArgumentException(
"\"" + split[2] + "\" isn't a correct number (value = \"" + value + "\")");
}
return new Peer<PotionType, Peer<Integer, Integer>>(pt, new Peer<>(data, amount));
return new Peer<>(pt, new Peer<>(data, amount));
}
@Override
@ -441,11 +438,6 @@ public class Challenge {
* <li>7 = lingering extended</li>
* <li>8 = lingering ++</li>
* </ul>
*
* @param is
* @param type
* @param data
* @return
*/
private boolean isSame(ItemStack is, PotionType type, int data) {
if (data <= 2 && is.getType() != Material.POTION)
@ -462,9 +454,85 @@ public class Challenge {
return false;
else if ((data == 1 || data == 4 || data == 7) && !pd.isExtended())
return false;
else if ((data == 2 || data == 5 || data == 8) && !pd.isUpgraded())
return false;
return true;
else return (data != 2 && data != 5 && data != 8) || pd.isUpgraded();
}
},
ECO {
@Override
public Double convert(String value) throws IllegalArgumentException {
if (value == null || "".equalsIgnoreCase(value))
throw new IllegalArgumentException("Value is empty or null");
try {
return Double.parseDouble(value);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(
"\"" + value + "\" isn't a correct number (value = \"" + value + "\")");
}
}
@Override
public boolean has(Player p, Object obj) {
if (obj instanceof Number) {
return EconomyManager.getBalance(p) >= ((Number) obj).doubleValue();
}
return false;
}
@Override
public void executeRequire(Player p, Object obj) {
if (obj instanceof Number && ((Number) obj).doubleValue() > 0) {
EconomyManager.withdrawBalance(p, ((Number) obj).doubleValue());
}
}
@Override
public void executeReward(Player p, Object obj) {
if (obj instanceof Number && ((Number) obj).doubleValue() > 0) {
EconomyManager.deposit(p, ((Number) obj).doubleValue());
}
}
},
BANK {
@Override
public Double convert(String value) throws IllegalArgumentException {
if (value == null || "".equalsIgnoreCase(value))
throw new IllegalArgumentException("Value is empty or null");
try {
return Double.parseDouble(value);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(
"\"" + value + "\" isn't a correct number (value = \"" + value + "\")");
}
}
@Override
public boolean has(Player p, Object obj) {
Island is = SkyBlock.getInstance().getIslandManager().getIsland(p);
if (is != null && obj instanceof Number && ((Number) obj).doubleValue() > 0) {
return is.getBankBalance() >= ((Number) obj).doubleValue();
}
return false;
}
@Override
public void executeRequire(Player p, Object obj) {
Island is = SkyBlock.getInstance().getIslandManager().getIsland(p);
BankManager bankManager = SkyBlock.getInstance().getBankManager();
if (is != null && obj instanceof Number && ((Number) obj).doubleValue() > 0) {
bankManager.withdraw(p, is, ((Number) obj).doubleValue(), false);
}
}
@Override
public void executeReward(Player p, Object obj) {
Island is = SkyBlock.getInstance().getIslandManager().getIsland(p);
BankManager bankManager = SkyBlock.getInstance().getBankManager();
if (is != null && obj instanceof Number) {
bankManager.deposit(p, is, ((Number) obj).doubleValue(), false);
}
}
};

View File

@ -173,10 +173,12 @@ public class PlayerManager {
done.put(c, count + 1);
addChallenge(uuid, c);
// Take items
for (Peer<Type, Object> peer : c.getRequires())
for (Peer<Type, Object> peer : c.getRequires()) {
peer.getKey().executeRequire(p, peer.getValue());
for (Peer<Type, Object> peer : c.getRewards())
}
for (Peer<Type, Object> peer : c.getRewards()) {
peer.getKey().executeReward(p, peer.getValue());
}
// Ok, send message
String broadcast = ChatColor.translateAlternateColorCodes('&',
SkyBlock.getInstance().getFileManager()

View File

@ -324,12 +324,15 @@ challenges:
# LEVEL:<level> ==> the player's island needs to be at least <level> level
# NEAR:<EntityType> <amount> ==> <amount> of <EntityType> must be next to the player
# POTION:<PotionType> <data> <amount>
# BANK:<amount>
# ECO:<amount>
# data must be: 0 = normal potion, 1 = extended, 2 = ++, 3 = splash, 4 = splash extended, 5 = splash ++, 6 = lingering, 7 = lingering extended, 8 = lingering ++
require:
- 'ITEM:cobblestone 128'
# The reward
# ITEM:<item> <count> means the player will have <count> <item> when the challenge is done
# CMD:<cmd> means a command will be executed by the console when the challenge is done
# BANK:<amount>
reward:
- 'CMD:eco give {player} 100'
- 'ITEM:iron_ingot 20'
@ -565,7 +568,8 @@ challenges:
- 'ITEM:cake 5'
- 'ITEM:pumpkin_pie 16'
reward:
- 'CMD:eco give {player} 300'
- 'CMD:eco give {player} 150'
- 'BANK:150'
- 'ITEM:diamond 10'
- 'ITEM:cow_spawn_egg 1'
maxtimes: 1
@ -583,7 +587,8 @@ challenges:
- '&c&lRewards:'
- '&e- 10 diamonds'
- '&e- 1 cow spawn egg'
- '&e- 300 $'
- '&e- 150 $'
- '&e- 150 $ to the bank island'
'2':
id: 2
name: '&2Hunter'
@ -1077,6 +1082,7 @@ challenges:
name: '&2The Neighbours'
require:
- 'NEAR:villager 10'
- 'BANK:100'
reward:
- 'CMD:eco give {player} 1000'
- 'ITEM:diamond_block 16'
@ -1091,6 +1097,7 @@ challenges:
lore:
- '&c&lFor this challenge, you''ll need:'
- '&e- Have 10 villagers in your proximity'
- '&e- 100 $ in the island bank'
- '&c&lRewards:'
- '&e- 16 diamond blocks'
- '&e- 1000 $'
@ -1820,6 +1827,7 @@ challenges:
id: 8
name: '&4THE IMPOSSIBLE CHALLENGE'
require:
- 'ECO:10000'
- 'ITEM:bedrock 64'
reward:
- 'CMD:eco give {player} 0'