Skip to content

Commit

Permalink
⬆️ Minecraft 1.21 (#21)
Browse files Browse the repository at this point in the history
* ⬆️ Minecraft 1.21

* ⬆️ Use java 21 in pipeline

* 📝 Update changelog

* 🐛 Detect type-specific enchantments

* ⬆️ Java 21 in build.gradle

* 🐛 Debug logs and fix attack speed

* 🔊 Use string interpolation in logs

* ✏️ Typo
  • Loading branch information
NatoBoram authored Jul 1, 2024
1 parent 4950371 commit dc807ef
Show file tree
Hide file tree
Showing 24 changed files with 474 additions and 181 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github-pages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: 17
java-version: 21
distribution: temurin
- uses: gradle/actions/setup-gradle@v3

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: 17
java-version: 21
distribution: temurin
- uses: gradle/actions/setup-gradle@v3

Expand Down
1 change: 1 addition & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
line-length: false
no-blanks-blockquote: false
no-duplicate-heading: false
6 changes: 3 additions & 3 deletions .minepkg-lock.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
gradle 8.7
java openjdk-17
java openjdk-21
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/) and this

## [Unreleased]

> [!NOTE]
> The Mace has lower damage per seconds than bare hands. The mod will not attempt to switch out of it, but won't switch into it either.
> [!WARNING]
> The Mace's enchantments are not supported at the moment. It's better to disable Switcheroo during your Mace activities.
### Added

- `/switcheroo enable` and `/switcheroo disable` commands

### Changed

- Updated to Minecraft 1.21

### Deprecated

### Removed
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ Switcheroo will use the least effective tool that's still effective to break blo

## Installation

[![cloth-config2](https://img.shields.io/badge/Cloth%20Config%20API-11.1.118-9cff55)](https://github.com/shedaniel/cloth-config)
[![fabric](https://img.shields.io/badge/Fabric%20API-0.92.0-dbd0b4)](https://github.com/FabricMC/fabric)
[![modmenu](https://img.shields.io/badge/Mod%20Menu-7.2.2-134bff)](https://github.com/TerraformersMC/ModMenu)
[![cloth-config2](https://img.shields.io/badge/Cloth%20Config%20API-15.0.127-9cff55)](https://github.com/shedaniel/cloth-config)
[![fabric](https://img.shields.io/badge/Fabric%20API-0.100.1-dbd0b4)](https://github.com/FabricMC/fabric)
[![modmenu](https://img.shields.io/badge/Mod%20Menu-11.0.0--rc.4-134bff)](https://github.com/TerraformersMC/ModMenu)

Downloads are available in [Releases](https://github.com/NatoBoram/switcheroo/releases). You can also test development builds by downloading them form the [Actions](https://github.com/NatoBoram/switcheroo/actions/workflows/gradle.yaml?query=branch%3Amain).

Expand All @@ -52,7 +52,7 @@ Downloads are available in [Releases](https://github.com/NatoBoram/switcheroo/re

## Contributing

Requires Java 17.
Requires Java 21.

### Developing

Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ tasks.withType(JavaCompile).configureEach {
// If Javadoc is generated, this must be specified in that task too.
it.options.encoding = "UTF-8"

// Minecraft 1.18 upwards uses Java 17.
it.options.release = 17
// Minecraft 1.21 upwards uses Java 21.
it.options.release = 21
}

java {
Expand All @@ -73,8 +73,8 @@ java {
// If you remove this line, sources will not be generated.
withSourcesJar()

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

jar {
Expand Down
16 changes: 8 additions & 8 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G -Dfile.encoding=UTF-8
org.gradle.jvmargs=-Xmx2G -Dfile.encoding=UTF-8
org.gradle.parallel=true

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.15.9
minecraft_version=1.21
yarn_mappings=1.21+build.2
loader_version=0.15.11

#Fabric api
fabric_version=0.92.0+1.20.1
# Fabric API
fabric_version=0.100.1+1.21

# Mod Properties
mod_version=1.0.13
maven_group=com.natoboram
archives_base_name=switcheroo

# Dependencies
clothconfig_version=11.1.118
modmenu_version=7.2.2
clothconfig_version=15.0.127
modmenu_version=11.0.0-rc.4
10 changes: 5 additions & 5 deletions minepkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ author = "Nato Boram"

# These are global requirements
[requirements]
minecraft = ">=1.20.1"
fabricLoader = ">=0.15.9"
minecraft = ">=1.21"
fabricLoader = ">=0.15.11"

[dependencies]
cloth-config = ">=11.1.118+mpkg.1"
fabric = ">=0.92.0+1.20.1"
modmenu = ">=7.2.2+mpkg.1"
cloth-config = ">=15.0.127+mpkg.1"
fabric = ">=0.92.0+1.21"
modmenu = ">=11.0.0-rc.4+mpkg.1"

[dev]
buildCommand = "./gradlew build"
53 changes: 36 additions & 17 deletions src/main/java/com/natoboram/switcheroo/BlockSwitch.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.natoboram.switcheroo;

import static net.fabricmc.api.EnvType.CLIENT;

import java.util.ArrayList;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import me.shedaniel.autoconfig.ConfigHolder;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.minecraft.block.BambooBlock;
Expand All @@ -22,6 +23,7 @@
import net.minecraft.block.SugarCaneBlock;
import net.minecraft.block.VineBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -33,7 +35,11 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.ShearsItem;
import net.minecraft.item.SwordItem;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
Expand All @@ -42,7 +48,7 @@
import net.minecraft.world.World;

/** Execute a switcheroo action when attacking a block. */
@Environment(EnvType.CLIENT)
@Environment(value = CLIENT)
public class BlockSwitch implements AttackBlockCallback {

private static final Logger LOGGER = LogManager.getLogger(Main.MOD_ID);
Expand All @@ -58,20 +64,29 @@ public class BlockSwitch implements AttackBlockCallback {
@Override
public ActionResult interact(final PlayerEntity player, final World world, final Hand hand, final BlockPos pos,
final Direction direction) {
if (player.isCreative() || player.isSpectator() || player.isSneaking())
return ActionResult.PASS;
final SwitcherooConfig config = CONFIG_HOLDER.getConfig();

final BlockState blockState = world.getBlockState(pos);
final Block block = blockState.getBlock();
final SwitcherooConfig config = CONFIG_HOLDER.getConfig();

if (player.isCreative() || player.isSpectator() || player.isSneaking() || !config.enabled) {
if (config.debug)
LOGGER.info("Skipping interaction with block {}", block.getName().getString());
return ActionResult.PASS;
}

// Blacklist some blocks
if (isBlacklisted(block, config)) {
if (config.debug)
LOGGER.info(block + " is blacklisted");
LOGGER.info("{} is blacklisted", block.getName().getString());
return ActionResult.PASS;
}

// Cache enchantments
final DynamicRegistryManager manager = world.getRegistryManager();
final Registry<Enchantment> enchantments = manager.get(RegistryKeys.ENCHANTMENT);
final RegistryEntry<Enchantment> silkTouchEntry = enchantments.getEntry(Enchantments.SILK_TOUCH).get();

// Use CROP_SWITCH to handle crops
if (world.getBlockState(pos).getBlock() instanceof CropBlock && config.enableCrop)
return CROP_SWITCH.interact(player, world, hand, pos, direction);
Expand Down Expand Up @@ -116,43 +131,47 @@ && axeFilter(block, stack.getItem()))
// If there's no effective tools, check for the mining speed
if (tools.isEmpty())
for (final ItemStack stack : inventory.main)
if (ItemStackUtil.getMiningSpeedMultiplier(stack, blockState) > 1.0F
if (ItemStackUtil.getMiningSpeedMultiplier(stack, blockState, world) > 1.0F
&& !(stack.getItem() instanceof SwordItem) && axeFilter(block, stack.getItem()))
tools.add(stack);

// Add Silk Touch
if (tools.isEmpty() && preferSilkTouch(block, config))
for (final ItemStack stack : inventory.main)
if (EnchantmentHelper.getLevel(Enchantments.SILK_TOUCH, stack) > 0)
if (EnchantmentHelper.getLevel(silkTouchEntry, stack) > 0)
tools.add(stack);
}

// Keep Silk Touch
if (preferSilkTouch(block, config)
&& tools.stream().anyMatch(tool -> EnchantmentHelper.getLevel(Enchantments.SILK_TOUCH, tool) > 0))
tools.removeIf(tool -> EnchantmentHelper.getLevel(Enchantments.SILK_TOUCH, tool) <= 0);
&& tools.stream().anyMatch(tool -> EnchantmentHelper.getLevel(silkTouchEntry, tool) > 0))
tools.removeIf(tool -> EnchantmentHelper.getLevel(silkTouchEntry, tool) <= 0);

// Filters enchanted items with low durability
ItemStackUtil.removeDamagedEnchantedItems(tools, config);

// Safety before launching streams
if (tools.isEmpty())
if (tools.isEmpty()) {
if (config.debug)
LOGGER.info("No tools found");
return ActionResult.PASS;
}

// Get best or worst tool
if (CLIENT.options.sprintKey.isPressed() || config.alwaysFastest)
ItemStackUtil.keepFastestTools(tools, blockState);
ItemStackUtil.keepFastestTools(tools, blockState, world);
else
ItemStackUtil.keepSlowestTools(tools, blockState);
ItemStackUtil.keepSlowestTools(tools, blockState, world);

final ItemStack mainHand = player.getMainHandStack();
final double mainHandSpeed = ItemStackUtil.getMiningSpeedMultiplier(mainHand, blockState);
final double mainHandSpeed = ItemStackUtil.getMiningSpeedMultiplier(mainHand, blockState, world);

// Stop if there's already a valid item in hand
if (tools.stream().anyMatch(stack -> mainHandSpeed == ItemStackUtil.getMiningSpeedMultiplier(stack, blockState)
&& ItemStack.areItemsEqual(stack, mainHand))) {
if (tools.stream()
.anyMatch(stack -> mainHandSpeed == ItemStackUtil.getMiningSpeedMultiplier(stack, blockState, world)
&& ItemStack.areItemsEqual(stack, mainHand))) {
if (config.debug)
LOGGER.info("There's already a " + mainHand.getItem() + " in hand.");
LOGGER.info("There's already a {} in hand", mainHand.getItem().getName().getString());
return ActionResult.PASS;
}

Expand Down
Loading

0 comments on commit dc807ef

Please sign in to comment.