World guard API error

Discussion in 'Plugin Development' started by astepos, Feb 1, 2024.

  1. Offline

    astepos

    please help me

    !up
     

    Attached Files:

    Last edited by a moderator: Feb 1, 2024
  2. Offline

    timtower Administrator Administrator Moderator

    @astepos Hover your mouse over the error, what does it say?
     
  3. Offline

    astepos

    here is the code
    Code:
    package org.examplea.spawnerkey;
    
    import com.sk89q.worldguard.WorldGuard;
    import com.sk89q.worldguard.bukkit.RegionContainer;
    import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    import com.sk89q.worldguard.protection.ApplicableRegionSet;
    import com.sk89q.worldguard.protection.regions.RegionContainer;
    import com.sk89q.worldguard.protection.regions.RegionQuery;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.NamespacedKey;
    import org.bukkit.block.Block;
    import org.bukkit.block.CreatureSpawner;
    import org.bukkit.command.PluginCommand;
    import org.bukkit.configuration.file.FileConfiguration;
    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.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.persistence.PersistentDataContainer;
    import org.bukkit.persistence.PersistentDataType;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.*;
    
    public class CustomSpawnerPlugin extends JavaPlugin implements Listener {private FileConfiguration config;
    private final List<WeightedEntityType> allowedEntityTypes = new ArrayList<>();
    private NamespacedKey spawnerKeyKey;
    
    public CustomSpawnerPlugin() { }public void onEnable() {this.getConfig().options().copyDefaults(true);
    this.saveDefaultConfig();
    this.config = this.getConfig();
    this.loadAllowedEntityTypes();
    PluginCommand reloadConfigCommand = this.getCommand("reloadconfig");
    if (reloadConfigCommand != null) {reloadConfigCommand.setExecutor(new ReloadConfigCommand(this));
    } else {this.getLogger().severe("Команда /reloadconfig не найдена!");
    }this.getServer().getPluginManager().registerEvents(this, this);
    Objects.requireNonNull(this.getCommand("spgivas")).setExecutor(new GiveSpawnerKeyCommand(this));
    this.saveDefaultConfig();
    this.config = this.getConfig();
    this.loadAllowedEntityTypes();
    this.spawnerKeyKey = new NamespacedKey(this, "spawner-key");
    }private void loadAllowedEntityTypes() {this.allowedEntityTypes.clear();
    List<Map<?, ?>> mobConfigList = this.config.getMapList("spawner_mobs");
    for (Map<?, ?> mobConfig : mobConfigList) {String entityTypeStr = (String) mobConfig.get("entity_type");
    EntityType entityType = EntityType.valueOf(entityTypeStr);
    int weight = (Integer) mobConfig.get("weight");
    this.allowedEntityTypes.add(new WeightedEntityType(entityType, weight));
    } }private void decreaseItemStack(Player player, ItemStack itemStack) {int firstSlot = player.getInventory().first(itemStack);
    if (firstSlot != -1) {ItemStack stackInSlot = player.getInventory().getItem(firstSlot);
    if (stackInSlot.getAmount() > 1) {ItemStack newItemStack = stackInSlot.clone();
    newItemStack.setAmount(newItemStack.getAmount() - 1);
    player.getInventory().setItem(firstSlot, newItemStack);
    } else {player.getInventory().clear(firstSlot);
    }player.updateInventory();
    } }@EventHandlerpublic void onInventoryClick(InventoryClickEvent event) {if (event.getWhoClicked() instanceof Player) {ItemStack clickedItem = event.getCurrentItem();
    if (this.isSpawnerKey(clickedItem)) {event.setCancelled(false);
    } } }@EventHandlerpublic void onPlayerDropItem(PlayerDropItemEvent event) {ItemStack droppedItem = event.getItemDrop().getItemStack();
    if (this.isSpawnerKey(droppedItem)) {event.setCancelled(false);
    } }@EventHandlerpublic void onPlayerInteract(PlayerInteractEvent event) {Player player = event.getPlayer();
    Action action = event.getAction();
    if (action == Action.RIGHT_CLICK_BLOCK) {Block clickedBlock = event.getClickedBlock();
    if (clickedBlock != null && clickedBlock.getType() == Material.SPAWNER) {if (!isInRegion(clickedBlock.getLocation())) {CreatureSpawner spawner = (CreatureSpawner) clickedBlock.getState();
    ItemStack heldItem = player.getInventory().getItemInMainHand();
    if (isSpawnerKey(heldItem) && hasCorrectUniqueKey(heldItem)) {spawner.setSpawnedType(getRandomEntityType());
    spawner.update();
    player.sendMessage("Спавнер сменил моба.");
    decreaseItemStack(player, heldItem);
    } } else {player.sendMessage("Нельзя использовать ключ в регионе WorldGuard.");
    } } } }private boolean isInRegion(Location location) {WorldGuardPlugin worldGuard = WorldGuardPlugin.inst();
    RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
    RegionQuery query = ((RegionContainer) container).createQuery();
    
    try {ApplicableRegionSet regions = query.getApplicableRegions(location);
    return !regions.getRegions().isEmpty();
    } catch (RegionQueryException e) {e.printStackTrace();
    }return false;
    }private boolean hasCorrectUniqueKey(ItemStack itemStack) {ItemMeta itemMeta = itemStack.getItemMeta();
    if (itemMeta == null) {return false;
    } else {PersistentDataContainer pdc = itemMeta.getPersistentDataContainer();
    String uniqueKey = pdc.get(this.spawnerKeyKey, PersistentDataType.STRING);
    return uniqueKey != null && uniqueKey.startsWith("spawner-key-");
    } }private boolean isSpawnerKey(ItemStack itemStack) {if (itemStack != null && itemStack.getType() == Material.PAPER) {ItemMeta itemMeta = itemStack.getItemMeta();
    return itemMeta != null && itemMeta.getPersistentDataContainer().has(this.spawnerKeyKey, PersistentDataType.STRING);
    } else {return false;
    } }private EntityType getRandomEntityType() {int totalWeight = this.allowedEntityTypes.stream().mapToInt(WeightedEntityType::getWeight).sum();
    int randomWeight = (new Random()).nextInt(totalWeight) + 1;
    for (WeightedEntityType weightedEntityType : this.allowedEntityTypes) {randomWeight -= weightedEntityType.getWeight();
    if (randomWeight <= 0) {return weightedEntityType.getEntityType();
    } }return EntityType.ZOMBIE; // Default if something goes wrong}private ItemStack createSpawnerKey() {ItemStack key = new ItemStack(Material.PAPER, 1);
    ItemMeta keyMeta = key.getItemMeta();
    if (keyMeta != null) {keyMeta.setDisplayName("§x§f§b§0§0§0§0§l§oК§x§f§b§1§4§0§0§l§oл§x§f§c§2§8§0§0§l§oю§x§f§c§3§d§0§0§l§oч §x§f§c§5§1§0§0§l§oс§x§f§d§6§5§0§0§l§oп§x§f§d§7§9§0§0§l§oа§x§f§e§8§d§0§0§l§oу§x§f§e§a§1§0§0§l§oн§x§f§e§b§6§0§0§l§oе§x§f§f§c§a§0§0§l§oр§x§f§f§d§e§0§0§l§oа");
    keyMeta.setLore(Collections.singletonList("Используйте его, чтобы сменить моба в спаунере."));
    String uniqueKey = "spawner-key-" + UUID.randomUUID();
    keyMeta.getPersistentDataContainer().set(this.spawnerKeyKey, PersistentDataType.STRING, uniqueKey);
    keyMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
    key.setItemMeta(keyMeta);
    }return key;
    }public void giveSpecialKey(Player player) {ItemStack spawnerKey = this.createSpawnerKey();
    player.getInventory().addItem(spawnerKey);
    }}
     

    Attached Files:

    Last edited by a moderator: Feb 1, 2024
  4. Offline

    timtower Administrator Administrator Moderator

    @astepos Then find the class that it is supposed to be?
    Probably in the worldguard source.
     
  5. Offline

    astepos

    what?
     
  6. Offline

    timtower Administrator Administrator Moderator

Share This Page