Solved Get location of a hopper

Discussion in 'Plugin Development' started by waremanu, May 29, 2013.

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

    waremanu

    How do I get the location of a hopper? In this case:
    Code:
        @EventHandler
        public void onInventoryMoveItem(InventoryMoveItemEvent e) {
            if (e.getInitiator().getHolder() instanceof Hopper){
                if (e.getItem().getType() == Material.DIRT){
                    e.setCancelled(true);
                    // TODO Location loc = *Location of the hopper*;
                }
            }
        }
     
  2. Offline

    Minecrell

    waremanu I'm not sure but I think it works like this:
    Code:java
    1. @EventHandler
    2. public void onInventoryMoveItem(InventoryMoveItemEvent e) {
    3. if (e.getInitiator().getHolder() instanceof Hopper) {
    4. if (e.getItem().getType() == Material.DIRT) {
    5. Hopper hopper = (Hopper) e.getInitiator().getHolder();
    6. Location hopperLocation = hopper.getLocation(); // The location of the hopper
    7. e.setCancelled(true);
    8. }
    9. }
    10. }
     
    waremanu likes this.
  3. Offline

    waremanu

    Thanks! I think this works.
    But I tried this to deg source block:
    Code:
                    Block source = (Block) e.getSource().getHolder();
                    Location sourceLocation = source.getLocation();
    But it is not working
     
  4. Offline

    Minecrell

    waremanu Hmm, didn't test it but you could try something like (I think you mean that the source hasn't to be a hopper so you can't use my example code from above):
    Code:java
    1. @EventHandler
    2. public void onInventoryMoveItem(InventoryMoveItemEvent event) {
    3. if (event.getSource().getHolder() instanceof BlockState) {
    4. BlockState blockState = (BlockState) event.getSource().getHolder();
    5.  
    6. // Know you can get the block or the location
    7. Location loc = blockState.getLocation();
    8. Block block = blockState.getBlock();
    9. }
    10. }

    Not sure if it works, only was an idea ;)
     
  5. Offline

    waremanu

    That don't works :/
     
  6. Offline

    Minecrell

  7. Offline

    waremanu

    Yeah, I get a error at
    BlockState blockState = (BlockState) event.getSource().getHolder();
     
  8. Offline

    Minecrell

  9. Offline

    waremanu

    Minecrell
    Code:
    2013-05-30 02:13:52 [SEVERE] java.lang.ClassCastException: org.bukkit.block.DoubleChest cannot be cast to org.bukkit.block.BlockState
    2013-05-30 02:13:52 [SEVERE]    at no.spillere.plugin.listener.MainListener.onInventoryMoveItem(MainListener.java:63)
    2013-05-30 02:13:52 [SEVERE]    at sun.reflect.GeneratedMethodAccessor77.invoke(Unknown Source)
    2013-05-30 02:13:52 [SEVERE]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    2013-05-30 02:13:52 [SEVERE]    at java.lang.reflect.Method.invoke(Unknown Source)
    2013-05-30 02:13:52 [SEVERE]    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    2013-05-30 02:13:52 [SEVERE]    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    2013-05-30 02:13:52 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    2013-05-30 02:13:52 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.TileEntityHopper.tryTakeInItemFromSlot(TileEntityHopper.java:296)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.TileEntityHopper.suckInItems(TileEntityHopper.java:262)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.TileEntityHopper.j(TileEntityHopper.java:181)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.TileEntityHopper.h(TileEntityHopper.java:173)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.World.tickEntities(World.java:1264)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.WorldServer.tickEntities(WorldServer.java:480)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:563)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
    2013-05-30 02:13:52 [SEVERE]    at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    
    Maybe because BlockState cannot be a DoubleChest.. Do you know how i fix this error?
     
  10. Offline

    Minecrell

    waremanu Well basicly it should work (but maybe doing nothing) because with if (event.getSource().getHolder() instanceof BlockState) { I check if the source holder can be cast to a BlockState... Hmm can you show me your event handler again? (especially the lines before 63)
     
  11. waremanu
    What's wrong with this code:
    ?
    If you get errors post them directly.
     
  12. Offline

    Minecrell

    We aren't speaking about hoppers anymore :p
     
  13. Offline

    waremanu

    Minecrell Line 63:
    Code:
    BlockState blockState = (BlockState) event.getSource().getHolder();
    But i removed this check 'cause nothing works if I don't remove this:
    Code:
    if (event.getSource().getHolder() instanceof BlockState) {
     
  14. Offline

    Minecrell

    waremanu Hmm, DoubleChest isn't a block state... I think you have to add it to the if clause...
    Code:java
    1. @EventHandler
    2. public void onInventoryMoveItem(InventoryMoveItemEvent event) {
    3. InventoryHolder sourceHolder = event.getSource().getHolder();
    4.  
    5. Location loc = null;
    6.  
    7. if (sourceHolder instanceof BlockState) {
    8. BlockState blockState = (BlockState) sourceHolder;
    9.  
    10. loc = blockState.getLocation();
    11. } else if (sourceHolder instanceof DoubleChest) {
    12. DoubleChest chest = (DoubleChest) sourceHolder;
    13. loc = chest.getLocation();
    14. }
    15.  
    16. if (loc != null) {
    17. Block block = loc.getBlock();
    18.  
    19. // Do whatever you want with the location
    20. } else {
    21. // Not a BlockState or a DoubleChest... Maybe I have to add some more classes here but I don't know
    22. }
    23. }
     
    waremanu likes this.
Thread Status:
Not open for further replies.

Share This Page