1.15.2 Trick java to consider skull as block

Discussion in 'Spigot Plugin Development' started by Ching, Feb 17, 2020.

  1. Hello there! I am currently developing a BlockRestrict Plugin but one of the few blocks that does not work is a skull. I need to make it so the system will only block the unique skull.

    For example;

    Currently;
    I Restrict placement of wither skeleton skull - the system says 'That is not a block!'

    What I would like;
    I restrict placement of wither skeleton skull - success - I can place a zombie head and not a wither skeleton skull

    Is there a way I could do this?

    Many Thanks.
     
  2. Hi, can you show actual code? may be the material is not correct, did you put api-version on your plugin.yml?
     
  3. Sure will add this right now;

    package com.sylvcraft;

    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.Map;

    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.PluginManager;
    import com.sylvcraft.events.BlockPlace;

    import com.sylvcraft.commands.br;


    public class BlockRestrict extends JavaPlugin {
    @Override
    public void onEnable() {
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(new BlockPlace(this), this);
    getCommand("br").setExecutor(new br(this));
    saveDefaultConfig();
    getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\n\nBlockRestricter has been Enabled\n\n");
    }
    public void msg(String msgCode, CommandSender sender) {
    String tmp = getConfig().getString("messages." + msgCode, msgCode) + ' ';
    if (tmp.trim().equals("")) return;
    for (String m : tmp.split("%br%")) {
    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', m));
    }
    }

    public void msg(String msgCode, CommandSender sender, Map<String, String> data) {
    String tmp = getConfig().getString("messages." + msgCode, msgCode) + ' ';
    if (tmp.trim().equals("")) return;
    for (Map.Entry<String, String> mapData : data.entrySet()) {
    tmp = tmp.replace(mapData.getKey(), mapData.getValue());
    }
    msg(tmp, sender);
    }
    public void onDisable() {
    getServer().getConsoleSender().sendMessage(ChatColor.RED + "\n\n\nBlockRestricter has been Disabled");
    }
    }

    package com.sylvcraft.commands;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import com.sylvcraft.BlockRestrict;

    public class br implements CommandExecutor {
    BlockRestrict plugin;

    public br(BlockRestrict instance) {
    plugin = instance;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    try {
    String targetBlock = ""; Player p = (sender instanceof Player)?(Player)sender:null;
    if (!sender.hasPermission("blockrestrict.admin")) {
    plugin.msg("access-denied", sender);
    return true;
    }

    if (args.length == 0) {
    if (!(sender instanceof Player)) {
    plugin.msg("specify-block", sender);
    return true;
    }
    if (!p.getInventory().getItemInMainHand().getType().isBlock()) {
    plugin.msg("not-a-block", sender);
    return true;
    }

    targetBlock = p.getInventory().getItemInMainHand().getType().name();
    } else {
    if (args[0].equalsIgnoreCase("list")) {
    listMaterials(sender);
    return true;
    }

    Material m = Material.matchMaterial(args[0]);
    if (m == null) {
    plugin.msg("invalid-item", sender);
    return true;
    }
    if (!m.isBlock()) {
    plugin.msg("not-a-block", sender);
    return true;
    }

    targetBlock = m.name();
    }



    List<String> blocks = plugin.getConfig().getStringList("blocks");
    if (blocks.contains(targetBlock)) {
    blocks.remove(targetBlock);
    plugin.msg("removed", sender);
    } else {
    blocks.add(targetBlock);
    plugin.msg("added", sender);
    }

    plugin.getConfig().set("blocks", blocks);
    plugin.saveConfig();
    return true;
    } catch (Exception ex) {
    return false;
    }
    }

    void listMaterials(CommandSender sender) {
    Map<String, String> data = new HashMap<String, String>();
    plugin.msg("list-header", sender);
    plugin.msg("", sender, data);
    }
    }




    package com.sylvcraft.events;

    import java.util.List;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import com.sylvcraft.BlockRestrict;
    import org.bukkit.event.block.BlockPlaceEvent;


    public class BlockPlace implements Listener {
    BlockRestrict plugin;
    public BlockPlace(BlockRestrict instance) {
    plugin = instance;
    }

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent e) {
    if (e.getPlayer().hasPermission("blockrestrict.bypass")) return;
    if (e.getPlayer().hasPermission("blockrestrict.bypass." + e.getBlockPlaced().getType().name().toLowerCase())) return;
    List<String> blocks = plugin.getConfig().getStringList("blocks");
    if (!blocks.contains(e.getBlockPlaced().getType().name())) return;

    plugin.msg("not-allowed", e.getPlayer());
    e.setCancelled(true);
    }
    }

    messages:
    access-denied: '&cAccess denied!'
    specify-block: You must pass the block material name
    not-a-block: '&cThat is not a block!'
    invalid-item: '&cThat is not a valid material!'
    removed: '&eThat block is now unrestricted.'
    added: '&eThat block is now restricted.'
    not-allowed: '&cYou are not allowed to place that block!'
    list-header: '&eBlock based materials:%br%%br%'
    list-data: '&6%value%'
    blocks:

    name: BlockRestrict
    author: JerryEgg
    main: com.sylvcraft.BlockRestrict
    version: 1.0
    description: Block the placement of blocks
    commands:
    br:
    description: Plugin command
    aliases: ["blockdisable", "blockrestrict", "bb"]
    br list:
    description: Plugin command

    Any assistance would be appreciated.
     
  4. Putting api-version: 1.15 on plugin.yml should work. if you don't do that, then some blocks are recognized as AIR
     
  5. Thankyou, worked. <3

    Another question then.. If thats okay with you -

    When I try to display the list of blocked items it displays;
    [15:15:10] [Server thread/INFO]: Block based materials:
    [15:15:10] [Server thread/INFO]:
    [15:15:10] [Server thread/INFO]:
    [15:15:10] [Server thread/INFO]: MemorySection[path='messages', root='YamlConfiguration']

    How could I fix this? The list of blocked items update in the config but can't display ingame.
     
  6. If you want to print a list or map you should loop it's values and print each one