Config with id:subid

Discussion in 'Plugin Development' started by darkdeath1332, Jul 1, 2013.

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

    darkdeath1332

    Hi,

    I want to ban item clicking of certain items, with this I can only use the ID and not the sub ID.

    How can I set my config with <id>:<subid> like 1:2 and then check for getTypeId and getData?

    Code:java
    1. @EventHandler(priority=EventPriority.HIGH)
    2. public void onBlockPlace2(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if (com.bukkit.darkdeath1332.main.itemids.contains(String.valueOf(player.getItemInHand().getTypeId()))){
    5.  
    6. int permid = player.getItemInHand().getTypeId();
    7.  
    8. if (!(player.hasPermission("bedcraft." + permid))) {
    9. event.setCancelled(true);
    10. player.setItemInHand(null);
    11. player.sendMessage(ChatColor.GOLD + "[BedCraftBI] " + ChatColor.RED + com.bukkit.darkdeath1332.main.bannedtext);
    12. }
    13.  
    14. }
    15. }
    16. }
     
  2. Offline

    sirantony

    Get the id of an item:
    Code:java
    1. player.getItemInHand().getTypeId();

    Get the sub-id of an item:
    Code:java
    1. player.getItemInHand().getDurability()


    To check you can do this:
    Code:java
    1. if (player.getItemInHand().getDurability() == 'sub-id'){
    2. //your code
    3. }
    4.  
     
  3. Offline

    fireblast709

    use it as a String. So (and yes, with the '') 'id:subid'
     
  4. Offline

    MP5K

    //edit: I got ninja'd :D
    Hello darkdeath1332,
    Is it a help for you if you know how to transfer an String "ID:SUB_ID" into an itemstack?
    Code:java
    1.  
    2. //I typed the code out of my mind so it may contains some typos.
    3. /* Format: ID:SUBID */
    4. public static final ItemStack getStackByString(String input){
    5. if(raw == null) return null;
    6. String[] raw = input.split(":");
    7. if(raw.lenght != 2) return null;
    8. try{
    9. Integer id = Integer.valueOf(raw[0]);
    10. Byte data = Byte.valueOf(raw[1];
    11. ItemStack stack = new ItemStack(id);
    12. MaterialData meta = stack.getData();
    13. meta.setData(data);
    14. stack.setData(meta);
    15. return stack;
    16. }catch(Exception e){ return null; };
    17. };

    if not try to check that:
    Code:java
    1. ItemStack stack = event.getPlayer().getItemInHand();
    2. player.hasPermission("bedcraft." + stack.getTypeID() + "." + stack.getData().getData())

    so to interact with black wool he would the permission "bedcraft.35.15"

    im pretty sure that wont work :D
    cause:
    A. getDurability() returns a Short and you cant compare an short to an Character
    B. getDurability() returns for example the Durability of an Pickaxe/axe/Sword?
    or am i wrong?

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

    darkdeath1332

    Its with a FTB ultimate server, like banning the useage of a new item with the id 3131:5.

    First I need to check the first line in my string config with;
    - 3131:5
    - 214:6
    - 324:6

    Code:java
    1. if (player.getItemInHand().getTypeId() == <3131 (ID)> && player.getItemInHand().getData() == <5(SUB ID)>) {
    2.  


    But with reading 3131 and 5 from my config, it will also check the other items in the config.

    This code works with only the ID
    Code:java
    1. @EventHandler(priority=EventPriority.HIGH)
    2. public void onBlockPlace2(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if (com.bukkit.darkdeath1332.main.itemids.contains(String.valueOf(player.getItemInHand().getTypeId()))){
    5.  
    6. int permid = player.getItemInHand().getTypeId();
    7.  
    8. if (!(player.hasPermission("bedcraft." + permid))) {
    9. event.setCancelled(true);
    10. player.setItemInHand(null);
    11. player.sendMessage(ChatColor.GOLD + "[BedCraftBI] " + ChatColor.RED + com.bukkit.darkdeath1332.main.bannedtext);
    12. }
    13.  
    14. }
    15. }
    16. }


    Main class;
    Code:java
    1. import java.util.List;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5.  
    6. public final class main extends JavaPlugin {
    7. public static List<String> itemids;
    8. public static List<String> itemids2;
    9. public static List<String> CommandMessage;
    10. public static String bannedtext;
    11. public static String Command;
    12. @Override
    13. public void onEnable() {
    14. this.saveDefaultConfig();
    15. itemids = this.getConfig().getStringList("BannedItems");
    16. itemids2 = this.getConfig().getStringList("PlacementBanned");
    17. CommandMessage = this.getConfig().getStringList("CommandMessage");
    18. bannedtext = this.getConfig().getString("Message");
    19. Command = this.getConfig().getString("Command");
    20.  
    21. getServer().getPluginManager().registerEvents(new com.bukkit.darkdeath1332.click(), this);
    22. getServer().getPluginManager().registerEvents(new com.bukkit.darkdeath1332.interact(), this);
    23. getServer().getPluginManager().registerEvents(new com.bukkit.darkdeath1332.place(), this);
    24. getCommand("banneditems").setExecutor(new Banneditems());
    25. }
    26.  
    27. }
     
  6. Offline

    sirantony

    You are wrong! I used that in my own code. And I know that it is a character but you have to replace it with a short (e.g.: 1 , 2 ,5 ,...) -_-

    EDIT: not a number but a short...
     
  7. Offline

    MP5K

    yea you are right for some reason getDurability() actually returns the subid lol...
     
Thread Status:
Not open for further replies.

Share This Page