Skip to content

Commit

Permalink
refactor: use pattern vars and enhanced switch
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed Dec 19, 2023
1 parent 63b6f2a commit f35a326
Show file tree
Hide file tree
Showing 34 changed files with 91 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public static void registerBundledTileInteraction(IBundledTileInteraction intera
return ((IBundledTile) tile).getBundledSignal(side ^ 1);
}

if (tile instanceof TileMultipart) {
TileMultipart tmp = (TileMultipart) tile;
if (tile instanceof TileMultipart tmp) {
byte[] signal = null;

// Source signal from all 4 perpendicular faces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ default void propagateBackward(@Nullable IPropagationPart prev, int mode) {
default boolean propagateTo(@Nullable IConnectable to, @Nullable IPropagationPart prev, int mode) {
if (to != null) {
if (to == prev) return false;
if (to instanceof IPropagationPart) {
IPropagationPart part = (IPropagationPart) to;
if (to instanceof IPropagationPart part) {
if (shouldPropagate(part, mode)) {
RedstonePropagator.propagateTo(part, this, mode);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ default int getPropagationMask() {
default boolean propagateTo(@Nullable IConnectable to, @Nullable IPropagationPart prev, int mode) {
if (to != null) {
if (to == prev) return false;
if (to instanceof IPropagationPart) {
IPropagationPart part = (IPropagationPart) to;
if (to instanceof IPropagationPart part) {
if (shouldPropagate(part, mode)) {
RedstonePropagator.propagateTo(part, this, mode);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ default boolean outsideCornerEdgeOpen(int s, int edgeRot) {

default boolean discoverStraightCenter(int s) {
MultiPart p = getStraightCenter(s);
if (p instanceof IConnectable) {
IConnectable connectable = (IConnectable) p;
if (p instanceof IConnectable connectable) {
return canConnectPart(connectable, s, -1) && connectable.connectStraight(this, s ^ 1, -1);
}

Expand All @@ -189,8 +188,7 @@ default boolean discoverStraightCenter(int s) {

default boolean discoverStraight(int s, int edgeRot) {
MultiPart p = getStraight(s, edgeRot);
if (p instanceof IConnectable) {
IConnectable connectable = (IConnectable) p;
if (p instanceof IConnectable connectable) {
return canConnectPart(connectable, s, edgeRot) && connectable.connectStraight(this, rotFromStraight(s, edgeRot), -1);
}

Expand All @@ -199,8 +197,7 @@ default boolean discoverStraight(int s, int edgeRot) {

default boolean discoverCorner(int s, int edgeRot) {
MultiPart p = getCorner(s, edgeRot);
if (p instanceof IConnectable) {
IConnectable connectable = (IConnectable) p;
if (p instanceof IConnectable connectable) {
return canConnectPart(connectable, s, edgeRot) &&
outsideCornerEdgeOpen(s, edgeRot) &&
connectable.canConnectCorner(rotFromCorner(s, edgeRot)) && //TODO shouldnt this be handled by next line?
Expand All @@ -214,8 +211,7 @@ default boolean discoverCorner(int s, int edgeRot) {
default boolean discoverStraightCenterOverride(int s) {
BlockPos pos = posOfStraight(s);
BlockEntity tile = getBlockLevel().getBlockEntity(pos);
if (tile instanceof IConnectable) {
IConnectable connectable = (IConnectable) tile;
if (tile instanceof IConnectable connectable) {
return canConnectPart(connectable, s, -1) && connectable.connectStraight(this, s, -1);
}

Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/mrtjp/projectred/lib/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public Point offset(int r, int amount) {

@Override
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point p = (Point) obj;
if (obj instanceof Point p) {
return x == p.x && y == p.y;
}
return false;
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/mrtjp/projectred/lib/Size.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public Vec2 toVec2() {

@Override
public boolean equals(Object obj) {
if (obj instanceof Size) {
Size size = (Size) obj;
if (obj instanceof Size size) {
return size.width == width && size.height == height;
}
return false;
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/mrtjp/projectred/lib/Vec2.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public Vec2 axialProject() {

@Override
public boolean equals(Object obj) {
if (obj instanceof Vec2) {
Vec2 other = (Vec2) obj;
if (obj instanceof Vec2 other) {
return dx == other.dx && dy == other.dy;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static void assertEqualsT(@Nullable Vector3 a, @Nullable Vector3 b) {
assertTrue(a.equalsT(b));
}

public List<TriTestCase> createTestCases() {
private List<TriTestCase> createTestCases() {
List<TriTestCase> testCases = new LinkedList<>();

// Simple half-quad on the XZ plane with normal facing up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt
@Override
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
BlockEntity tile = builder.getParameter(LootContextParams.BLOCK_ENTITY);
if (tile instanceof BatteryBoxTile) {
BatteryBoxTile batteryBoxTile = (BatteryBoxTile) tile;
if (tile instanceof BatteryBoxTile batteryBoxTile) {
return Collections.singletonList(batteryBoxTile.createStackWithStoredPower()); // Retain power inside itemstack
}
return super.getDrops(state, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ public InteractionResult onBlockActivated(Player player, InteractionHand hand, B
ItemStack held = player.getItemInHand(hand);

// Try to rotate block
if (held.getItem() instanceof IScrewdriver) {
IScrewdriver screwdriver = (IScrewdriver) held.getItem();
if (held.getItem() instanceof IScrewdriver screwdriver) {
if (screwdriver.canUse(player, held)) {
if (!level.isClientSide) {
side = (side + 1) % 6;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public InteractionResult onBlockActivated(Player player, InteractionHand hand, B
ItemStack held = player.getItemInHand(hand);

// Try to rotate block
if (held.getItem() instanceof IScrewdriver) {
IScrewdriver screwdriver = (IScrewdriver) held.getItem();
if (held.getItem() instanceof IScrewdriver screwdriver) {

if (screwdriver.canUse(player, held)) {
if (!level.isClientSide) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ default int toAbsoluteMask(int intMask) {

//@formatter:off
static int rotationToDir(int r) {
switch (r) {
case 0: return 3;
case 1: return 4;
case 2: return 2;
case 3: return 5;
default: return -1;
}
return switch (r) {
case 0 -> 3;
case 1 -> 4;
case 2 -> 2;
case 3 -> 5;
default -> -1;
};
}

static int dirToRotation(int dir) {
switch (dir) {
case 2: return 2;
case 3: return 0;
case 4: return 1;
case 5: return 3;
default: return -1;
}
return switch (dir) {
case 2 -> 2;
case 3 -> 0;
case 4 -> 1;
case 5 -> 3;
default -> -1;
};
}
//@formatter:on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ public void readDesc(MCDataInput in) {
@Override
public void read(MCDataInput in, int key) {
switch (key) {
case COLOUR_PACKET:
colour = in.readByte();
break;
default:
super.read(in, key);
case COLOUR_PACKET -> colour = in.readByte();
default -> super.read(in, key);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void buildTooltip(List<Component> tooltip) {
};
}

interface ButtonController {
protected interface ButtonController {
void getTooltip(List<Component> tooltip);
void onClick();
boolean isSelected();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void onTabStateChanged(TabState prevState, TabState newState) {

public abstract void buildTooltip(List<Component> tooltip);

enum TabState {
public enum TabState {
ALL_CLOSED, // Unselected with no extension
CLOSED, // Unselected with extension
MINIMIZED, // Selected with no extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,19 @@ private Rect getPaddedFrame() {
}

private int getXForAlignment(Font fontRenderer, Component text) {
switch (horizontalAlignment) {
case LEFT:
return getPaddedFrame().x();
case CENTER:
return getPaddedFrame().midX() - fontRenderer.width(text) / 2;
case RIGHT:
return getPaddedFrame().maxX() - fontRenderer.width(text);
default:
return getPaddedFrame().x();
}
return switch (horizontalAlignment) {
case LEFT -> getPaddedFrame().x();
case CENTER -> getPaddedFrame().midX() - fontRenderer.width(text) / 2;
case RIGHT -> getPaddedFrame().maxX() - fontRenderer.width(text);
};
}

private int getYForAlignment(Font fontRenderer) {
switch (verticalAlignment) {
case TOP:
return getPaddedFrame().y();
case CENTER:
return getPaddedFrame().midY() - fontRenderer.lineHeight / 2;
case BOTTOM:
return getPaddedFrame().maxY() - fontRenderer.lineHeight;
default:
return getPaddedFrame().y();
}
return switch (verticalAlignment) {
case TOP -> getPaddedFrame().y();
case CENTER -> getPaddedFrame().midY() - fontRenderer.lineHeight / 2;
case BOTTOM -> getPaddedFrame().maxY() - fontRenderer.lineHeight;
};
}

public enum HorizontalAlignment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ public class LithographyTableTile extends FabricationMachineTile {

@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
switch (slot) {
case 0:
return stack.getItem() instanceof PhotomaskSetItem;
case 1:
return stack.getItem() instanceof BaseSiliconWaferItem;
default:
return false;
}
return switch (slot) {
case 0 -> stack.getItem() instanceof PhotomaskSetItem;
case 1 -> stack.getItem() instanceof BaseSiliconWaferItem;
default -> false;
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ public class PlottingTableTile extends FabricationMachineTile {

@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
switch (slot) {
case 0:
return stack.getItem() instanceof ICBlueprintItem;
case 1:
return stack.getItem() instanceof BlankPhotomaskItem;
default:
return false;
}
return switch (slot) {
case 0 -> stack.getItem() instanceof ICBlueprintItem;
case 1 -> stack.getItem() instanceof BlankPhotomaskItem;
default -> false;
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ public IItemRenderer getItemRenderer() {
return new IItemRenderer() {
@Override
public void renderItem(ItemStack stack, ItemTransforms.TransformType transformType, PoseStack mStack, MultiBufferSource getter, int packedLight, int packedOverlay) {
if (!(stack.getItem() instanceof MultipartLightPartItem)) return;
if (!(stack.getItem() instanceof MultipartLightPartItem lightItem)) return;

MultipartLightPartItem lightItem = (MultipartLightPartItem) stack.getItem();
CCRenderState ccrs = CCRenderState.instance();
ccrs.reset();
ccrs.brightness = packedLight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ public IllumarLampItemRenderer(BakedModel wrapped) {
@Override
public void renderItem(ItemStack stack, ItemTransforms.TransformType transformType, PoseStack mStack, MultiBufferSource getter, int packedLight, int packedOverlay) {
Item item = stack.getItem();
if (!(item instanceof BlockItem)) return;
if (!(item instanceof BlockItem blockItem)) return;

BlockItem blockItem = (BlockItem) item;
if (!(blockItem.getBlock() instanceof IllumarLampBlock)) return;
if (!(blockItem.getBlock() instanceof IllumarLampBlock block)) return;

IllumarLampBlock block = (IllumarLampBlock) blockItem.getBlock();
if (!block.isInverted()) {
// Non-inverted blocks can use default Minecraft BlockItem model. Shouldn't happen
// because this renderer should only be registered to inverted variants.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,14 @@ public void handlePacket(PacketCustom packet, ServerPlayer sender, ServerGamePac

private void handleIncrTimerMessage(Level world, PacketCustom packet) {
MultiPart part = readPartIndex(world, packet);
if (part instanceof ComplexGatePart.ITimerGuiLogic) {
ComplexGatePart.ITimerGuiLogic gate = (ComplexGatePart.ITimerGuiLogic) part;
if (part instanceof ComplexGatePart.ITimerGuiLogic gate) {
gate.setTimerMax(gate.getTimerMax() + packet.readShort());
}
}

private void handleIncrCounterMessage(Level world, PacketCustom packet) {
MultiPart part = readPartIndex(world, packet);
if (part instanceof ComplexGatePart.ICounterGuiLogic) {
ComplexGatePart.ICounterGuiLogic gate = (ComplexGatePart.ICounterGuiLogic) part;
if (part instanceof ComplexGatePart.ICounterGuiLogic gate) {
int actionId = packet.readByte();
switch (actionId) {
case 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public ModelState getModelTransform() {
@Override
public void renderItem(ItemStack stack, ItemTransforms.TransformType transformType, PoseStack mStack, MultiBufferSource getter, int packedLight, int packedOverlay) {

if (!(stack.getItem() instanceof GatePartItem)) return;

GatePartItem gateItem = (GatePartItem) stack.getItem();
if (!(stack.getItem() instanceof GatePartItem gateItem)) return;

CCRenderState ccrs = CCRenderState.instance();
ccrs.reset();
Expand Down
Loading

0 comments on commit f35a326

Please sign in to comment.