FurnaceInventory for Block on specific Location

Discussion in 'Plugin Development' started by Einsiedler98, Apr 23, 2014.

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

    Einsiedler98

    Hello, I'd like my plugin to change or to fill thefuel of a furnace on a specific location. First the location is just given as integers, later i will ascertain them by variables.

    Code:java
    1. public boolean PowerManagementFiller(CommandSender sender) {
    2.  
    3. Player p = null;
    4.  
    5. if (sender instanceof Player) {
    6. p = (Player)sender;
    7. } else {
    8. sender.sendMessage("§8[§4Error§8]§r Du bist kein Spieler!");
    9. return true;
    10. }
    11.  
    12. Location location = new Location(p.getWorld(), 100, 70, 100);
    13. Block block = location.getWorld().getBlockAt(location);
    14. FurnaceInventory fi = fi.setFuel(new ItemStack(Material.COAL, 64));
    15.  
    16. //temporary return statement
    17. return false;
    18. }

    My problems are:
    1) There is an error in FurnaceInventory fi = fi.setFuel(new ItemStack(Material.COAL, 64)): Type mismatch: cannot convert from void to FurnaceInventory
    I don't want any return statement from FurnaceInventory so in my eyes this is void.
    2) I don't know how to specificate the inventory of this Furnace on this location.
     
  2. Offline

    Einsiedler98

    Has anybody an idea how to solve these problems?
     
  3. Offline

    TrollTaylor

    This is what your supposed to have
    Code:java
    1. Furnace f = (Furnace) block;
    2. FurnaceInventory fi = f.getInventory();
    3. fi.setFuel(new ItemStack(Material.COAL, 64));
     
  4. Offline

    raGan.

    I believe it's
    Code:
    Furnace f = (Furnace) block.getState();
    Not to mention checking if said block is actually a furnace.
     
  5. Offline

    Einsiedler98

    TrollTaylor
    This seems to be the right attempt but without raGan. 's it won't work because the this can't be casted. And because I don't need to test whether the block is a furnace, this is the solution.
    Many thanks to the two of you! :)

    I try to let my plugin interact with industrial craft 2-experimental mod. I know that the IC2 Energy Network is reorganizing at the moment and don't work correctly in all entities eg the EU/t. But storage blocks are luckily seeming to work quite correctly and so i want by plugin instead of setting the fuel of a furnace to set the energy amount of eg an mfsu.
    I now have assigned it like this:
    Code:java
    1. Location location = new Location(p.getWorld(), -15, 87, 168);
    2. Block block = location.getWorld().getBlockAt(location);
    3. IEnergyStorage mfsu = (IEnergyStorage) block.getState();
    4. mfsu.setStored(10000);
    5.  

    And it told me that it could be casted:
    Code:
    Caused by: java.lang.ClassCastException: za.co.mcportcentral.block.CraftCustomContainer cannot be cast to ic2.api.tile.IEnergyStorage
    Anybody there who is quite good with ic2 api?

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

    raGan.

    I think this thread will get locked in no time. :(
     
  7. Offline

    Einsiedler98

    Why? Because this may belongs to ic2? Then please told me, on whom should I approach?
     
  8. Offline

    raGan.

  9. Offline

    Einsiedler98

    Ok I unterstand that. But I have still problems with tripping objects on a specific location. Now I have a lever I'd like to interact with:
    Code:java
    1. Location location = new Location(p.getWorld(), -15, 87, 168);
    2. Block block = location.getWorld().getBlockAt(location);
    3. Lever lever = (Lever)block.getState();
    4. lever.setPowered(true);


    It again tells me, that it couldn't cast block to lever.
    That whole thing is so important to me because this isn't an PlayerInteractEvent or something where you could trip the block because of the players interaction but a command.
     
  10. Offline

    raGan.

    If you take a look at Lever class, you will notice that it actually extends MaterialData and not BlockState.

    So you need to get BlockState from the Block object, and then get, update, and set its MaterialData. (Lever)
     
  11. Offline

    Einsiedler98

    Don't know how to do that exactly. Some kind of that?
    Code:java
    1. Location location = new Location(p.getWorld(), -15, 87, 168);
    2. Block block = location.getWorld().getBlockAt(location);
    3. BlockState bs = block.getState();
    4. block.getType();
    5. bs.update();
    6. block.setType(Material.LEVER);
    7. block.setPowered(true);
     
  12. Offline

    raGan.

    No, you need to change MaterialData, not type.
    Code:java
    1. // you might also want to check if this is instance of lever before casting
    2. Lever lever = (Lever)bs.getData();
    3. lever.setPowered(true);
    4. bs.setData(lever);
    5. bs.update() // you might not actually need this, I'm not sure
     
    Einsiedler98 likes this.
  13. Offline

    Einsiedler98

    Ok, so I check whether it is instanceof or not and it says no. But it is.. If I don't do the if check before, it throwas an error that it couldnt be casted :(
    Code:java
    1. Location location = new Location(p.getWorld(), -15, 87, 168);
    2. Block block = location.getWorld().getBlockAt(location);
    3. BlockState bs = block.getState();
    4. if (block instanceof Lever) {
    5. Lever lever = (Lever)bs.getData();
    6. lever.setPowered(true);
    7. bs.setData(lever);
    8. bs.update(); // you might not actually need this, I'm not sure
    9. } else {
    10. sender.sendMessage("Error");
    11. }
     
  14. Offline

    MCForger

    Einsiedler98
    Instead of doing an instance check why not just check the Material type of the block then cast?
     
  15. Offline

    raGan.

    It means that the block you are working with isn't a lever.
     
  16. Offline

    Einsiedler98

    MCForger
    Thanks. Now it works fine for me.

    But it is...

    Suppose that the lever should be turned off again a few ticks eg 10 later. Now I have that:
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.class, new Runnable() {
    2. public void run() {
    3. lever.setPowered(false);
    4. bs.setData(lever);
    5. bs.update();
    6. }
    7. }, 10L);

    But "this" doesn't work for me because my command is executed in an external class (onCommand wich implements CommandExecutor) and the method expect Plugin plugin, which I really don't know what's that. http://jd.bukkit.org/rb/doxygen/df/...eduler.html#addd6e203f7ec3a8371e09606f136b2f8
    I believe it is something with class JavaPlugin but this is extendet in my Main class, so I don't know how to reference that correctly. Maybe it is something completly different...
     
  17. Offline

    DxDy

    Regarding the Plugin-Stuff. Your Plugin is obviously a plugin, isn't it? That's because it extends JavaPlugin, which itself implements the Interface Plugin (an interface basically says what methods a something (ie Plugin) is supposed to have, but not how they do the work in those methods. That's what implementing classes are for ;) ). You can thus just pass the instance of your plugin to the scheduler. To get it just pass it to your external class.
     
  18. Offline

    raGan.

    Einsiedler98
    Getting to know better how java works in general before trying to code Bukkit plugins would make this a lot easier for you, and possibly save you some time. I suggest you finding a good tutorial that provides thorough explanation of object-oriented programming.
     
  19. Offline

    Einsiedler98

    raGan. I know the basics about Java and even object-oriented programming. And I just solved the problem, just had an little fallacy in which parameters my constructor gets.
    Thanks raGan. an DxDy for your great help :)
     
Thread Status:
Not open for further replies.

Share This Page