Remove block moved to utils
This commit is contained in:
parent
06b4af59bc
commit
c4ecef5dcb
4 changed files with 82 additions and 57 deletions
|
@ -27,24 +27,22 @@ package techreborn.items.tool.industrial;
|
||||||
import net.fabricmc.api.EnvType;
|
import net.fabricmc.api.EnvType;
|
||||||
import net.fabricmc.api.Environment;
|
import net.fabricmc.api.Environment;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.block.Blocks;
|
|
||||||
import net.minecraft.client.item.TooltipContext;
|
import net.minecraft.client.item.TooltipContext;
|
||||||
import reborncore.common.util.StringUtils;
|
|
||||||
import net.minecraft.entity.LivingEntity;
|
import net.minecraft.entity.LivingEntity;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.ItemGroup;
|
import net.minecraft.item.ItemGroup;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.item.Items;
|
import net.minecraft.item.Items;
|
||||||
import net.minecraft.item.ToolMaterials;
|
import net.minecraft.item.ToolMaterials;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
|
||||||
import net.minecraft.tag.BlockTags;
|
import net.minecraft.tag.BlockTags;
|
||||||
import net.minecraft.text.LiteralText;
|
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.util.*;
|
import net.minecraft.util.ActionResult;
|
||||||
|
import net.minecraft.util.DefaultedList;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
|
import net.minecraft.util.TypedActionResult;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.math.Direction;
|
import net.minecraft.util.math.Direction;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import reborncore.common.util.ChatUtils;
|
|
||||||
import reborncore.common.util.ItemUtils;
|
import reborncore.common.util.ItemUtils;
|
||||||
import team.reborn.energy.Energy;
|
import team.reborn.energy.Energy;
|
||||||
import techreborn.config.TechRebornConfig;
|
import techreborn.config.TechRebornConfig;
|
||||||
|
@ -53,6 +51,7 @@ import techreborn.items.tool.ChainsawItem;
|
||||||
import techreborn.utils.InitUtils;
|
import techreborn.utils.InitUtils;
|
||||||
import techreborn.utils.MessageIDs;
|
import techreborn.utils.MessageIDs;
|
||||||
import techreborn.utils.TagUtils;
|
import techreborn.utils.TagUtils;
|
||||||
|
import techreborn.utils.ToolsUtil;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -93,35 +92,19 @@ public class IndustrialChainsawItem extends ChainsawItem {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void breakBlock(BlockPos pos, ItemStack stack, World world, LivingEntity entityLiving, BlockPos oldPos) {
|
|
||||||
if (oldPos == pos) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Energy.of(stack).use(cost)){
|
|
||||||
BlockState blockState = world.getBlockState(pos);
|
|
||||||
if (blockState.getHardness(world, pos) == -1.0F) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(!(entityLiving instanceof PlayerEntity)){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
blockState.getBlock().afterBreak(world, (PlayerEntity) entityLiving, pos, blockState, world.getBlockEntity(pos), stack);
|
|
||||||
world.setBlockState(pos, Blocks.AIR.getDefaultState());
|
|
||||||
world.removeBlockEntity(pos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//ChainsawItem
|
//ChainsawItem
|
||||||
@Override
|
@Override
|
||||||
public boolean postMine(ItemStack stack, World worldIn, BlockState blockIn, BlockPos pos, LivingEntity entityLiving) {
|
public boolean postMine(ItemStack stack, World worldIn, BlockState blockIn, BlockPos pos, LivingEntity entityLiving) {
|
||||||
List<BlockPos> wood = new ArrayList<>();
|
List<BlockPos> wood = new ArrayList<>();
|
||||||
|
List<BlockPos> leaves = new ArrayList<>();
|
||||||
if (ItemUtils.isActive(stack)) {
|
if (ItemUtils.isActive(stack)) {
|
||||||
findWood(worldIn, pos, wood, new ArrayList<>());
|
findWood(worldIn, pos, wood, leaves);
|
||||||
|
wood.remove(pos);
|
||||||
wood.stream()
|
wood.stream()
|
||||||
.filter(p -> Energy.of(stack).use(cost))
|
.filter(p -> Energy.of(stack).simulate().use(cost))
|
||||||
.forEach(pos1 -> breakBlock(pos1, stack, worldIn, entityLiving, pos));
|
.forEach(pos1 -> ToolsUtil.breakBlock(stack, worldIn, pos1, entityLiving, cost));
|
||||||
|
leaves.remove(pos);
|
||||||
|
leaves.forEach(pos1 -> ToolsUtil.breakBlock(stack, worldIn, pos1, entityLiving, 0));
|
||||||
}
|
}
|
||||||
return super.postMine(stack, worldIn, blockIn, pos, entityLiving);
|
return super.postMine(stack, worldIn, blockIn, pos, entityLiving);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,24 +30,23 @@ import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.block.Blocks;
|
import net.minecraft.block.Blocks;
|
||||||
import net.minecraft.block.Material;
|
import net.minecraft.block.Material;
|
||||||
import net.minecraft.client.item.TooltipContext;
|
import net.minecraft.client.item.TooltipContext;
|
||||||
import reborncore.common.util.StringUtils;
|
|
||||||
import net.minecraft.entity.LivingEntity;
|
import net.minecraft.entity.LivingEntity;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.ItemGroup;
|
import net.minecraft.item.ItemGroup;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.item.Items;
|
import net.minecraft.item.Items;
|
||||||
import net.minecraft.item.ToolMaterials;
|
import net.minecraft.item.ToolMaterials;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
|
||||||
import net.minecraft.text.LiteralText;
|
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.util.*;
|
import net.minecraft.util.ActionResult;
|
||||||
|
import net.minecraft.util.DefaultedList;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
|
import net.minecraft.util.TypedActionResult;
|
||||||
import net.minecraft.util.hit.BlockHitResult;
|
import net.minecraft.util.hit.BlockHitResult;
|
||||||
import net.minecraft.util.hit.HitResult;
|
import net.minecraft.util.hit.HitResult;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.math.Direction;
|
import net.minecraft.util.math.Direction;
|
||||||
import net.minecraft.world.RayTraceContext;
|
import net.minecraft.world.RayTraceContext;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import reborncore.common.util.ChatUtils;
|
|
||||||
import reborncore.common.util.ItemUtils;
|
import reborncore.common.util.ItemUtils;
|
||||||
import team.reborn.energy.Energy;
|
import team.reborn.energy.Energy;
|
||||||
import techreborn.config.TechRebornConfig;
|
import techreborn.config.TechRebornConfig;
|
||||||
|
@ -55,6 +54,7 @@ import techreborn.init.TRContent;
|
||||||
import techreborn.items.tool.DrillItem;
|
import techreborn.items.tool.DrillItem;
|
||||||
import techreborn.utils.InitUtils;
|
import techreborn.utils.InitUtils;
|
||||||
import techreborn.utils.MessageIDs;
|
import techreborn.utils.MessageIDs;
|
||||||
|
import techreborn.utils.ToolsUtil;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -71,11 +71,12 @@ public class IndustrialDrillItem extends DrillItem {
|
||||||
this.transferLimit = 1000;
|
this.transferLimit = 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<BlockPos> getTargetBlocks(World worldIn, BlockPos pos, @Nullable PlayerEntity playerIn) {
|
private Set<BlockPos> getTargetBlocks(World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving) {
|
||||||
Set<BlockPos> targetBlocks = new HashSet<>();
|
Set<BlockPos> targetBlocks = new HashSet<>();
|
||||||
if (playerIn == null) {
|
if (!(entityLiving instanceof PlayerEntity) || entityLiving == null) {
|
||||||
return new HashSet<>();
|
return new HashSet<>();
|
||||||
}
|
}
|
||||||
|
PlayerEntity playerIn = (PlayerEntity) entityLiving;
|
||||||
|
|
||||||
//Put a dirt block down to raytrace with to stop it raytracing past the intended block
|
//Put a dirt block down to raytrace with to stop it raytracing past the intended block
|
||||||
worldIn.setBlockState(pos, Blocks.DIRT.getDefaultState());
|
worldIn.setBlockState(pos, Blocks.DIRT.getDefaultState());
|
||||||
|
@ -117,16 +118,6 @@ public class IndustrialDrillItem extends DrillItem {
|
||||||
return targetBlocks;
|
return targetBlocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void breakBlock(BlockPos pos, World world, PlayerEntity playerIn, ItemStack drill) {
|
|
||||||
BlockState blockState = world.getBlockState(pos);
|
|
||||||
|
|
||||||
if(Energy.of(drill).use(cost)){
|
|
||||||
blockState.getBlock().onBlockRemoved(blockState, world, pos, blockState, true);
|
|
||||||
blockState.getBlock().afterBreak(world, playerIn, pos, blockState, world.getBlockEntity(pos), drill);
|
|
||||||
world.setBlockState(pos, Blocks.AIR.getDefaultState());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean shouldBreak(PlayerEntity playerIn, World worldIn, BlockPos originalPos, BlockPos pos) {
|
private boolean shouldBreak(PlayerEntity playerIn, World worldIn, BlockPos originalPos, BlockPos pos) {
|
||||||
if (originalPos.equals(pos)) {
|
if (originalPos.equals(pos)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -148,17 +139,13 @@ public class IndustrialDrillItem extends DrillItem {
|
||||||
|
|
||||||
// DrillItem
|
// DrillItem
|
||||||
@Override
|
@Override
|
||||||
public boolean postMine(ItemStack stack, World worldIn, BlockState blockIn, BlockPos pos, LivingEntity entityLiving) {
|
public boolean postMine(ItemStack stack, World worldIn, BlockState stateIn, BlockPos pos, LivingEntity entityLiving) {
|
||||||
PlayerEntity playerIn = null;
|
|
||||||
if ((entityLiving instanceof PlayerEntity)) {
|
|
||||||
playerIn = (PlayerEntity) entityLiving;
|
|
||||||
}
|
|
||||||
if(ItemUtils.isActive(stack)){
|
if(ItemUtils.isActive(stack)){
|
||||||
for (BlockPos additionalPos : getTargetBlocks(worldIn, pos, playerIn)) {
|
for (BlockPos additionalPos : getTargetBlocks(worldIn, pos, entityLiving)) {
|
||||||
breakBlock(additionalPos, worldIn, playerIn, stack);
|
ToolsUtil.breakBlock(stack, worldIn, additionalPos, entityLiving, cost);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return super.postMine(stack, worldIn, blockIn, pos, entityLiving);
|
return super.postMine(stack, worldIn, stateIn, pos, entityLiving);
|
||||||
}
|
}
|
||||||
|
|
||||||
// PickaxeItem
|
// PickaxeItem
|
||||||
|
|
|
@ -36,16 +36,13 @@ import net.minecraft.entity.attribute.EntityAttributes;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.*;
|
import net.minecraft.item.*;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.text.LiteralText;
|
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.util.*;
|
import net.minecraft.util.*;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import reborncore.api.items.ItemStackModifiers;
|
import reborncore.api.items.ItemStackModifiers;
|
||||||
import reborncore.common.powerSystem.PowerSystem;
|
import reborncore.common.powerSystem.PowerSystem;
|
||||||
import reborncore.common.util.ChatUtils;
|
|
||||||
import reborncore.common.util.ItemDurabilityExtensions;
|
import reborncore.common.util.ItemDurabilityExtensions;
|
||||||
import reborncore.common.util.ItemUtils;
|
import reborncore.common.util.ItemUtils;
|
||||||
import reborncore.common.util.StringUtils;
|
|
||||||
import team.reborn.energy.Energy;
|
import team.reborn.energy.Energy;
|
||||||
import team.reborn.energy.EnergyHolder;
|
import team.reborn.energy.EnergyHolder;
|
||||||
import team.reborn.energy.EnergySide;
|
import team.reborn.energy.EnergySide;
|
||||||
|
|
58
src/main/java/techreborn/utils/ToolsUtil.java
Normal file
58
src/main/java/techreborn/utils/ToolsUtil.java
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
/*
|
||||||
|
* This file is part of TechReborn, licensed under the MIT License (MIT).
|
||||||
|
*
|
||||||
|
* Copyright (c) 2018 TechReborn
|
||||||
|
*
|
||||||
|
* 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 techreborn.utils;
|
||||||
|
|
||||||
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.block.Blocks;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import team.reborn.energy.Energy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author drcrazy
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ToolsUtil {
|
||||||
|
public static void breakBlock(ItemStack tool, World world, BlockPos pos, LivingEntity entityLiving, int cost) {
|
||||||
|
if (!(entityLiving instanceof PlayerEntity)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!Energy.of(tool).use(cost)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
BlockState blockState = world.getBlockState(pos);
|
||||||
|
if (blockState.getHardness(world, pos) == -1.0F) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
blockState.getBlock().afterBreak(world, (PlayerEntity) entityLiving, pos, blockState, world.getBlockEntity(pos), tool);
|
||||||
|
world.setBlockState(pos, Blocks.AIR.getDefaultState());
|
||||||
|
world.removeBlockEntity(pos);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue