Skip to content

Commit 388cc06

Browse files
authored
Merge pull request #36 from LaBoulangerie/fix/lack_of_RandomGenerator
Money, timber et java module
2 parents a391aae + 757dbd1 commit 388cc06

10 files changed

Lines changed: 89 additions & 212 deletions

File tree

build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ dependencies {
7070
compileOnly("redis.clients:jedis:5.1.3") {
7171
isTransitive = false
7272
}
73-
compileOnly("com.github.MilkBowl:VaultAPI:1.7.1")
7473
compileOnly("fr.minelet:minelet-api:1.2.0")
7574
compileOnly("me.clip:placeholderapi:2.11.6")
7675
compileOnly("com.palmergames.bukkit.towny:towny:0.100.4.0")

src/main/java/net/laboulangerie/laboulangeriemmo/LaBoulangerieMmo.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import java.text.NumberFormat;
66
import java.util.Arrays;
77
import java.util.Locale;
8-
import java.util.logging.Level;
98
import org.betonquest.betonquest.api.BetonQuestApi;
109
import org.betonquest.betonquest.api.BetonQuestApiService;
11-
import org.bukkit.plugin.RegisteredServiceProvider;
1210
import org.bukkit.plugin.java.JavaPlugin;
1311
import net.laboulangerie.laboulangeriemmo.api.ability.AbilitiesRegistry;
1412
import net.laboulangerie.laboulangeriemmo.api.player.MmoPlayerListener;
@@ -49,11 +47,9 @@
4947
import net.laboulangerie.laboulangeriemmo.listener.ServerListener;
5048
import net.laboulangerie.laboulangeriemmo.listener.XpBoostListener;
5149
import net.laboulangerie.laboulangeriemmo.utils.WolrdGuardSupport;
52-
import net.milkbowl.vault.economy.Economy;
5350

5451
public class LaBoulangerieMmo extends JavaPlugin {
5552
public static LaBoulangerieMmo PLUGIN;
56-
public static Economy ECONOMY = null;
5753
public static double XP_MULTIPLIER = 0.1;
5854
public static TalentsRegistry talentsRegistry = null;
5955
public static AbilitiesRegistry abilitiesRegistry = null;
@@ -89,12 +85,6 @@ public void onEnable() {
8985
saveDefaultConfig();
9086
reloadNumberFormatter();
9187
setupMineLetProtection();
92-
if (!setupEconomy()) {
93-
getLogger().log(Level.SEVERE, "Can't load the plugin, Vault isn't present");
94-
getServer().getPluginManager().disablePlugin(this);
95-
return;
96-
}
97-
9888
if (getServer().getPluginManager().getPlugin("MythicMobs") != null) {
9989
MYTHICMOBS_SUPPORT = true;
10090
getLogger().info("Hooked into MythicMobs!");
@@ -241,14 +231,4 @@ private void reloadNumberFormatter() {
241231
formatter.applyPattern("#.##");
242232
}
243233

244-
private boolean setupEconomy() {
245-
if (getServer().getPluginManager().getPlugin("Vault") == null)
246-
return false;
247-
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
248-
if (rsp == null) {
249-
return false;
250-
}
251-
ECONOMY = rsp.getProvider();
252-
return ECONOMY != null;
253-
}
254234
}

src/main/java/net/laboulangerie/laboulangeriemmo/core/abilities/woodcutting/Timber.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public Timber(AbilityArchetype archetype) {
3636
{1, 1, -1}, {-1, 1, 1}, {-1, 1, -1}, {1, -1, 0}, {-1, -1, 0}, {0, -1, 1},
3737
{0, -1, -1}, {1, -1, 1}, {1, -1, -1}, {-1, -1, 1}, {-1, -1, -1}};
3838

39-
private static final int RANGE = 5;
39+
private static final int TIER_ONE_RANGE = 3;
40+
private static final int TIER_TWO_RANGE = 5;
41+
private static final int TIER_THREE_RANGE = 7;
4042

4143
private Material initType;
4244
private Location initLocation;
@@ -63,7 +65,8 @@ public void trigger(Event baseEvent, int level) {
6365

6466
initType = block.getType();
6567
initLocation = block.getLocation();
66-
List<List<Block>> treeByDepth = findTreeByDepth();
68+
int range = rangeForLevel(level);
69+
List<List<Block>> treeByDepth = findTreeByDepth(range);
6770

6871
for (int depth = 0; depth < treeByDepth.size(); depth++) {
6972
List<Block> allowedBlocks = treeByDepth.get(depth).stream()
@@ -74,19 +77,25 @@ public void trigger(Event baseEvent, int level) {
7477

7578
long delay = depth * 5L;
7679
if (delay == 0L) {
77-
allowedBlocks.forEach(this::breakIfAllowed);
80+
allowedBlocks.forEach(candidate -> breakIfAllowed(candidate, range));
7881
} else {
7982
new BukkitRunnable() {
8083
@Override
8184
public void run() {
82-
allowedBlocks.forEach(Timber.this::breakIfAllowed);
85+
allowedBlocks.forEach(candidate -> Timber.this.breakIfAllowed(candidate, range));
8386
}
8487
}.runTaskLater(LaBoulangerieMmo.PLUGIN, delay);
8588
}
8689
}
8790
}
8891

89-
private List<List<Block>> findTreeByDepth() {
92+
int rangeForLevel(int level) {
93+
if (level >= getTier(2)) return TIER_THREE_RANGE;
94+
if (level >= getTier(1)) return TIER_TWO_RANGE;
95+
return TIER_ONE_RANGE;
96+
}
97+
98+
private List<List<Block>> findTreeByDepth(int range) {
9099
List<List<Block>> treeByDepth = new ArrayList<>();
91100
Queue<TreeNode> queue = new ArrayDeque<>();
92101
Set<Location> visited = new HashSet<>();
@@ -95,14 +104,14 @@ private List<List<Block>> findTreeByDepth() {
95104
while (!queue.isEmpty()) {
96105
TreeNode node = queue.remove();
97106
Location location = node.block().getLocation();
98-
if (!visited.add(location) || !isMatchingTreeBlock(node.block())) continue;
107+
if (!visited.add(location) || !isMatchingTreeBlock(node.block(), range)) continue;
99108

100109
while (treeByDepth.size() <= node.depth()) treeByDepth.add(new ArrayList<>());
101110
treeByDepth.get(node.depth()).add(node.block());
102111

103112
for (int[] coordinate : REL_COORDINATES) {
104113
Block neighbour = node.block().getRelative(coordinate[0], coordinate[1], coordinate[2]);
105-
if (!visited.contains(neighbour.getLocation()) && isInsideRange(neighbour)) {
114+
if (!visited.contains(neighbour.getLocation()) && isInsideRange(neighbour, range)) {
106115
queue.add(new TreeNode(neighbour, node.depth() + 1));
107116
}
108117
}
@@ -111,21 +120,21 @@ private List<List<Block>> findTreeByDepth() {
111120
return treeByDepth;
112121
}
113122

114-
private boolean isMatchingTreeBlock(Block candidate) {
123+
private boolean isMatchingTreeBlock(Block candidate, int range) {
115124
Material logType = Material.getMaterial(initType.toString().replace("_WOOD", "_LOG"));
116125
Material woodType = Material.getMaterial(initType.toString().replace("_LOG", "_WOOD"));
117-
return isInsideRange(candidate)
126+
return isInsideRange(candidate, range)
118127
&& (candidate.getType() == logType || candidate.getType() == woodType);
119128
}
120129

121-
private boolean isInsideRange(Block candidate) {
130+
private boolean isInsideRange(Block candidate, int range) {
122131
return candidate.getY() >= initLocation.getBlockY()
123-
&& Math.abs(candidate.getX() - initLocation.getBlockX()) <= RANGE
124-
&& Math.abs(candidate.getZ() - initLocation.getBlockZ()) <= RANGE;
132+
&& Math.abs(candidate.getX() - initLocation.getBlockX()) <= range
133+
&& Math.abs(candidate.getZ() - initLocation.getBlockZ()) <= range;
125134
}
126135

127-
private void breakIfAllowed(Block candidate) {
128-
if (!isMatchingTreeBlock(candidate)
136+
private void breakIfAllowed(Block candidate, int range) {
137+
if (!isMatchingTreeBlock(candidate, range)
129138
|| !LaBoulangerieMmo.PLUGIN.getTalentProtection().canBreak(player, candidate)) {
130139
return;
131140
}

src/main/java/net/laboulangerie/laboulangeriemmo/core/rareloot/RareLootEngine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.HashSet;
44
import java.util.Locale;
55
import java.util.Set;
6+
import java.util.SplittableRandom;
67
import java.util.random.RandomGenerator;
78
import net.laboulangerie.laboulangeriemmo.LaBoulangerieMmo;
89
import net.laboulangerie.laboulangeriemmo.api.player.MmoPlayer;
@@ -23,7 +24,7 @@ public final class RareLootEngine {
2324
private volatile RareLootRegistry registry = RareLootRegistry.empty();
2425

2526
public RareLootEngine(RareLootItemProviderRegistry providers) {
26-
this(providers, RandomGenerator.getDefault());
27+
this(providers, new SplittableRandom());
2728
}
2829

2930
RareLootEngine(RareLootItemProviderRegistry providers, RandomGenerator random) {

src/main/java/net/laboulangerie/laboulangeriemmo/listener/MmoListener.java

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import java.util.List;
66

77
import org.bukkit.Bukkit;
8-
import org.bukkit.OfflinePlayer;
98
import org.bukkit.configuration.file.FileConfiguration;
10-
import org.bukkit.configuration.ConfigurationSection;
119
import org.bukkit.entity.Player;
1210
import org.bukkit.event.EventHandler;
1311
import org.bukkit.event.Listener;
@@ -26,40 +24,21 @@
2624
import net.laboulangerie.laboulangeriemmo.core.XpBar;
2725
import net.laboulangerie.laboulangeriemmo.events.PlayerLevelUpEvent;
2826
import net.laboulangerie.laboulangeriemmo.events.XpCountDownFinishedEvent;
29-
import net.milkbowl.vault.economy.EconomyResponse;
3027

3128
public class MmoListener implements Listener {
3229

3330
@EventHandler
3431
public void onLevelUp(PlayerLevelUpEvent event) {
3532
FileConfiguration config = LaBoulangerieMmo.PLUGIN.getConfig();
3633

37-
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(event.getPlayer().getUniqueId());
38-
Player player = offlinePlayer.getPlayer();
34+
Player player = Bukkit.getPlayer(event.getPlayer().getUniqueId());
3935
Talent talent = event.getTalent();
4036
TalentArchetype talentArchetype = LaBoulangerieMmo.talentsRegistry.getTalent(talent.getTalentId());
4137

4238
List<TagResolver.Single> resolvers = new ArrayList<>();
4339
resolvers.add(Placeholder.parsed("level", Integer.toString(event.getNewLevel())));
4440
resolvers.add(Placeholder.parsed("talent", talent.getDisplayName()));
4541

46-
ConfigurationSection rewards =
47-
config.getConfigurationSection("level-up-rewards." + talent.getTalentId());
48-
double amount = calculateLevelRewards(
49-
rewards, event.getPreviousLevel(), event.getNewLevel(), config.getString("rewards-rounding-method", "no"));
50-
boolean rewardPaid = false;
51-
if (amount > 0) {
52-
EconomyResponse response = LaBoulangerieMmo.ECONOMY.depositPlayer(offlinePlayer, amount);
53-
if (response.transactionSuccess()) {
54-
rewardPaid = true;
55-
resolvers.add(Placeholder.parsed("reward", LaBoulangerieMmo.ECONOMY.format(amount)));
56-
} else {
57-
LaBoulangerieMmo.PLUGIN.getLogger().warning("Unable to pay " + amount + " to "
58-
+ offlinePlayer.getName() + " for " + talent.getTalentId() + " levels "
59-
+ (event.getPreviousLevel() + 1) + "-" + event.getNewLevel() + ": " + response.errorMessage);
60-
}
61-
}
62-
6342
if (player == null) return;
6443
Component prefix = MiniMessage.miniMessage().deserialize(config.getString("lang.prefix"));
6544

@@ -86,9 +65,8 @@ public void onLevelUp(PlayerLevelUpEvent event) {
8665
}
8766
}
8867

89-
String levelMessage = rewardPaid ? "lang.messages.level-up" : "lang.messages.level-up-no-reward";
9068
Component lvlUpComponent = MiniMessage.miniMessage()
91-
.deserialize(config.getString(levelMessage), TagResolver.resolver(resolvers));
69+
.deserialize(config.getString("lang.messages.level-up"), TagResolver.resolver(resolvers));
9270
player.sendMessage(prefix.append(lvlUpComponent));
9371

9472
Component titleComponent =
@@ -129,54 +107,6 @@ public void onCountDownFinished(XpCountDownFinishedEvent event) {
129107
player.sendActionBar(message);
130108
}
131109

132-
static double calculateLevelReward(ConfigurationSection rewards, int level, double levelXp) {
133-
if (rewards == null || level <= 0) return 0;
134-
135-
double amount = processMoneyAmount(rewards.getString("*"), levelXp);
136-
amount += processMoneyAmount(rewards.getString(Integer.toString(level)), levelXp);
137-
138-
ConfigurationSection progressive = rewards.getConfigurationSection("progressive");
139-
if (progressive == null || !progressive.isSet("levels-per-step")
140-
|| !progressive.isSet("amount-per-step")) {
141-
return amount;
142-
}
143-
144-
int levelsPerStep = progressive.getInt("levels-per-step");
145-
double amountPerStep = progressive.getDouble("amount-per-step");
146-
if (levelsPerStep <= 0 || amountPerStep <= 0) return amount;
147-
148-
return amount + ((level / levelsPerStep) + 1) * amountPerStep;
149-
}
150-
151-
static double calculateLevelRewards(
152-
ConfigurationSection rewards, int previousLevel, int newLevel, String roundingMethod) {
153-
return calculateLevelRewards(
154-
rewards, previousLevel, newLevel, roundingMethod, LaBoulangerieMmo.XP_MULTIPLIER);
155-
}
156-
157-
static double calculateLevelRewards(ConfigurationSection rewards, int previousLevel, int newLevel,
158-
String roundingMethod, double xpMultiplier) {
159-
if (newLevel <= previousLevel) return 0;
160-
if (xpMultiplier <= 0 || !Double.isFinite(xpMultiplier)) return 0;
161-
162-
double total = 0;
163-
for (int level = Math.max(1, previousLevel + 1); level <= newLevel; level++) {
164-
double levelXp = Math.pow(level / xpMultiplier, 2);
165-
total += roundReward(calculateLevelReward(rewards, level, levelXp), roundingMethod);
166-
}
167-
return total;
168-
}
169-
170-
private static double roundReward(double amount, String method) {
171-
if (method == null) return amount;
172-
return switch (method) {
173-
case "closest" -> Math.round(amount);
174-
case "up" -> Math.ceil(amount);
175-
case "down" -> Math.floor(amount);
176-
default -> amount;
177-
};
178-
}
179-
180110
private static boolean crossedLevel(PlayerLevelUpEvent event, int requiredLevel) {
181111
return requiredLevel > event.getPreviousLevel() && requiredLevel <= event.getNewLevel();
182112
}
@@ -187,22 +117,4 @@ private static void sendAbilityMessage(Player player, Component prefix, String m
187117
player.sendMessage(prefix.append(component));
188118
}
189119

190-
private static double processMoneyAmount(String rawAmount, double levelXp) {
191-
if (rawAmount == null) return 0;
192-
193-
if (rawAmount.endsWith("%")) {
194-
double percentage = 0;
195-
try {
196-
percentage = Double.parseDouble(rawAmount.split("%")[0]);
197-
} catch (Exception e) {
198-
}
199-
return levelXp * percentage / 100;
200-
}
201-
try {
202-
return Double.parseDouble(rawAmount);
203-
} catch (Exception e) {
204-
}
205-
206-
return 0;
207-
}
208120
}

src/main/resources/config.yml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ lang:
1414
delay: 200
1515
messages:
1616
ability_log: <yellow>You used <aqua><ability></aqua>, cooldown for <aqua><cooldown> <unit></aqua>
17-
level-up: <yellow>You reached level <aqua><level> <talent></aqua> ! You earned <aqua><reward></aqua>
18-
level-up-no-reward: <yellow>You reached level <aqua><level> <talent></aqua> !
17+
level-up: <yellow>You reached level <aqua><level> <talent></aqua> !
1918
level-up-title: <bold><gradient:yellow:green>↑ LEVEL UP ↑
2019
level-up-subtitle: <aqua>Level <level> <talent>
2120
ability-unlocked: <yellow>Habilité <aqua><ability></aqua> débloquée !
@@ -319,9 +318,9 @@ talents:
319318
- 65
320319
- 95
321320
tier_descriptions:
321+
- "Abat les bûches connectées de même type dans un rayon horizontal de 3 blocs"
322322
- "Abat les bûches connectées de même type dans un rayon horizontal de 5 blocs"
323-
- "Abat les bûches connectées de même type dans un rayon horizontal de 5 blocs"
324-
- "Abat les bûches connectées de même type dans un rayon horizontal de 5 blocs"
323+
- "Abat les bûches connectées de même type dans un rayon horizontal de 7 blocs"
325324
stun:
326325
display_name: Étourdissement
327326
display_item: NETHER_STAR
@@ -571,26 +570,6 @@ talent-grinding:
571570
DIAMOND_HOE: 100
572571
NETHERITE_HOE: 200
573572

574-
#Define money rewards when a player levels up in one talent
575-
level-up-rewards:
576-
miner:
577-
progressive:
578-
levels-per-step: 10
579-
amount-per-step: 10
580-
hunter:
581-
progressive:
582-
levels-per-step: 10
583-
amount-per-step: 10
584-
lumberjack:
585-
progressive:
586-
levels-per-step: 10
587-
amount-per-step: 10
588-
farmer:
589-
progressive:
590-
levels-per-step: 10
591-
amount-per-step: 10
592-
rewards-rounding-method: "no" # Round rewards defined above, accepted values no, closest, up, down
593-
594573
blockus-ignored-blocks:
595574
- CARROTS
596575
- WHEAT

src/main/resources/plugin.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ version: 2.3.2
33
main: net.laboulangerie.laboulangeriemmo.LaBoulangerieMmo
44
api-version: '1.21.11'
55
authors: [TheHunter365, Grooble_, Pikachuz3, PainOchoco, BlackoutBurst, SunshineDev]
6-
depend: [Vault]
76
softdepend: [PlaceholderAPI, Towny, BetonQuest, WorldGuard, MythicMobs, LibsDisguises, Minelet]
87

98
libraries:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.laboulangerie.laboulangeriemmo.core.abilities.woodcutting;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.List;
6+
import org.junit.jupiter.api.Test;
7+
import net.laboulangerie.laboulangeriemmo.api.ability.AbilityArchetype;
8+
9+
class TimberTest {
10+
@Test
11+
void usesRangeForCurrentTier() {
12+
AbilityArchetype archetype = new AbilityArchetype();
13+
archetype.requiredLevel = 35;
14+
archetype.tiers = List.of(35, 65, 95);
15+
Timber timber = new Timber(archetype);
16+
17+
assertEquals(3, timber.rangeForLevel(35));
18+
assertEquals(3, timber.rangeForLevel(64));
19+
assertEquals(5, timber.rangeForLevel(65));
20+
assertEquals(5, timber.rangeForLevel(94));
21+
assertEquals(7, timber.rangeForLevel(95));
22+
assertEquals(7, timber.rangeForLevel(100));
23+
}
24+
}

0 commit comments

Comments
 (0)