Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions AdvancedCore/Resources/Rewards/ExampleAdvanced.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,17 @@ ActionBar:
Message: '&cThis is an actionbar!'
# Delay until action bar goes away (in ticks)
Delay: 30

OnlyOneLucky: false
# The example below would give a 1 in 10 chance of receiving an extra $100, and a 1 in 50 chance of an extra $1000.
Lucky:
'10':
Messages:
Plyaer: 'You were lucky and received an extra $100!'
Money: 100
'50':
Messages:
Plyaer: 'You were lucky and received an extra $1000!'
Money: 1000


2 changes: 1 addition & 1 deletion AdvancedCore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.Ben12345rocks</groupId>
<artifactId>AdvancedCore</artifactId>
<version>2.8.1</version>
<version>2.8.2</version>
<packaging>jar</packaging>
<name>AdvancedCore</name>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class Reward {

/** The reward type. */
private String rewardType;

private boolean forceOffline;

/** The delay enabled. */
Expand Down Expand Up @@ -232,9 +233,13 @@ public class Reward {
private int effectRadius;

private ArrayList<String> javascripts;

private ArrayList<String> priority;

private HashMap<Integer, String> luckyRewards;

private boolean onlyOneLucky;

/**
* @return the name
*/
Expand Down Expand Up @@ -331,8 +336,8 @@ public boolean checkDelayed(User user) {
public boolean checkRandomChance() {
return MiscUtils.getInstance().checkChance(getRandomChance(), 100);
}
public void givePriorityReward(User user, final HashMap<String,String> placeholders) {

public void givePriorityReward(User user, final HashMap<String, String> placeholders) {
for (String str : getPriority()) {
Reward reward = RewardHandler.getInstance().getReward(str);
if (reward.canGiveReward(user)) {
Expand All @@ -341,7 +346,7 @@ public void givePriorityReward(User user, final HashMap<String,String> placehold
}
}
}

public boolean canGiveReward(User user) {
if (hasPermission(user)) {
return true;
Expand Down Expand Up @@ -1093,6 +1098,32 @@ public void giveRewardReward(User user, boolean online, HashMap<String, String>
}
}

public void giveLucky(User user, HashMap<String, String> placeholders) {
HashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
for (Entry<Integer, String> entry : luckyRewards.entrySet()) {
if (MiscUtils.getInstance().checkChance(1, entry.getKey())) {
// new RewardBuilder(getConfig().getData(),
// entry.getValue()).withPlaceHolder(placeholders).send(user);
map.put(entry.getValue(), entry.getKey());
}
}

map = ArrayUtils.getInstance().sortByValuesStr(map, false);
if (map.size() > 0) {
if (isOnlyOneLucky()) {
for (Entry<String, Integer> entry : map.entrySet()) {
new RewardBuilder(getConfig().getData(), entry.getKey()).withPlaceHolder(placeholders).send(user);
return;
}

} else {
for (Entry<String, Integer> entry : map.entrySet()) {
new RewardBuilder(getConfig().getData(), entry.getKey()).withPlaceHolder(placeholders).send(user);
}
}
}
}

/**
* Give reward user.
*
Expand All @@ -1114,7 +1145,7 @@ public void giveRewardUser(User user, HashMap<String, String> phs) {
Date date = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
phs.put("CurrentDate", "" + new SimpleDateFormat("EEE, d MMM yyyy HH:mm").format(date));
final HashMap<String, String> placeholders = new HashMap<String, String>(phs);
givePriorityReward(user,placeholders);
givePriorityReward(user, placeholders);
giveRandom(user, true, placeholders);
runJavascript(user, true, placeholders);
int money = getMoneyToGive();
Expand All @@ -1132,6 +1163,7 @@ public void giveRewardUser(User user, HashMap<String, String> phs) {
sendMessage(user, money, exp, placeholders);
checkChoiceRewards(user);
sendFirework(user);
giveLucky(user, placeholders);

plugin.debug("Gave " + user.getPlayerName() + " reward " + name);

Expand Down Expand Up @@ -1298,7 +1330,7 @@ public void load(File folder, String reward) {

public void loadValues() {
setRewardType(getConfig().getRewardType());

forceOffline = getConfig().getForceOffline();

setDelayEnabled(getConfig().getDelayedEnabled());
Expand Down Expand Up @@ -1391,8 +1423,36 @@ public void loadValues() {
effectData = getConfig().getEffectData();
effectParticles = getConfig().getEffectParticles();
effectRadius = getConfig().getEffectRadius();

priority = getConfig().getPriority();

luckyRewards = new HashMap<Integer, String>();

for (String str : getConfig().getLuckyRewards()) {
if (StringUtils.getInstance().isInt(str)) {
int num = Integer.parseInt(str);
if (num > 0) {
String path = getConfig().getLuckyRewardsPath(num);
luckyRewards.put(num, path);
}
}
}

onlyOneLucky = getConfig().getOnlyOneLucky();
}

/**
* @return the luckyRewards
*/
public HashMap<Integer, String> getLuckyRewards() {
return luckyRewards;
}

/**
* @return the onlyOneLucky
*/
public boolean isOnlyOneLucky() {
return onlyOneLucky;
}

/**
Expand All @@ -1403,7 +1463,8 @@ public boolean isForceOffline() {
}

/**
* @param forceOffline the forceOffline to set
* @param forceOffline
* the forceOffline to set
*/
public void setForceOffline(boolean forceOffline) {
this.forceOffline = forceOffline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,22 @@ public ArrayList<String> getFireworkColorsFadeOut() {
return (ArrayList<String>) getData().getList("Firework.FadeOutColor", new ArrayList<String>());
}

public Set<String> getLuckyRewards() {
if (getData().getConfigurationSection("Lucky") != null) {
return getData().getConfigurationSection("Lucky").getKeys(false);
} else {
return new HashSet<String>();
}
}

public boolean getOnlyOneLucky() {
return getData().getBoolean("OnlyOneLucky");
}

public String getLuckyRewardsPath(int num) {
return "Lucky." + num;
}

/**
* Gets the firework enabled.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.time.temporal.WeekFields;
import java.util.Locale;

import org.bukkit.Bukkit;

import com.Ben12345rocks.AdvancedCore.AdvancedCoreHook;
import com.Ben12345rocks.AdvancedCore.Data.ServerData;
import com.Ben12345rocks.AdvancedCore.Listeners.DateChangedEvent;
Expand Down Expand Up @@ -96,37 +94,33 @@ public boolean hasWeekChanged() {
* Update.
*/
public void update() {
Bukkit.getScheduler().runTaskAsynchronously(plugin.getPlugin(), new Runnable() {

@Override
public void run() {
boolean dayChanged = false;
boolean weekChanged = false;
boolean monthChanged = false;
if (hasDayChanged()) {
plugin.debug("Day changed");
dayChanged = true;
}
if (hasWeekChanged()) {
plugin.debug("Week Changed");
weekChanged = true;
}
if (hasMonthChanged()) {
plugin.debug("Month Changed");
monthChanged = true;
}

if (dayChanged) {
forceChanged(TimeType.DAY);
}
if (weekChanged) {
forceChanged(TimeType.WEEK);
}
if (monthChanged) {
forceChanged(TimeType.MONTH);
}
}
});

boolean dayChanged = false;
boolean weekChanged = false;
boolean monthChanged = false;
if (hasDayChanged()) {
plugin.debug("Day changed");
dayChanged = true;
}
if (hasWeekChanged()) {
plugin.debug("Week Changed");
weekChanged = true;
}
if (hasMonthChanged()) {
plugin.debug("Month Changed");
monthChanged = true;
}

if (dayChanged) {
forceChanged(TimeType.DAY);
}
if (weekChanged) {
forceChanged(TimeType.WEEK);
}
if (monthChanged) {
forceChanged(TimeType.MONTH);
}

}

public void forceChanged(TimeType time) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,32 @@ public int compare(Entry<User, Integer> o1, Entry<User, Integer> o2) {
return sortedMap;
}

public HashMap<String, Integer> sortByValuesStr(HashMap<String, Integer> unsortMap, final boolean order) {

List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet());

// Sorting the list based on values
Collections.sort(list, new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
if (order) {
return o1.getValue().compareTo(o2.getValue());
} else {
return o2.getValue().compareTo(o1.getValue());

}
}
});

// Maintaining insertion order with the help of LinkedList
HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Entry<String, Integer> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}

return sortedMap;
}

public HashMap<User, Long> sortByValuesLong(HashMap<User, Long> unsortMap, final boolean order) {

List<Entry<User, Long>> list = new LinkedList<Entry<User, Long>>(unsortMap.entrySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public boolean checkChance(double chance, double outOf) {

double randomNum = ThreadLocalRandom.current().nextDouble(outOf);

plugin.debug("Chance: " + chance + ", RandomNum: " + randomNum);
plugin.debug("Chance Required: " + chance + ", RandomNum: " + randomNum);

if (randomNum <= chance) {
return true;
Expand Down