Skip to content

Commit

Permalink
Keys.TOOL_RULES
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed May 26, 2024
1 parent 7ac71d7 commit 36aaf56
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/org/spongepowered/api/data/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.spongepowered.api.data.type.StairShape;
import org.spongepowered.api.data.type.StructureMode;
import org.spongepowered.api.data.type.Tilt;
import org.spongepowered.api.data.type.ToolRule;
import org.spongepowered.api.data.type.TropicalFishShape;
import org.spongepowered.api.data.type.VillagerType;
import org.spongepowered.api.data.type.WallConnectionState;
Expand Down Expand Up @@ -600,7 +601,8 @@ public final class Keys {
public static final Key<Value<Boolean>> CAN_GRIEF = Keys.key(ResourceKey.sponge("can_grief"), Boolean.class);

/**
* The set of harvestable {@link BlockType}s with an {@link ItemStack}. {@link #EFFICIENCY}
* The set of harvestable {@link BlockType}s with an {@link ItemStack}.
* See {@link #TOOL_RULES} for the rules resulting in this set.
* Readonly
*/
public static final Key<SetValue<BlockType>> CAN_HARVEST = Keys.setKey(ResourceKey.sponge("can_harvest"), BlockType.class);
Expand Down Expand Up @@ -887,6 +889,7 @@ public final class Keys {

/**
* The efficiency of an {@link ItemStack} tool. Affects mining speed of supported materials. {@link #CAN_HARVEST}
* Removing this from a tool makes it no longer a tool.
*/
public static final Key<Value<Double>> EFFICIENCY = Keys.key(ResourceKey.sponge("efficiency"), Double.class);

Expand Down Expand Up @@ -3119,6 +3122,12 @@ public final class Keys {
*/
public static final Key<Value<Integer>> TOOL_DAMAGE_PER_BLOCK = Keys.key(ResourceKey.sponge("tool_damage_per_block"), Integer.class);

/**
* The {@link ToolRule rules} of an {@link ItemStack} tool.
* See {@link #CAN_HARVEST} for a list of {@link BlockType block types}.
*/
public static final Key<ListValue<ToolRule>> TOOL_RULES = Keys.listKey(ResourceKey.sponge("tool_rules"), ToolRule.class);

/**
* The {@link ItemTier} of an {@link ItemStack} tool.
* Readonly
Expand Down
183 changes: 183 additions & 0 deletions src/main/java/org/spongepowered/api/data/type/ToolRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.data.type;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.tag.Tag;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
* A tool rule that applied via {@link org.spongepowered.api.data.Keys#TOOL_RULES} to an {@link org.spongepowered.api.item.inventory.ItemStack}
* determines what {@link BlockType block types} can be mined at what speed and whether they drop items.
*/
public interface ToolRule {


static ToolRule minesAndDrops(List<BlockType> blocks, double speed) {
return ToolRule.factory().minesAndDrops(blocks, speed);
}

static ToolRule minesAndDrops(Tag<BlockType> blockTypeTag, double speed) {
return ToolRule.factory().minesAndDrops(blockTypeTag, speed);
}

static ToolRule deniesDrops(List<BlockType> blocks) {
return ToolRule.factory().deniesDrops(blocks);
}

static ToolRule deniesDrops(Tag<BlockType> blockTypeTag) {
return ToolRule.factory().deniesDrops(blockTypeTag);
}

static ToolRule overrideSpeed(List<BlockType> blocks, double speed) {
return ToolRule.factory().overrideSpeed(blocks, speed);
}

static ToolRule overrideSpeed(Tag<BlockType> blockTypeTag, double speed) {
return ToolRule.factory().overrideSpeed(blockTypeTag, speed);
}

static ToolRule forBlocks(List<BlockType> blocks, @Nullable Double speed, @Nullable Boolean drops) {
return ToolRule.factory().forBlocks(blocks, speed, drops);
}

static ToolRule forTag(Tag<BlockType> blockTypeTag, @Nullable Double speed, @Nullable Boolean drops) {
return ToolRule.factory().forTag(blockTypeTag, speed, drops);
}

/**
* Returns the {@link BlockType block types} this rule applies to.
*
* @return the affected blocks
*/
Set<BlockType> blocks();

/**
* Returns the speed override.
* <p>If present overrides the default {@link org.spongepowered.api.data.Keys#EFFICIENCY mining speed}.</p>
*
* @return the speed override
*/
Optional<Double> speed();

/**
* Returns the drops override.
* <p>If present determines whether a block mined with this rule drops its item</p>
*
* @return the drops override
*/
Optional<Boolean> drops();

private static Factory factory() {
return Sponge.game().factoryProvider().provide(Factory.class);
}

interface Factory {

/**
* Generates a rule for mining and dropping given blocks at given speed.
*
* @param blocks the blocks
* @param speed the speed
* @return the generated rule
*/
ToolRule minesAndDrops(List<BlockType> blocks, double speed);

/**
* Generates a rule for mining and dropping given block type tag at given speed.
*
* @param blockTypeTag the block type tag
* @param speed the speed
*
* @return the generated rule
*/
ToolRule minesAndDrops(Tag<BlockType> blockTypeTag, double speed);

/**
* Generates a rule for preventing drops for given blocks.
*
* @param blocks the blocks
*
* @return the generated rule
*/
ToolRule deniesDrops(List<BlockType> blocks);

/**
* Generates a rule for preventing drops for given block type tag.
*
* @param blockTypeTag the block type tag
*
* @return the generated rule
*/
ToolRule deniesDrops(Tag<BlockType> blockTypeTag);

/**
* Generates a rule overriding mining speed drops for given blocks.
*
* @param blocks the blocks
* @param speed the speed
*
* @return the generated rule
*/
ToolRule overrideSpeed(List<BlockType> blocks, double speed);

/**
* Generates a rule overriding mining speed drops for given block type tag.
*
* @param blockTypeTag the block type tag
* @param speed the speed
*
* @return the generated rule
*/
ToolRule overrideSpeed(Tag<BlockType> blockTypeTag, double speed);

/**
* Generates a rule for blocks. Optionally overriding speed and/or drops.
*
* @param blocks the blocks
* @param speed the optional override speed
* @param drops the optional drops override
*
* @return the generated rule
*/
ToolRule forBlocks(List<BlockType> blocks, @Nullable Double speed, @Nullable Boolean drops);

/**
* Generates a rule for a block type tag. Optionally overriding speed and/or drops.
*
* @param blockTypeTag the block type tag
* @param speed the optional override speed
* @param drops the optional drops override
*
* @return the generated rule
*/
ToolRule forTag(Tag<BlockType> blockTypeTag, @Nullable Double speed, @Nullable Boolean drops);
}
}

0 comments on commit 36aaf56

Please sign in to comment.