Solved Avoiding Repetition - too many if then statements

Discussion in 'Spigot Plugin Development' started by Globdom, Jul 20, 2021.

Thread Status:
Not open for further replies.
  1. Hi all,

    I am struggling to figure out how to best simplify my code. The plugin I have created looks for a copper block and turns it into its oxidized state on click. The issue here is I am specifying each variant of copper (waxed, stairs, slabs, etc.) and it feels like it violates the DRY principle. I have tried researching, but have not found much luck solving this issue. Any tips to improve the efficiency are appreciated. Code is below:

    Code (Text):
    @EventHandler
        public void onClick(PlayerInteractEvent event){



            if(event.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.SPONGE))
                if (event.getPlayer().getInventory().getItemInMainHand().getItemMeta().hasLore()){




                    Player player = (Player) event.getPlayer();
               
                    if (event.getAction() == Action.LEFT_CLICK_BLOCK){

                 
                        if (event.getClickedBlock().getType() == Material.COPPER_BLOCK) {
                            event.getClickedBlock().setType(Material.OXIDIZED_COPPER);

                        }
                        if (event.getClickedBlock().getType() == Material.CUT_COPPER) {
                            event.getClickedBlock().setType(Material.OXIDIZED_CUT_COPPER);

                        }

                        if (event.getClickedBlock().getType() == Material.WAXED_CUT_COPPER) {
                            event.getClickedBlock().setType(Material.WAXED_OXIDIZED_CUT_COPPER);

                        }


                        if (event.getClickedBlock().getType() == Material.CUT_COPPER_SLAB){
                            event.getClickedBlock().setType(Material.OXIDIZED_CUT_COPPER_SLAB);
                        }

                        if (event.getClickedBlock().getType() == Material.CUT_COPPER_STAIRS){
                            event.getClickedBlock().setType(Material.OXIDIZED_CUT_COPPER_STAIRS);
                        }


                        if (event.getClickedBlock().getType() == Material.WAXED_COPPER_BLOCK){
                                event.getClickedBlock().setType(Material.WAXED_OXIDIZED_COPPER);
                            }

                        if (event.getClickedBlock().getType() == Material.WAXED_CUT_COPPER_SLAB) {
                            event.getClickedBlock().setType(Material.WAXED_OXIDIZED_CUT_COPPER_SLAB);
                        }

                        if (event.getClickedBlock().getType() == Material.WAXED_CUT_COPPER_STAIRS){
                            event.getClickedBlock().setType(Material.WAXED_OXIDIZED_CUT_COPPER_STAIRS);
                        }

                    }

                }

        }
     
    #1 Globdom, Jul 20, 2021
    Last edited: Jul 20, 2021
  2. Just have a map like

    Code (Text):
    Map<Material, Material> mats = new HashMap<>();
     
    Add your material to the map using the key as the material you want to check for and the value being the material you want to replace it with, then in the event check if the clicked block type is in the map and if so set the type using the value of that material.
     
    • Like Like x 1
    • Agree Agree x 1
Thread Status:
Not open for further replies.