Redesign the tool system to rely on the new vanilla tags. (#7970)

Changed:
- ToolType is replaced with ToolAction, which can selectively represent which actions are supported by an item.
- Added a canPerformAction method to query ToolActions.
- Renamed canHarvestBlock to isCorrectToolForDrops to match vanilla.
- Added TierSortingRegistry, which can be used to add custom tiers and supports sorting between vanilla or with other mods.

How to use:

For the breaking system there's 2 methods:
- getDestroySpeed decides if an item *can* mine the block fast. Override if you have multiple tags or your item isn't a DiggerItem.
- isCorrectToolForDrops decides if an item will get drops. Returning false will also slow down mining, regardless of getDestroySpeed. Override if you have type-dependant tiers or your item isn't a DiggerItem.

For the tier system mods can just call `TierSortingRegistry.registerTier(tier, "name", List.of(lesser tiers), List.of(better tiers));` to register their tiers.
There's helper methods to query tiers such as TierSortingRegsitry.isCorrectTierForDrops.

The ToolActions solve 2 problems:
1. distinguishing which kind of digger an item can be
2. querying if the item can perform specific secondary actions.
Any item can override `canPerformAction` to customize which actions it performs.
This commit is contained in:
David Quintana authored and GitHub committed 2021-08-10 15:45:08 -07:00
1 parent 52d7df06da
commit 10b51f257f
34 files changed
+1012 -441

No files matched your search

@@ -0,0 +1,73 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@SuppressWarnings("ClassCanBeRecord")
public class ToolAction
{
private static final Map<String, ToolAction> actions = new ConcurrentHashMap<>();
/**
* Returns all registered actions.
* This collection can be kept around, and will update itself in response to changes to the map.
* See {@link ConcurrentHashMap#values()} for details.
*/
public static Collection<ToolAction> getActions()
{
return Collections.unmodifiableCollection(actions.values());
}
/**
* Gets or creates a new ToolAction for the given name.
*/
public static ToolAction get(String name)
{
return actions.computeIfAbsent(name, ToolAction::new);
}
/**
* Returns the name of this tool action
*/
public String name()
{
return name;
}
@Override
public String toString()
{
return "ToolAction[" + name + "]";
}
private final String name;
/**
* Use {@link #get(String)} to get or create a ToolAction
*/
private ToolAction(String name)
{
this.name = name;
}
}