Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 05ceb6f

Browse files
jaskarthTheGlitch76kitlith
authored
add tool types (#73)
* add tool types * Apply suggestions from code review Co-authored-by: Glitch <[email protected]> * Move ToolType class to its own subproject Use HashMap constructor instead of Maps method * FIXUP! Co-authored-by: Glitch <[email protected]> Co-authored-by: Kitlith <[email protected]>
1 parent 5413a7a commit 05ceb6f

File tree

11 files changed

+100
-11
lines changed

11 files changed

+100
-11
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
archivesBaseName = "patchwork-extensions-item"
22
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-tooltype', configuration: 'dev')
6+
}

patchwork-extensions-item/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import com.google.common.collect.ImmutableMap;
3232
import com.google.common.collect.Multimap;
33+
import net.minecraftforge.common.ToolType;
3334

3435
import net.minecraft.block.BlockState;
3536
import net.minecraft.client.font.TextRenderer;
@@ -563,7 +564,7 @@ default int getItemStackLimit(ItemStack stack) {
563564
}
564565

565566
// TODO: Call locations: Patches: MiningToolItem, Forge classes: IForgeItemStack, ForgeHooks*
566-
Set<Object /* TODO: ToolType */> getToolTypes(ItemStack stack);
567+
Set<ToolType> getToolTypes(ItemStack stack);
567568

568569
// TODO: Call locations: Forge classes: IForgeItemStack, ForgeHooks
569570
/**
@@ -576,7 +577,7 @@ default int getItemStackLimit(ItemStack stack) {
576577
* @param blockState The block to harvest
577578
* @return Harvest level, or -1 if not the specified tool type.
578579
*/
579-
int getHarvestLevel(ItemStack stack, Object /* TODO: ToolType */ tool, @Nullable PlayerEntity player, @Nullable BlockState blockState);
580+
int getHarvestLevel(ItemStack stack, ToolType tool, @Nullable PlayerEntity player, @Nullable BlockState blockState);
580581

581582
// TODO: Call locations: Patches: EnchantmentHelper, Forge classes: IForgeItemStack
582583
/**

patchwork-extensions-item/src/main/java/net/patchworkmc/impl/extensions/item/PatchworkItemSettingsExtensions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.concurrent.Callable;
2424
import java.util.function.Supplier;
2525

26+
import net.minecraftforge.common.ToolType;
27+
2628
import net.minecraft.client.render.item.BuiltinModelItemRenderer;
2729
import net.minecraft.item.Item;
2830

@@ -36,13 +38,13 @@
3638
public interface PatchworkItemSettingsExtensions {
3739
Item.Settings setNoRepair();
3840

39-
Item.Settings addToolType(Object /* TODO: ToolType */ type, int level);
41+
Item.Settings addToolType(ToolType type, int level);
4042

4143
Item.Settings setTEISR(Supplier<Callable<BuiltinModelItemRenderer>> teisr);
4244

4345
boolean canRepair();
4446

45-
Map<Object /* TODO: ToolType */, Integer> getToolClasses();
47+
Map<ToolType, Integer> getToolClasses();
4648

4749
@Environment(EnvType.CLIENT)
4850
BuiltinModelItemRenderer getTeisr();

patchwork-extensions-item/src/main/java/net/patchworkmc/mixin/extensions/item/MixinItem.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.annotation.Nullable;
2727

2828
import com.google.common.collect.Maps;
29+
import net.minecraftforge.common.ToolType;
2930
import net.minecraftforge.common.extensions.IForgeItem;
3031
import org.spongepowered.asm.mixin.Mixin;
3132
import org.spongepowered.asm.mixin.Shadow;
@@ -50,7 +51,7 @@
5051

5152
@Mixin(Item.class)
5253
public abstract class MixinItem implements IForgeItem {
53-
@Unique private Map<Object /* TODO: ToolType */, Integer> toolClasses;
54+
@Unique private Map<ToolType, Integer> toolClasses;
5455
protected boolean canRepair;
5556

5657
@Inject(at = @At("RETURN"), method = "<init>")
@@ -81,12 +82,12 @@ public boolean isRepairable(ItemStack stack) {
8182
}
8283

8384
@Override
84-
public Set<Object /* TODO: ToolType */> getToolTypes(ItemStack stack) {
85+
public Set<ToolType> getToolTypes(ItemStack stack) {
8586
return toolClasses.keySet();
8687
}
8788

8889
@Override
89-
public int getHarvestLevel(ItemStack stack, Object /* TODO: ToolType */ tool, @Nullable PlayerEntity player, @Nullable BlockState blockState) {
90+
public int getHarvestLevel(ItemStack stack, ToolType tool, @Nullable PlayerEntity player, @Nullable BlockState blockState) {
9091
return toolClasses.getOrDefault(tool, -1);
9192
}
9293

patchwork-extensions-item/src/main/java/net/patchworkmc/mixin/extensions/item/MixinItemSettings.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424

25+
import net.minecraftforge.common.ToolType;
2526
import org.spongepowered.asm.mixin.Mixin;
2627
import org.spongepowered.asm.mixin.Unique;
2728

@@ -33,7 +34,7 @@
3334
@Mixin(Item.Settings.class)
3435
public abstract class MixinItemSettings implements PatchworkItemSettingsExtensions {
3536
@Unique private boolean canRepair = true;
36-
@Unique private final Map<Object /* TODO: ToolType */, Integer> toolClasses = new HashMap<>();
37+
@Unique private final Map<ToolType, Integer> toolClasses = new HashMap<>();
3738

3839
@Override
3940
public Settings setNoRepair() {
@@ -42,7 +43,7 @@ public Settings setNoRepair() {
4243
}
4344

4445
@Override
45-
public Settings addToolType(Object /* TODO: ToolType */ type, int level) {
46+
public Settings addToolType(ToolType type, int level) {
4647
toolClasses.put(type, level);
4748
return (Settings) (Object) this;
4849
}
@@ -53,7 +54,7 @@ public boolean canRepair() {
5354
}
5455

5556
@Override
56-
public Map<Object /* TODO: ToolType */, Integer> getToolClasses() {
57+
public Map<ToolType, Integer> getToolClasses() {
5758
return toolClasses;
5859
}
5960
}

patchwork-extensions-item/src/main/resources/fabric.mod.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
],
1717
"depends": {
1818
"fabricloader": ">=0.8.4",
19-
"fabric": "*"
19+
"fabric": "*",
20+
"patchwork-tooltype": "*"
2021
},
2122
"mixins": [
2223
"patchwork-extensions-item.mixins.json"

patchwork-tooltype/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
archivesBaseName = "patchwork-tooltype"
2+
version = getSubprojectVersion(project, "0.1.0")
3+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.common;
21+
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import java.util.regex.Pattern;
25+
26+
public final class ToolType {
27+
private static final Pattern VALID_NAME = Pattern.compile("[^a-z_]"); //Only a-z and _ are allowed, meaning names must be lower case and use _ to separate words.
28+
private static final Map<String, ToolType> values = new HashMap<>();
29+
30+
public static final ToolType AXE = get("axe");
31+
public static final ToolType PICKAXE = get("pickaxe");
32+
public static final ToolType SHOVEL = get("shovel");
33+
private final String name;
34+
35+
private ToolType(String name) {
36+
this.name = name;
37+
}
38+
39+
public static ToolType get(String name) {
40+
if (VALID_NAME.matcher(name).find()) {
41+
throw new IllegalArgumentException("ToolType.get() called with invalid name: " + name);
42+
}
43+
44+
return values.computeIfAbsent(name, k -> new ToolType(name));
45+
}
46+
47+
public String getName() {
48+
return name;
49+
}
50+
}
22.2 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"schemaVersion": 1,
3+
"id": "patchwork-tooltype",
4+
"name": "Patchwork ToolType",
5+
"version": "${version}",
6+
"description": "Includes the Minecraft Forge ToolType class",
7+
"environment": "*",
8+
"license": "LGPL-2.1-only",
9+
"icon": "assets/patchwork-tooltype/icon.png",
10+
"contact": {
11+
"issues": "https://github.com/PatchworkMC/patchwork-api/issues",
12+
"sources": "https://github.com/PatchworkMC/patchwork-api"
13+
},
14+
"authors": [
15+
"PatchworkMC"
16+
],
17+
"depends": {
18+
"fabricloader": ">=0.8.4",
19+
"fabric": "*"
20+
},
21+
"custom": {
22+
"modmenu:api": true,
23+
"modmenu:parent": "patchwork"
24+
}
25+
}

0 commit comments

Comments
 (0)