Skip to content

Commit 6522e4a

Browse files
authored
Merge pull request #7 from theo546/dev
Merge dev into main
2 parents d358fcf + 496eea2 commit 6522e4a

5 files changed

Lines changed: 74 additions & 4 deletions

File tree

.github/workflows/release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
- id: commit
2525
run: echo "short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
2626
- name: Delete existing release
27+
if: github.ref != 'refs/heads/dev'
2728
env:
2829
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2930
run: |
@@ -52,5 +53,6 @@ jobs:
5253
name: RandomDrop ${{ steps.version.outputs.version }}
5354
files: ${{ steps.prepare.outputs.file }}
5455
prerelease: ${{ github.ref == 'refs/heads/dev' }}
56+
generate_release_notes: true
5557
env:
5658
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ The configuration file is generated inside `plugins/RandomDrop/config.yml`. Nota
3030
- **KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE** – keep custom names when an item is randomized (default: `false`)
3131
- **CLAIM_CRAFTED_ITEMS** – mark crafted items as claimed (default: `true`)
3232
- **RANDOMIZE_CRAFT** – randomize crafting outputs (default: `true`)
33+
- **PERSIST_LOOT_TABLE** – keep the randomized loot table across restarts (default: `true`)

RandomDrop/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.theo546</groupId>
66
<artifactId>RandomDrop</artifactId>
7-
<version>0.0.0</version>
7+
<version>1.1.0</version>
88
<packaging>jar</packaging>
99

1010
<name>RandomDrop</name>

RandomDrop/src/main/java/com/theo546/randomdrop/Main.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// RandomDrop License | Copyright theo546 - github.com/theo546
22
package com.theo546.randomdrop;
33

4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.nio.charset.StandardCharsets;
7+
import java.security.MessageDigest;
8+
import java.security.NoSuchAlgorithmException;
49
import java.util.ArrayList;
510
import java.util.Arrays;
611
import java.util.Collections;
@@ -13,6 +18,7 @@
1318
import org.bukkit.Material;
1419
import org.bukkit.Location;
1520
import org.bukkit.World;
21+
import org.bukkit.configuration.file.YamlConfiguration;
1622
import org.bukkit.enchantments.Enchantment;
1723
import org.bukkit.entity.Item;
1824
import org.bukkit.inventory.ItemStack;
@@ -29,8 +35,28 @@ public class Main extends JavaPlugin {
2935
public static boolean KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE;
3036
public static boolean CLAIM_CRAFTED_ITEMS;
3137
public static boolean RANDOMIZE_CRAFT;
38+
public static boolean PERSIST_LOOT_TABLE;
3239
public static Map<Material, Material> LOOT_TABLE;
3340

41+
private File getLootTableFile() {
42+
String hash = sha256(String.valueOf(SEED)).substring(0, 16);
43+
return new File(getDataFolder(), "loot_table_" + hash + ".yml");
44+
}
45+
46+
private static String sha256(String input) {
47+
try {
48+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
49+
byte[] encoded = digest.digest(input.getBytes(StandardCharsets.UTF_8));
50+
StringBuilder sb = new StringBuilder();
51+
for (byte b : encoded) {
52+
sb.append(String.format("%02x", b));
53+
}
54+
return sb.toString();
55+
} catch (NoSuchAlgorithmException e) {
56+
throw new RuntimeException(e);
57+
}
58+
}
59+
3460
@Override
3561
public void onEnable() {
3662
getConfig().addDefault("RANDOMIZE_DURABILITY", false);
@@ -41,6 +67,7 @@ public void onEnable() {
4167
getConfig().addDefault("KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE", false);
4268
getConfig().addDefault("CLAIM_CRAFTED_ITEMS", true);
4369
getConfig().addDefault("RANDOMIZE_CRAFT", true);
70+
getConfig().addDefault("PERSIST_LOOT_TABLE", true);
4471
getConfig().options().copyDefaults(true);
4572
saveConfig();
4673

@@ -52,13 +79,19 @@ public void onEnable() {
5279
KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE = getConfig().getBoolean("KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE");
5380
CLAIM_CRAFTED_ITEMS = getConfig().getBoolean("CLAIM_CRAFTED_ITEMS");
5481
RANDOMIZE_CRAFT = getConfig().getBoolean("RANDOMIZE_CRAFT");
82+
PERSIST_LOOT_TABLE = getConfig().getBoolean("PERSIST_LOOT_TABLE");
5583

5684
initLootTable();
5785
getServer().getPluginManager().registerEvents(new Listener(), this);
5886
}
5987

6088
private void initLootTable() {
6189
LOOT_TABLE = new HashMap<>();
90+
File file = getLootTableFile();
91+
if (PERSIST_LOOT_TABLE && !getDataFolder().exists()) {
92+
getDataFolder().mkdirs();
93+
}
94+
6295
World world = Bukkit.getWorlds().get(0);
6396
Location spawn = world.getSpawnLocation().clone();
6497
spawn.setY(255);
@@ -75,10 +108,44 @@ private void initLootTable() {
75108
}
76109
}
77110

111+
YamlConfiguration yaml = new YamlConfiguration();
112+
if (PERSIST_LOOT_TABLE && file.exists()) {
113+
try {
114+
yaml.load(file);
115+
for (String key : yaml.getKeys(false)) {
116+
Material k = Material.getMaterial(key);
117+
Material v = Material.getMaterial(yaml.getString(key));
118+
if (k != null && v != null) {
119+
LOOT_TABLE.put(k, v);
120+
}
121+
}
122+
} catch (Exception e) {
123+
getLogger().warning("Failed to load loot table: " + e.getMessage());
124+
}
125+
}
126+
78127
List<Material> shuffled = new ArrayList<>(valid);
79128
Collections.shuffle(shuffled, new Random(SEED));
80-
for (int i = 0; i < valid.size(); i++) {
81-
LOOT_TABLE.put(valid.get(i), shuffled.get(i));
129+
shuffled.removeAll(LOOT_TABLE.values());
130+
131+
boolean updated = false;
132+
for (Material m : valid) {
133+
if (!LOOT_TABLE.containsKey(m)) {
134+
if (shuffled.isEmpty()) break;
135+
LOOT_TABLE.put(m, shuffled.remove(0));
136+
updated = true;
137+
}
138+
}
139+
140+
if (PERSIST_LOOT_TABLE && (updated || !file.exists())) {
141+
for (Map.Entry<Material, Material> entry : LOOT_TABLE.entrySet()) {
142+
yaml.set(entry.getKey().name(), entry.getValue().name());
143+
}
144+
try {
145+
yaml.save(file);
146+
} catch (IOException e) {
147+
getLogger().warning("Failed to save loot table: " + e.getMessage());
148+
}
82149
}
83150
}
84151

RandomDrop/src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
main: com.theo546.randomdrop.Main
22
name: RandomDrop
3-
version: "1.0.6"
3+
version: "1.1.0"
44
api-version: 1.21
55
author: theo546
66
description: A Paper plugin to randomize the Minecraft loot table!

0 commit comments

Comments
 (0)