Skip to content

Commit 5ac877c

Browse files
committed
Avoid a few Block#getType calls in Pipes
1 parent ca07afd commit 5ac877c

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

src/main/java/com/sk89q/craftbook/mechanics/pipe/Pipes.java

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@ public void onSignChange(SignChangeEvent event) {
102102
}
103103

104104
private static boolean isPiston(Block block) {
105-
return block.getType() == Material.PISTON || block.getType() == Material.STICKY_PISTON;
105+
Material type = block.getType();
106+
return type == Material.PISTON || type == Material.STICKY_PISTON;
106107
}
107108

108109
private static ChangedSign getSignOnPiston(Block block) {
109110
BlockData blockData = block.getBlockData();
110111
BlockFace facing = BlockFace.SELF;
111-
if(blockData instanceof Directional) {
112-
facing = ((Directional) blockData).getFacing();
112+
if(blockData instanceof Directional directional) {
113+
facing = directional.getFacing();
113114
}
114115

115116
for(BlockFace face : LocationUtil.getDirectFaces()) {
@@ -136,7 +137,8 @@ private void searchNearbyPipes(Block block, Set<Vector> visitedPipes, List<ItemS
136137
//Use the queue to search blocks.
137138
while (!searchQueue.isEmpty()) {
138139
Block bl = searchQueue.poll();
139-
if (bl.getType() == Material.PISTON) {
140+
Material blType = bl.getType();
141+
if (blType == Material.PISTON) {
140142
Piston p = (Piston) bl.getBlockData();
141143

142144
ChangedSign sign = getSignOnPiston(bl);
@@ -199,7 +201,7 @@ private void searchNearbyPipes(Block block, Set<Vector> visitedPipes, List<ItemS
199201
items.removeAll(filteredItems);
200202
items.addAll(newItems);
201203
}
202-
} else if (bl.getType() == Material.DROPPER) {
204+
} else if (blType == Material.DROPPER) {
203205
ChangedSign sign = getSignOnPiston(bl);
204206

205207
HashSet<ItemStack> pFilters = new HashSet<>();
@@ -276,31 +278,33 @@ private void searchNearbyPipes(Block block, Set<Vector> visitedPipes, List<ItemS
276278
}
277279

278280
Block off = bl.getRelative(x, y, z);
281+
Material offType = off.getType();
279282

280-
if (!isValidPipeBlock(off)) continue;
283+
if (!isValidPipeBlock(offType)) continue;
281284

282285
if (visitedPipes.contains(off.getLocation().toVector())) continue;
283286
visitedPipes.add(off.getLocation().toVector());
284287

285-
if(ItemUtil.isStainedGlass(bl.getType()) && ItemUtil.isStainedGlass(off.getType()) && bl.getType() != off.getType()) continue;
288+
if(ItemUtil.isStainedGlass(blType) && ItemUtil.isStainedGlass(offType) && blType != offType) continue;
286289

287-
if(off.getType() == Material.GLASS || ItemUtil.isStainedGlass(off.getType())) {
290+
if(offType == Material.GLASS || ItemUtil.isStainedGlass(offType)) {
288291
searchQueue.add(off);
289-
} else if (off.getType() == Material.GLASS_PANE || ItemUtil.isStainedGlassPane(off.getType())) {
292+
} else if (offType == Material.GLASS_PANE || ItemUtil.isStainedGlassPane(offType)) {
290293
Block offsetBlock = off.getRelative(x, y, z);
291-
if (!isValidPipeBlock(offsetBlock)) continue;
294+
Material offsetBlockType = offsetBlock.getType();
295+
if (!isValidPipeBlock(offsetBlockType)) continue;
292296
if (visitedPipes.contains(offsetBlock.getLocation().toVector())) continue;
293-
if(ItemUtil.isStainedGlassPane(off.getType())) {
294-
if((ItemUtil.isStainedGlass(bl.getType())
295-
|| ItemUtil.isStainedGlassPane(bl.getType())) && ItemUtil.getStainedColor(off.getType()) != ItemUtil
296-
.getStainedColor(offsetBlock.getType())
297-
|| (ItemUtil.isStainedGlass(offsetBlock.getType())
298-
|| ItemUtil.isStainedGlassPane(offsetBlock.getType())) && ItemUtil.getStainedColor(off.getType()) != ItemUtil
299-
.getStainedColor(offsetBlock.getType())) continue;
297+
if(ItemUtil.isStainedGlassPane(offType)) {
298+
if((ItemUtil.isStainedGlass(blType)
299+
|| ItemUtil.isStainedGlassPane(blType)) && ItemUtil.getStainedColor(offType) != ItemUtil
300+
.getStainedColor(offsetBlockType)
301+
|| (ItemUtil.isStainedGlass(offsetBlockType)
302+
|| ItemUtil.isStainedGlassPane(offsetBlockType)) && ItemUtil.getStainedColor(offType) != ItemUtil
303+
.getStainedColor(offsetBlockType)) continue;
300304
}
301305
visitedPipes.add(offsetBlock.getLocation().toVector());
302306
searchQueue.add(off.getRelative(x, y, z));
303-
} else if(off.getType() == Material.PISTON)
307+
} else if(offType == Material.PISTON)
304308
searchQueue.addFirst(off); //Pistons are treated with higher priority.
305309
}
306310
}
@@ -309,19 +313,13 @@ private void searchNearbyPipes(Block block, Set<Vector> visitedPipes, List<ItemS
309313
}
310314
}
311315

312-
private static boolean isValidPipeBlock(Block block) {
313-
switch (block.getType()) {
314-
case GLASS:
315-
case PISTON:
316-
case STICKY_PISTON:
317-
case DROPPER:
318-
case GLASS_PANE:
319-
return true;
320-
default:
321-
return ItemUtil.isStainedGlass(block.getType())
322-
|| ItemUtil.isStainedGlassPane(block.getType())
323-
|| SignUtil.isWallSign(block);
324-
}
316+
private static boolean isValidPipeBlock(Material type) {
317+
return switch (type) {
318+
case GLASS, PISTON, STICKY_PISTON, DROPPER, GLASS_PANE -> true;
319+
default -> ItemUtil.isStainedGlass(type)
320+
|| ItemUtil.isStainedGlassPane(type)
321+
|| SignUtil.isWallSign(type);
322+
};
325323
}
326324

327325
private void startPipe(Block block, List<ItemStack> items, boolean request) {
@@ -351,17 +349,18 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {
351349

352350
Piston p = (Piston) block.getBlockData();
353351
Block fac = block.getRelative(p.getFacing());
354-
355-
if (fac.getType() == Material.CHEST
356-
|| fac.getType() == Material.TRAPPED_CHEST
357-
|| fac.getType() == Material.DROPPER
358-
|| fac.getType() == Material.DISPENSER
359-
|| fac.getType() == Material.HOPPER
360-
|| fac.getType() == Material.BARREL
361-
|| fac.getType() == Material.CHISELED_BOOKSHELF
362-
|| fac.getType() == Material.CRAFTER
363-
|| fac.getType() == Material.DECORATED_POT
364-
|| Tag.SHULKER_BOXES.isTagged(fac.getType())) {
352+
Material facType = fac.getType();
353+
354+
if (facType == Material.CHEST
355+
|| facType == Material.TRAPPED_CHEST
356+
|| facType == Material.DROPPER
357+
|| facType == Material.DISPENSER
358+
|| facType == Material.HOPPER
359+
|| facType == Material.BARREL
360+
|| facType == Material.CHISELED_BOOKSHELF
361+
|| facType == Material.CRAFTER
362+
|| facType == Material.DECORATED_POT
363+
|| Tag.SHULKER_BOXES.isTagged(facType)) {
365364
for (ItemStack stack : ((InventoryHolder) fac.getState()).getInventory().getContents()) {
366365

367366
if (!ItemUtil.isStackValid(stack))
@@ -386,7 +385,7 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {
386385
}
387386

388387
if (!items.isEmpty()) {
389-
if (fac.getType() == Material.CRAFTER)
388+
if (facType == Material.CRAFTER)
390389
leftovers.addAll(InventoryUtil.addItemsToCrafter((Crafter) fac.getState(), items.toArray(new ItemStack[items.size()])));
391390
else {
392391
for (ItemStack item : items) {
@@ -395,7 +394,7 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {
395394
}
396395
}
397396
}
398-
} else if (fac.getType() == Material.FURNACE || fac.getType() == Material.BLAST_FURNACE || fac.getType() == Material.SMOKER) {
397+
} else if (facType == Material.FURNACE || facType == Material.BLAST_FURNACE || facType == Material.SMOKER) {
399398

400399
Furnace f = (Furnace) fac.getState();
401400

@@ -425,7 +424,7 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {
425424
leftovers.add(ItemUtil.addToStack(f.getInventory().getResult(), item));
426425
}
427426
} else f.getInventory().setResult(null);
428-
} else if (fac.getType() == Material.JUKEBOX) {
427+
} else if (facType == Material.JUKEBOX) {
429428

430429
Jukebox juke = (Jukebox) fac.getState();
431430

src/main/java/com/sk89q/craftbook/util/SignUtil.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.sk89q.craftbook.util;
1818

1919
import com.sk89q.craftbook.bukkit.util.CraftBookBukkitUtil;
20+
import org.bukkit.Material;
2021
import org.bukkit.Tag;
2122
import org.bukkit.block.Block;
2223
import org.bukkit.block.BlockFace;
@@ -61,7 +62,11 @@ public static boolean isStandingSign(Block block) {
6162
}
6263

6364
public static boolean isWallSign(Block block) {
64-
return Tag.WALL_SIGNS.isTagged(block.getType());
65+
return isWallSign(block.getType());
66+
}
67+
68+
public static boolean isWallSign(Material type) {
69+
return Tag.WALL_SIGNS.isTagged(type);
6570
}
6671

6772
/**

0 commit comments

Comments
 (0)