rangeable tools independent from mods

This commit is contained in:
Jörg Henke 2021-11-20 20:12:32 +01:00
parent f54b78597a
commit 72ef6c5a07
11 changed files with 13 additions and 386 deletions

View File

@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.12.5
# Mod Properties
mod_version = 1.17.1.5
mod_version = 1.17.1.6
maven_group = de.jottyfan.minecraft
archives_base_name = quickiefabric

View File

@ -31,7 +31,7 @@ public class BlockCottonplant extends CropBlock {
DefaultedList<ItemStack> list = DefaultedList.of();
list.add(new ItemStack(getSeedsItem())); // the one from the seed
if (isMature(state)) {
list.add(new ItemStack(getSeedsItem(), new Random().nextInt(3)));
list.add(new ItemStack(getSeedsItem(), new Random().nextInt(2)));
list.add(new ItemStack(QuickieItems.COTTON, new Random().nextFloat() > 0.9f ? 2 : 1));
}
return list;
@ -43,7 +43,6 @@ public class BlockCottonplant extends CropBlock {
list.add(itemStack);
});
ItemScatterer.spawn(world, pos, list);
}
@Override

View File

@ -1,22 +1,10 @@
package de.jottyfan.minecraft.quickiefabric.tools;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import de.jottyfan.minecraft.quickiefabric.blocks.QuickieBlocks;
import de.jottyfan.minecraft.quickiefabric.tools.externalmods.Byg;
import de.jottyfan.minecraft.quickiefabric.tools.externalmods.IdentifierGroup;
import de.jottyfan.minecraft.quickiefabric.tools.externalmods.Terrestria;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
/**
*
@ -24,81 +12,6 @@ import net.minecraft.util.Identifier;
*
*/
public interface ToolRangeable {
public static final Set<Block> SHOVEL_EFFECTIVE_ON = Sets
.newHashSet(new Block[] { Blocks.GRAVEL, Blocks.SAND, Blocks.GRASS_BLOCK, Blocks.DIRT, Blocks.CLAY,
Blocks.FARMLAND, Blocks.DIRT_PATH, Blocks.RED_SAND, Blocks.SOUL_SAND });
public static final Set<Identifier> SHOVEL_EXTERNAL_EFFECTIVE_ON = mergeSets(
Byg.createFromStrings("black_sand", "white_sand", "peat", "meadow_dirt", "mud_block", "blue_sand", "purple_sand"),
Terrestria.createFromStrings("basalt_sand"));
public static final Set<Block> HOE_EFFECTIVE_ON = Sets.newHashSet(new Block[] { Blocks.BAMBOO, Blocks.BAMBOO_SAPLING,
Blocks.ATTACHED_MELON_STEM, Blocks.ATTACHED_PUMPKIN_STEM, Blocks.SUNFLOWER, Blocks.CORNFLOWER, Blocks.SEAGRASS,
Blocks.TALL_GRASS, Blocks.TALL_SEAGRASS, Blocks.GRASS, Blocks.KELP_PLANT, Blocks.KELP });
public static final Set<Identifier> HOE_EXTERNAL_EFFECTIVE_ON = mergeSets();
public static final List<List<Block>> AXE_EFFECTIVE_ON = Lists.newArrayList(
Arrays.asList(Blocks.ACACIA_LOG, Blocks.STRIPPED_ACACIA_LOG, Blocks.ACACIA_WOOD, Blocks.STRIPPED_ACACIA_WOOD),
Arrays.asList(Blocks.BIRCH_LOG, Blocks.STRIPPED_BIRCH_LOG, Blocks.BIRCH_WOOD, Blocks.STRIPPED_BIRCH_WOOD),
Arrays.asList(Blocks.DARK_OAK_LOG, Blocks.STRIPPED_DARK_OAK_LOG, Blocks.DARK_OAK_WOOD,
Blocks.STRIPPED_DARK_OAK_WOOD),
Arrays.asList(Blocks.JUNGLE_LOG, Blocks.STRIPPED_JUNGLE_LOG, Blocks.JUNGLE_WOOD, Blocks.STRIPPED_JUNGLE_WOOD),
Arrays.asList(Blocks.OAK_LOG, Blocks.STRIPPED_OAK_LOG, Blocks.OAK_WOOD, Blocks.STRIPPED_OAK_WOOD),
Arrays.asList(Blocks.SPRUCE_LOG, Blocks.STRIPPED_SPRUCE_LOG, Blocks.SPRUCE_WOOD, Blocks.STRIPPED_SPRUCE_WOOD),
Arrays.asList(Blocks.ACACIA_PLANKS), Arrays.asList(Blocks.BIRCH_PLANKS), Arrays.asList(Blocks.DARK_OAK_PLANKS),
Arrays.asList(Blocks.JUNGLE_PLANKS), Arrays.asList(Blocks.OAK_PLANKS), Arrays.asList(Blocks.SPRUCE_PLANKS),
Arrays.asList(Blocks.ACACIA_SLAB), Arrays.asList(Blocks.BIRCH_SLAB), Arrays.asList(Blocks.DARK_OAK_SLAB),
Arrays.asList(Blocks.JUNGLE_SLAB), Arrays.asList(Blocks.OAK_SLAB), Arrays.asList(Blocks.SPRUCE_SLAB),
Arrays.asList(Blocks.ACACIA_STAIRS), Arrays.asList(Blocks.BIRCH_STAIRS), Arrays.asList(Blocks.DARK_OAK_STAIRS),
Arrays.asList(Blocks.JUNGLE_STAIRS), Arrays.asList(Blocks.OAK_STAIRS), Arrays.asList(Blocks.SPRUCE_STAIRS),
Arrays.asList(Blocks.POTTED_BROWN_MUSHROOM, Blocks.BROWN_MUSHROOM_BLOCK, Blocks.BROWN_MUSHROOM,
Blocks.MUSHROOM_STEM),
Arrays.asList(Blocks.POTTED_RED_MUSHROOM, Blocks.RED_MUSHROOM_BLOCK, Blocks.RED_MUSHROOM, Blocks.MUSHROOM_STEM));
public static final Set<IdentifierGroup> AXE_EFFECTIVE_ON_EXTERNAL = mergeGroupSets(
Terrestria.createFromSet(Terrestria.WOOD.getGroups("log", "quarter_log", "log_bare", "log_top", "wood", "")),
// only because terrestria leaves are not instanceof LeavesBlock
Terrestria.createFromSet(Terrestria.WOOD.getGroups("leaves")),
Byg.createFromSet(Byg.WOOD.getGroups("log", "wood")), Byg.createFromSet(Byg.MUSHROOM.getGroups("block", "stem")));
public static final Set<Block> PICKAXE_EFFECTIVE_ON = Sets
.newHashSet(new Block[] { Blocks.GLOWSTONE, QuickieBlocks.ORE_NETHER_SULPHOR, QuickieBlocks.ORE_SALPETER,
QuickieBlocks.ORE_SAND_SALPETER, QuickieBlocks.ORE_SULPHOR });
public static final Set<Identifier> PICKAXE_EXTERNAL_EFFECTIVE_ON = mergeSets(
Byg.createFromStrings("soapstone", "black_sandstone", "dacite", "rocky_stone", "red_rock", "pink_sandstone",
"purple_sandstone", "scoria_stone", "scoria_cobblestone"));
/**
* merge all sets
*
* @param sets the sets
* @return one merged set
*/
@SafeVarargs
public static Set<Identifier> mergeSets(Set<Identifier>... sets) {
Set<Identifier> result = new HashSet<>();
for (Set<Identifier> set : sets) {
result.addAll(set);
}
return result;
}
/**
* merge all group sets
*
* @param sets the sets
* @return one merged set
*/
@SafeVarargs
public static Set<IdentifierGroup> mergeGroupSets(Set<IdentifierGroup>... sets) {
Set<IdentifierGroup> result = new HashSet<>();
for (Set<IdentifierGroup> set : sets) {
result.addAll(set);
}
return result;
}
/**
* @param stack the item stack that keeps the range
@ -116,7 +29,7 @@ public interface ToolRangeable {
public boolean canBreakNeighbors(BlockState blockState);
/**
* get list of blocks that belong together
* get list of blocks that belong together (could be useful for stripped logs)
*
* @param block of the set
* @return the list of blocks or null if not found

View File

@ -4,15 +4,13 @@ import java.util.List;
import com.google.common.collect.Lists;
import de.jottyfan.minecraft.quickiefabric.tools.externalmods.IdentifierGroup;
import net.fabricmc.fabric.api.tool.attribute.v1.ToolManager;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.LeavesBlock;
import net.minecraft.item.AxeItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ToolMaterial;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
/**
*
@ -38,30 +36,12 @@ public class ToolRangeableAxe extends AxeItem implements ToolRangeable {
@Override
public boolean canBreakNeighbors(BlockState blockIn) {
return getBlockList(blockIn.getBlock()) != null;
return ToolManager.handleIsEffectiveOn(blockIn, new ItemStack(this), null)
|| blockIn.getBlock() instanceof LeavesBlock;
}
@Override
public List<Block> getBlockList(Block block) {
for (List<Block> blockList : AXE_EFFECTIVE_ON) {
if (blockList.contains(block)) {
return blockList;
}
}
for (IdentifierGroup externalIDs : AXE_EFFECTIVE_ON_EXTERNAL) {
for (Identifier externalID : externalIDs.getIdentifiers()) {
Block registeredBlock = Registry.BLOCK.get(externalID); // may be null if mods are not available
if (registeredBlock != null && registeredBlock.equals(block)) {
return Lists.newArrayList(block);
}
}
}
if (block instanceof LeavesBlock) {
return Lists.newArrayList(block);
}
if (super.isSuitableFor(block.getDefaultState())) {
return Lists.newArrayList(block);
}
return null;
}
}

View File

@ -2,12 +2,10 @@ package de.jottyfan.minecraft.quickiefabric.tools;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.common.collect.Lists;
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
import net.fabricmc.fabric.api.tool.attribute.v1.ToolManager;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.HoeItem;
@ -16,11 +14,9 @@ import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.ToolMaterials;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3i;
import net.minecraft.util.registry.Registry;
/**
*
@ -28,7 +24,6 @@ import net.minecraft.util.registry.Registry;
*
*/
public class ToolSpeedpowderHoe extends HoeItem implements ToolRangeable {
private static final Logger LOGGER = LogManager.getLogger(ToolSpeedpowderHoe.class);
public static final Integer DEFAULT_PLOW_RANGE = 4;
public HarvestRange range;
@ -63,23 +58,9 @@ public class ToolSpeedpowderHoe extends HoeItem implements ToolRangeable {
@Override
public boolean canBreakNeighbors(BlockState blockState) {
boolean result = super.isSuitableFor(blockState) || HOE_EFFECTIVE_ON.contains(blockState.getBlock()) || checkExternalBlock(blockState.getBlock());
if (!result) {
if (!blockState.isAir()) {
LOGGER.debug("cannot break block {} with that speedpowder hoe", Registry.BLOCK.getId(blockState.getBlock()));
}
}
return result;
return ToolManager.handleIsEffectiveOn(blockState, new ItemStack(this), null);
}
private boolean checkExternalBlock(Block block) {
boolean result = false;
for (Identifier externalID : HOE_EXTERNAL_EFFECTIVE_ON) {
Block registeredBlock = Registry.BLOCK.get(externalID); // may be null if mods are not available
result = result || (registeredBlock != null && registeredBlock.equals(block));
}
return result;
}
@Override
public List<Block> getBlockList(Block block) {

View File

@ -5,6 +5,7 @@ import java.util.List;
import com.google.common.collect.Lists;
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
import net.fabricmc.fabric.api.tool.attribute.v1.ToolManager;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;
@ -12,8 +13,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.item.PickaxeItem;
import net.minecraft.item.ToolMaterials;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
/**
*
@ -41,17 +40,7 @@ public class ToolSpeedpowderPickaxe extends PickaxeItem implements ToolRangeable
@Override
public boolean canBreakNeighbors(BlockState blockIn) {
return super.isSuitableFor(blockIn) || super.isSuitableFor(blockIn) || PICKAXE_EFFECTIVE_ON.contains(blockIn.getBlock())
|| checkExternalBlock(blockIn.getBlock());
}
private boolean checkExternalBlock(Block block) {
boolean result = false;
for (Identifier externalID : PICKAXE_EXTERNAL_EFFECTIVE_ON) {
Block registeredBlock = Registry.BLOCK.get(externalID); // may be null if mods are not available
result = result || (registeredBlock != null && registeredBlock.equals(block));
}
return result;
return ToolManager.handleIsEffectiveOn(blockIn, new ItemStack(this), null);
}
@Override

View File

@ -2,12 +2,10 @@ package de.jottyfan.minecraft.quickiefabric.tools;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.common.collect.Lists;
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
import net.fabricmc.fabric.api.tool.attribute.v1.ToolManager;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;
@ -16,10 +14,8 @@ import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.ShovelItem;
import net.minecraft.item.ToolMaterials;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
/**
@ -28,7 +24,6 @@ import net.minecraft.world.World;
*
*/
public class ToolSpeedpowderShovel extends ShovelItem implements ToolRangeable {
private static final Logger LOGGER = LogManager.getLogger(ToolSpeedpowderShovel.class);
public static final Integer DEFAULT_HARVEST_RANGE = 3;
public HarvestRange range;
@ -80,22 +75,7 @@ public class ToolSpeedpowderShovel extends ShovelItem implements ToolRangeable {
@Override
public boolean canBreakNeighbors(BlockState blockState) {
boolean result = super.isSuitableFor(blockState) || SHOVEL_EFFECTIVE_ON.contains(blockState.getBlock()) || checkExternalBlock(blockState.getBlock());
if (!result) {
if (!blockState.isAir()) {
LOGGER.debug("cannot break block {} with that speedpowder shovel", Registry.BLOCK.getId(blockState.getBlock()));
}
}
return result;
}
private boolean checkExternalBlock(Block block) {
boolean result = false;
for (Identifier externalID : SHOVEL_EXTERNAL_EFFECTIVE_ON) {
Block registeredBlock = Registry.BLOCK.get(externalID); // may be null if mods are not available
result = result || (registeredBlock != null && registeredBlock.equals(block));
}
return result;
return ToolManager.handleIsEffectiveOn(blockState, new ItemStack(this), null);
}
@Override

View File

@ -1,71 +0,0 @@
package de.jottyfan.minecraft.quickiefabric.tools.externalmods;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.util.Identifier;
/**
*
* @author jotty
*
*/
public class Byg extends Identifier {
public static final IdentifierGroups WOOD = new IdentifierGroups("aspen", "baobab", "blue_enchanted", "cherry",
"cika", "cypress", "ebony", "fir", "green_enchanted", "holly", "jacaranda", "mahogany", "mangrove", "pine",
"rainbow_eucalyptus", "redwood", "skyris", "willow", "witch_hazel", "zelkova");
public static final IdentifierGroups MUSHROOM = new IdentifierGroups("blue_glowshroom", "purple_glowshroom",
"red_glowshroom", "yellow_glowshroom");
/**
* create identifier preluded by byg (for biomes you'll go)
*
* @param id the id within byg
*/
public Byg(String id) {
super("byg", id);
}
/**
* create a new set of Byg Identifiers
*
* @param names the names
* @return a set of identifiers
*/
public static final Set<Identifier> createFromStrings(String... names) {
Set<Identifier> set = new HashSet<>();
for (String name : names) {
set.add(new Byg(name));
}
return set;
}
/**
* create from groups
*
* @param groups the groups
* @return the set of indentifier groups
*/
public static final Set<IdentifierGroup> createFromGroups(IdentifierGroup... groups) {
Set<IdentifierGroup> set = new HashSet<>();
for (IdentifierGroup group : groups) {
group.setModId("byg");
set.add(group);
}
return set;
}
/**
* create from set
*
* @param set the set
* @return the set of idenfifier groups
*/
public static final Set<IdentifierGroup> createFromSet(Set<IdentifierGroup> set) {
for (IdentifierGroup group : set) {
group.setModId("byg");
}
return set;
}
}

View File

@ -1,44 +0,0 @@
package de.jottyfan.minecraft.quickiefabric.tools.externalmods;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.util.Identifier;
/**
*
* @author jotty
*
*/
public class IdentifierGroup {
private final Set<String> names;
private String modId;
public IdentifierGroup(String prefix, String... suffix) {
this.names = new HashSet<>();
for (String s : suffix) {
names.add(new StringBuilder(prefix).append("_").append(s).toString());
}
}
/**
* get the identifiers for the mod
*
* @return the set of identifiers
*/
public Set<Identifier> getIdentifiers(){
Set<Identifier> set = new HashSet<>();
for (String name : names) {
set.add(new Identifier(modId, name));
}
return set;
}
public String getModId() {
return modId;
}
public void setModId(String modId) {
this.modId = modId;
}
}

View File

@ -1,31 +0,0 @@
package de.jottyfan.minecraft.quickiefabric.tools.externalmods;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author jotty
*
*/
public class IdentifierGroups {
private final Set<IdentifierGroup> groups;
private final String[] prefixes;
public IdentifierGroups(String... prefixes) {
this.prefixes = prefixes;
groups = new HashSet<>();
}
private void fillGroups(String... suffixes) {
for (String prefix : prefixes) {
groups.add(new IdentifierGroup(prefix, suffixes));
}
}
public Set<IdentifierGroup> getGroups(String... suffixes){
fillGroups(suffixes);
return groups;
}
}

View File

@ -1,69 +0,0 @@
package de.jottyfan.minecraft.quickiefabric.tools.externalmods;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.util.Identifier;
/**
*
* @author jotty
*
*/
public class Terrestria extends Identifier {
public static final IdentifierGroups WOOD = new IdentifierGroups("cypress", "hemlock", "japanese_maple",
"rainbow_eucalyptus", "redwood", "rubber", "sakura", "willow", "yucca_palm");
/**
* create identifier preluded by terrestria
*
* @param id the id within terrestria
*/
public Terrestria(String id) {
super("terrestria", id);
}
/**
* create a new set of Terrestria Identifiers
*
* @param names the names
* @return a set of identifiers
*/
public static final Set<Identifier> createFromStrings(String... names) {
Set<Identifier> set = new HashSet<>();
for (String name : names) {
set.add(new Terrestria(name));
}
return set;
}
/**
* create from groups
*
* @param groups the groups
* @return the list of indentifier groups
*/
public static final Set<IdentifierGroup> createFromGroups(IdentifierGroup... groups) {
Set<IdentifierGroup> set = new HashSet<>();
for (IdentifierGroup group : groups) {
group.setModId("terrestria");
set.add(group);
}
return set;
}
/**
* create from set
*
* @param set the set
* @return the set of idenfifier groups
*/
public static final Set<IdentifierGroup> createFromSet(Set<IdentifierGroup> set) {
for (IdentifierGroup group : set) {
group.setModId("terrestria");
}
return set;
}
}