mirror of
https://github.com/CarmJos/ScriptItems
synced 2024-09-19 13:25:50 +00:00
主要功能完成,添加测试指令。
This commit is contained in:
parent
dca7b71a0c
commit
a8d79fd214
@ -38,6 +38,10 @@ public class Main extends EasyPlugin {
|
||||
return false;
|
||||
}
|
||||
|
||||
info("加载物品配置...");
|
||||
this.itemsManager = new ItemsManager();
|
||||
this.itemsManager.initialize();
|
||||
|
||||
info("注册指令...");
|
||||
|
||||
info("注册监听器...");
|
||||
@ -77,7 +81,6 @@ public class Main extends EasyPlugin {
|
||||
Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log);
|
||||
}
|
||||
|
||||
|
||||
public static void info(String... messages) {
|
||||
getInstance().log(messages);
|
||||
}
|
||||
|
@ -0,0 +1,43 @@
|
||||
package cc.carm.plugin.commanditem.command;
|
||||
|
||||
import cc.carm.plugin.commanditem.CommandItemAPI;
|
||||
import cc.carm.plugin.commanditem.item.ItemSettings;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ItemsCommand implements CommandExecutor, TabCompleter {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command,
|
||||
@NotNull String alias, @NotNull String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
ItemSettings settings = CommandItemAPI.getItemsManager().listItemSettings().values().stream().findFirst().orElse(null);
|
||||
if (settings != null) {
|
||||
player.getInventory().addItem(settings.generateItem(1));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
|
||||
@NotNull String alias, @NotNull String[] args) {
|
||||
List<String> allCompletes = new ArrayList<>();
|
||||
|
||||
return allCompletes.stream()
|
||||
.filter(s -> StringUtil.startsWithIgnoreCase(s, args[args.length - 1]))
|
||||
.limit(10).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package cc.carm.plugin.commanditem.item;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
@ -15,6 +16,10 @@ public class ItemActionGroup {
|
||||
this.actions = actions;
|
||||
}
|
||||
|
||||
public void execute(Player player) {
|
||||
actions.forEach(action -> action.execute(player));
|
||||
}
|
||||
|
||||
public static @NotNull ItemActionGroup read(@NotNull List<String> actionsString) {
|
||||
List<ItemAction> actions = actionsString.stream()
|
||||
.map(ItemAction::read).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
@ -50,14 +50,19 @@ public class ItemSettings {
|
||||
return name;
|
||||
}
|
||||
|
||||
public @Nullable ItemStack getItemStack(int amount) {
|
||||
ItemStack originalItem = this.item == null ? null : this.item.getItemStack(amount);
|
||||
public ItemStackConfig getItemConfig() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public @Nullable ItemStack generateItem(int amount) {
|
||||
ItemStackConfig config = getItemConfig();
|
||||
ItemStack originalItem = config == null ? null : config.getItemStack(amount);
|
||||
if (originalItem == null) return null;
|
||||
return applyItem(originalItem);
|
||||
}
|
||||
|
||||
public @Nullable ItemStack getItemStack() {
|
||||
return getItemStack(1);
|
||||
public @Nullable ItemStack generateItem() {
|
||||
return generateItem(1);
|
||||
}
|
||||
|
||||
@Unmodifiable
|
||||
|
@ -1,10 +1,19 @@
|
||||
package cc.carm.plugin.commanditem.listener;
|
||||
|
||||
import cc.carm.plugin.commanditem.CommandItemAPI;
|
||||
import cc.carm.plugin.commanditem.item.CommandItem;
|
||||
import cc.carm.plugin.commanditem.item.ItemActionGroup;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.entity.EntityPickupItemEvent;
|
||||
import org.bukkit.event.inventory.CraftItemEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ItemListener implements Listener {
|
||||
|
||||
@ -16,6 +25,18 @@ public class ItemListener implements Listener {
|
||||
*/
|
||||
@EventHandler
|
||||
public void onClick(PlayerInteractEvent event) {
|
||||
if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
|
||||
|
||||
ItemStack item = event.getPlayer().getInventory().getItemInMainHand();
|
||||
CommandItem commandItem = CommandItemAPI.getItemsManager().parseCommandItem(item);
|
||||
if (commandItem == null) return;
|
||||
|
||||
Player player = event.getPlayer();
|
||||
ItemActionGroup actions = commandItem.getConfiguration().getPlayerActions(player);
|
||||
|
||||
if (actions == null) return;
|
||||
|
||||
actions.execute(player);
|
||||
|
||||
}
|
||||
|
||||
@ -26,7 +47,10 @@ public class ItemListener implements Listener {
|
||||
*/
|
||||
@EventHandler
|
||||
public void onCraft(CraftItemEvent event) {
|
||||
boolean shouldCancel = Arrays.stream(event.getInventory().getMatrix())
|
||||
.anyMatch(matrix -> CommandItemAPI.getItemsManager().isCommandItem(matrix));
|
||||
|
||||
if (shouldCancel) event.setCancelled(true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,7 +60,12 @@ public class ItemListener implements Listener {
|
||||
*/
|
||||
@EventHandler
|
||||
public void onPickup(EntityPickupItemEvent event) {
|
||||
if (event.getEntity().getType() == EntityType.PLAYER) return;
|
||||
|
||||
ItemStack item = event.getItem().getItemStack();
|
||||
if (CommandItemAPI.getItemsManager().isCommandItem(item)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -5,6 +5,7 @@ import cc.carm.plugin.commanditem.configuration.PluginConfig;
|
||||
import cc.carm.plugin.commanditem.item.CommandItem;
|
||||
import cc.carm.plugin.commanditem.item.ItemSettings;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
@ -102,7 +103,7 @@ public class ItemsManager {
|
||||
}
|
||||
|
||||
public @Nullable CommandItem parseCommandItem(@Nullable ItemStack item) {
|
||||
if (item == null) return null;
|
||||
if (item == null || item.getType() == Material.AIR) return null;
|
||||
if (!item.hasItemMeta()) return null;
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) return null;
|
||||
|
@ -12,3 +12,10 @@ softdepend:
|
||||
- PlaceholderAPI
|
||||
|
||||
api-version: 1.13
|
||||
|
||||
commands:
|
||||
"CommandItem":
|
||||
aliases:
|
||||
- "CMDItem"
|
||||
description: "CommandItem的主要指令。"
|
||||
usage: "/CommandItem help"
|
Loading…
Reference in New Issue
Block a user