Piston stuff ain't fully working :(

Discussion in 'Plugin Development' started by CreeperShift, Mar 25, 2013.

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

    CreeperShift

    Hey! First off, what I'm trying to do:
    I have a old map which has lots of glass buildings which naturally spawned. Now the map is quite large and I can't protect it all with worldedit or w/e, so I was trying this:

    It basically registers every glass block placed by players in a MySQL database and removes it when they break it. If the glass block is not in the database then they can't break it. Now the issue I'm having is stopping pistons from breaking it.

    Currently half works, if the glass was placed by a player it's moved, the place where the glass block was before is no longer in the database (I checked), which means it also removes it. However except for the very last block thats pushed, all of the blocks cannot be broken.

    It also doesn't care whether or not the blocks are actually protected ones EXCEPT if it's the first one being pushed.

    Need some help plox :3

    Code:
    package net.creepcraft.iPencil;
     
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.List;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPistonExtendEvent;
    import org.bukkit.event.block.BlockPistonRetractEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
     
    public class BlockEvent implements Listener {
     
        public Glassprotect plugin;
     
        public BlockEvent (Glassprotect Instance)
        {
            this.plugin = Instance;
         
        }
     
        @EventHandler
        public void onBlockPistonExtend(BlockPistonExtendEvent event)
        {
         
            List<Block> pushedblocks = event.getBlocks();
            int lenght = event.getLength();
            int f = 0;
         
            plugin.getServer().broadcastMessage(lenght + "blocks are being pushed!");
         
            while (f <= lenght){
             
                if (pushedblocks.get(f).getType() == Material.GLASS){
             
                try
                {
                  int X = pushedblocks.get(f).getX();
                  int Y = pushedblocks.get(f).getY();
                  int Z = pushedblocks.get(f).getZ();
                  String World = event.getBlock().getWorld().getName();
     
                  PreparedStatement Statement = this.plugin.getDatabaseClient().ExecuteQuery("SELECT * FROM protected_blocks WHERE x = ? AND y = ? AND z = ? AND world = ?;");
     
                  Statement.setInt(1, X);
                  Statement.setInt(2, Y);
                  Statement.setInt(3, Z);
                  Statement.setString(4, World);
     
                  ResultSet Row = Statement.executeQuery();
     
               
                  if (!Row.next()){
                   
                      event.setCancelled(true);
                   
                      f = 14;
               
                  }else{
             
                     
                      Statement = this.plugin.getDatabaseClient().ExecuteQuery("DELETE FROM protected_blocks WHERE x = ? AND y = ? AND z = ? AND world = ?;");
     
                      Statement.setInt(1, X);
                      Statement.setInt(2, Y);
                      Statement.setInt(3, Z);
                      Statement.setString(4, World);
     
                      Statement.executeUpdate();
                   
                    int newX = event.getDirection().getModX();
                    int newY = event.getDirection().getModY();
                    int newZ = event.getDirection().getModZ();
                 
                    plugin.getServer().broadcastMessage(X + "increased X by" + newX + " ,"+ Y + "increased Y by" + newY + " ,"+ Z + "increased Z by" + newZ);
                 
                    PreparedStatement bStatement = this.plugin.getDatabaseClient().ExecuteQuery("INSERT INTO protected_blocks (x, y, z, world) VALUES (?, ?, ?, ?)");
     
     
                      bStatement.setInt(1, X+newX);
                      bStatement.setInt(2, Y+newY);
                      bStatement.setInt(3, Z+newZ);
                      bStatement.setString(4, World);
                      bStatement.execute();
                 
                    f++;
                                }
                }
               
                  catch (Exception e)
                  {
                    e.printStackTrace();     
            }
               
                }else{
                    f++;
                 
                }
                }
         
         
        }
     
     
        @EventHandler
        public void onBlockPistonRetract(BlockPistonRetractEvent event)
        {
         
         
         
        }
     
     
     
     
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event){
         
            if(event.getBlock().getType().equals(Material.GLASS)){
             
                try {
                    int X = event.getBlock().getX();
                    int Y = event.getBlock().getY();
                    int Z = event.getBlock().getZ();
                    String World = event.getBlock().getWorld().getName();
     
                          PreparedStatement bStatement = this.plugin.getDatabaseClient().ExecuteQuery("INSERT INTO protected_blocks (x, y, z, world) VALUES (?, ?, ?, ?)");
     
     
                          bStatement.setInt(1, X);
                          bStatement.setInt(2, Y);
                          bStatement.setInt(3, Z);
                          bStatement.setString(4, World);
                          bStatement.execute();
     
                        }
     
               
                  catch (Exception e)
                  {
                    e.printStackTrace();
               
             
             
             
     
             
             
            }
            }
         
            return;
     
        }
     
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event){
         
            if(event.getBlock().getType().equals(Material.GLASS)){
             
             
                try
                {
                  int X = event.getBlock().getX();
                  int Y = event.getBlock().getY();
                  int Z = event.getBlock().getZ();
                  String World = event.getBlock().getWorld().getName();
     
                  PreparedStatement Statement = this.plugin.getDatabaseClient().ExecuteQuery("SELECT * FROM protected_blocks WHERE x = ? AND y = ? AND z = ? AND world = ?;");
     
                  Statement.setInt(1, X);
                  Statement.setInt(2, Y);
                  Statement.setInt(3, Z);
                  Statement.setString(4, World);
     
                  ResultSet Row = Statement.executeQuery();
     
               
                  if (Row.next()){
               
     
     
                      Statement = this.plugin.getDatabaseClient().ExecuteQuery("DELETE FROM protected_blocks WHERE x = ? AND y = ? AND z = ? AND world = ?;");
     
                      Statement.setInt(1, X);
                      Statement.setInt(2, Y);
                      Statement.setInt(3, Z);
                      Statement.setString(4, World);
     
                      Statement.executeUpdate();
               
                   
     
                  }else{
                   
                   
                      if (!(event.getPlayer().getItemInHand().getTypeId() == 97)){
                      event.setCancelled(true);
                      event.getPlayer().sendMessage(ChatColor.RED + "You cannot break sphere glass!");
                   
                  }else{
                   
                      return;
                  }
               
                  }
               
                }
                catch (Exception e)
                {
                  e.printStackTrace();
               
                }
             
             
             
            }
         
            return;
         
        }
     
    }
    
    Illustration of the error:
    [​IMG]
     
  2. Offline

    CreeperShift

    Pretty please? :3

    Is the code thing scaring everyone away? :(

    Late night bumpz

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  3. Offline

    CreeperShift

  4. Offline

    TheUpdater

    like to bump?

    well i dont know how to fix code but if glass was there befor
    u can store it as player place block Event

    just go in to world edit config and change size of claim

    EDIT:you cant store it as player place block Event
     
  5. Offline

    CreeperShift

    Wtf man that doesn't make ANY sense :p

    player place block event what? The player placing/breaking part works flawless.

    and go into world edit and change size of claim? I don't even have worldedit, nor claims oO I don't want to protect an area, I just want to protect the glass, not anything else.
     
  6. Offline

    TheUpdater

    wow that can be hard lol only glass then you need to walk around and claim all glass =)D
     
  7. Offline

    Codisimus

    Did you even read the OP?
     
    CreeperShift likes this.
  8. Offline

    CreeperShift

    Faith in humanity lost :D

    This is the plugin development forum. If I would be looking for worldguard/edit help, I would post there. The glass is already COMPLETELY IMMUNE to players. They CANNOT break it!

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

    Codisimus

    The reason I haven't posted anything helpful is bc I don't fully understand the issue. I know wut you are trying to do but I don't see how pistons break it. A video example would proly help.
     
  10. Offline

    CreeperShift

    Instead of me making a video, would you prefer to hop onto my test server? Much quicker that way.

    Codisimus

    Here, take my extremely realistic drawing LOL! xD

    [​IMG]

    Does that make it any clearer? It basically pushes all blocks, but afterwards it only puts the last block back into the MySQL database :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  11. Offline

    Codisimus

  12. Offline

    CreeperShift

    Codisimus Ah dang, can you see the picture at least? Does it explain?
     
  13. Offline

    Codisimus

    CreeperShift Yes, it was perfect. Go through the list backwards and I bet it will work for you.
     
  14. Offline

    CreeperShift

    so f--? and just loop through it backwards? I'll give it a shot.
     
Thread Status:
Not open for further replies.

Share This Page