speedpowderaxe works

This commit is contained in:
Jörg Henke 2020-07-30 22:04:12 +02:00
parent f52175c1c4
commit 77bf438714
3 changed files with 151 additions and 168 deletions

View File

@ -1,16 +1,8 @@
package de.jottyfan.minecraft.quickiefabric;
import java.util.ArrayList;
import java.util.List;
import de.jottyfan.minecraft.quickiefabric.blocks.QuickieBlocks;
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
import net.fabricmc.api.ModInitializer;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.feature.OreFeatureConfig;
/**
*
@ -25,31 +17,6 @@ public class QuickieFabric implements ModInitializer {
RegistryManager.registerTools();
RegistryManager.registerEvents();
RegistryManager.registerBlocks();
Registry.BIOME.forEach(this::handleBiome);
}
/**
* add the quickiefabric ores to the biome
*
* @param biome
* the biome
*/
private void handleBiome(Biome biome) {
if (biome.getCategory() == Biome.Category.NETHER) {
RegistryManager.generateOreForTarget(biome, OreFeatureConfig.Target.NETHER_ORE_REPLACEABLES, QuickieBlocks.ORE_NETHER_SULPHOR, 24, 10, 0, 0, 128);
} else if (biome.getCategory() != Biome.Category.THEEND) {
} else {
List<BlockState> sandlike = new ArrayList<>();
sandlike.add(Blocks.SAND.getDefaultState());
sandlike.add(Blocks.SANDSTONE.getDefaultState());
sandlike.add(Blocks.SANDSTONE_WALL.getDefaultState());
sandlike.add(Blocks.CHISELED_SANDSTONE.getDefaultState());
List<BlockState> dirtlike = new ArrayList<>();
dirtlike.add(Blocks.DIRT.getDefaultState());
RegistryManager.generateOreForBlocks(biome, sandlike, sandlike, sandlike, QuickieBlocks.SAND_SALPETER, 10, 64, 196, 255);
RegistryManager.generateOreForBlocks(biome, sandlike, sandlike, sandlike, QuickieBlocks.ORE_SAND_SALPETER, 10, 64, 196, 255);
RegistryManager.generateOreForTarget(biome, OreFeatureConfig.Target.NATURAL_STONE, QuickieBlocks.ORE_SULPHOR, 16, 4, 4, 196, 255);
RegistryManager.generateOreForTarget(biome, OreFeatureConfig.Target.NATURAL_STONE, QuickieBlocks.ORE_SALPETER, 12, 10, 4, 196, 255);
}
Registry.BIOME.forEach(RegistryManager::handleBiome);
}
}

View File

@ -1,5 +1,23 @@
package de.jottyfan.minecraft.quickiefabric.event;
import java.util.ArrayList;
import java.util.List;
import de.jottyfan.minecraft.quickiefabric.tools.HarvestRange;
import de.jottyfan.minecraft.quickiefabric.tools.QuickieTools;
import de.jottyfan.minecraft.quickiefabric.tools.ToolRangeable;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.ExperienceOrbEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
/**
*
* @author jotty
@ -11,123 +29,104 @@ public class EventBlockBreak {
UPWARDS, ALL;
}
// /**
// * TODO: use AttackBlockCallback instead?
// * @param event
// */
// @SubscribeEvent
// public void doBreakBlock(BlockEvent.BreakEvent event) {
// PlayerEntity player = event.getPlayer();
// ItemStack mainHandItemStack = player.getHeldItemMainhand();
// if (mainHandItemStack != null) {
// Item item = mainHandItemStack.getItem();
// if (item instanceof ToolRangeable) {
// CompoundNBT nbt = mainHandItemStack.getTag();
// Integer level = nbt != null ? nbt.getInt("level") : 0;
// ToolRangeable tool = (ToolRangeable) item;
// BlockPos pos = event.getPos();
// World world = event.getWorld().getWorld();
// BlockState blockState = world.getBlockState(pos);
// Block block = blockState.getBlock();
// handleRangeableTools(tool, level, world, block, pos, player);
// }
// }
// }
//
// /**
// * hande the rangeable tools break event
// *
// * @param tool
// * the tool that has been used
// * @param world
// * the world
// * @param block
// * the block to break
// * @param pos
// * the position of the current block
// * @param player
// * the current player
// */
// private void handleRangeableTools(ToolRangeable tool, Integer level, World world, Block currentBlock, BlockPos pos,
// PlayerEntity player) {
// List<Block> validBlocks = tool.getBlockList(currentBlock);
// HarvestRange range = tool.getRange();
// if (range != null) {
// range = range.addXYZ(level);
// }
// if (RegistryManager.AXE_SPEEDPOWDER.equals(tool)) {
// breakBlockRecursive(new ArrayList<>(), world, validBlocks, pos, tool, range, BlockBreakDirection.UPWARDS, player);
// } else if (RegistryManager.PICKAXE_SPEEDPOWDER.equals(tool)) {
// breakBlockRecursive(new ArrayList<>(), world, validBlocks, pos, tool, range, BlockBreakDirection.ALL, player);
// } else if (RegistryManager.SHOVEL_SPEEDPOWDER.equals(tool)) {
// breakBlockRecursive(new ArrayList<>(), world, validBlocks, pos, tool, range, BlockBreakDirection.ALL, player);
// }
// }
//
// /**
// * break block recursively;
// *
// * @param visitedBlocks
// * the positions of visited blocks
// * @param world
// * the world
// * @param validBlocks
// * the blocks to break
// * @param tool
// * the tool used
// * @param range
// * the range left over
// * @param pos
// * the position
// * @param blockBreakDirection
// * the direction for the recursive call
// * @param player
// * the player
// */
// private void breakBlockRecursive(List<String> visitedBlocks, World world, List<Block> validBlocks, BlockPos pos,
// ToolRangeable tool, HarvestRange range, BlockBreakDirection blockBreakDirection, PlayerEntity player) {
// if (visitedBlocks.contains(pos.toString())) {
// return; // reduce loops
// } else if (validBlocks == null) {
// return; // reduce loops
// } else {
// visitedBlocks.add(pos.toString());
// }
// BlockState blockState = world.getBlockState(pos);
// if (tool.canBreakNeigbbors(blockState)) {
// Block currentBlock = blockState.getBlock();
// if (validBlocks.contains(currentBlock)) {
// world.setBlockState(pos, Blocks.AIR.getDefaultState());
// Block.spawnDrops(blockState, world, pos);
// if (range == null || range.getxRange() > 1 || range.getyRange() > 1 || range.getzRange() > 1) {
// HarvestRange nextRadius = range == null ? null : range.addXYZ(-1);
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.north(), tool, nextRadius, blockBreakDirection,
// player);
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.north().east(), tool, nextRadius,
// blockBreakDirection, player);
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.north().west(), tool, nextRadius,
// blockBreakDirection, player);
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.south(), tool, nextRadius, blockBreakDirection,
// player);
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.south().east(), tool, nextRadius,
// blockBreakDirection, player);
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.south().west(), tool, nextRadius,
// blockBreakDirection, player);
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.east(), tool, nextRadius, blockBreakDirection,
// player);
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.west(), tool, nextRadius, blockBreakDirection,
// player);
//
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.up(), tool, nextRadius, blockBreakDirection,
// player);
//
// if (BlockBreakDirection.ALL.equals(blockBreakDirection)) {
// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.down(), tool, nextRadius, blockBreakDirection,
// player);
// }
// }
// }
// }
// }
public void doBreakBlock(World world, BlockPos blockPos, BlockState blockState, PlayerEntity playerEntity) {
ItemStack mainHandItemStack = playerEntity.getEquippedStack(EquipmentSlot.MAINHAND);
if (mainHandItemStack != null) {
Item item = mainHandItemStack.getItem();
if (item instanceof ToolRangeable) {
CompoundTag nbt = mainHandItemStack.getTag();
Integer level = nbt != null ? nbt.getInt("level") : 0;
ToolRangeable tool = (ToolRangeable) item;
Block block = blockState.getBlock();
handleRangeableTools(tool, level, world, block, blockPos, playerEntity);
world.spawnEntity(new ExperienceOrbEntity(world, blockPos.getX(), blockPos.getY(), blockPos.getZ(), 1)); // reward for using rangeable tool
}
}
}
/**
* hande the rangeable tools break event
*
* @param tool
* the tool that has been used
* @param world
* the world
* @param block
* the block to break
* @param pos
* the position of the current block
* @param player
* the current player
*/
private void handleRangeableTools(ToolRangeable tool, Integer level, World world, Block currentBlock, BlockPos pos, PlayerEntity player) {
List<Block> validBlocks = tool.getBlockList(currentBlock);
HarvestRange range = tool.getRange();
if (range != null) {
range = range.addXYZ(level);
}
if (QuickieTools.SPEEDPOWDERAXE.equals(tool)) {
breakBlockRecursive(new ArrayList<>(), world, validBlocks, pos, tool, range, BlockBreakDirection.UPWARDS, player);
} else if (QuickieTools.SPEEDPOWDERPICKAXE.equals(tool)) {
breakBlockRecursive(new ArrayList<>(), world, validBlocks, pos, tool, range, BlockBreakDirection.ALL, player);
} else if (QuickieTools.SPEEDPOWDERSHOVEL.equals(tool)) {
breakBlockRecursive(new ArrayList<>(), world, validBlocks, pos, tool, range, BlockBreakDirection.ALL, player);
}
}
/**
* break block recursively;
*
* @param visitedBlocks
* the positions of visited blocks
* @param world
* the world
* @param validBlocks
* the blocks to break
* @param tool
* the tool used
* @param range
* the range left over
* @param pos
* the position
* @param blockBreakDirection
* the direction for the recursive call
* @param player
* the player
*/
private void breakBlockRecursive(List<String> visitedBlocks, World world, List<Block> validBlocks, BlockPos pos, ToolRangeable tool, HarvestRange range, BlockBreakDirection blockBreakDirection,
PlayerEntity player) {
if (visitedBlocks.contains(pos.toString())) {
return; // reduce loops
} else if (validBlocks == null) {
return; // reduce loops
} else {
visitedBlocks.add(pos.toString());
}
BlockState blockState = world.getBlockState(pos);
if (tool.canBreakNeigbbors(blockState)) {
Block currentBlock = blockState.getBlock();
if (validBlocks.contains(currentBlock)) {
Block.dropStacks(blockState, world, pos);
world.setBlockState(pos, Blocks.AIR.getDefaultState());
currentBlock.onStacksDropped(blockState, world, pos, stack);
if (range == null || range.getxRange() > 1 || range.getyRange() > 1 || range.getzRange() > 1) {
HarvestRange nextRadius = range == null ? null : range.addXYZ(-1);
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.north(), tool, nextRadius, blockBreakDirection, player);
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.north().east(), tool, nextRadius, blockBreakDirection, player);
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.north().west(), tool, nextRadius, blockBreakDirection, player);
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.south(), tool, nextRadius, blockBreakDirection, player);
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.south().east(), tool, nextRadius, blockBreakDirection, player);
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.south().west(), tool, nextRadius, blockBreakDirection, player);
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.east(), tool, nextRadius, blockBreakDirection, player);
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.west(), tool, nextRadius, blockBreakDirection, player);
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.up(), tool, nextRadius, blockBreakDirection, player);
if (BlockBreakDirection.ALL.equals(blockBreakDirection)) {
breakBlockRecursive(visitedBlocks, world, validBlocks, pos.down(), tool, nextRadius, blockBreakDirection, player);
}
}
}
}
}
}

View File

@ -1,5 +1,6 @@
package de.jottyfan.minecraft.quickiefabric.init;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
@ -7,6 +8,7 @@ import org.apache.logging.log4j.Logger;
import de.jottyfan.minecraft.quickiefabric.blocks.QuickieBlocks;
import de.jottyfan.minecraft.quickiefabric.event.BreakBlockCallback;
import de.jottyfan.minecraft.quickiefabric.event.EventBlockBreak;
import de.jottyfan.minecraft.quickiefabric.items.QuickieItems;
import de.jottyfan.minecraft.quickiefabric.tools.QuickieTools;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
@ -82,26 +84,14 @@ public class RegistryManager {
public static final void registerTools() {
LOGGER.debug("registering quickiefabric tools");
registerItem(QuickieTools.SPEEDPOWDERAXE, "speedpowderaxe");
registerItem(QuickieTools.SPEEDPOWDERPICKAXE, "speedpowderpickaxe");
registerItem(QuickieTools.SPEEDPOWDERSHOVEL, "speedpowdershovel");
}
public static final void registerEvents() {
LOGGER.debug("registering quickiefabric events");
BreakBlockCallback.EVENT.register((world, blockPos, blockState, playerEntity) -> {
// TODO: add code to break the corresponding surroundings also if hand hold the right tool
// return ActionResult.PASS; // if the breaking replaces another event, but this does not appear for the speedpowder tools
LOGGER.info("{} broke block {} at {}", playerEntity.getName().asString(), blockState.getBlock().getTranslationKey(), blockPos.toString());
BlockState blockStateDown = world.getBlockState(blockPos);
if (!blockStateDown.isAir()) { // TODO: because we will only break the same blocks, that check becomes obsolete later
ItemStack itemStack = new ItemStack(blockStateDown.getBlock().asItem());
// spawn entity in world
Block.dropStack(world, blockPos.down(), itemStack);
world.setBlockState(blockPos.down(), Blocks.AIR.getDefaultState());
LOGGER.info("also broke block {} at {}", playerEntity.getName().asString(), blockState.getBlock().getTranslationKey(), blockPos.down().toString());
}
new EventBlockBreak().doBreakBlock(world, blockPos, blockState, playerEntity);
return ActionResult.SUCCESS;
});
}
@ -132,4 +122,31 @@ public class RegistryManager {
// ConfiguredPlacement<CountRangeConfig> placement = Placement.COUNT_RANGE.configure(rdc);
// biome.addFeature(GenerationStep.Feature.UNDERGROUND_ORES, Feature.SIMPLE_BLOCK.configure(config).withPlacement(placement));
}
/**
* add the quickiefabric ores to the biome
*
* @param biome
* the biome
*/
public static final void handleBiome(Biome biome) {
if (biome.getCategory() == Biome.Category.NETHER) {
RegistryManager.generateOreForTarget(biome, OreFeatureConfig.Target.NETHER_ORE_REPLACEABLES, QuickieBlocks.ORE_NETHER_SULPHOR, 24, 10, 0, 0, 128);
} else if (biome.getCategory() != Biome.Category.THEEND) {
} else {
List<BlockState> sandlike = new ArrayList<>();
sandlike.add(Blocks.SAND.getDefaultState());
sandlike.add(Blocks.SANDSTONE.getDefaultState());
sandlike.add(Blocks.SANDSTONE_WALL.getDefaultState());
sandlike.add(Blocks.CHISELED_SANDSTONE.getDefaultState());
List<BlockState> dirtlike = new ArrayList<>();
dirtlike.add(Blocks.DIRT.getDefaultState());
RegistryManager.generateOreForBlocks(biome, sandlike, sandlike, sandlike, QuickieBlocks.SAND_SALPETER, 10, 64, 196, 255);
RegistryManager.generateOreForBlocks(biome, sandlike, sandlike, sandlike, QuickieBlocks.ORE_SAND_SALPETER, 10, 64, 196, 255);
RegistryManager.generateOreForTarget(biome, OreFeatureConfig.Target.NATURAL_STONE, QuickieBlocks.ORE_SULPHOR, 16, 4, 4, 196, 255);
RegistryManager.generateOreForTarget(biome, OreFeatureConfig.Target.NATURAL_STONE, QuickieBlocks.ORE_SALPETER, 12, 10, 4, 196, 255);
}
}
}