Solved Problem with shooting block

Discussion in 'Plugin Development' started by envic, Sep 15, 2016.

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

    envic

    Hi guys.
    I want to let players, shoot, in hand TNT into air by using Gun powder in their Offhand!
    & I think i made a mistake here:
    And there is no crash log!

    Code:
    package ir.envica.bombs.bombs.tnt;
    
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.TNTPrimed;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.util.Vector;
    
    public class TNTListener implements Listener {
    
        private Location PlayerLocation;
        private TNTPrimed TNT;
        private Vector velocity;
        private float Yaw;
        private int IntY;
    
        // Get TNT Yaw
        public float getYaw() {
            return Yaw;
        }
    
        // GetVelocity
        public Vector getVelocity() {
            return velocity;
        }
    
        // Get TNT Yaw as integer
        public int getIntY() {
            return IntY;
        }
    
        public TNTPrimed getTNT() {
            return TNT;
        }
    
        public Location getPEyeLocation() {
            return PlayerLocation;
        }
    
        // register velocity
        public void regTNTVelocity(Vector vector) {
            velocity = vector;
        }
    
        public void regPlayerEyeLoca(Player P, Location Loc) {
            // Loc = P.getLocation().add(0.0, 1.0, 0.0);
            Loc = P.getEyeLocation();
            PlayerLocation = Loc;
        }
    
        public void regTNTPrimed(TNTPrimed tntprimed) {
            TNT = tntprimed;
        }
    
        public void registerYaw(Location loc) {
            Yaw = loc.getYaw();
        }
    
        public int regYawInt(float f) {
            // int result = floatToInteger(f);
            int RoundedResult = Math.round(f);
            return RoundedResult;
        }
    
        public String StringToInteger(String string) {
            StringBuilder sb = new StringBuilder();
            sb.append("");
            sb.append(string);
            String strI = sb.toString();
    
            // int result = Integer.parseInt(strI);
    
            return strI;
        }
    
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent event) {
            if (event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK))
                return;
            if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
                return;
    
            Player P = event.getPlayer();
    
            if (!P.getInventory().getItemInMainHand().equals(Material.TNT))
                return;
            if (P.getInventory().getItemInMainHand().getAmount() == 0)
                return;
            if (P.getInventory().getItemInOffHand().equals(Material.TNT)) {
                P.sendMessage(ChatColor.RED + "Please hold TNT in your main hand!");
                return;
            }
    
            // Player Location
            Location PLoc = P.getLocation();
    
            // setup player location
            regPlayerEyeLoca(P, PLoc);
    
            // spawn a block where player placed TNT
            regTNTPrimed(getPEyeLocation().getWorld().spawn(getPEyeLocation(), TNTPrimed.class));
    
            int TNTs = P.getInventory().getItemInMainHand().getAmount();
            P.getInventory().getItemInMainHand().setAmount(TNTs - 1);
    
            // Register Yaw As Float and Integer
            TNTFallingActionRegistration(PLoc);
    
            // Giving velocity to TNT
            TNTFallingActions(P);
        }
    
        public void TNTFallingActionRegistration(Location Loc) {
            registerYaw(Loc);
            regYawInt(getYaw());
        }
    
        public void TNTFallingActions(Player player) {
            if (!player.getInventory().getItemInOffHand().equals(Material.SULPHUR))
                return;
            int GunPowderSize = player.getInventory().getItemInOffHand().getAmount();
    
            if (GunPowderSize == 0)
                return;
    
            int HandledSize = multiplierHandler(GunPowderSize);
    
            char Vector = 'A';
            switch (Vector) {
            case 'A':
                getTNT().setVelocity(getPEyeLocation().getDirection().multiply(HandledSize));
                player.getInventory().getItemInOffHand().setAmount(GunPowderSize - HandledSize);
                player.sendMessage(ChatColor.GREEN + "You've been shoot a TNT");
            }
        }
    
        public int multiplierHandler(int i) {
            if (i > 6) {
                return 5;
            } else {
                return i;
            }
        }
    }
    
     
  2. Offline

    Tecno_Wizard

    @envic, if you don't tell us what's wrong with it, we cant tell you how to fix it.
     
  3. Offline

    envic

    @Tecno_Wizard
    It's not working! at all! and i don't know what's wrong and where is wrong!
    so i'll put some break point! and share the result!
    but if you got some thing tell me!
     
  4. Offline

    ipodtouch0218

    Doesn't work as in what? I can only think of 3 things.

    1) Event doesn't fire. Test with either debug lines or if you get the message to put the tnt in your main hand. If so, check if you registered the listeners in your onEnable() of your main class.

    2) TNT doesn't spawn in the first place. (Even though it should)

    3) TNT spawns but doesn't gain the velocity. I've never worked with vectors and velocities and the sort with plugins yet, so I can't offer any help with this. Maybe another user.

    Just some suggestions:
    StringToInt method can be replaced with Integer#parseInt(String)
    Check if the event Action is RIGHT_CLICK_AIR (maybe RIGHT_CLICK_BLOCK too), it's just easier than checking if it's not any of the others.
    Why are you using a switch to set the velocity?
     
  5. Offline

    htmlman1

    If there's no errors it's perfectly possible that the event isn't getting called at all. Can you try replacing the spawn line with a System.out.println or something similar to show that the line is getting called?
     
  6. Offline

    envic

    @ipodtouch0218
    I totally comfortable with String builder for covering StringToInteger And for IntegerToString i'm using Integer.parseInt(strI).
    @htmlman1
    already called but i made an stupid mistake







    After putting 15 Break i found it here:
    Code:
    P.getInventory().getItemInOffHand().equals(Material.TNT)
    instead of
    P.getInventory().getItemInOffHand().getType().equals(Material.TNT)
     
Thread Status:
Not open for further replies.

Share This Page