-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #775 from DaFuqs/2.1.0/1.19.2/master
Added Treetap
- Loading branch information
Showing
21 changed files
with
589 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
107 changes: 107 additions & 0 deletions
107
src/main/java/net/id/paradiselost/blocks/blockentity/TreeTapBlockEntity.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package net.id.paradiselost.blocks.blockentity; | ||
|
||
import net.id.incubus_core.be.InventoryBlockEntity; | ||
import net.id.paradiselost.recipe.ParadiseLostRecipeTypes; | ||
import net.id.paradiselost.recipe.TreeTapRecipe; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.inventory.Inventories; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.network.Packet; | ||
import net.minecraft.network.listener.ClientPlayPacketListener; | ||
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.state.property.Properties; | ||
import net.minecraft.util.Hand; | ||
import net.minecraft.util.ItemScatterer; | ||
import net.minecraft.util.collection.DefaultedList; | ||
import net.minecraft.util.math.BlockPos; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Optional; | ||
|
||
public class TreeTapBlockEntity extends BlockEntity implements InventoryBlockEntity { | ||
|
||
private final DefaultedList<ItemStack> inventory; | ||
|
||
public TreeTapBlockEntity(BlockPos pos, BlockState state) { | ||
super(ParadiseLostBlockEntityTypes.TREE_TAP, pos, state); | ||
inventory = DefaultedList.ofSize(1, ItemStack.EMPTY); | ||
} | ||
|
||
public void handleUse(PlayerEntity player, Hand hand, ItemStack handStack) { | ||
ItemStack stored = inventory.get(0); | ||
inventory.set(0, handStack); | ||
player.setStackInHand(hand, stored); | ||
markDirty(); | ||
} | ||
|
||
@Override | ||
public @NotNull HopperStrategy getHopperStrategy() { | ||
return HopperStrategy.IN_ANY_OUT_BOTTOM; | ||
} | ||
|
||
@Override | ||
public DefaultedList<ItemStack> getItems() { | ||
return inventory; | ||
} | ||
|
||
@Override | ||
public void readNbt(NbtCompound nbt) { | ||
super.readNbt(nbt); | ||
|
||
Inventories.readNbt(nbt, inventory); | ||
} | ||
|
||
@Override | ||
public void writeNbt(NbtCompound nbt) { | ||
super.writeNbt(nbt); | ||
Inventories.writeNbt(nbt, inventory); | ||
} | ||
|
||
public BlockState getTappedState() { | ||
return this.world.getBlockState(this.pos.offset(getCachedState().get(Properties.HORIZONTAL_FACING).getOpposite())); | ||
} | ||
|
||
public void tryCraft() { | ||
ItemStack stack = getStack(0); | ||
if (stack.isEmpty()) { | ||
return; | ||
} | ||
|
||
Optional<TreeTapRecipe> recipe = this.world.getRecipeManager().getFirstMatch(ParadiseLostRecipeTypes.TREE_TAP_RECIPE_TYPE, this, this.world); | ||
if (recipe.isPresent()) { | ||
ItemStack output = recipe.get().craft(this); | ||
stack.decrement(1); | ||
|
||
// TODO: play a sound? | ||
if (stack.isEmpty()) { | ||
this.inventory.set(0, output); | ||
updateInClientWorld(); | ||
} else { | ||
ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), output); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public NbtCompound toInitialChunkDataNbt() { | ||
NbtCompound nbtCompound = new NbtCompound(); | ||
this.writeNbt(nbtCompound); | ||
return nbtCompound; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Packet<ClientPlayPacketListener> toUpdatePacket() { | ||
return BlockEntityUpdateS2CPacket.create(this); | ||
} | ||
|
||
public void updateInClientWorld() { | ||
((ServerWorld) world).getChunkManager().markForUpdate(pos); | ||
} | ||
|
||
} |
141 changes: 141 additions & 0 deletions
141
src/main/java/net/id/paradiselost/blocks/mechanical/TreeTapBlock.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package net.id.paradiselost.blocks.mechanical; | ||
|
||
import net.id.paradiselost.blocks.blockentity.TreeTapBlockEntity; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.BlockRenderType; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.ShapeContext; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.inventory.Inventory; | ||
import net.minecraft.item.ItemPlacementContext; | ||
import net.minecraft.screen.ScreenHandler; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.state.StateManager; | ||
import net.minecraft.state.property.DirectionProperty; | ||
import net.minecraft.state.property.Properties; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.BlockMirror; | ||
import net.minecraft.util.BlockRotation; | ||
import net.minecraft.util.Hand; | ||
import net.minecraft.util.ItemScatterer; | ||
import net.minecraft.util.hit.BlockHitResult; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Direction; | ||
import net.minecraft.util.math.random.Random; | ||
import net.minecraft.util.shape.VoxelShape; | ||
import net.minecraft.world.BlockView; | ||
import net.minecraft.world.World; | ||
import net.minecraft.world.WorldView; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class TreeTapBlock extends ParadiseLostBlockWithEntity { | ||
|
||
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING; | ||
private static final VoxelShape SHAPE = Block.createCuboidShape(0, 0, 0, 16, 5, 16); | ||
|
||
public TreeTapBlock(Settings settings) { | ||
super(settings, true); | ||
} | ||
|
||
@Override | ||
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { | ||
if (!player.isSneaking() && world.getBlockEntity(pos) instanceof TreeTapBlockEntity treeTapBlockEntity) { | ||
treeTapBlockEntity.handleUse(player, hand, player.getStackInHand(hand)); | ||
return ActionResult.success(world.isClient()); | ||
} | ||
return super.onUse(state, world, pos, player, hand, hit); | ||
} | ||
|
||
@Override | ||
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { | ||
return SHAPE; | ||
} | ||
|
||
@Override | ||
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) { | ||
Direction direction = state.get(FACING); | ||
if (!direction.getAxis().isHorizontal()) { | ||
return false; | ||
} | ||
|
||
BlockPos blockPos = pos.offset(direction.getOpposite()); | ||
BlockState blockState = world.getBlockState(blockPos); | ||
return blockState.isSideSolidFullSquare(world, blockPos, direction); | ||
} | ||
|
||
@Override | ||
public BlockRenderType getRenderType(BlockState state) { | ||
return BlockRenderType.MODEL; | ||
} | ||
|
||
@Override | ||
public BlockState getPlacementState(ItemPlacementContext ctx) { | ||
Direction direction = ctx.getSide(); | ||
if (!direction.getAxis().isHorizontal()) { | ||
return null; | ||
} | ||
|
||
return this.getDefaultState().with(FACING, direction); | ||
} | ||
|
||
@Override | ||
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { | ||
builder.add(WATERLOGGED, POWERED, FACING); | ||
} | ||
|
||
@Override | ||
public BlockState rotate(BlockState state, BlockRotation rotation) { | ||
return state.with(FACING, rotation.rotate(state.get(FACING))); | ||
} | ||
|
||
@Override | ||
public BlockState mirror(BlockState state, BlockMirror mirror) { | ||
return state.rotate(mirror.getRotation(state.get(FACING))); | ||
} | ||
|
||
@Override | ||
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { | ||
super.randomTick(state, world, pos, random); | ||
|
||
if (!world.isClient && world.getBlockEntity(pos) instanceof TreeTapBlockEntity treeTapBlockEntity) { | ||
treeTapBlockEntity.tryCraft(); | ||
} | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { | ||
return new TreeTapBlockEntity(pos, state); | ||
} | ||
|
||
@Override | ||
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { | ||
if (!state.isOf(newState.getBlock())) { | ||
scatterContents(world, pos); | ||
world.updateComparators(pos, this); | ||
} | ||
super.onStateReplaced(state, world, pos, newState, moved); | ||
} | ||
|
||
public static void scatterContents(World world, BlockPos pos) { | ||
Block block = world.getBlockState(pos).getBlock(); | ||
BlockEntity blockEntity = world.getBlockEntity(pos); | ||
if (blockEntity instanceof Inventory inventory) { | ||
ItemScatterer.spawn(world, pos, inventory); | ||
world.updateComparators(pos, block); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasComparatorOutput(BlockState state) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getComparatorOutput(BlockState state, @NotNull World world, BlockPos pos) { | ||
return ScreenHandler.calculateComparatorOutput(world.getBlockEntity(pos)); | ||
} | ||
|
||
} |
This file contains 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
Oops, something went wrong.