Skip to content

Commit

Permalink
fix crash
Browse files Browse the repository at this point in the history
closes #49
  • Loading branch information
MelanX committed Mar 31, 2024
1 parent 58c6679 commit c79c301
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -6,3 +6,20 @@ Swaps the tools to the effective one if it's in hotbar.

[![Modrinth](https://img.shields.io/modrinth/game-versions/L9JLNLqk?color=00AF5C&label=modrinth&logo=modrinth)](https://modrinth.com/mod/automatic-tool-swap)
[![Modrinth](https://img.shields.io/modrinth/dt/L9JLNLqk?color=00AF5C&logo=modrinth)](https://modrinth.com/mod/automatic-tool-swap)

For mod devs who want to add compatibility with Automatic Tool Swap, you simply need to implement
`de.melanx.toolswap.DiggerLike` into your tool class if it's not a child of `DiggerItem`. Also add the one method,
alternatively the other methods. This way, your tool will be considered when swapping. Just add this to your
`build.gradle`:

```groovy
repositories {
maven {
url = "https://modmaven.dev/"
}
}
dependencies {
compileOnly fg.deobf("de.melanx:ToolSwap:1.20.1-5.0.3+")
}
```
7 changes: 6 additions & 1 deletion src/main/java/de/melanx/toolswap/ClientToolSwap.java
Expand Up @@ -300,7 +300,12 @@ public static void searchForSwitching(MultiPlayerGameMode multiPlayerGameMode, B
for (ToolEntry entry : finalToolList) {
for (ResourceLocation id : mineables) {
//noinspection ConstantConditions
if (Objects.equals(entry.getType().location(), id) && TierSortingRegistry.isCorrectTierForDrops(entry.getToolItem().getTier(), state)) {
DiggerLike diggerItem = entry.getToolItem();
if (diggerItem == null) {
continue;
}

if (Objects.equals(entry.getType().location(), id) && TierSortingRegistry.isCorrectTierForDrops(diggerItem.getTier(), state)) {
ClientToolSwap.switchTo(player, entry.getStack());
return;
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/de/melanx/toolswap/DiggerLike.java
@@ -0,0 +1,17 @@
package de.melanx.toolswap;

import net.minecraft.world.item.Tier;

public interface DiggerLike {

Tier getTier();

default int getHarvestLevel() {
//noinspection deprecation
return this.getTier().getLevel();
}

default float getEfficiency() {
return this.getTier().getSpeed();
}
}
17 changes: 10 additions & 7 deletions src/main/java/de/melanx/toolswap/ToolEntry.java
Expand Up @@ -8,7 +8,7 @@
import javax.annotation.Nullable;

public record ToolEntry(TagKey<Block> type,
ItemStack stack) {
ItemStack stack) {

public TagKey<Block> getType() {
return this.type;
Expand All @@ -19,20 +19,23 @@ public ItemStack getStack() {
}

@Nullable
public DiggerItem getToolItem() {
return this.stack.getItem() instanceof DiggerItem item ? item : null;
public DiggerLike getToolItem() {
if (this.stack.getItem() instanceof DiggerLike diggerLike) {
return diggerLike;
}

return this.stack.getItem() instanceof DiggerItem diggerItem ? diggerItem::getTier : null;
}

public int getHarvestLevel() {
DiggerItem item = this.getToolItem();
DiggerLike item = this.getToolItem();
//noinspection deprecation
return item != null ? item.getTier().getLevel() : -1;
}

public float getEfficiency() {
DiggerItem item = this.getToolItem();
//noinspection deprecation
return item != null ? item.getTier().getLevel() : 0.0F;
DiggerLike item = this.getToolItem();
return item != null ? item.getTier().getSpeed() : 0.0F;
}

@Override
Expand Down

0 comments on commit c79c301

Please sign in to comment.