Initial commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>ProjectCranked</artifactId>
|
||||
<groupId>net.crankedpvp</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>LootTable</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<artifactId>Utils</artifactId>
|
||||
<groupId>net.crankedpvp</groupId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.crankedpvp</groupId>
|
||||
<artifactId>StorageUtilsShaded</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/../StorageUtils/target/StorageUtils-1.0-shaded.jar</systemPath>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,39 @@
|
||||
package net.crankedpvp.loottable;
|
||||
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.RandomUtils;
|
||||
import net.crankedpvp.loottable.data.LootTable;
|
||||
import net.crankedpvp.loottable.data.LootTableManager;
|
||||
import net.crankedpvp.loottable.struct.LootTableType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class LootTableAPI {
|
||||
|
||||
private static LootTableManager lootTableManager = LootTablePlugin.getInstance().getLootTableManager();
|
||||
|
||||
public static ItemStack getRandomLootItem(LootTableType lootTableType) {
|
||||
LootTable lootTable = lootTableManager.getLootTable(lootTableType);
|
||||
if (lootTable.getLootMap().isEmpty()) {
|
||||
Bukkit.getConsoleSender().sendMessage(CC.translate("&c[LootTableAPI] LootTable " + lootTableType.getFancy() + " is empty, cannot get item, returned grass instead."));
|
||||
return new ItemStack(Material.GRASS);
|
||||
}
|
||||
return RandomUtils.getRandomItem(lootTable.getLootMap());
|
||||
}
|
||||
|
||||
public static List<ItemStack> getAllLootFromLootTable(LootTableType type) {
|
||||
LootTable lootTable = lootTableManager.getLootTable(type);
|
||||
Set<ItemStack> loot = lootTable.getLootMap().keySet();
|
||||
return loot.stream().sorted(Comparator.comparing(ItemStack::getType)).collect(Collectors.toList());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package net.crankedpvp.loottable;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.mattstudios.mf.base.CommandManager;
|
||||
import me.mattstudios.mf.base.components.TypeResult;
|
||||
import net.crankedpvp.loottable.commands.LootTableCommand;
|
||||
import net.crankedpvp.loottable.data.LootTableManager;
|
||||
import net.crankedpvp.loottable.struct.LootTableType;
|
||||
import net.crankedpvp.loottable.typeadapters.LootMapSerializer;
|
||||
import net.crankedpvp.storageutils.StorageUtils;
|
||||
import net.crankedpvp.storageutils.gson.Gson;
|
||||
import net.crankedpvp.storageutils.gson.reflect.TypeToken;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Getter
|
||||
public class LootTablePlugin extends JavaPlugin {
|
||||
@Getter
|
||||
private static LootTablePlugin instance;
|
||||
private LootTableManager lootTableManager;
|
||||
private CommandManager commandManager;
|
||||
private final Type mapType = new TypeToken<Map<ItemStack, Double>>(){}.getType();
|
||||
private Gson gson;
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
this.gson = StorageUtils.getInstance().getGson().newBuilder().registerTypeAdapter(mapType, new LootMapSerializer()).create();
|
||||
this.lootTableManager = new LootTableManager(this);
|
||||
lootTableManager.loadAll();
|
||||
this.commandManager = new CommandManager(this, true);
|
||||
registerCommandParameters();
|
||||
registerCommandCompletion();
|
||||
commandManager.register(new LootTableCommand());
|
||||
Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, ()-> {
|
||||
lootTableManager.saveAll();
|
||||
|
||||
}, 60L, 10*20*60);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
lootTableManager.saveAll();
|
||||
instance = null;
|
||||
}
|
||||
|
||||
private void registerCommandParameters() {
|
||||
this.commandManager.getParameterHandler().register(LootTableType.class, argument -> {
|
||||
LootTableType type = LootTableType.valueOf(String.valueOf(argument));
|
||||
return new TypeResult(type, argument);
|
||||
});
|
||||
}
|
||||
|
||||
private void registerCommandCompletion() {
|
||||
this.commandManager.getCompletionHandler().register("#loottables", input -> Arrays.stream(LootTableType.values()).map(LootTableType::name).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package net.crankedpvp.loottable.commands;
|
||||
|
||||
import me.mattstudios.mf.annotations.*;
|
||||
import me.mattstudios.mf.base.CommandBase;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.PlayerUtils;
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemUtils;
|
||||
import net.crankedpvp.loottable.LootTableAPI;
|
||||
import net.crankedpvp.loottable.LootTablePlugin;
|
||||
import net.crankedpvp.loottable.guis.LootTableMainGUI;
|
||||
import net.crankedpvp.loottable.struct.LootTableType;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@Command("loottable")
|
||||
@Alias({"loottables", "loot"})
|
||||
public class LootTableCommand extends CommandBase {
|
||||
@Permission("cranked.admin")
|
||||
@Default
|
||||
public void onDefault(CommandSender sender) {
|
||||
LootTableMainGUI lootTableMainGUI = new LootTableMainGUI((Player) sender);
|
||||
lootTableMainGUI.getMainGUI().open((Player)sender);
|
||||
}
|
||||
|
||||
|
||||
@Permission("cranked.admin")
|
||||
@SubCommand("save")
|
||||
public void onSave(CommandSender sender) {
|
||||
LootTablePlugin.getInstance().getLootTableManager().saveAll();
|
||||
sender.sendMessage(CC.Green + "Saved!!!");
|
||||
}
|
||||
|
||||
@Permission("cranked.admin")
|
||||
@SubCommand("random")
|
||||
public void onRandomItem(CommandSender sender, @Completion("#players") Player receiver, @Completion("#loottables") LootTableType lootTableType) {
|
||||
PlayerUtils.giveOrDropProtectedItem(receiver, LootTableAPI.getRandomLootItem(lootTableType), 60);
|
||||
sender.sendMessage(CC.translate("&aGiven " + receiver.getName() + " a random " + lootTableType.getFancy() + " item."));
|
||||
receiver.sendMessage(CC.translate("&aReceived a random " + lootTableType.getFancy() + " item."));
|
||||
}
|
||||
|
||||
@Permission("cranked.admin")
|
||||
@SubCommand("additem")
|
||||
@WrongUsage("&c/loottable additem <loot-table> <chance>")
|
||||
public void addItem(Player player, @Completion("#loottables") LootTableType lootTableType, Double chance) {
|
||||
if (ItemUtils.isAirOrNull(player.getItemInHand())) {
|
||||
player.sendMessage(CC.translate("&c&l(!)&c Please hold an item!"));
|
||||
return;
|
||||
}
|
||||
player.sendMessage(CC.translate("&a&l(!)&a Added " + ItemUtils.getDisplayName(player.getItemInHand()) + " &ato the " + lootTableType.getFancy() + " &aLoottable!"));
|
||||
LootTablePlugin.getInstance().getLootTableManager().getLootTable(lootTableType).addItem(chance, player.getItemInHand().clone());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package net.crankedpvp.loottable.data;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.loottable.struct.LootTableType;
|
||||
import net.crankedpvp.storageutils.utils.KeyToSave;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Getter
|
||||
public class LootTable implements KeyToSave {
|
||||
private Map<ItemStack, Double> lootMap;
|
||||
private String lootTableType;
|
||||
|
||||
public LootTable(LootTableType lootTableType) {
|
||||
this.lootTableType = lootTableType.name();
|
||||
this.lootMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public void addItem(double chance, ItemStack item) {
|
||||
lootMap.put(item, chance);
|
||||
}
|
||||
|
||||
public void removeItem(ItemStack item) {
|
||||
lootMap.remove(item);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return lootTableType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setKey(String key) {
|
||||
this.lootTableType = key;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.crankedpvp.loottable.data;
|
||||
|
||||
import net.crankedpvp.loottable.LootTablePlugin;
|
||||
import net.crankedpvp.loottable.struct.LootTableType;
|
||||
import net.crankedpvp.storageutils.StorageUtils;
|
||||
import net.crankedpvp.storageutils.utils.SaveLoadManager;
|
||||
|
||||
public class LootTableManager extends SaveLoadManager<LootTable> {
|
||||
public LootTableManager(LootTablePlugin plugin) {
|
||||
super(plugin, LootTablePlugin.getInstance().getGson(), "/data", LootTable.class);
|
||||
}
|
||||
|
||||
public LootTable getLootTable(LootTableType lootTableType) {
|
||||
if (!containsKey(lootTableType.name())) {
|
||||
LootTable lootTable = new LootTable(lootTableType);
|
||||
addToCache(lootTable);
|
||||
}
|
||||
return getFromCache(lootTableType.name());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package net.crankedpvp.loottable.guis;
|
||||
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import dev.triumphteam.gui.guis.PaginatedGui;
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemUtils;
|
||||
import net.crankedpvp.loottable.LootTablePlugin;
|
||||
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.loottable.struct.LootTableType;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Getter
|
||||
public class LootTableEditGUI {
|
||||
private PaginatedGui lootEditGUI;
|
||||
|
||||
public LootTableEditGUI(LootTableType lootTableType) {
|
||||
this.lootEditGUI = new PaginatedGui(6, 54, lootTableType.getFancy() + " Loot");
|
||||
this.lootEditGUI.setItem(6, 1, new GuiItem(createPreviousItem(), event -> {
|
||||
LootTableMainGUI lootTableMainGUI = new LootTableMainGUI((Player) event.getWhoClicked());
|
||||
lootTableMainGUI.getMainGUI().open(event.getWhoClicked());
|
||||
}));
|
||||
|
||||
lootEditGUI.setDefaultClickAction(event -> {
|
||||
event.setCancelled(true);
|
||||
});
|
||||
for (Map.Entry<ItemStack, Double> entry : LootTablePlugin.getInstance().getLootTableManager().getLootTable(lootTableType).getLootMap().entrySet()) {
|
||||
ItemStack item = entry.getKey();
|
||||
double chance = entry.getValue();
|
||||
if (ItemUtils.isAirOrNull(item)) continue;
|
||||
lootEditGUI.addItem(new GuiItem(new ItemBuilder(item.clone()).addLoreLine("").addLoreLine(CC.translate(lootTableType.getColor() + "Chance: &f" + chance + "%")).addLoreLine("").addLoreLine(CC.translate("&cRight-Click &7items in gui to remove")).build(), event -> {
|
||||
if (event.isRightClick()) {
|
||||
event.getWhoClicked().sendMessage(CC.translate("&c&l(!)&c Removed " + ItemUtils.getDisplayName(item) + "&c from the " + lootTableType.getFancy() + " - " + lootTableType.getFancy()));
|
||||
lootEditGUI.removeItem(event.getSlot());
|
||||
LootTablePlugin.getInstance().getLootTableManager().getLootTable(lootTableType).removeItem(item);
|
||||
LootTableEditGUI editGUI = new LootTableEditGUI(lootTableType);
|
||||
editGUI.getLootEditGUI().open(event.getWhoClicked());
|
||||
} else {
|
||||
event.getWhoClicked().getInventory().addItem(item);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
public ItemStack createPreviousItem() {
|
||||
return new ItemBuilder(Material.ARROW, CC.translate("&c<- Go Back"), CC.translate("&7Right-Click&7 to go back.")).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package net.crankedpvp.loottable.guis;
|
||||
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import dev.triumphteam.gui.guis.PaginatedGui;
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.loottable.LootTablePlugin;
|
||||
import net.crankedpvp.loottable.struct.LootTableType;
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@Getter
|
||||
|
||||
public class LootTableMainGUI {
|
||||
private PaginatedGui mainGUI;
|
||||
|
||||
public LootTableMainGUI(Player player) {
|
||||
this.mainGUI = new PaginatedGui(3, 27, "Loottable Menu");
|
||||
mainGUI.setDefaultClickAction(event -> event.setCancelled(true));
|
||||
for (LootTableType tableType : LootTableType.values()) {
|
||||
mainGUI.addItem(new GuiItem(new ItemBuilder(tableType.getMaterial(), CC.translate(tableType.getColor() + "&l" + tableType.getFancy()), CC.translate("&7Click to open this loottable.")).build(), event -> {
|
||||
LootTableEditGUI lootTableEditGUI = new LootTableEditGUI(tableType);
|
||||
lootTableEditGUI.getLootEditGUI().open(event.getWhoClicked());
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package net.crankedpvp.loottable.struct;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.crankedutils.utils.StringUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum LootTableType {
|
||||
KOTH(Material.STICK, ChatColor.LIGHT_PURPLE),
|
||||
ABYSS_KEY(Material.TRIPWIRE_HOOK, ChatColor.RED),
|
||||
MYTHIC_KEY(Material.TRIPWIRE_HOOK, ChatColor.YELLOW),
|
||||
SEASON_KEY(Material.TRIPWIRE_HOOK, ChatColor.BLUE),
|
||||
UNDERWORLD_TIER_1(Material.CHEST, ChatColor.GRAY),
|
||||
UNDERWORLD_TIER_2(Material.TRAPPED_CHEST, ChatColor.GREEN),
|
||||
UNDERWORLD_TIER_3(Material.ENDER_CHEST, ChatColor.AQUA),
|
||||
UNDERWORLD_TIER_4(Material.ENCHANTMENT_TABLE, ChatColor.YELLOW),
|
||||
UNDERWORLD_TIER_5(Material.ENDER_PORTAL_FRAME, ChatColor.RED),
|
||||
UNDERWORLD_TIER_6(Material.BEACON, ChatColor.DARK_RED),
|
||||
SLOTBOT(Material.PAPER, ChatColor.AQUA),
|
||||
ENVOY(Material.CHEST, ChatColor.YELLOW),
|
||||
//EVENT(Material.WATCH, ChatColor.GOLD),
|
||||
//VOYAGE_TIER_1(Material.ENDER_PORTAL_FRAME, ChatColor.GRAY),
|
||||
//VOYAGE_TIER_2(Material.ENDER_PORTAL_FRAME, ChatColor.GREEN),
|
||||
//VOYAGE_TIER_3(Material.ENDER_PORTAL_FRAME, ChatColor.AQUA),
|
||||
//VOYAGE_TIER_4(Material.ENDER_PORTAL_FRAME, ChatColor.YELLOW),
|
||||
//VOYAGE_TIER_5(Material.ENDER_PORTAL_FRAME, ChatColor.RED),
|
||||
//VOYAGE_TIER_6(Material.ENDER_PORTAL_FRAME, ChatColor.DARK_RED),
|
||||
WARZONE_CHEST(Material.CHEST, ChatColor.AQUA),
|
||||
;
|
||||
|
||||
public String getFancy() {
|
||||
return StringUtils.getProperCase(this.name().replace("_", " "));
|
||||
}
|
||||
private final Material material;
|
||||
private final ChatColor color;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package net.crankedpvp.loottable.typeadapters;
|
||||
|
||||
|
||||
import net.crankedpvp.storageutils.gson.*;
|
||||
import net.crankedpvp.storageutils.utils.ItemStackSerializer;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
public class LootMapSerializer implements JsonSerializer<Map<ItemStack, Double>> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Map<ItemStack, Double> itemStackDoubleMap, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
itemStackDoubleMap.forEach((itemStack, aDouble) -> {
|
||||
try {
|
||||
jsonObject.add(ItemStackSerializer.serialize(itemStack), new JsonPrimitive(aDouble));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return jsonObject;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
name: CrankedLootTables
|
||||
main: net.crankedpvp.loottable.LootTablePlugin
|
||||
version: ${git.total.commit.count}.0
|
||||
description: "§fCommit ID: §a${git.commit.id.abbrev}"
|
||||
website: https://crankedpvp.net/
|
||||
depend:
|
||||
- CrankedUtils
|
||||
- StorageUtil
|
||||
Reference in New Issue
Block a user