1.15.2 Changing rail direction not working correctly

Discussion in 'Spigot Plugin Development' started by NocturnalDev, May 6, 2020.

  1. Hello,

    Currently, it changes the rail direction in 3 directions as opposed to all of them even though the logic checks out. The code posted below. Ex. It does not ever point straight it is always curved.


    Code (Java):
    @EventHandler
        public void onMinecartPuzzleShift(PlayerToggleSneakEvent event) {


            Player player = event.getPlayer();

            if (remainingPlayers.contains(player)) {

                if (player.isSneaking()) {


                    HashSet<Material> materialsToIgnore = new HashSet<>();
                    materialsToIgnore.add(Material.BARRIER);
                    materialsToIgnore.add(Material.AIR);

                    Block block = player.getTargetBlock(materialsToIgnore, 50);

                    //Bukkit.broadcastMessage(block.getType().toString());

                    if (railroadLocations != null && !railroadLocations.isEmpty()) {


                        if (railroadLocations.contains(block.getLocation())) {


                            if (block.getType() == Material.RAIL) {


                                BlockState blockState = block.getState();

                                Rail data = (Rail) blockState.getBlockData();

                                switch (data.getShape()) {
                                    case NORTH_EAST:
                                        data.setShape(Rail.Shape.NORTH_SOUTH);
                                        break;
                                    case NORTH_WEST:
                                        data.setShape(Rail.Shape.EAST_WEST);
                                        break;
                                    case SOUTH_EAST:
                                        data.setShape(Rail.Shape.SOUTH_WEST);
                                        break;
                                    case SOUTH_WEST:
                                        data.setShape(Rail.Shape.NORTH_WEST);
                                        break;
                                    case NORTH_SOUTH:
                                        data.setShape(Rail.Shape.SOUTH_EAST);
                                        break;
                                    case EAST_WEST:
                                        data.setShape(Rail.Shape.NORTH_EAST);
                                        break;
                                }

                                block.getWorld().playSound(block.getLocation(), Sound.BLOCK_PISTON_EXTEND, 0.25F, 0F);
                                block.setBlockData(data);
                                Bukkit.broadcastMessage(data.getShape().toString());

                            }

                        }
                    }


                }
    NOTE: For some reason, it prints out two of the bukkit broadcast when I crouch over the rail - which it shouldn't, it should just print one. Why is this?
     
  2. Why shuld only be one sent
    Try using the update() method on the block data