enhancement of lucky draws

This commit is contained in:
NK 2022-12-20 17:17:35 +00:00
parent 4a3ec6bc65
commit dcea7c10a7
8 changed files with 111 additions and 47 deletions

View file

@ -16,8 +16,6 @@ public class BetaCommand extends Command {
@Override
public void onCommand(CommandSender sender, String[] args) {
if (sender instanceof Player player) {
//ZombiePet a = new ZombiePet(player.getLocation(), player);
//((CraftWorld) player.getWorld()).getHandle().b(a);
}

View file

@ -17,5 +17,6 @@ public class CommandManager {
new ShopCommand();
new BetaCommand();
new MinerCommand();
new LuckyDrawCommand();
}
}

View file

@ -0,0 +1,25 @@
package me.night.nullvalkyrie.commands;
import me.night.nullvalkyrie.ui.inventory.LuckyDraw;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
public class LuckyDrawCommand extends Command {
public LuckyDrawCommand() {
super("luckydraw", new String[]{"ld"}, "Generate a lucky draw", "");
}
@Override
public void onCommand(CommandSender sender, String[] args) {
if (sender instanceof Player player) {
new LuckyDraw().UI(player);
}
}
@Override
public List<String> onTabComplete(CommandSender sender, String[] args) {
return null;
}
}

View file

@ -4,28 +4,29 @@ import org.bukkit.Material;
public enum Items {
ETERNALSTARE("Eternal Stare", 29, Rarity.LEGENDARY, Material.COAL), // legendary charm
MORNINGTEA("Morning Tea", 28, Rarity.EPIC, Material.IRON_INGOT), // epic emote
PARACHUTE("Parachute - Doomed Chorus", 11, Rarity.EPIC, Material.GOLD_INGOT), // epic parachute
RALLYCAR("Rally Car - Doomed Chorus", 10, Rarity.EPIC, Material.REDSTONE), //epic backpack
WORLDAFLAME("World Aflame", 6.5, Rarity.LEGENDARY, Material.LAPIS_LAZULI), // legendary background
MOLOTOVCOTAIL("Molotov Cotail - Soul Flame", 5.5, Rarity.EPIC, Material.COPPER_INGOT), // epic throwable
KATANA("Katana - Silent Echo", 4.67, Rarity.EPIC, Material.EMERALD), // epic melee
DLQ33("DL Q33 - Doomed Chorus", 4, Rarity.EPIC, Material.QUARTZ), // epic gun
DAME("Dame - Shot Caller", 1.25, Rarity.EPIC, Material.DIAMOND), // character epic
KILO141("Kilo 141 - Demonsong", 0.08, Rarity.LEGENDARY, Material.NETHERITE_INGOT); // weapon legendary
ETERNALSTARE("Eternal Stare", 29, Rarity.LEGENDARY, Material.COAL, 10), // legendary charm
MORNINGTEA("Morning Tea", 28, Rarity.EPIC, Material.IRON_INGOT, 12), // epic emote
PARACHUTE("Parachute - Doomed Chorus", 11, Rarity.EPIC, Material.GOLD_INGOT, 14), // epic parachute
RALLYCAR("Rally Car - Doomed Chorus", 10, Rarity.EPIC, Material.REDSTONE, 16), //epic backpack
WORLDAFLAME("World Aflame", 6.5, Rarity.LEGENDARY, Material.LAPIS_LAZULI, 28), // legendary background
MOLOTOVCOTAIL("Molotov Cotail - Soul Flame", 5.5, Rarity.EPIC, Material.COPPER_INGOT, 30), // epic throwable
KATANA("Katana - Silent Echo", 4.67, Rarity.EPIC, Material.EMERALD, 32), // epic melee
DLQ33("DL Q33 - Doomed Chorus", 4, Rarity.EPIC, Material.QUARTZ, 34), // epic gun
DAME("Dame - Shot Caller", 1.25, Rarity.EPIC, Material.DIAMOND, 19), // character epic
KILO141("Kilo 141 - Demonsong", 0.08, Rarity.LEGENDARY, Material.NETHERITE_INGOT, 25); // weapon legendary
private final String name;
private final double weight;
private final Rarity rarity;
private final Material material;
private final int slot;
Items(String name, double weight, Rarity rarity, Material material) {
Items(String name, double weight, Rarity rarity, Material material, int slot) {
this.name = name;
this.weight = weight;
this.rarity = rarity;
this.material = material;
this.slot = slot;
}
public static Items getByName(String name) {
for (Items item : Items.values()) {
@ -50,4 +51,8 @@ public enum Items {
public Material getMaterial() {
return material;
}
public int getSlot() {
return slot;
}
}

View file

@ -3,19 +3,11 @@ package me.night.nullvalkyrie.enums;
import org.bukkit.Material;
public enum MinerType {
DIAMOND("Diamond", Material.DIAMOND_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"),
EMERALD("Emerald", Material.EMERALD_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"),
GOLD("Gold", Material.GOLD_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"),
IRON("Iron", Material.IRON_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"),
REDSTONE("Redstone", Material.REDSTONE_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"),
COAL("Coal", Material.COAL_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"),
LAPIS("Lapis", Material.LAPIS_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"),
QUARTZ("Quartz", Material.NETHER_QUARTZ_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"),
OBSIDIAN("Obsidian", Material.OBSIDIAN, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"),
NETHERITE("Netherite", Material.ANCIENT_DEBRIS, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198");
DIAMOND("Diamond", Material.DIAMOND_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), EMERALD("Emerald", Material.EMERALD_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), GOLD("Gold", Material.GOLD_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), IRON("Iron", Material.IRON_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), REDSTONE("Redstone", Material.REDSTONE_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), COAL("Coal", Material.COAL_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), LAPIS("Lapis", Material.LAPIS_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), QUARTZ("Quartz", Material.NETHER_QUARTZ_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), OBSIDIAN("Obsidian", Material.OBSIDIAN, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), NETHERITE("Netherite", Material.ANCIENT_DEBRIS, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198");
private final String name;
private final Material material;
private final String headTexture;
MinerType(String name, Material material, String headTexture) {
this.name = name;
this.material = material;

View file

@ -3,6 +3,7 @@ package me.night.nullvalkyrie.ui.inventory;
import me.night.nullvalkyrie.Main;
import me.night.nullvalkyrie.enums.Items;
import me.night.nullvalkyrie.util.RandomCollection;
import me.night.nullvalkyrie.util.Util;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@ -18,13 +19,15 @@ import java.util.Collections;
import java.util.List;
public class InventoryListener implements Listener {
private final RandomCollection<String> randomCollection;
public static RandomCollection<String> randomCollection;
public InventoryListener() {
randomCollection = new RandomCollection<>();
for (Items e : Items.values()) {
randomCollection.add(e.getWeight(), e.getName());
}
}
@EventHandler
public void onClick(InventoryClickEvent e) {
if (e.getCurrentItem() == null) return;
@ -63,6 +66,11 @@ public class InventoryListener implements Listener {
if (e.getRawSlot() == 0) {
player.closeInventory();
} else if (e.getRawSlot() == 22) {
if (randomCollection.getAll().size() == 0) {
player.closeInventory();
player.sendMessage(ChatColor.RED + "You already got all the rewards!");
return;
}
List<String> colors = List.of("WHITE", "ORANGE", "MAGENTA", "LIGHT_BLUE", "YELLOW", "LIME", "PINK", "GRAY", "LIGHT_GRAY", "CYAN", "PURPLE", "BLUE", "BROWN", "GREEN", "RED", "BLACK");
List<String> slot1 = new ArrayList<>(colors);
List<String> slot2 = new ArrayList<>(colors);
@ -85,6 +93,7 @@ public class InventoryListener implements Listener {
int i = 0;
int ii = 0;
int time = 0;
@Override
public void run() {
if (colors.size() - 1 <= i) i = 0;
@ -139,7 +148,30 @@ public class InventoryListener implements Listener {
LuckyDraw.GUI.setItem(slot, new ItemStack(Material.AIR));
}
String s = randomCollection.getRandom();
if (s != null) {
randomCollection.remove(s);
int slot = 0;
for (Items e : Items.values())
if (e.getName().equals(s))
slot = e.getSlot();
LuckyDraw.GUI.remove(Items.getByName(s).getMaterial());
ItemStack got = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);
ItemMeta gotmeta = got.getItemMeta();
gotmeta.setDisplayName(ChatColor.RED + "You already got this reward!");
got.setItemMeta(gotmeta);
LuckyDraw.GUI.setItem(slot, got);
for (String s1 : randomCollection.getAll()) {
ItemStack item = new ItemStack(Items.getByName(s1).getMaterial());
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.GREEN + Items.getByName(s1).getName());
List<String> lore = meta.getLore() == null ? new ArrayList<>() : meta.getLore();
lore.add(0, "");
lore.add(1, Util.color("&bChance: " + randomCollection.getChance(s1) + "%"));
lore.add(2, Items.getByName(s1).getRarity().getDisplay());
meta.setLore(lore);
item.setItemMeta(meta);
LuckyDraw.GUI.setItem(Items.getByName(s1).getSlot(), item);
}
Items it = Items.getByName(s);
ItemStack item = new ItemStack(it.getMaterial(), 1);
ItemMeta meta = item.getItemMeta();
@ -148,6 +180,7 @@ public class InventoryListener implements Listener {
meta.setLore(List.of(it.getRarity().getDisplay()));
item.setItemMeta(meta);
player.getInventory().addItem(item);
} else player.closeInventory();
}
}.runTaskLater(Main.getPlugin(Main.class), 5L * 20L);

View file

@ -1,6 +1,7 @@
package me.night.nullvalkyrie.ui.inventory;
import me.night.nullvalkyrie.enums.Items;
import me.night.nullvalkyrie.util.Util;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@ -17,16 +18,24 @@ public class LuckyDraw extends GUIManager {
setCloseButton(true);
setFrame(true, Material.BLUE_STAINED_GLASS_PANE);
int[] slots = new int[]{10, 12, 14, 16, 28, 30, 32, 34, 19, 25};
int count = 0;
for (Items name : Items.values()) {
ItemStack item = new ItemStack(name.getMaterial());
for (String s1 : InventoryListener.randomCollection.getAll()) {
Items it = Items.getByName(s1);
ItemStack item = new ItemStack(it.getMaterial());
ItemMeta meta = item.getItemMeta();
if (meta == null) return;
meta.setDisplayName(ChatColor.GOLD + name.getName());
meta.setLore(List.of(name.getRarity().getDisplay()));
meta.setDisplayName(ChatColor.GREEN + s1);
meta.setLore(List.of("", Util.color("&bChance:" + InventoryListener.randomCollection.getChance(s1) + "%"), it.getRarity().getDisplay()));
item.setItemMeta(meta);
GUI.setItem(slots[count], item);
count++;
GUI.setItem(it.getSlot(), item);
}
for (int s : slots) {
if (GUI.getItem(s) == null) {
ItemStack got = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);
ItemMeta gotmeta = got.getItemMeta();
gotmeta.setDisplayName(ChatColor.RED + "You already got this reward!");
got.setItemMeta(gotmeta);
GUI.setItem(s, got);
}
}
ItemStack roll = new ItemStack(Material.ARROW);
ItemMeta meta = roll.getItemMeta();

View file

@ -1,9 +1,6 @@
package me.night.nullvalkyrie.util;
import java.util.HashMap;
import java.util.NavigableMap;
import java.util.Random;
import java.util.TreeMap;
import java.util.*;
public class RandomCollection<E> {
@ -36,7 +33,7 @@ public class RandomCollection<E> {
return map.ceilingEntry(value).getValue();
}
public double getChance(E v) {
public long getChance(E v) {
double c = 0;
for (E d : chance.keySet()) {
if (d == v) {
@ -44,6 +41,10 @@ public class RandomCollection<E> {
break;
}
}
return (c / total) * 100;
return Math.round((c / total) * 100);
}
// write a method to get all the values in the collection so you can iterate over them
public List<E> getAll() {
return new ArrayList<>(map.values());
}
}