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
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- id: commit
run: echo "short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
- name: Delete existing release
if: github.ref != 'refs/heads/dev'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
Expand Down Expand Up @@ -52,5 +53,6 @@ jobs:
name: RandomDrop ${{ steps.version.outputs.version }}
files: ${{ steps.prepare.outputs.file }}
prerelease: ${{ github.ref == 'refs/heads/dev' }}
generate_release_notes: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ The configuration file is generated inside `plugins/RandomDrop/config.yml`. Nota
- **KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE** – keep custom names when an item is randomized (default: `false`)
- **CLAIM_CRAFTED_ITEMS** – mark crafted items as claimed (default: `true`)
- **RANDOMIZE_CRAFT** – randomize crafting outputs (default: `true`)
- **PERSIST_LOOT_TABLE** – keep the randomized loot table across restarts (default: `true`)
2 changes: 1 addition & 1 deletion RandomDrop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.theo546</groupId>
<artifactId>RandomDrop</artifactId>
<version>0.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>RandomDrop</name>
Expand Down
71 changes: 69 additions & 2 deletions RandomDrop/src/main/java/com/theo546/randomdrop/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// RandomDrop License | Copyright theo546 - github.com/theo546
package com.theo546.randomdrop;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -13,6 +18,7 @@
import org.bukkit.Material;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
Expand All @@ -29,8 +35,28 @@ public class Main extends JavaPlugin {
public static boolean KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE;
public static boolean CLAIM_CRAFTED_ITEMS;
public static boolean RANDOMIZE_CRAFT;
public static boolean PERSIST_LOOT_TABLE;
public static Map<Material, Material> LOOT_TABLE;

private File getLootTableFile() {
String hash = sha256(String.valueOf(SEED)).substring(0, 16);
return new File(getDataFolder(), "loot_table_" + hash + ".yml");
}

private static String sha256(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encoded = digest.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte b : encoded) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

@Override
public void onEnable() {
getConfig().addDefault("RANDOMIZE_DURABILITY", false);
Expand All @@ -41,6 +67,7 @@ public void onEnable() {
getConfig().addDefault("KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE", false);
getConfig().addDefault("CLAIM_CRAFTED_ITEMS", true);
getConfig().addDefault("RANDOMIZE_CRAFT", true);
getConfig().addDefault("PERSIST_LOOT_TABLE", true);
getConfig().options().copyDefaults(true);
saveConfig();

Expand All @@ -52,13 +79,19 @@ public void onEnable() {
KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE = getConfig().getBoolean("KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE");
CLAIM_CRAFTED_ITEMS = getConfig().getBoolean("CLAIM_CRAFTED_ITEMS");
RANDOMIZE_CRAFT = getConfig().getBoolean("RANDOMIZE_CRAFT");
PERSIST_LOOT_TABLE = getConfig().getBoolean("PERSIST_LOOT_TABLE");

initLootTable();
getServer().getPluginManager().registerEvents(new Listener(), this);
}

private void initLootTable() {
LOOT_TABLE = new HashMap<>();
File file = getLootTableFile();
if (PERSIST_LOOT_TABLE && !getDataFolder().exists()) {
getDataFolder().mkdirs();
}

World world = Bukkit.getWorlds().get(0);
Location spawn = world.getSpawnLocation().clone();
spawn.setY(255);
Expand All @@ -75,10 +108,44 @@ private void initLootTable() {
}
}

YamlConfiguration yaml = new YamlConfiguration();
if (PERSIST_LOOT_TABLE && file.exists()) {
try {
yaml.load(file);
for (String key : yaml.getKeys(false)) {
Material k = Material.getMaterial(key);
Material v = Material.getMaterial(yaml.getString(key));
if (k != null && v != null) {
LOOT_TABLE.put(k, v);
}
}
} catch (Exception e) {
getLogger().warning("Failed to load loot table: " + e.getMessage());
}
}

List<Material> shuffled = new ArrayList<>(valid);
Collections.shuffle(shuffled, new Random(SEED));
for (int i = 0; i < valid.size(); i++) {
LOOT_TABLE.put(valid.get(i), shuffled.get(i));
shuffled.removeAll(LOOT_TABLE.values());

boolean updated = false;
for (Material m : valid) {
if (!LOOT_TABLE.containsKey(m)) {
if (shuffled.isEmpty()) break;
LOOT_TABLE.put(m, shuffled.remove(0));
updated = true;
}
}

if (PERSIST_LOOT_TABLE && (updated || !file.exists())) {
for (Map.Entry<Material, Material> entry : LOOT_TABLE.entrySet()) {
yaml.set(entry.getKey().name(), entry.getValue().name());
}
try {
yaml.save(file);
} catch (IOException e) {
getLogger().warning("Failed to save loot table: " + e.getMessage());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion RandomDrop/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main: com.theo546.randomdrop.Main
name: RandomDrop
version: "1.0.6"
version: "1.1.0"
api-version: 1.21
author: theo546
description: A Paper plugin to randomize the Minecraft loot table!
Expand Down