Repository navigation
Routine: Switch
Collects large batches of if routines into one simple switch routine.
== Syntax
- 'switch[all] {first part of conditional}':
- '{second part}':
- 'your routines here'
- '{different second part}':
- 'more routines here'
etc.== Explanation The switch routine can be used to replace many nearly-identical if routines in a row with just one switch. The benefits of doing this are a little bit less repetition, and a small speed gain. To show why this happens, let's say you have the following switch:
- 'switch attacker.wielding':
- 'WOOD_SWORD': '+3'
- 'STONE_SWORD': '+5'
- 'IRON_SWORD': '+7'
- 'DIAMOND_SWORD': '+10'
- 'GOLD_SWORD': '+10'
- 'WOOD_AXE': '+2'
- 'STONE_AXE': '+4'
- 'IRON_AXE': '+8'
- 'DIAMOND_AXE': '+11'
- 'GOLD_AXE': '+7'
- 'BOW': '+10'
- 'AIR': '+1'
- 'SAND': '+1'
- 'CACTUS': '+1'
- 'GRAVEL': '+1'
- 'WOOD': '+1'
- 'SANDSTONE': '+1'
- 'WHEAT': '+1'
- 'APPLE': '+1'
- 'RAW_FISH': '+1'
- 'STRING': '+1'
- 'COAL': '+1'
- 'STICK': '+1'
- 'WOOD_SWORD': '+1'
- 'STONE_SWORD': '+2'
- 'IRON_SWORD': '+3'
- 'DIAMOND_SWORD': '+6'
- 'GOLD_SWORD': '+15'
- 'WOOD_AXE': '+3'
- 'STONE_AXE': '+4'
- 'IRON_AXE': '+6'
- 'DIAMOND_AXE': '+11'
- 'GOLD_AXE': '+28'This has a whopping 34 different items it checks for. If this were a bunch of if statements, MD would have to go through, one by one and check each one. If you were wielding a wooden sword for instance, it would match the first one, then still have to go through all 33 remaining routines essentially wasting its time. A switch statement, however, stops checking after the first one it found. This isn't a huge improvement, but it might be noticeable and worth it if your config is 1.1 MB large and full of these situations.
== Examples Say you have something like this in your Damage event:
- 'if attacker.wielding.WOOD_SWORD': '+3'
- 'if attacker.wielding.STONE_SWORD': '+5'
- 'if attacker.wielding.IRON_SWORD': '+7'
- 'if attacker.wielding.DIAMOND_SWORD': '+10'
- 'if attacker.wielding.GOLD_SWORD': '+10'
- 'if attacker.wielding.WOOD_AXE': '+2'
- 'if attacker.wielding.STONE_AXE': '+4'
- 'if attacker.wielding.IRON_AXE': '+8'
- 'if attacker.wielding.DIAMOND_AXE': '+11'
- 'if attacker.wielding.GOLD_AXE': '+7'
- 'if attacker.wielding.BOW': '+10'
- 'if attacker.wielding.AIR': '+1'
- 'if attacker.wielding.SAND': '+1'
- 'if attacker.wielding.CACTUS': '+1'
- 'if attacker.wielding.GRAVEL': '+1'
- 'if attacker.wielding.WOOD': '+1'
- 'if attacker.wielding.SANDSTONE': '+1'
- 'if attacker.wielding.WHEAT': '+1'
- 'if attacker.wielding.APPLE': '+1'
- 'if attacker.wielding.RAW_FISH': '+1'
- 'if attacker.wielding.STRING': '+1'
- 'if attacker.wielding.COAL': '+1'
- 'if attacker.wielding.STICK': '+1'
- 'if attacker.wielding.WOOD_SWORD': '+1'
- 'if attacker.wielding.STONE_SWORD': '+2'
- 'if attacker.wielding.IRON_SWORD': '+3'
- 'if attacker.wielding.DIAMOND_SWORD': '+6'
- 'if attacker.wielding.GOLD_SWORD': '+15'
- 'if attacker.wielding.WOOD_AXE': '+3'
- 'if attacker.wielding.STONE_AXE': '+4'
- 'if attacker.wielding.IRON_AXE': '+6'
- 'if attacker.wielding.DIAMOND_AXE': '+11'
- 'if attacker.wielding.GOLD_AXE': '+28'
- 'if attacker.wielding.BOW':
- 'tag.attacker.bowweakness': '8'
- 'if attacker_tagvalue_weaknesstimer == 0':
- '_bowweakness'
- 'if attacker.hasanyitem.EYE_OF_ENDER*1':
- 'if !target.type.Player':
- 'set.target_fireticks':
- 'set.250'
- '+2'
- 'if attacker.wielding.BOW': '+2'
- 'if target.type.Zombie':
- 'if attacker.wielding.AIR': '+1'
- 'if attacker.wielding.SAND': '+1'
- 'if attacker.wielding.CACTUS': '+1'
- 'if attacker.wielding.GRAVEL': '+1'
- 'if attacker.wielding.WOOD': '+1'
- 'if attacker.wielding.SANDSTONE': '+1'
- 'if attacker.wielding.WHEAT': '+1'
- 'if attacker.wielding.APPLE': '+1'
- 'if attacker.wielding.RAW_FISH': '+1'
- 'if attacker.wielding.STRING': '+1'
- 'if attacker.wielding.COAL': '+1'
- 'if attacker.wielding.STICK': '+1'
- 'if target.type.Spider':
- 'if attacker.wielding.AIR': '+1'
- 'if attacker.wielding.SAND': '+1'
- 'if attacker.wielding.CACTUS': '+1'
- 'if attacker.wielding.GRAVEL': '+1'
- 'if attacker.wielding.WOOD': '+1'
- 'if attacker.wielding.SANDSTONE': '+1'
- 'if attacker.wielding.WHEAT': '+1'
- 'if attacker.wielding.APPLE': '+1'
- 'if attacker.wielding.RAW_FISH': '+1'
- 'if attacker.wielding.STRING': '+1'
- 'if attacker.wielding.COAL': '+1'
- 'if attacker.wielding.STICK': '+1'
- 'if target.type.Creeper':
- 'if attacker.wielding.AIR': '+1'
- 'if attacker.wielding.SAND': '+1'
- 'if attacker.wielding.CACTUS': '+1'
- 'if attacker.wielding.GRAVEL': '+1'
- 'if attacker.wielding.WOOD': '+1'
- 'if attacker.wielding.SANDSTONE': '+1'
- 'if attacker.wielding.WHEAT': '+1'
- 'if attacker.wielding.APPLE': '+1'
- 'if attacker.wielding.RAW_FISH': '+1'
- 'if attacker.wielding.STRING': '+1'
- 'if attacker.wielding.COAL': '+1'
- 'if attacker.wielding.STICK': '+1'Since no entity can ever be wielding two things at once, only one of the attacker.wielding groups will be fired at once. This will be much better as a few switch routines:
- 'switch attacker.wielding':
- 'WOOD_SWORD': '+3'
- 'STONE_SWORD': '+5'
- 'IRON_SWORD': '+7'
- 'DIAMOND_SWORD': '+10'
- 'GOLD_SWORD': '+10'
- 'WOOD_AXE': '+2'
- 'STONE_AXE': '+4'
- 'IRON_AXE': '+8'
- 'DIAMOND_AXE': '+11'
- 'GOLD_AXE': '+7'
- 'BOW': '+10'
- 'AIR': '+1'
- 'SAND': '+1'
- 'CACTUS': '+1'
- 'GRAVEL': '+1'
- 'WOOD': '+1'
- 'SANDSTONE': '+1'
- 'WHEAT': '+1'
- 'APPLE': '+1'
- 'RAW_FISH': '+1'
- 'STRING': '+1'
- 'COAL': '+1'
- 'STICK': '+1'
- 'WOOD_SWORD': '+1'
- 'STONE_SWORD': '+2'
- 'IRON_SWORD': '+3'
- 'DIAMOND_SWORD': '+6'
- 'GOLD_SWORD': '+15'
- 'WOOD_AXE': '+3'
- 'STONE_AXE': '+4'
- 'IRON_AXE': '+6'
- 'DIAMOND_AXE': '+11'
- 'GOLD_AXE': '+28'
- 'BOW':
- 'tag.attacker.bowweakness': '8'
- 'if attacker_tagvalue_weaknesstimer == 0':
- '_bowweakness'
- 'if attacker.hasanyitem.EYE_OF_ENDER*1':
- 'if !target.type.Player':
- 'set.target_fireticks':
- 'set.250'
- '+2'
- 'if attacker.wielding.BOW': '+2'
- 'if target.type.Zombie':
- 'switch attacker.wielding':
- 'AIR': '+1'
- 'SAND': '+1'
- 'CACTUS': '+1'
- 'GRAVEL': '+1'
- 'WOOD': '+1'
- 'SANDSTONE': '+1'
- 'WHEAT': '+1'
- 'APPLE': '+1'
- 'RAW_FISH': '+1'
- 'STRING': '+1'
- 'COAL': '+1'
- 'STICK': '+1'
- 'if target.type.Spider':
- 'switch attacker.wielding':
- 'AIR': '+1'
- 'SAND': '+1'
- 'CACTUS': '+1'
- 'GRAVEL': '+1'
- 'WOOD': '+1'
- 'SANDSTONE': '+1'
- 'WHEAT': '+1'
- 'APPLE': '+1'
- 'RAW_FISH': '+1'
- 'STRING': '+1'
- 'COAL': '+1'
- 'STICK': '+1'
- 'if target.type.Creeper':
- 'switch attacker.wielding':
- 'AIR': '+1'
- 'SAND': '+1'
- 'CACTUS': '+1'
- 'GRAVEL': '+1'
- 'WOOD': '+1'
- 'SANDSTONE': '+1'
- 'WHEAT': '+1'
- 'APPLE': '+1'
- 'RAW_FISH': '+1'
- 'STRING': '+1'
- 'COAL': '+1'
- 'STICK': '+1'