Skip to content

Commit

Permalink
Remove usages of var, brackets around returns
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Dec 6, 2024
1 parent 228297d commit c8d6cf4
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static void handleSyncTask(final PacketByteBuf buf) {

if (PENDING_EXECUTION_TASKS.containsKey(uuid)) {
MinecraftClient.getInstance().execute(() -> { // Execute the task on the main thread
final var task = PENDING_EXECUTION_TASKS.remove(uuid);
final Consumer<RegistryByteBuf> task = PENDING_EXECUTION_TASKS.remove(uuid);
task.accept(new RegistryByteBuf(buf, MinecraftClient.getInstance().getNetworkHandler().getRegistryManager()));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator;
import net.minecraft.block.entity.BannerPattern;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.BannerPatternsComponent;
import net.minecraft.item.BannerItem;
Expand All @@ -33,6 +34,7 @@
import net.minecraft.recipe.input.CraftingRecipeInput;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.DyeColor;
import net.minecraft.world.World;

Expand Down Expand Up @@ -75,7 +77,7 @@ public ItemStack craft(CraftingRecipeInput input, RegistryWrapper.WrapperLookup

final BannerPattern_1_13_2 pattern = getBannerPattern(input);
if (pattern != null) {
final var patternKey = lookup.getOrThrow(RegistryKeys.BANNER_PATTERN).getOrThrow(pattern.getKey());
final RegistryEntry.Reference<BannerPattern> patternKey = lookup.getOrThrow(RegistryKeys.BANNER_PATTERN).getOrThrow(pattern.getKey());
DyeColor color = ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_12_2) ? DyeColor.BLACK : DyeColor.WHITE;
for (int i = 0; i < input.size(); i++) {
Item item = input.getStackInSlot(i).getItem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.inventory.RecipeInputInventory;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.recipe.*;
import net.minecraft.recipe.book.CraftingRecipeCategory;
import net.minecraft.recipe.input.CraftingRecipeInput;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.screen.ScreenHandler;
Expand Down Expand Up @@ -729,13 +732,13 @@ public static List<RecipeInfo> getRecipes(final ProtocolVersion targetVersion) {
*/
@ApiStatus.Internal
public static void setCraftingResultSlot(final int syncId, final ScreenHandler screenHandler, final RecipeInputInventory inventory) {
final var network = MinecraftClient.getInstance().getNetworkHandler();
final var world = MinecraftClient.getInstance().world;
final var craftingRecipeInput = inventory.createRecipeInput();
final ClientPlayNetworkHandler network = MinecraftClient.getInstance().getNetworkHandler();
final ClientWorld world = MinecraftClient.getInstance().world;
final CraftingRecipeInput input = inventory.createRecipeInput();

final var result = getRecipeManager()
.getFirstMatch(RecipeType.CRAFTING, craftingRecipeInput, world) // Get the first matching recipe
.map(recipe -> recipe.value().craft(craftingRecipeInput, network.getRegistryManager())) // Craft the recipe to get the result
final ItemStack result = getRecipeManager()
.getFirstMatch(RecipeType.CRAFTING, input, world) // Get the first matching recipe
.map(recipe -> recipe.value().craft(input, network.getRegistryManager())) // Craft the recipe to get the result
.orElse(ItemStack.EMPTY); // If there is no recipe, set the result to air

// Update the result slot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec2f;
Expand All @@ -50,13 +51,15 @@ public void trackPlayerAction(final PlayerActionC2SPacket.Action action, final B
}

public void handleBlockBreakAck(final BlockPos blockPos, final BlockState expectedState, final PlayerActionC2SPacket.Action action, final boolean allGood) {
final var player = MinecraftClient.getInstance().player;
if (player == null) return;
final ClientPlayerEntity player = MinecraftClient.getInstance().player;
if (player == null) {
return;
}

final var world = MinecraftClient.getInstance().getNetworkHandler().getWorld();
final ClientWorld world = MinecraftClient.getInstance().getNetworkHandler().getWorld();

final var oldPlayerState = unAckedActions.remove(Pair.of(blockPos, action));
final var actualState = world.getBlockState(blockPos);
final Pair<Vec3d, Vec2f> oldPlayerState = unAckedActions.remove(Pair.of(blockPos, action));
final BlockState actualState = world.getBlockState(blockPos);

if ((oldPlayerState == null || !allGood || action != PlayerActionC2SPacket.Action.START_DESTROY_BLOCK && actualState != expectedState) && (actualState != expectedState || ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_15_2))) {
world.setBlockState(blockPos, expectedState, Block.NOTIFY_ALL | Block.FORCE_STATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public class WorldHeightSupport {
public static PacketHandler handleJoinGame(final PacketHandler parentHandler) {
return wrapper -> {
parentHandler.handle(wrapper);
if (wrapper.isCancelled()) return;
if (wrapper.isCancelled()) {
return;
}

if (wrapper.user().getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.c0_28toc0_30)) {
for (CompoundTag dimension : wrapper.get(Types.NAMED_COMPOUND_TAG, 0).getCompoundTag("minecraft:dimension_type").getListTag("value", CompoundTag.class)) {
Expand All @@ -58,7 +60,9 @@ public static PacketHandler handleJoinGame(final PacketHandler parentHandler) {
public static PacketHandler handleRespawn(final PacketHandler parentHandler) {
return wrapper -> {
parentHandler.handle(wrapper);
if (wrapper.isCancelled()) return;
if (wrapper.isCancelled()) {
return;
}

if (wrapper.user().getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.c0_28toc0_30)) {
changeDimensionTagHeight(wrapper.user(), wrapper.get(Types.NAMED_COMPOUND_TAG, 0));
Expand All @@ -69,7 +73,9 @@ public static PacketHandler handleRespawn(final PacketHandler parentHandler) {
public static PacketHandler handleChunkData(final PacketHandler parentHandler) {
return wrapper -> {
parentHandler.handle(wrapper);
if (wrapper.isCancelled()) return;
if (wrapper.isCancelled()) {
return;
}

if (wrapper.user().getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.c0_28toc0_30)) {
wrapper.resetReader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import de.florianmichael.classic4j.model.classicube.account.CCAccount;
import de.florianmichael.viafabricplus.ViaFabricPlus;
import de.florianmichael.viafabricplus.injection.access.IServerInfo;
import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator;
Expand Down Expand Up @@ -83,8 +84,10 @@ private ChannelFuture setServerInfoAndHandleDisconnect(InetSocketAddress address
@Redirect(method = "run", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/session/Session;getUsername()Ljava/lang/String;"))
private String useClassiCubeUsername(Session instance) {
if (this.viaFabricPlus$useClassiCubeAccount) {
final var account = ViaFabricPlus.global().getSaveManager().getAccountsSave().getClassicubeAccount();
if (account != null) return account.username();
final CCAccount account = ViaFabricPlus.global().getSaveManager().getAccountsSave().getClassicubeAccount();
if (account != null) {
return account.username();
}
}
return instance.getUsername();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ private boolean changeSpeedCalculation(PlayerEntity instance, RegistryEntry<Stat
final boolean hasMiningFatigue = instance.hasStatusEffect(statusEffect);
if (hasMiningFatigue && ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_7_6)) {
f.set(f.get() * (1.0F - (this.getStatusEffect(StatusEffects.MINING_FATIGUE).getAmplifier() + 1) * 0.2F));
if (f.get() < 0) f.set(0);
if (f.get() < 0) {
f.set(0);
}
return false; // disable original code
}
return hasMiningFatigue;
Expand All @@ -213,7 +215,9 @@ private boolean changeSpeedCalculation(PlayerEntity instance, RegistryEntry<Stat
@Inject(method = "getBlockBreakingSpeed", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/effect/StatusEffectUtil;hasHaste(Lnet/minecraft/entity/LivingEntity;)Z", shift = At.Shift.BEFORE))
private void changeSpeedCalculation(BlockState block, CallbackInfoReturnable<Float> cir, @Local LocalFloatRef f) {
final float efficiency = (float) this.getAttributeValue(EntityAttributes.MINING_EFFICIENCY);
if (efficiency <= 0) return;
if (efficiency <= 0) {
return;
}

final float speed = this.inventory.getBlockBreakingSpeed(block);
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(LegacyProtocolVersion.r1_4_4tor1_4_5) && this.canHarvest(block)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.minecraft.network.encryption.PlayerKeyPair;
import net.minecraft.network.encryption.PlayerPublicKey;
import net.raphimc.minecraftauth.step.bedrock.StepMCChain;
import net.raphimc.minecraftauth.step.bedrock.session.StepFullBedrockSession;
import net.raphimc.viabedrock.api.BedrockProtocolVersion;
import net.raphimc.viabedrock.protocol.storage.AuthChainData;
import org.spongepowered.asm.mixin.Final;
Expand Down Expand Up @@ -93,7 +94,7 @@ private void setupConnectionSessions(CallbackInfo ci, @Local ClientConnection cl

userConnection.put(new ChatSession1_19_1(uuid, privateKey, new ProfileKey(expiresAt, publicKey, publicKeyData.keySignature())));
if (ProtocolTranslator.getTargetVersion() == ProtocolVersion.v1_19) {
final var legacyKeySignature = ((ILegacyKeySignatureStorage) (Object) publicKeyData).viafabricplus$getLegacyPublicKeySignature();
final byte[] legacyKeySignature = ((ILegacyKeySignatureStorage) (Object) publicKeyData).viafabricplus$getLegacyPublicKeySignature();
if (legacyKeySignature != null) {
userConnection.put(new ChatSession1_19_0(uuid, privateKey, new ProfileKey(expiresAt, publicKey, legacyKeySignature)));
}
Expand All @@ -102,7 +103,7 @@ private void setupConnectionSessions(CallbackInfo ci, @Local ClientConnection cl
ViaFabricPlus.global().getLogger().error("Could not get public key signature. Joining servers with enforce-secure-profiles enabled will not work!");
}
} else if (ProtocolTranslator.getTargetVersion() == BedrockProtocolVersion.bedrockLatest) {
final var bedrockSession = ViaFabricPlus.global().getSaveManager().getAccountsSave().refreshAndGetBedrockAccount();
final StepFullBedrockSession.FullBedrockSession bedrockSession = ViaFabricPlus.global().getSaveManager().getAccountsSave().refreshAndGetBedrockAccount();
if (bedrockSession != null) {
final StepMCChain.MCChain mcChain = bedrockSession.getMcChain();
final UUID deviceId = mcChain.getXblXsts().getInitialXblSession().getXblDeviceToken().getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Mixin(GameModeSelectionScreen.class)
public abstract class MixinGameModeSelectionScreen extends Screen {
Expand All @@ -52,14 +53,14 @@ public MixinGameModeSelectionScreen(Text title) {
@Inject(method = "<init>", at = @At("RETURN"))
private void fixUIWidth(CallbackInfo ci) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_7_6)) {
final var gameModeSelections = new ArrayList<>(Arrays.stream(GameModeSelectionScreen.GameModeSelection.values()).toList());
final List<GameModeSelectionScreen.GameModeSelection> selections = new ArrayList<>(Arrays.stream(GameModeSelectionScreen.GameModeSelection.values()).toList());

gameModeSelections.remove(GameModeSelectionScreen.GameModeSelection.SPECTATOR);
selections.remove(GameModeSelectionScreen.GameModeSelection.SPECTATOR);
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(LegacyProtocolVersion.r1_2_4tor1_2_5)) {
gameModeSelections.remove(GameModeSelectionScreen.GameModeSelection.ADVENTURE);
selections.remove(GameModeSelectionScreen.GameModeSelection.ADVENTURE);
}

viaFabricPlus$unwrappedGameModes = gameModeSelections.toArray(GameModeSelectionScreen.GameModeSelection[]::new);
viaFabricPlus$unwrappedGameModes = selections.toArray(GameModeSelectionScreen.GameModeSelection[]::new);
UI_WIDTH = viaFabricPlus$unwrappedGameModes.length * 31 - 5;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ private void onSwitchTo(int recipeId, CallbackInfo ci) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_13_2)) {
ci.cancel();

if (recipeId >= this.getRecipes().size()) return;
if (recipeId >= this.getRecipes().size()) {
return;
}

final ClientPlayerInteractionManager interactionManager = MinecraftClient.getInstance().interactionManager;
final ClientPlayerEntity player = MinecraftClient.getInstance().player;
Expand All @@ -72,14 +74,18 @@ private void onSwitchTo(int recipeId, CallbackInfo ci) {
if (!this.merchantInventory.getStack(0).isEmpty()) {
final int count = this.merchantInventory.getStack(0).getCount();
interactionManager.clickSlot(syncId, 0, 0, SlotActionType.QUICK_MOVE, player);
if (count == this.merchantInventory.getStack(0).getCount()) return;
if (count == this.merchantInventory.getStack(0).getCount()) {
return;
}
}

// move 2nd input slot to inventory
if (!this.merchantInventory.getStack(1).isEmpty()) {
final int count = this.merchantInventory.getStack(1).getCount();
interactionManager.clickSlot(syncId, 1, 0, SlotActionType.QUICK_MOVE, player);
if (count == this.merchantInventory.getStack(1).getCount()) return;
if (count == this.merchantInventory.getStack(1).getCount()) {
return;
}
}

// refill the slots
Expand Down Expand Up @@ -110,7 +116,9 @@ private void modifyCanInsertIntoSlot(ItemStack stack, Slot slot, CallbackInfoRet
}
}
}
if (slot == 39) return;
if (slot == 39) {
return;
}

final boolean wasHoldingItem = !player.currentScreenHandler.getCursorStack().isEmpty();
interactionManager.clickSlot(syncId, slot, 0, SlotActionType.PICKUP, player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ private void loadAdditionalData(CallbackInfo ci) {
@Inject(method = "handleItemToClient", at = @At("RETURN"))
private void addAttributeFixData(CallbackInfoReturnable<Item> cir) {
final Item item = cir.getReturnValue();
if (item == null) return;
if (item == null) {
return;
}

final String identifier = viaFabricPlus$itemIdentifiers.get(item.identifier());
if (identifier != null && viaFabricPlus$itemAttributes.containsKey(identifier)) {
Expand Down Expand Up @@ -121,11 +123,17 @@ private void addAttributeFixData(CallbackInfoReturnable<Item> cir) {
@Inject(method = "handleItemToServer", at = @At("RETURN"))
private void removeAttributeFixData(CallbackInfoReturnable<Item> cir) {
final Item item = cir.getReturnValue();
if (item == null) return;
if (item == null) {
return;
}
final CompoundTag tag = item.tag();
if (tag == null) return;
if (tag == null) {
return;
}
final CompoundTag attributeFixTag = tag.removeUnchecked(nbtTagName("attributeFix"));
if (attributeFixTag == null) return;
if (attributeFixTag == null) {
return;
}

if (attributeFixTag.contains("RemoveAttributeModifiers")) {
tag.remove("AttributeModifiers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static void handleLegacyAcknowledgePlayerDigging(Protocol1_18_2To1_19 in
final PlayerActionC2SPacket.Action action = data.readEnumConstant(PlayerActionC2SPacket.Action.class);
final boolean allGood = data.readBoolean();

final var mixinInteractionManager = (IClientPlayerInteractionManager) MinecraftClient.getInstance().interactionManager;
final IClientPlayerInteractionManager mixinInteractionManager = (IClientPlayerInteractionManager) MinecraftClient.getInstance().interactionManager;
mixinInteractionManager.viaFabricPlus$get1_18_2InteractionManager().handleBlockBreakAck(pos, blockState, action, allGood);
} catch (Throwable t) {
throw new RuntimeException("Failed to handle BlockBreakAck packet data", t);
Expand Down
Loading

0 comments on commit c8d6cf4

Please sign in to comment.