Entity (CraftItem) Help

Discussion in 'Plugin Development' started by -_Husky_-, Jun 28, 2012.

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

    -_Husky_-

    hey guys, I'm trying to get the entities around an entity. although, this method wont work
    PHP:
    <entity>.getNearbyEntity(5,5,5).contains(264);
    ;). Help please.
     
  2. Offline

    Craftiii4

    you are checking it for an int?

    This will give of a list of the entitys, log it to see ^^

    For example, i use this in a plugin;

    Code:java
    1. Entity chicken = block.getWorld().spawnCreature(
    2. block.getLocation(), EntityType.CHICKEN);
    3.  
    4.  
    5. int limit = 0;
    6. while (chicken == null && limit < 5){
    7. chicken = block.getWorld().spawnCreature(
    8. block.getLocation(), EntityType.CHICKEN);
    9. limit++;
    10. }
    11.  
    12.  
    13. List<Entity> list = chicken.getNearbyEntities(
    14. radius, radius, radius);
    15. chicken.remove();
    16.  
    17. int test01 = 0;
    18. int test02 = list.size();
    19.  
    20. while (test02 > test01) {
    21.  
    22. Entity entity = list.get(test01);
    23. if (entity instanceof Player) {
    24. ((Player) entity).getPlayer().sendMessage(
    25. ChatColor.GOLD
    26. + "5 minutes left! - " + ChatColor.RED + "Max!");
    27.  
    28. }
    29.  
    30. test01++;
    31. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  3. the methode is working exactly as it designed to do, return true if the specified object is in the list, and because there are no integers at the list, it return false
     
    -_Husky_- likes this.
  4. Offline

    stelar7

    .getNearbyEntities(5,5,5);
    returns a List<Entity> all the entities around the player, 5 blocks in every direction (up, down, left, right, forward and backward)
    so, it wont help to check if it contains an integer
     
  5. Offline

    -_Husky_-

    // Edit... Ok, i logged it, and it returns CraftItems.. How can i cast them to a item such as diamond?

    ferrybig stelar7
     
  6. .getNearbyEntity(5,5,5)
    actually returns an Collection<Entity> you can see if an Entity is an item whit instanceof , and call the getItem() on it for an item stack, on an itemStack, you can call getType, to see if its diamond
     
  7. Offline

    -_Husky_-

    Example please. i don't understand.
     
  8. Code:
    List<Entity> entities = player.getNearbyEntities(5,5,5); // Get entities in a 5x5x5 box size around the player
    
    for(Entity ent : entities) // loop through each entitiy
    {
        if(ent instanceof CraftItem) // if is that Entity an instance of CraftItem (which extends Entity)
        {
            CraftItem itemEnt = (CraftItem)ent; // cast Entity to CraftItem
    
            // now see what methods are available, it should provide something to get the type ID or Material for that dropped item.
        }
    }
     
    -_Husky_- likes this.
  9. Offline

    -_Husky_-

    Alright, What I'm trying to achieve..

    • Listen for 2 diamond being thrown
    • Listen for a stick being thrown
    If Both requirements are met, remove them both, and Spawn a Diamond sword, I've done alot of messing about with things, and none of them work, Can someone please set the code up for me? For future reference too (Notes if possible, I should be able to figure it out without them, but still)

    Digi
    V10lator
    ferrybig
    stelar7

    I've tagged you because you seem to have some knowledge on this. Sorry if you are not interested.

    If there is anyone else that is interested, Please go ahead and do this.

    Thankyou again, Husky.
     
  10. Offline

    Deleted user

    Items are entities?
     
  11. I see some bad points at this, if you want to craft an iron sword, you need to thhrow the stick first, else you make shears. basicly, you need an list of the recipes, and try to match it whit the items laying on the ground
     
  12. Offline

    -_Husky_-

    It's Just going to be for swords, armour, tools. Nothing else.. ;D
     
  13. you need to have an set/list/array for al the recipes, when there is an item ropped, you need to check if you can make an recipe whit those items (loop them al) and make the recipe
    look out whit armor, they have the same base materials, only at diferent amounts
     
  14. Offline

    -_Husky_-

    Would you mind doing this? I dont know how this would operate

    Like, i can do armour, no problem. But pickaxes and other tools would be an issue..
     
  15. Actually armor could be a problem, because you'll have to drop exacly the amount of ingots it requires to craft that armor, if you drop 1 at a time it will craft boots... unless you make a command or something to trigger the craft manually.

    And yes, pickaxe and axe is a problem because you need a shape... I have no ideea how to diferentiate between those... you could change the recipe to use 3 sticks and 2 iron.
    Code:
    S I
    S I
    S
    looks like an axe to me :p but I get that you don't need it in the workbench, this is just to vizualize that it could be changed to that.

    Still, what are you mainly trying to achieve ?
     
  16. Offline

    -_Husky_-

    Well, i am making a plugin, theres classes, and one is blacksmith, so you need to make an anvil, (all this code is done AND works)
    Then you need to throw materials onto the "anvil" to craft forged items (pickaxe, armour)

    Could you make the plugin wait 10 seconds before performing anything? (crafting anything)

    That could also add a realistic touch to the plugin, because it needs time to form, :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  17. Waiting can be done with a scheduler.

    If you want really realistic blacksmithing, I suggest you search on youtube how smelting is done in the Dark Messiah and Gothic 1 games.
     
  18. Offline

    -_Husky_-

    Yeah, I am using flint to scratch the sword sharper, but can't get my head around this, or I extremely over think :/

    Are you able to do something like this for me (just code, not compiled)


    Edit - if you want to change recipes (and your willing to do this) go ahead. :)
     
  19. I don't really know your plan for the crafting steps so I can't really help with any code... but you really should try to do it yourself first and then post if you have any difficulties :}
     
  20. Offline

    -_Husky_-

    I've tried coding it a few times, I will give it One last hoorah, then post my code, thanks for your help Digi

    Well, I need help, I've come to a halt, I need to get the entities Around the one entity that was just thrown... which is why i think i was stuck, i tried to get the amount of something in an itemstack, but it just returns '1', Even when i throw 60. :/ Help Digi?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  21. I have no clue where you tried that and how you did it, so can you post the code ? :)
     
  22. Offline

    -_Husky_-

    Code:java
    1. public void onSmithCraft(PlayerDropItemEvent e) {
    2. Player p = e.getPlayer();
    3. List<Entity> nearby = p.getNearbyEntities(5,5,5);
    4. Location l = p.getLocation();
    5. if(api.getJob(p) == "Blacksmith") {
    6. CraftItem c = null;
    7. ItemStack i = null;
    8. for(Entity ent : nearby) // loop through each entitiy
    9. {
    10. if(ent instanceof CraftItem) // if is that Entity an instance of CraftItem (which extends Entity)
    11. {
    12. c = (CraftItem)ent;
    13. i = c.getItemStack();
    14.  
    15. }
    16. }
    17. //
    18. int a = i.getAmount();
    19. if(a == 8) {
    20. if(i.getType() == Material.IRON_INGOT) {
    21. i.setAmount(0);
    22. c.remove();
    23. p.getWorld().dropItem(l, new ItemStack(307,1));
    24. p.sendMessage(ChatColor.GREEN + "[hSkyrim] You have just crafted an iron chestplate.");
    25. } else if(i.getType() == Material.GOLD_INGOT) {
    26.  
    27. } else if(i.getType() == Material.DIAMOND) {
    28.  
    29. }
    30. }
    31. }
    32. }
     
  23. You're not breaking the loop when you find an item, so the i can be re-asigned to an entity that was detected last but not necesarily dropped last.
    Also, indent your code as it will be alot easier to read by everybody, including yourself. In Eclipse you can use Ctrl+i to do it for you.
     
    ferrybig likes this.
  24. Offline

    -_Husky_-

    Right i got some code that is working, although there is 1 bug.

    When you throw an item, it then registers that. after that. it will pickup whatever you threw.

    If you just throw the 8 iron (grouped) straight out, it wont work. here's the code. (indented)

    Code:java
    1. @EventHandler
    2. public void onSmithCraft(PlayerDropItemEvent e) {
    3. Player p = e.getPlayer();
    4. List<Entity> nearby = p.getNearbyEntities(5,5,5);
    5. Location l = p.getLocation();
    6. if(api.getJob(p) == "Blacksmith") {
    7. CraftItem c = null;
    8. ItemStack i = null;
    9. for(Entity ent : nearby) // loop through each entitiy
    10. {
    11. if(ent instanceof CraftItem) // if is that Entity an instance of CraftItem (which extends Entity)
    12. {
    13. c = (CraftItem)ent;
    14. i = c.getItemStack();
    15. if(i.getAmount() == 8) {
    16. if(i.getTypeId() == 265) {
    17. i.setAmount(0);
    18. c.remove();
    19. p.getWorld().dropItem(l, new ItemStack(307,1));
    20. p.sendMessage(ChatColor.GREEN + "[hSkyrim] You have just crafted an iron chestplate.");
    21. } else if(i.getType() == Material.GOLD_INGOT) {
    22.  
    23. } else if(i.getType() == Material.DIAMOND) {
    24.  
    25. }
    26. }
    27. break;
    28. }
    29. }
    30. }
    31. }


    hah, Never mind, i will use an itemstack to check if it has the amount... it works this time,

    Code:
    Code:java
    1. @EventHandler
    2. public void onSmithCraft(PlayerDropItemEvent e) {
    3. Player p = e.getPlayer();
    4. Location l = p.getLocation();
    5. if(api.getJob(p) == "Blacksmith") {
    6. ItemStack i = e.getItemDrop().getItemStack();
    7. int amount = 0;
    8. i = e.getItemDrop().getItemStack();
    9. amount = i.getAmount();
    10. if(amount == 8) {
    11. if(i.getTypeId() == 265) {
    12. i.setAmount(0);
    13. e.getItemDrop().remove();
    14. p.getWorld().dropItem(l, new ItemStack(307,1));
    15. p.sendMessage(ChatColor.GREEN + "[hSkyrim] You have just crafted an iron chestplate.");
    16. } else if(i.getType() == Material.GOLD_INGOT) {
    17. if(amount == 8) {
    18. i.setAmount(0);
    19. e.getItemDrop().remove();
    20. p.getWorld().dropItem(l, new ItemStack(Material.GOLD_CHESTPLATE,1));
    21. p.sendMessage(ChatColor.GREEN + "[hSkyrim] You have just crafted an gold chestplate.");
    22. }
    23. } else if(i.getType() == Material.DIAMOND) {
    24.  
    25. }
    26. }
    27. }
    28. }


    Can you do something similar for Say a Pickaxe Digi? I don't understand that.

    // Edit - got it, thanks anyway Digi!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  25. Indent your code already, code must be easily read by everybody, it shouldn't be stacked tightly, spacing is of no importance between expressions, use as much spacing as you need to make it easily readable !
    And when you finally use Ctrl+i to make eclipse indent it for you, you'll be able to read it much easier.

    Also, you don't understand your own code ? Apart from the inexistent indentation which makes it a pain to read xD
     
  26. Offline

    -_Husky_-

    It's cool Digi, I got it. <3 Anyway, I did CTRL + I and it indented it, In eclipse then i paste it here and bam. :/
     
  27. And bam what ? That's the result, unindented ? :confused: Use pastebin then :p
    Or "bam I got it" as in you understand the code better ? :}
     
  28. Offline

    -_Husky_-

    both.. ;3
     
  29. Offline

    stelar7

    if you use the [ syntax] tag, the formatting dissapears
     
    -_Husky_- likes this.
  30. I must say I had never problems whit the [ syntax] tag and line indents lost, as showing this example:
    Code:YAML
    1.  
    2. commands:
    3. test:
    4. usage: /<command>
     
Thread Status:
Not open for further replies.

Share This Page