Plugin Help Material.getMaterial

Discussion in 'Plugin Help/Development/Requests' started by ScorixEar, Jun 10, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    ScorixEar

    hello guys and girls,
    I want to make a give command - not that difficult but there should also be the item names included
    here my code
    Code:
    public void onCommandUse(Player p, String[] args) {
            if(args[0].equalsIgnoreCase(Material.getMaterial(args[0]).toString()))
            {
                boolean isNumeric = args[1].matches("[0-9]+");
                if(isNumeric)
                {
                    ItemStack r = new ItemStack(Material.getMaterial(args[0]), Integer.parseInt(args[1]));
                    p.sendMessage("§6[§aVenoria§6] §bEs wurden allen Spielern §a"+args[1]+"§c "+Material.getMaterial(args[0]).toString()+"§b gegeben.");
                    for(Player z:Bukkit.getOnlinePlayers())
                    {
                      
                        z.getInventory().addItem(r);
                        z.sendMessage("§6[§aVenoria§6] §bDir wurde von §6"+p.getName()+" §a"+args[1]+" §c"+Material.getMaterial(args[0]).toString()+"§b gegeben.");
                    }
                }
                else
                {
                    p.sendMessage("§6[§aVenoria§6] §cDein zweites Argument ist keine Zahl!");
                }
    but they don't accept the minecraft:command_block or command_block for example (also not Bukkit COMMAND)
     
  2. Offline

    I Al Istannen

    @ScorixEar Haven't tried and won't be able today, but try using matchMaterial.
    Quote from "getMaterial()": "This is a normal lookup, names must be the precise name they are given in the enum."
    Qoute from "matchMaterial()": "This is a match lookup; names will be converted to uppercase, then stripped of special characters in an attempt to format it like the enum."
    Looks better.
     
  3. Offline

    ScorixEar

    Are you following me or why you answer to all my questions? xD
    Code:
    [22:00:31] [Server thread/INFO]: minecrafthaifl issued server command: /giveall command_block 1
    [22:00:31] [Server thread/ERROR]: Could not pass event PlayerCommandPreprocessEvent to PvPsystem v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1125) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:968) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:710) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at minecrafthaifl.pvp.command.CommandGiveAll.onCommandUse(CommandGiveAll.java:24) ~[?:?]
        at minecrafthaifl.pvp.command.CommandHandler.onCommand(CommandHandler.java:83) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot_server.jar:git-Spigot-ea179b3-6e0120a]
        ... 16 more
    [22:02:22] [Server thread/INFO]: Stopping the server
    
    line 24 is:
    Code:
    if(args[0].equalsIgnoreCase(Material.matchMaterial(args[0]).toString()))
     
  4. Offline

    I Al Istannen

    @ScorixEar Material wasn't found. But apart from this, what should that if do? Just check if th Material is null. It will be if no Material is found. I guess you will have to split the "minecraft:" off by yourself. You could also use String#replaceAll to replace the "minecraft:". That would have the advantage, that, if no "minecraft:" prefix is entered nothing will break.
     
  5. Offline

    pie_flavor

    @I Al Istannen This doesn't change the fact that the enum names are not the same as the minecraft names.
    @ScorixEar Messing about with reflection, will find a way in a few minutes
     
  6. Offline

    I Al Istannen

    @pie_flavor I think they are for most items. But you are right.
    Good Luck! :)
     
  7. Offline

    ScorixEar

    oh no...
    ok, and i have another question
    i can set Scoreboards but how can i remove them?
     
  8. Offline

    I Al Istannen

    @pie_flavor @ScorixEar That works:
    Code:
    Material material = Material.matchMaterial(args[1]);
    if (material == null) {
      material = Bukkit.getUnsafe().getMaterialFromInternalName(args[1]);
    }
    
    
    It is the code that Bukkit uses for "/give": here

    @ScorixEar As far as I know you can't. You can set it to the default or a new Blank however. Use "Bukkit.getScoreboardManager().getMainScoreboard()" for the deafult one or "Bukkit.getScoreboardManager().getNewScoreboard()" for a new, empty one.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  9. Offline

    pie_flavor

    @I Al Istannen @ScorixEar This also works.
    Code:
    public Material lookupMaterial(String s) {
            try {
                Package cbroot = getServer().getClass().getPackage();
                Class<?> CraftMagicNumbers = Class.forName(cbroot.getName()+".util.CraftMagicNumbers", false, getServer().getClass().getClassLoader());
                Method m = CraftMagicNumbers.getMethod("getMaterialFromInternalName", String.class);
                Material material = (Material) m.invoke(null, s);
                return material;
            } catch (NoSuchMethodException | SecurityException | ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                return null;
            }
        }
     
  10. Offline

    ScorixEar

    The problem is @I Al Istannen, that getUnsafe a deprecated method is.
    I can't disable a Scoreboard? That doesn't make sense. I see this on other server
    I set the scoreboard like this:
    Code:
    Scoreboard playersboard=Bukkit.getScoreboardManager().getNewScoreboard();
                    Objective objective = playersboard.registerNewObjective("Stats", "Stats");
                    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                    objective.setDisplayName("Stats "+p.getName());
                    p.setScoreboard(playersboard);
    
     
  11. Offline

    I Al Istannen

    @ScorixEar That method is what Bukkit uses in its give command. It is also what pie used in bis code. You will habe to use that vor build your own. I don't know if the method will be remived in the future, but if it will there will be a New one. I would use it and live with it.
    Removing the scoreboard. If you want it to look linke there is no scoreboard (Like joining an vanilla Server for the first time) use the getNewScoreboard() and set that AS the players scoreboard. I think that is what you Art searching.

    Sorry for spelling, german autocorrect on phone -.
    -
     
Thread Status:
Not open for further replies.

Share This Page