Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Added command rewards to treasure chests
Browse files Browse the repository at this point in the history
  • Loading branch information
RadBuilder committed Oct 22, 2017
1 parent 3f2c46f commit e1ce6b9
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package be.isach.ultracosmetics.treasurechests;

import be.isach.ultracosmetics.UltraCosmeticsData;
import be.isach.ultracosmetics.util.CustomConfiguration;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Material;

/**
* A command reward.
*
* @author RadBuilder
* @since 10-21-2017
*/
public class CommandReward {
private String name;
private Material material;
private int chance;
private boolean messageEnabled;
private String message;
private List<String> commands;

public CommandReward(String path) {
CustomConfiguration config = UltraCosmeticsData.get().getPlugin().getConfig();
chance = config.getInt(path + ".Chance");
messageEnabled = config.getBoolean(path + ".Message.enabled");
message = config.getString(path + ".Message.message");
commands = config.getStringList(path + ".Commands");
material = Material.valueOf(config.getString(path + ".Material"));
name = ChatColor.translateAlternateColorCodes('&', config.getString(path + ".Name"));
}

public int getChance() {
return chance;
}

public boolean getMessageEnabled() {
return messageEnabled;
}

public String getMessage() {
return message;
}

public List<String> getCommands() {
return commands;
}

public String getName() {
return name;
}

public Material getMaterial() {
return material;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import be.isach.ultracosmetics.cosmetics.type.ParticleEffectType;
import be.isach.ultracosmetics.cosmetics.type.PetType;
import be.isach.ultracosmetics.cosmetics.type.SuitType;
import be.isach.ultracosmetics.util.CustomConfiguration;
import be.isach.ultracosmetics.util.MathUtils;
import be.isach.ultracosmetics.util.SoundUtil;
import be.isach.ultracosmetics.util.Sounds;
import be.isach.ultracosmetics.util.TextUtil;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
Expand All @@ -39,27 +41,41 @@
* Created by sacha on 19/08/15.
*/
public class TreasureRandomizer {
private static final int MONEY_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Money.Chance");
private static final int GADGET_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Gadgets.Chance");
private static final int AMMO_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Gadgets-Ammo.Chance");
private static final int MORPHS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Morphs.Chance");
private static final int PETS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Pets.Chance");
private static final int EFFECTS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Effects.Chance");
private static final int MOUNTS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Mounts.Chance");
private static final int HATS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Hats.Chance");
private static final int HELMET_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Suits.Chance") / 4;
private static final int CHESTPLATE_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Suits.Chance") / 4;
private static final int LEGGINGS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Suits.Chance") / 4;
private static final int BOOTS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Suits.Chance") / 4;
private static final int EMOTES_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Emotes.Chance");

Player player;
private static List<GadgetType> gadgetList = new ArrayList<>();
private static List<GadgetType> ammoList = new ArrayList<>();
private static List<ParticleEffectType> particleEffectList = new ArrayList<>();
private static List<MountType> mountList = new ArrayList<>();
private static List<PetType> petList = new ArrayList<>();
private static List<MorphType> morphList = new ArrayList<>();
private static List<HatType> hatList = new ArrayList<>();
private static List<SuitType> helmetList = new ArrayList<>();
private static List<SuitType> chestplateList = new ArrayList<>();
private static List<SuitType> leggingList = new ArrayList<>();
private static List<SuitType> bootList = new ArrayList<>();
private static List<EmoteType> emoteList = new ArrayList<>();
private static List<CommandReward> commandRewardList = new ArrayList<>();

private static Random random = new Random();

private Player player;
public Location loc;
private ItemStack itemStack;
private String name;

public static List<GadgetType> gadgetList = new ArrayList<>();
public static List<GadgetType> ammoList = new ArrayList<>();
public static List<ParticleEffectType> particleEffectList = new ArrayList<>();
public static List<MountType> mountList = new ArrayList<>();
public static List<PetType> petList = new ArrayList<>();
public static List<MorphType> morphList = new ArrayList<>();
public static List<HatType> hatList = new ArrayList<>();
public static List<SuitType> helmetList = new ArrayList<>();
public static List<SuitType> chestplateList = new ArrayList<>();
public static List<SuitType> leggingList = new ArrayList<>();
public static List<SuitType> bootList = new ArrayList<>();
public static List<EmoteType> emoteList = new ArrayList<>();

private static Random random = new Random();

private enum ResultType {
AMMO,
GADGET,
Expand All @@ -73,25 +89,12 @@ private enum ResultType {
CHESTPLATE,
LEGGINGS,
BOOTS,
EMOTE
EMOTE,
COMMAND
}

private static final List<ResultType> RESULT_TYPES = new ArrayList<>();

private static final int MONEY_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Money.Chance");
private static final int GADGET_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Gadgets.Chance");
private static final int AMMO_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Gadgets-Ammo.Chance");
private static final int MORPHS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Morphs.Chance");
private static final int PETS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Pets.Chance");
private static final int EFFECTS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Effects.Chance");
private static final int MOUNTS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Mounts.Chance");
private static final int HATS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Hats.Chance");
private static final int HELMET_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Suits.Chance") / 4;
private static final int CHESTPLATE_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Suits.Chance") / 4;
private static final int LEGGINGS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Suits.Chance") / 4;
private static final int BOOTS_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Suits.Chance") / 4;
private static final int EMOTES_CHANCE = SettingsManager.getConfig().getInt("TreasureChests.Loots.Emotes.Chance");

private static void setupChance(List<ResultType> resultRef, int percent, ResultType resultType) {
for (int i = 0; i < percent; i++) {
resultRef.add(resultType);
Expand Down Expand Up @@ -177,7 +180,19 @@ public TreasureRandomizer(final Player player, Location location) {
if (emoteType.canBeFound()
&& !player.hasPermission(emoteType.getPermission()))
emoteList.add(emoteType);

if (commandRewardList.isEmpty()) {
CustomConfiguration config = UltraCosmeticsData.get().getPlugin().getConfig();

for (String key : config.getConfigurationSection("TreasureChests.Loots.Commands").getKeys(false)) {
String path = "TreasureChests.Loots.Commands." + key;
if (config.getBoolean(path + ".Enabled")) {
String cancelPermission = config.getString(path + ".Cancel-If-Permission");
if (cancelPermission.equals("no") || !player.hasPermission(cancelPermission)) {
commandRewardList.add(new CommandReward(path));
}
}
}
}

if (!Category.MOUNTS.isEnabled())
mountList.clear();
Expand Down Expand Up @@ -253,6 +268,12 @@ public TreasureRandomizer(final Player player, Location location) {
&& !emoteList.isEmpty()
&& (boolean) SettingsManager.getConfig().get("TreasureChests.Loots.Emotes.Enabled"))
setupChance(RESULT_TYPES, EMOTES_CHANCE, ResultType.EMOTE);

for (CommandReward commandReward : commandRewardList) {
for (int i = 0; i < commandReward.getChance(); i++) {
RESULT_TYPES.add(ResultType.COMMAND);
}
}
}

private String getMessage(String s) {
Expand Down Expand Up @@ -324,6 +345,9 @@ public void giveRandomThing() {
case EMOTE:
giveRandomEmote();
break;
case COMMAND:
giveRandomCommandReward();
break;
}

} catch (IndexOutOfBoundsException | IllegalArgumentException exception) {
Expand Down Expand Up @@ -363,6 +387,7 @@ public void clear() {
leggingList.clear();
bootList.clear();
emoteList.clear();
commandRewardList.clear();
RESULT_TYPES.clear();
types.clear();
}
Expand Down Expand Up @@ -530,6 +555,26 @@ public void giveRandomMorph() {
.replace("%name%", player.getName()).replace("%morph%", (UltraCosmeticsData.get().arePlaceholdersColored()) ? morph.getName() : TextUtil.filterColor(morph.getName())));
}

public void giveRandomCommandReward() {
ArrayList<CommandReward> rewards = new ArrayList<>();
for (CommandReward commandReward : commandRewardList) {
for (int i = 0; i < commandReward.getChance(); i++) {
rewards.add(commandReward);
}
}
CommandReward reward = rewards.get(random.nextInt(rewards.size()));
rewards.clear();
for (String command : reward.getCommands()) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ChatColor.translateAlternateColorCodes('&', command.replace("%name%", player.getName())));
}
name = reward.getName().replace("%name%", player.getName());
itemStack = new ItemStack(reward.getMaterial());
spawnRandomFirework(loc);

if (reward.getMessageEnabled())
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', reward.getMessage().replace("%name%", player.getName()).replace("%prefix%", MessageManager.getMessage("Prefix"))));
}


public static FireworkEffect getRandomFireworkEffect() {
if (!UltraCosmeticsData.get().getPlugin().isEnabled())
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ TreasureChests:
Message:
enabled: false
message: '%prefix% &6&l%name% found rare %emote%'
Commands:
shoutout:
Name: '&d&lShoutout'
Material: 'NETHER_STAR'
Enabled: false
Chance: 100
Message:
enabled: false
message: '%prefix% &6&l%name% found a rare shoutout!'
Cancel-If-Permission: 'no'
Commands:
- 'say %name% is awesome!'
flower:
Name: '&e&lFlower'
Material: 'YELLOW_FLOWER'
Enabled: false
Chance: 100
Message:
enabled: true
message: '%prefix% &6&l%name% found a flower!'
Cancel-If-Permission: 'example.yellowflower'
Commands:
- 'give %name% yellow_flower 1'
- 'pex user %name% add example.yellowflower'
Designs:
Classic:
center-block: '169:0'
Expand Down

0 comments on commit e1ce6b9

Please sign in to comment.