-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
1,503 additions
and
5 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
56 changes: 56 additions & 0 deletions
56
src/main/java/net/cr24/primeval/block/entity/LogPileBlockEntity.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,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
114
src/main/java/net/cr24/primeval/block/functional/LogPileBlock.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,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); | ||
} | ||
} |
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,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); | ||
} | ||
|
||
} |
Oops, something went wrong.