Created
March 9, 2020 16:02
-
-
Save Lanse505/b246826460ebf7fdbf12b0d31fbb3cbf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.teamacronymcoders.essence.blocks; | |
| import com.hrznstudio.titanium.block.BasicBlock; | |
| import com.teamacronymcoders.essence.Essence; | |
| import com.teamacronymcoders.essence.utils.helpers.EssenceColorHelper; | |
| import net.minecraft.block.Block; | |
| import net.minecraft.block.BlockState; | |
| import net.minecraft.block.material.Material; | |
| import net.minecraft.block.material.MaterialColor; | |
| import net.minecraft.entity.item.ItemEntity; | |
| import net.minecraft.entity.player.PlayerEntity; | |
| import net.minecraft.item.BlockItemUseContext; | |
| import net.minecraft.item.DyeColor; | |
| import net.minecraft.item.ItemStack; | |
| import net.minecraft.nbt.CompoundNBT; | |
| import net.minecraft.state.EnumProperty; | |
| import net.minecraft.state.StateContainer; | |
| import net.minecraft.tags.ItemTags; | |
| import net.minecraft.util.ActionResultType; | |
| import net.minecraft.util.Hand; | |
| import net.minecraft.util.ResourceLocation; | |
| import net.minecraft.util.math.BlockPos; | |
| import net.minecraft.util.math.BlockRayTraceResult; | |
| import net.minecraft.util.math.RayTraceResult; | |
| import net.minecraft.world.IBlockReader; | |
| import net.minecraft.world.World; | |
| import net.minecraftforge.common.ToolType; | |
| import javax.annotation.Nullable; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Set; | |
| public class EssenceBrickBlock extends BasicBlock { | |
| public static final EnumProperty<DyeColor> DYE_COLOR = EnumProperty.create("color", DyeColor.class); | |
| public EssenceBrickBlock() { | |
| super(Block.Properties.create(Material.ROCK).harvestLevel(1).harvestTool(ToolType.PICKAXE).hardnessAndResistance(1.5F, 1200F)); | |
| } | |
| @Override | |
| public void onBlockHarvested(World worldIn, BlockPos pos, BlockState state, PlayerEntity player) { | |
| DyeColor color = state.get(DYE_COLOR); | |
| ItemStack stack = new ItemStack(this); | |
| CompoundNBT compoundNBT = new CompoundNBT(); | |
| compoundNBT.putInt("color", color.getId()); | |
| stack.setTag(compoundNBT); | |
| ItemEntity entity = new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack); | |
| entity.setDefaultPickupDelay(); | |
| worldIn.addEntity(entity); | |
| } | |
| @SuppressWarnings("deprecation") | |
| @Override | |
| public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult result) { | |
| if (worldIn.isRemote) { | |
| return ActionResultType.PASS; | |
| } | |
| ItemStack stack = player.getHeldItem(handIn); | |
| Set<ResourceLocation> tags = stack.getItem().getTags(); | |
| List<DyeColor> colors = new ArrayList<>(); | |
| tags.stream().filter(tag -> !colors.contains(EssenceColorHelper.getColorByTag(new ItemTags.Wrapper(tag)))) | |
| .forEach(tag -> { | |
| DyeColor color = EssenceColorHelper.getColorByTag(new ItemTags.Wrapper(tag)); | |
| if (color != null) { | |
| colors.add(color); | |
| } | |
| }); | |
| if (colors.size() > 1) { | |
| DyeColor color = colors.stream().skip(Essence.RANDOM.nextInt(colors.size())).findFirst().orElse(null); | |
| if (color != null) { | |
| state.with(DYE_COLOR, color); | |
| return ActionResultType.SUCCESS; | |
| } | |
| } | |
| return ActionResultType.PASS; | |
| } | |
| @Override | |
| public MaterialColor getMaterialColor(BlockState state, IBlockReader worldIn, BlockPos pos) { | |
| return state.get(DYE_COLOR).getMapColor(); | |
| } | |
| @Nullable | |
| @Override | |
| public BlockState getStateForPlacement(BlockItemUseContext context) { | |
| ItemStack stack = context.getItem(); | |
| if (stack.hasTag()) { | |
| CompoundNBT compoundNBT = stack.getTag(); | |
| if (compoundNBT.contains("color")) { | |
| return getDefaultState().with(DYE_COLOR, DyeColor.byId(compoundNBT.getInt("color"))); | |
| } | |
| } | |
| return getDefaultState().with(DYE_COLOR, DyeColor.CYAN); | |
| } | |
| @Override | |
| public ItemStack getPickBlock(BlockState state, RayTraceResult target, IBlockReader world, BlockPos pos, PlayerEntity player) { | |
| DyeColor color = state.get(DYE_COLOR); | |
| ItemStack stack = new ItemStack(this); | |
| CompoundNBT compoundNBT = new CompoundNBT(); | |
| compoundNBT.putInt("color", color.getId()); | |
| stack.setTag(compoundNBT); | |
| return stack; | |
| } | |
| @Override | |
| protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) { | |
| builder.add(DYE_COLOR); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment