more logic

This commit is contained in:
MeexReay 2024-12-30 18:57:41 +03:00
parent 8a21fb53d2
commit 9ebf1d89a7
7 changed files with 95 additions and 38 deletions

View File

@ -12,6 +12,26 @@ spigot {
main "ru.themixray.puton.Main"
apiVersion "1.17"
named "PutOn"
command {
named "helmet"
description "Puts on item from your hand as helmet"
}
command {
named "chestplate"
description "Puts on item from your hand as chestplate"
}
command {
named "leggings"
description "Puts on item from your hand as leggings"
}
command {
named "boots"
description "Puts on item from your hand as boots"
}
}
}

View File

@ -1,50 +1,47 @@
package ru.themixray.puton;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.command.*;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import ru.themixray.puton.command.BootsCommand;
import ru.themixray.puton.command.ChestplateCommand;
import ru.themixray.puton.command.HelmetCommand;
import ru.themixray.puton.command.LeggingsCommand;
import ru.themixray.puton.listener.SwapItemListener;
import ru.themixray.puton.util.UnrealConfig;
import java.util.Objects;
import java.lang.reflect.Field;
import java.util.List;
public class Main extends JavaPlugin implements Listener {
public void onEnable() {
Config.loadConfig(new UnrealConfig(this, getDataFolder(), "config.yml"));
Bukkit.getPluginManager().registerEvents(this, this);
Bukkit.getPluginManager().registerEvents(new SwapItemListener(), this);
registerCommand(getCommand("helmet"), Config.HELMET_COMMANDS, new HelmetCommand());
registerCommand(getCommand("chestplate"), Config.CHESTPLATE_COMMANDS, new ChestplateCommand());
registerCommand(getCommand("leggings"), Config.LEGGINGS_COMMANDS, new LeggingsCommand());
registerCommand(getCommand("boots"), Config.BOOTS_COMMANDS, new BootsCommand());
}
public static final int HELMET_SLOT = 39;
public static final int CHESTPLATE_SLOT = 40;
public static final int LEGGINGS_SLOT = 41;
public static final int BOOTS_SLOT = 42;
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
ItemStack item = e.getCursor();
if (e.getSlotType() == InventoryType.SlotType.ARMOR) {
if (e.getSlot() == HELMET_SLOT && Config.ALLOW_HELMET) {
e.getWhoClicked().setItemOnCursor(e.getCurrentItem());
e.getWhoClicked().getInventory().setHelmet(item);
e.setCancelled(true);
} else if (e.getSlot() == CHESTPLATE_SLOT && Config.ALLOW_CHESTPLATE) {
e.getWhoClicked().setItemOnCursor(e.getCurrentItem());
e.getWhoClicked().getInventory().setHelmet(item);
e.setCancelled(true);
} else if (e.getSlot() == LEGGINGS_SLOT && Config.ALLOW_LEGGINGS) {
e.getWhoClicked().setItemOnCursor(e.getCurrentItem());
e.getWhoClicked().getInventory().setHelmet(item);
e.setCancelled(true);
} else if (e.getSlot() == BOOTS_SLOT && Config.ALLOW_BOOTS) {
e.getWhoClicked().setItemOnCursor(e.getCurrentItem());
e.getWhoClicked().getInventory().setHelmet(item);
e.setCancelled(true);
}
public CommandMap getCommandMap() {
try {
Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
bukkitCommandMap.setAccessible(true);
return (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void registerCommand(PluginCommand command, List<String> names, CommandExecutor executor) {
if (names.isEmpty()) {
command.unregister(getCommandMap());
return;
}
command.setExecutor(executor);
command.setName(names.get(0));
if (names.size() > 1) command.setAliases(names.subList(1, names.size()));
command.setTabCompleter((a, b, c, d) -> List.of());
}
}

View File

@ -1,4 +1,4 @@
package ru.themixray.puton.commands;
package ru.themixray.puton.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;

View File

@ -1,4 +1,4 @@
package ru.themixray.puton.commands;
package ru.themixray.puton.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;

View File

@ -1,4 +1,4 @@
package ru.themixray.puton.commands;
package ru.themixray.puton.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;

View File

@ -1,4 +1,4 @@
package ru.themixray.puton.commands;
package ru.themixray.puton.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;

View File

@ -0,0 +1,40 @@
package ru.themixray.puton.listener;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;
import ru.themixray.puton.Config;
public class SwapItemListener implements Listener {
public static final int HELMET_SLOT = 39;
public static final int CHESTPLATE_SLOT = 40;
public static final int LEGGINGS_SLOT = 41;
public static final int BOOTS_SLOT = 42;
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
ItemStack item = e.getCursor();
if (e.getSlotType() == InventoryType.SlotType.ARMOR) {
if (e.getSlot() == HELMET_SLOT && Config.ALLOW_HELMET) {
e.getWhoClicked().setItemOnCursor(e.getCurrentItem());
e.getWhoClicked().getInventory().setHelmet(item);
e.setCancelled(true);
} else if (e.getSlot() == CHESTPLATE_SLOT && Config.ALLOW_CHESTPLATE) {
e.getWhoClicked().setItemOnCursor(e.getCurrentItem());
e.getWhoClicked().getInventory().setHelmet(item);
e.setCancelled(true);
} else if (e.getSlot() == LEGGINGS_SLOT && Config.ALLOW_LEGGINGS) {
e.getWhoClicked().setItemOnCursor(e.getCurrentItem());
e.getWhoClicked().getInventory().setHelmet(item);
e.setCancelled(true);
} else if (e.getSlot() == BOOTS_SLOT && Config.ALLOW_BOOTS) {
e.getWhoClicked().setItemOnCursor(e.getCurrentItem());
e.getWhoClicked().getInventory().setHelmet(item);
e.setCancelled(true);
}
}
}
}