Skip to content

Commit

Permalink
loggenpile
Browse files Browse the repository at this point in the history
  • Loading branch information
MBatt1 committed Aug 21, 2023
1 parent 52191b7 commit ef4aaed
Show file tree
Hide file tree
Showing 45 changed files with 1,503 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/main/java/net/cr24/primeval/block/PrimevalBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ public class PrimevalBlocks {

//// Plant blocks
// Oak Trees
public static final Block OAK_LOG = registerBlock("oak_log", new PillarBlock(SETTINGS_LOG()), Weight.HEAVY, Size.LARGE, PRIMEVAL_BLOCKS);
public static final Block OAK_LOG = registerBlockWithoutItem("oak_log", new PillarBlock(SETTINGS_LOG()));
public static final Block OAK_TRUNK = registerBlockWithoutItem("oak_trunk", new TrunkBlock(SETTINGS_TRUNK().nonOpaque(), OakTrunker.INSTANCE));
public static final Block OAK_LEAVES = registerBlockWithoutItem("oak_leaves", new LeafBlock(FabricBlockSettings.copyOf(Blocks.OAK_LEAVES)));
// Birch Trees
public static final Block BIRCH_LOG = registerBlock("birch_log", new PillarBlock(SETTINGS_LOG()), Weight.HEAVY, Size.LARGE, PRIMEVAL_BLOCKS);
public static final Block BIRCH_LOG = registerBlockWithoutItem("birch_log", new PillarBlock(SETTINGS_LOG()));
public static final Block BIRCH_TRUNK = registerBlockWithoutItem("birch_trunk", new TrunkBlock(SETTINGS_TRUNK().nonOpaque(), BirchTrunker.INSTANCE));
public static final Block BIRCH_LEAVES = registerBlockWithoutItem("birch_leaves", new LeafBlock(FabricBlockSettings.copyOf(Blocks.OAK_LEAVES)));
// Spruce Trees
public static final Block SPRUCE_LOG = registerBlock("spruce_log", new PillarBlock(SETTINGS_LOG()), Weight.HEAVY, Size.LARGE, PRIMEVAL_BLOCKS);
public static final Block SPRUCE_LOG = registerBlockWithoutItem("spruce_log", new PillarBlock(SETTINGS_LOG()));
public static final Block SPRUCE_TRUNK = registerBlockWithoutItem("spruce_trunk", new TrunkBlock(SETTINGS_TRUNK().nonOpaque(), SpruceTrunker.INSTANCE));
public static final Block SPRUCE_LEAVES = registerBlockWithoutItem("spruce_leaves", new LeafBlock(FabricBlockSettings.copyOf(Blocks.OAK_LEAVES)));
// Saplings+
Expand Down Expand Up @@ -153,6 +153,9 @@ public class PrimevalBlocks {
public static final Block WICKER_BARS = registerBlock("wicker_bars", new PaneBlock(SETTINGS_REFINED_WOOD().nonOpaque()), Weight.LIGHT, Size.MEDIUM, PRIMEVAL_BLOCKS);
public static final Block ROPE = registerBlock("rope", new ChainBlock(FabricBlockSettings.of(Material.PLANT).strength(0.2F).sounds(BlockSoundGroup.GRASS)), Weight.LIGHT, Size.SMALL, PRIMEVAL_BLOCKS);
public static final Block ROPE_LADDER = registerBlock("rope_ladder", new SuspendedLadderBlock(FabricBlockSettings.of(Material.WOOD, MapColor.OAK_TAN).strength(0.3F).sounds(BlockSoundGroup.WOOD).nonOpaque()), Weight.NORMAL, Size.MEDIUM, PRIMEVAL_BLOCKS);
public static final Block OAK_LOG_PILE = registerBlockWithoutItem("oak_log_pile", new LogPileBlock(FabricBlockSettings.of(Material.WOOD, MapColor.BROWN).strength(1.0f, 3.0f).sounds(BlockSoundGroup.WOOD).nonOpaque().requiresTool()));
public static final Block BIRCH_LOG_PILE = registerBlockWithoutItem("birch_log_pile", new LogPileBlock(FabricBlockSettings.of(Material.WOOD, MapColor.BROWN).strength(1.0f, 3.0f).sounds(BlockSoundGroup.WOOD).nonOpaque().requiresTool()));
public static final Block SPRUCE_LOG_PILE = registerBlockWithoutItem("spruce_log_pile", new LogPileBlock(FabricBlockSettings.of(Material.WOOD, MapColor.BROWN).strength(1.0f, 3.0f).sounds(BlockSoundGroup.WOOD).nonOpaque().requiresTool()));


// Crops
Expand Down Expand Up @@ -206,6 +209,7 @@ public class PrimevalBlocks {
public static final BlockEntityType<PitKilnBlockEntity> PIT_KILN_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, PrimevalMain.getId("pit_kiln_block_entity"), FabricBlockEntityTypeBuilder.create(PitKilnBlockEntity::new, PIT_KILN).build());
public static final BlockEntityType<AshPileBlockEntity> ASH_PILE_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, PrimevalMain.getId("ash_pile_block_entity"), FabricBlockEntityTypeBuilder.create(AshPileBlockEntity::new, ASH_PILE).build());
public static final BlockEntityType<LayingItemBlockEntity> LAYING_ITEM_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, PrimevalMain.getId("laying_item_block_entity"), FabricBlockEntityTypeBuilder.create(LayingItemBlockEntity::new, LAYING_ITEM).build());
public static final BlockEntityType<LogPileBlockEntity> LOG_PILE_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, PrimevalMain.getId("log_pile_block_entity"), FabricBlockEntityTypeBuilder.create(LogPileBlockEntity::new, OAK_LOG_PILE, BIRCH_LOG_PILE, SPRUCE_LOG_PILE).build());
public static final BlockEntityType<CrateBlockEntity> CRATE_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, PrimevalMain.getId("crate_block_entity"), FabricBlockEntityTypeBuilder.create(CrateBlockEntity::new, OAK_CRATE, BIRCH_CRATE, SPRUCE_CRATE).build());
public static final BlockEntityType<StoragePotBlockEntity> LARGE_POT_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, PrimevalMain.getId("large_pot_block_entity"), FabricBlockEntityTypeBuilder.create(StoragePotBlockEntity::new, LARGE_FIRED_CLAY_POT).build());
public static final BlockEntityType<WickerBasketBlockEntity> WICKER_BASKET_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, PrimevalMain.getId("wicker_basket_block_entity"), FabricBlockEntityTypeBuilder.create(WickerBasketBlockEntity::new, WICKER_BASKET).build());
Expand Down Expand Up @@ -289,6 +293,9 @@ private static void registerFlammables() {
FlammableBlockRegistry.getDefaultInstance().add(SPRUCE_LOG, 5, 10);
FlammableBlockRegistry.getDefaultInstance().add(SPRUCE_TRUNK, 1, 3);
FlammableBlockRegistry.getDefaultInstance().add(SPRUCE_LEAVES, 50, 60);
FlammableBlockRegistry.getDefaultInstance().add(OAK_LOG_PILE, 5, 10);
FlammableBlockRegistry.getDefaultInstance().add(BIRCH_LOG_PILE, 5, 10);
FlammableBlockRegistry.getDefaultInstance().add(SPRUCE_LOG_PILE, 5, 10);

FlammableBlockRegistry.getDefaultInstance().add(OAK_SAPLING, 50, 80);
FlammableBlockRegistry.getDefaultInstance().add(BIRCH_SAPLING, 50, 80);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.cr24.primeval.block.entity;

import net.cr24.primeval.block.PrimevalBlocks;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.util.Clearable;
import net.minecraft.util.math.BlockPos;

public class LogPileBlockEntity extends BlockEntity implements Clearable {

private ItemStack item;

public LogPileBlockEntity(BlockPos pos, BlockState state) {
super(PrimevalBlocks.LOG_PILE_BLOCK_ENTITY, pos, state);
item = ItemStack.EMPTY;
}

public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
this.item = ItemStack.fromNbt(nbt.getCompound(("Item")));
}

protected void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
nbt.put("Item", this.item.writeNbt(new NbtCompound()));
}

public BlockEntityUpdateS2CPacket toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}

public void setItem(ItemStack newItem) {
ItemStack copy = newItem.copy();
copy.setCount(1);
this.item = copy;
}

@Override
public NbtCompound toInitialChunkDataNbt() {
NbtCompound nbtCompound = new NbtCompound();
nbtCompound.put("Item", this.item.writeNbt(new NbtCompound()));
return nbtCompound;
}

public ItemStack getItem() {
return this.item.copy();
}

@Override
public void clear() {
item = ItemStack.EMPTY;
}
}
114 changes: 114 additions & 0 deletions src/main/java/net/cr24/primeval/block/functional/LogPileBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package net.cr24.primeval.block.functional;

import net.cr24.primeval.block.LayeredBlock;
import net.cr24.primeval.block.entity.LayingItemBlockEntity;
import net.cr24.primeval.block.entity.LogPileBlockEntity;
import net.cr24.primeval.block.entity.PrimevalCampfireBlockEntity;
import net.cr24.primeval.item.tool.PrimevalShovelItem;
import net.cr24.primeval.recipe.OpenFireRecipe;
import net.cr24.primeval.recipe.PrimevalRecipes;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.piston.PistonBehavior;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;

public class LogPileBlock extends BlockWithEntity implements Waterloggable {

public static final BooleanProperty WATERLOGGED;
public static final IntProperty AMOUNT;

public LogPileBlock(Settings settings) {
super(settings);
this.setDefaultState(this.getDefaultState().with(WATERLOGGED, false).with(AMOUNT, 0));
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!player.getAbilities().allowModifyWorld) return ActionResult.PASS;
ItemStack itemStack = player.getStackInHand(hand);
BlockEntity blockEntity = world.getBlockEntity(pos);
if (hand == Hand.MAIN_HAND && blockEntity instanceof LogPileBlockEntity) {
ItemStack logItem = ((LogPileBlockEntity) blockEntity).getItem();
int amount = state.get(AMOUNT);
if (itemStack.isEmpty() && !world.isClient) {
player.giveItemStack(logItem);
if (amount > 0) {
world.setBlockState(pos, state.with(AMOUNT, amount-1));
} else {
world.setBlockState(pos, Blocks.AIR.getDefaultState());
}
world.playSound(null, pos, SoundEvents.BLOCK_WOOD_BREAK, SoundCategory.BLOCKS, 0.3f, world.getRandom().nextFloat() * 0.4f + 0.8f);
return ActionResult.SUCCESS;
}
}
return ActionResult.PASS;
}

public FluidState getFluidState(BlockState state) {
return state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state);
}

public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (state.get(WATERLOGGED)) {
world.createAndScheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
}
return state;
}

public PistonBehavior getPistonBehavior(BlockState state) {
return PistonBehavior.DESTROY;
}

protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(WATERLOGGED, AMOUNT);
}

static {
WATERLOGGED = Properties.WATERLOGGED;
AMOUNT = IntProperty.of("amount", 0, 7);
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new LogPileBlockEntity(pos, state);
}

public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
}

@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return Block.createCuboidShape(1.0D, 0.0D, 1.0D, 15.0D, (4.0D * Math.ceil((1+state.get(AMOUNT))/2.0d)), 15.0D);
}

public VoxelShape getCollisionShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return Block.createCuboidShape(1.0D, 0.0D, 1.0D, 15.0D, (4.0D * Math.ceil((1+state.get(AMOUNT))/2.0d)), 15.0D);
}
}
152 changes: 152 additions & 0 deletions src/main/java/net/cr24/primeval/item/LogItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package net.cr24.primeval.item;

import net.cr24.primeval.PrimevalMain;
import net.cr24.primeval.block.entity.LogPileBlockEntity;
import net.cr24.primeval.block.functional.LogPileBlock;
import net.minecraft.advancement.criterion.Criteria;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.Property;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;

public class LogItem extends WeightedBlockItem {

private final Block logPileBlock;

public LogItem(Block block, Block logPileBlock, Settings settings, Weight weight, Size size) {
super(block, settings, weight, size);
this.logPileBlock = logPileBlock;
}

public ActionResult useOnBlock(ItemUsageContext context) {
World world = context.getWorld();
BlockPos pos = context.getBlockPos();
BlockState state = world.getBlockState(pos);
ActionResult actionResult;
if (state.getBlock() instanceof LogPileBlock && state.get(LogPileBlock.AMOUNT) < 7) {
world.setBlockState(pos, state.with(LogPileBlock.AMOUNT, state.get(LogPileBlock.AMOUNT)+1));
if (context.getPlayer() != null && !context.getPlayer().isCreative()) context.getStack().decrement(1);
world.playSound(null, pos, SoundEvents.BLOCK_WOOD_PLACE, SoundCategory.BLOCKS, 0.3f, world.getRandom().nextFloat() * 0.4f + 0.8f);
actionResult = ActionResult.success(world.isClient);
} else {
actionResult = this.place(new ItemPlacementContext(context));
}
if (!actionResult.isAccepted() && this.isFood()) {
ActionResult actionResult2 = this.use(context.getWorld(), context.getPlayer(), context.getHand()).getResult();
return actionResult2 == ActionResult.CONSUME ? ActionResult.CONSUME_PARTIAL : actionResult2;
} else {
return actionResult;
}
}

public ActionResult place(ItemPlacementContext context) {
if (!context.canPlace()) {
return ActionResult.FAIL;
} else {
ItemPlacementContext itemPlacementContext = this.getPlacementContext(context);
if (itemPlacementContext == null) {
return ActionResult.FAIL;
} else {
BlockState blockState = this.getPlacementState(itemPlacementContext);
if (blockState == null) {
return ActionResult.FAIL;
} else if (!this.place(itemPlacementContext, blockState)) {
return ActionResult.FAIL;
} else {
BlockPos blockPos = itemPlacementContext.getBlockPos();
World world = itemPlacementContext.getWorld();
PlayerEntity playerEntity = itemPlacementContext.getPlayer();
ItemStack itemStack = itemPlacementContext.getStack();
BlockState blockState2 = world.getBlockState(blockPos);
if (blockState2.isOf(blockState.getBlock())) {
blockState2 = this.placeFromNbt(blockPos, world, itemStack, blockState2);
this.postPlacement(blockPos, world, playerEntity, itemStack, blockState2);
blockState2.getBlock().onPlaced(world, blockPos, blockState2, playerEntity, itemStack);
if (playerEntity instanceof ServerPlayerEntity) {
Criteria.PLACED_BLOCK.trigger((ServerPlayerEntity)playerEntity, blockPos, itemStack);
}
}

BlockSoundGroup blockSoundGroup = blockState2.getSoundGroup();
world.playSound(playerEntity, blockPos, this.getPlaceSound(blockState2), SoundCategory.BLOCKS, (blockSoundGroup.getVolume() + 1.0F) / 2.0F, blockSoundGroup.getPitch() * 0.8F);
world.emitGameEvent(GameEvent.BLOCK_PLACE, blockPos, GameEvent.Emitter.of(playerEntity, blockState2));
if (playerEntity == null || !playerEntity.getAbilities().creativeMode) {
itemStack.decrement(1);
}

return ActionResult.success(world.isClient);
}
}
}
}

@Nullable
protected BlockState getPlacementState(ItemPlacementContext context) {
BlockState blockState;
if (context.getPlayer() != null && context.getPlayer().isSneaking()) {
World world = context.getWorld();
BlockPos pos = context.getBlockPos();
blockState = logPileBlock.getDefaultState().with(LogPileBlock.WATERLOGGED, world.getFluidState(pos).getFluid() == Fluids.WATER);
} else {
blockState = this.getBlock().getPlacementState(context);
}
return blockState != null && this.canPlace(context, blockState) ? blockState : null;
}

private BlockState placeFromNbt(BlockPos pos, World world, ItemStack stack, BlockState state) {
BlockState blockState = state;
NbtCompound nbtCompound = stack.getNbt();
if (nbtCompound != null) {
NbtCompound nbtCompound2 = nbtCompound.getCompound("BlockStateTag");
StateManager<Block, BlockState> stateManager = state.getBlock().getStateManager();
Iterator var9 = nbtCompound2.getKeys().iterator();

while(var9.hasNext()) {
String string = (String)var9.next();
Property<?> property = stateManager.getProperty(string);
if (property != null) {
String string2 = nbtCompound2.get(string).asString();
blockState = with(blockState, property, string2);
}
}
}

if (blockState != state) {
world.setBlockState(pos, blockState, 2);
}

if (blockState.getBlock() instanceof LogPileBlock) {
BlockEntity be = world.getBlockEntity(pos);
if (be instanceof LogPileBlockEntity) {
ItemStack s = stack.copy();
s.setCount(1);
((LogPileBlockEntity) be).setItem(s);
}
}

return blockState;
}

private static <T extends Comparable<T>> BlockState with(BlockState state, Property<T> property, String name) {
return property.parse(name).map((value) -> state.with(property, value)).orElse(state);
}

}
Loading

0 comments on commit ef4aaed

Please sign in to comment.