Skip to content

Commit

Permalink
1.0.4: added randomized craft, added more configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
theo546 committed Sep 17, 2019
1 parent 0240a5f commit 14a4c93
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 97 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ The configuration file is located in the `RandomDrop` folder inside the `plugins

### RANDOMIZE_DURABILITY
(default: false)
is to randomize the durability of the randomized item IF the item is damageable.
is to randomize the durability of a randomized item IF the item is damageable.

### RANDOMIZE_DURABILITY_OF_CRAFTED_ITEMS
(default: false)
is to randomize the durability of a crafted item IF the item is damageable.

### CLAIMED_LORE_TEXT
(default: §r§7§lCLAIMED)
Expand All @@ -34,3 +38,11 @@ will, as its name imply, once the unclaimed item is drop, keep the enchant of th
### KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE
(default: false)
will, as its name imply, once the unclaimed item is drop, keep the custom name of the unclaimed item to the randomized item.

### CLAIM_CRAFTED_ITEMS
(default: true)
will, as its name imply, claim the item that is gonna be crafted.

### RANDOMIZE_CRAFT
(default: false)
will, as its name imply, randomize the result item from a crafting table. However, it will not randomize the recipe.
2 changes: 1 addition & 1 deletion RandomDrop/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.3"
version: "1.0.4"
api-version: 1.14
author: theo546
description: A plugin to randomize the items you should normally get!
110 changes: 15 additions & 95 deletions RandomDrop/src/com/theo546/randomdrop/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,20 @@
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;

public class Listener implements org.bukkit.event.Listener {
@SuppressWarnings("deprecation")

@EventHandler
public void onItemSpawn(ItemSpawnEvent event) {
Item item = event.getEntity();

ItemStack itemstack = item.getItemStack();

byte magic_id = 0;
if(Main.PAST_FLATTENING == false) {
// PRE-FLATTENING SUPPORT
ItemStack itemstack_check = itemstack;
itemstack_check.setDurability((short) 0);
magic_id = itemstack.getData().getData();
}

if(itemstack.hasItemMeta() == true) {
ItemMeta itemmeta = itemstack.getItemMeta();
if(itemmeta.hasLore() == true) {
Expand All @@ -47,97 +33,31 @@ public void onItemSpawn(ItemSpawnEvent event) {
}
}
}

Material material = itemstack.getType();
int itemstack_amount = itemstack.getAmount();

int loop = 0;
Material item_to_drop;
while (true) {
item_to_drop = Main.getRandomItemFromItemWithSeed(material, loop, magic_id);
try {
ItemStack itemstack_drop = itemstack;
itemstack_drop.setType(item_to_drop);

ItemMeta itemmeta_drop;
if(!itemstack.hasItemMeta()) {
itemmeta_drop = Bukkit.getServer().getItemFactory().getItemMeta(itemstack_drop.getType());
}
else {
itemmeta_drop = itemstack.getItemMeta();
}

List<String> str = Arrays.asList(Main.CLAIMED_LORE_TEXT);

itemmeta_drop.setLore(str);

if(Main.KEEP_ENCHANT_ON_DROPPED_UNCLAIMED_ITEM == false) {
if(itemmeta_drop.hasEnchants()) {
for(Enchantment e: itemmeta_drop.getEnchants().keySet()) {
itemmeta_drop.removeEnchant(e);
}
}
}

if(Main.KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE == false) {
if(itemmeta_drop.hasDisplayName()) {
itemmeta_drop.setDisplayName("");
}
}

int item_max_durability = item_to_drop.getMaxDurability();

if(item_max_durability != 0) {
try {
// 1.13+
if(itemmeta_drop instanceof Damageable) {
if(Main.RANDOMIZE_DURABILITY == false) {
((Damageable) itemmeta_drop).setDamage(0);
}
else {
int random = 0 + (int) (Math.random() * ((item_max_durability - 0) + 1));
((Damageable) itemmeta_drop).setDamage(random);
}
}
}
catch (java.lang.NoClassDefFoundError e) {
// 1.8 support
if(Main.RANDOMIZE_DURABILITY == false) {
short durability = 0;
itemstack_drop.setDurability(durability);
}
else {
short random2 = (short) (0 + (int)(Math.random() * ((item_max_durability - 0) + 1)));
itemstack_drop.setDurability(random2);
}
}
}

itemstack_drop.setAmount(itemstack_amount);
itemstack_drop.setItemMeta(itemmeta_drop);

item.setItemStack(itemstack_drop);

break;
}
catch (java.lang.IllegalArgumentException | java.lang.NullPointerException ex) {
loop ++;
}
}
item.setItemStack(Main.randomizeItemStack(itemstack, true, Main.RANDOMIZE_DURABILITY));
}
@EventHandler
public void onItemCraft(PrepareItemCraftEvent event) {
CraftingInventory result = event.getInventory();
ItemStack itemstack_result = result.getResult();
if(itemstack_result == null) return;

ItemMeta itemmeta_result;
if(!itemstack_result.hasItemMeta()) {
if(itemstack_result.hasItemMeta() == false) {
itemmeta_result = Bukkit.getServer().getItemFactory().getItemMeta(itemstack_result.getType());
}
else {
itemmeta_result = itemstack_result.getItemMeta();
}
List<String> str = Arrays.asList(Main.CLAIMED_LORE_TEXT);
itemmeta_result.setLore(str);
itemstack_result.setItemMeta(itemmeta_result);
if(Main.CLAIM_CRAFTED_ITEMS == true && Main.RANDOMIZE_CRAFT == false) {
List<String> str = Arrays.asList(Main.CLAIMED_LORE_TEXT);
itemmeta_result.setLore(str);
itemstack_result.setItemMeta(itemmeta_result);
}
if(Main.RANDOMIZE_CRAFT == true) {
result.setResult(Main.randomizeItemStack(itemstack_result, Main.CLAIM_CRAFTED_ITEMS, Main.RANDOMIZE_DURABILITY_OF_CRAFTED_ITEMS));
}
if(Main.RANDOMIZE_DURABILITY_OF_CRAFTED_ITEMS == true && Main.RANDOMIZE_CRAFT == false) {
itemstack_result.setItemMeta(Main.randomizeDurability(true, itemstack_result));
}
}
}
107 changes: 107 additions & 0 deletions RandomDrop/src/com/theo546/randomdrop/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,52 @@
package com.theo546.randomdrop;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import com.theo546.randomdrop.Listener;

public class Main extends JavaPlugin {
public static boolean RANDOMIZE_DURABILITY;
public static boolean RANDOMIZE_DURABILITY_OF_CRAFTED_ITEMS;
public static String CLAIMED_LORE_TEXT;
public static int SEED;
public static boolean KEEP_ENCHANT_ON_DROPPED_UNCLAIMED_ITEM;
public static boolean KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE;
public static boolean CLAIM_CRAFTED_ITEMS;
public static boolean RANDOMIZE_CRAFT;
public static boolean PAST_FLATTENING;

@Override
public void onEnable() {
getConfig().addDefault("RANDOMIZE_DURABILITY", false);
getConfig().addDefault("RANDOMIZE_DURABILITY_OF_CRAFTED_ITEMS", false);
getConfig().addDefault("CLAIMED_LORE_TEXT", "§r§7§lCLAIMED");
getConfig().addDefault("SEED", System.currentTimeMillis());
getConfig().addDefault("KEEP_ENCHANT_ON_DROPPED_UNCLAIMED_ITEM", false);
getConfig().addDefault("KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE", false);
getConfig().addDefault("CLAIM_CRAFTED_ITEMS", true);
getConfig().addDefault("RANDOMIZE_CRAFT", false);
getConfig().options().copyDefaults(true);
saveConfig();

RANDOMIZE_DURABILITY = getConfig().getBoolean("RANDOMIZE_DURABILITY");
RANDOMIZE_DURABILITY_OF_CRAFTED_ITEMS = getConfig().getBoolean("RANDOMIZE_DURABILITY_OF_CRAFTED_ITEMS");
CLAIMED_LORE_TEXT = getConfig().getString("CLAIMED_LORE_TEXT");
SEED = getConfig().getInt("SEED");
KEEP_ENCHANT_ON_DROPPED_UNCLAIMED_ITEM = getConfig().getBoolean("KEEP_ENCHANT_ON_DROPPED_UNCLAIMED_ITEM");
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");

String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3].replaceAll("v", "").replaceAll("_", "").split("R")[0];
if(version.endsWith(".")) version = version.substring(0, version.length() - 1);
Expand Down Expand Up @@ -64,6 +79,98 @@ public static Material getRandomItemFromItemWithSeed(Material material, int fall

return names.get(random_int);
}
@SuppressWarnings("deprecation")
public static ItemStack randomizeItemStack(ItemStack itemstack, boolean claim, boolean randomize_durability) {
Material material = itemstack.getType();
int itemstack_amount = itemstack.getAmount();

byte magic_id = 0;
if(Main.PAST_FLATTENING == false) {
// PRE-FLATTENING SUPPORT
ItemStack itemstack_check = itemstack;
itemstack_check.setDurability((short) 0);
magic_id = itemstack.getData().getData();
}

int loop = 0;
Material item_to_drop;
while (true) {
item_to_drop = Main.getRandomItemFromItemWithSeed(material, loop, magic_id);
try {
ItemStack itemstack_drop = itemstack;
itemstack_drop.setType(item_to_drop);

ItemMeta itemmeta_drop;
if(itemstack.hasItemMeta() == false) {
itemmeta_drop = Bukkit.getServer().getItemFactory().getItemMeta(itemstack_drop.getType());
}
else {
itemmeta_drop = itemstack.getItemMeta();
}

List<String> str = Arrays.asList(Main.CLAIMED_LORE_TEXT);

if(claim == true) {
itemmeta_drop.setLore(str);
}

if(Main.KEEP_ENCHANT_ON_DROPPED_UNCLAIMED_ITEM == false) {
if(itemmeta_drop.hasEnchants()) {
for(Enchantment e: itemmeta_drop.getEnchants().keySet()) {
itemmeta_drop.removeEnchant(e);
}
}
}

if(Main.KEEP_ITEM_CUSTOMNAME_ON_RANDOMIZE == false) {
if(itemmeta_drop.hasDisplayName()) {
itemmeta_drop.setDisplayName("");
}
}

itemmeta_drop = randomizeDurability(randomize_durability, itemstack_drop);

itemstack_drop.setAmount(itemstack_amount);
itemstack_drop.setItemMeta(itemmeta_drop);

return itemstack_drop;
}
catch (java.lang.IllegalArgumentException | java.lang.NullPointerException ex) {
loop ++;
}
}
}
@SuppressWarnings("deprecation")
public static ItemMeta randomizeDurability(boolean randomize_durability, ItemStack itemstack) {
int item_max_durability = itemstack.getType().getMaxDurability();
ItemMeta itemmeta = itemstack.getItemMeta();

if(item_max_durability != 0) {
try {
// 1.13+
if(itemmeta instanceof Damageable) {
if(randomize_durability == false) {
((Damageable) itemmeta).setDamage(0);
}
else {
int random = 0 + (int) (Math.random() * ((item_max_durability - 0) + 1));
((Damageable) itemmeta).setDamage(random);
}
}
}
catch (java.lang.NoClassDefFoundError e) {
// 1.8 support
if(randomize_durability == false) {
itemstack.setDurability((short) 0);
}
else {
short random2 = (short) (0 + (int) (Math.random() * ((item_max_durability - 0) + 1)));
itemstack.setDurability(random2);
}
}
}
return itemmeta;
}
public static int pseudoRandom(int seed, int i, int add, int fallback, int magic_id) {
Random randnum = new Random();
randnum.setSeed(seed*add+fallback+magic_id);
Expand Down

0 comments on commit 14a4c93

Please sign in to comment.