1
mirror of https://github.com/CarmJos/ScriptItems synced 2024-09-19 21:35:50 +00:00

主要功能完成,添加测试指令。

This commit is contained in:
Carm Jos 2022-03-12 01:11:46 +08:00
parent dca7b71a0c
commit a8d79fd214
7 changed files with 100 additions and 7 deletions

View File

@ -38,6 +38,10 @@ public class Main extends EasyPlugin {
return false; return false;
} }
info("加载物品配置...");
this.itemsManager = new ItemsManager();
this.itemsManager.initialize();
info("注册指令..."); info("注册指令...");
info("注册监听器..."); info("注册监听器...");
@ -76,8 +80,7 @@ public class Main extends EasyPlugin {
public void outputInfo() { public void outputInfo() {
Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log); Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log);
} }
public static void info(String... messages) { public static void info(String... messages) {
getInstance().log(messages); getInstance().log(messages);
} }

View File

@ -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());
}
}

View File

@ -1,5 +1,6 @@
package cc.carm.plugin.commanditem.item; package cc.carm.plugin.commanditem.item;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
@ -15,6 +16,10 @@ public class ItemActionGroup {
this.actions = actions; this.actions = actions;
} }
public void execute(Player player) {
actions.forEach(action -> action.execute(player));
}
public static @NotNull ItemActionGroup read(@NotNull List<String> actionsString) { public static @NotNull ItemActionGroup read(@NotNull List<String> actionsString) {
List<ItemAction> actions = actionsString.stream() List<ItemAction> actions = actionsString.stream()
.map(ItemAction::read).filter(Objects::nonNull).collect(Collectors.toList()); .map(ItemAction::read).filter(Objects::nonNull).collect(Collectors.toList());

View File

@ -50,14 +50,19 @@ public class ItemSettings {
return name; return name;
} }
public @Nullable ItemStack getItemStack(int amount) { public ItemStackConfig getItemConfig() {
ItemStack originalItem = this.item == null ? null : this.item.getItemStack(amount); return item;
}
public @Nullable ItemStack generateItem(int amount) {
ItemStackConfig config = getItemConfig();
ItemStack originalItem = config == null ? null : config.getItemStack(amount);
if (originalItem == null) return null; if (originalItem == null) return null;
return applyItem(originalItem); return applyItem(originalItem);
} }
public @Nullable ItemStack getItemStack() { public @Nullable ItemStack generateItem() {
return getItemStack(1); return generateItem(1);
} }
@Unmodifiable @Unmodifiable

View File

@ -1,10 +1,19 @@
package cc.carm.plugin.commanditem.listener; 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.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.CraftItemEvent; import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
public class ItemListener implements Listener { public class ItemListener implements Listener {
@ -16,6 +25,18 @@ public class ItemListener implements Listener {
*/ */
@EventHandler @EventHandler
public void onClick(PlayerInteractEvent event) { 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 @EventHandler
public void onCraft(CraftItemEvent event) { 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 @EventHandler
public void onPickup(EntityPickupItemEvent event) { 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);
}
} }

View File

@ -5,6 +5,7 @@ import cc.carm.plugin.commanditem.configuration.PluginConfig;
import cc.carm.plugin.commanditem.item.CommandItem; import cc.carm.plugin.commanditem.item.CommandItem;
import cc.carm.plugin.commanditem.item.ItemSettings; import cc.carm.plugin.commanditem.item.ItemSettings;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
@ -102,7 +103,7 @@ public class ItemsManager {
} }
public @Nullable CommandItem parseCommandItem(@Nullable ItemStack item) { 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; if (!item.hasItemMeta()) return null;
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
if (meta == null) return null; if (meta == null) return null;

View File

@ -12,3 +12,10 @@ softdepend:
- PlaceholderAPI - PlaceholderAPI
api-version: 1.13 api-version: 1.13
commands:
"CommandItem":
aliases:
- "CMDItem"
description: "CommandItem的主要指令。"
usage: "/CommandItem help"