Skip to content

Commit

Permalink
Code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus1002 committed Jan 5, 2022
1 parent cb7fff7 commit 289639c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.teamabnormals.incubation.common.block;

import java.util.function.Supplier;

import javax.annotation.Nullable;

import com.teamabnormals.incubation.common.block.entity.BirdNestBlockEntity;
import com.teamabnormals.incubation.core.registry.IncubationBlockEntityTypes;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -20,9 +25,6 @@
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.registries.ForgeRegistries;

import javax.annotation.Nullable;
import java.util.function.Supplier;

public class BirdNestBlock extends BaseEntityBlock {
protected static final VoxelShape SHAPE = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 3.0D, 16.0D);
public static final IntegerProperty EGGS = IntegerProperty.create("eggs", 1, 6);
Expand Down Expand Up @@ -55,9 +57,9 @@ public BlockState updateShape(BlockState stateIn, Direction facing, BlockState f
@Override
public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit) {
if (player.mayBuild()) {
int i = state.getValue(EGGS);
ItemStack itemstack = player.getItemInHand(handIn);
if (this.egg.get() != Items.AIR && itemstack.getItem() == this.egg.get()) {
int i = state.getValue(EGGS);
if (i < 6) {
if (!player.getAbilities().instabuild) {
itemstack.shrink(1);
Expand All @@ -66,18 +68,24 @@ public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Play
}
} else {
popResource(worldIn, pos, new ItemStack(this.egg.get()));

if (i > 1)
worldIn.setBlock(pos, state.setValue(EGGS, i - 1), 3);
else
worldIn.setBlock(pos, this.getEmptyNest().defaultBlockState(), 3);
this.removeEgg(worldIn, pos, state);
}
return InteractionResult.sidedSuccess(worldIn.isClientSide);
} else {
return super.use(state, worldIn, pos, player, handIn, hit);
}
}

private void removeEgg(Level world, BlockPos pos, BlockState state) {
int i = state.getValue(EGGS);
if (i > 1) {
world.setBlock(pos, state.setValue(EGGS, i - 1), 3);
} else {
world.setBlock(pos, this.getEmptyNest().defaultBlockState(), 3);
}
}

@Override
public ItemStack getCloneItemStack(BlockGetter worldIn, BlockPos pos, BlockState state) {
return new ItemStack(this.getEgg());
}
Expand All @@ -100,14 +108,17 @@ public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return level.isClientSide ? null : createTickerHelper(type, IncubationBlockEntityTypes.BIRD_NEST.get(), BirdNestBlockEntity::serverTick);
}

@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(EGGS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;

import java.util.Map;
Expand Down Expand Up @@ -45,17 +47,9 @@ public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Play
if (player.mayBuild()) {
ItemStack itemstack = player.getItemInHand(handIn);
Item item = itemstack.getItem();
Block nest = this.getNest(item);

Block nest = null;

for (Supplier<? extends Item> supplier : NESTS.keySet()) {
if (item == supplier.get()) {
nest = NESTS.get(supplier);
break;
}
}

if (nest != null && ((BirdNestBlock) nest).getEgg() != Items.AIR) {
if (nest != null) {
if (!player.getAbilities().instabuild && !worldIn.isClientSide) {
itemstack.shrink(1);
}
Expand All @@ -76,15 +70,14 @@ public boolean canSurvive(BlockState state, LevelReader worldIn, BlockPos pos) {
}

public Block getNest(Item item) {
Block nest = null;

for (Supplier<? extends Item> supplier : NESTS.keySet()) {
if (item == supplier.get()) {
nest = NESTS.get(supplier);
break;
if (item != Items.AIR) {
for (Supplier<? extends Item> egg : NESTS.keySet()) {
if (item == egg.get()) {
return NESTS.get(egg);
}
}
}

return nest;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.teamabnormals.incubation.common.block.EmptyNestBlock;
import com.teamabnormals.incubation.core.Incubation;
import com.teamabnormals.incubation.core.other.IncubationConstants;

import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
Expand Down

0 comments on commit 289639c

Please sign in to comment.