2022-09-23 23:25:00 +02:00
|
|
|
package com.minelittlepony.unicopia.block;
|
|
|
|
|
2022-09-25 00:14:29 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
2022-09-23 23:25:00 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2023-04-30 11:46:33 +02:00
|
|
|
import com.minelittlepony.unicopia.server.world.ZapAppleStageStore;
|
|
|
|
|
2022-09-23 23:25:00 +02:00
|
|
|
import net.minecraft.block.*;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.ItemPlacementContext;
|
2023-05-24 20:18:19 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2022-09-23 23:25:00 +02:00
|
|
|
import net.minecraft.server.world.ServerWorld;
|
|
|
|
import net.minecraft.sound.BlockSoundGroup;
|
|
|
|
import net.minecraft.state.StateManager;
|
2022-09-25 00:14:29 +02:00
|
|
|
import net.minecraft.state.property.*;
|
2022-09-23 23:25:00 +02:00
|
|
|
import net.minecraft.util.math.*;
|
|
|
|
import net.minecraft.util.math.random.Random;
|
2022-09-25 00:14:29 +02:00
|
|
|
import net.minecraft.world.*;
|
2022-09-23 23:25:00 +02:00
|
|
|
|
2022-09-25 00:14:29 +02:00
|
|
|
public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock {
|
2022-09-23 23:25:00 +02:00
|
|
|
public static final EnumProperty<ZapAppleStageStore.Stage> STAGE = EnumProperty.of("stage", ZapAppleStageStore.Stage.class);
|
|
|
|
|
|
|
|
ZapAppleLeavesBlock() {
|
|
|
|
super(Settings.of(Material.LEAVES)
|
|
|
|
.strength(500, 1200)
|
|
|
|
.ticksRandomly()
|
|
|
|
.sounds(BlockSoundGroup.AZALEA_LEAVES)
|
|
|
|
.nonOpaque()
|
|
|
|
.allowsSpawning(UBlocks::canSpawnOnLeaves)
|
|
|
|
.suffocates(UBlocks::never)
|
|
|
|
.blockVision(UBlocks::never)
|
|
|
|
);
|
|
|
|
setDefaultState(getDefaultState().with(STAGE, ZapAppleStageStore.Stage.HIBERNATING));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
|
|
|
|
super.appendProperties(builder);
|
|
|
|
builder.add(STAGE);
|
|
|
|
}
|
|
|
|
|
2022-10-07 00:30:26 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasRandomTicks(BlockState state) {
|
2023-05-23 20:30:21 +02:00
|
|
|
return !state.get(PERSISTENT);
|
2022-10-07 00:30:26 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 23:25:00 +02:00
|
|
|
@Override
|
|
|
|
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
|
|
|
|
super.randomTick(state, world, pos, random);
|
2023-05-24 20:18:19 +02:00
|
|
|
tryAdvanceStage(state, world, pos, random);
|
2022-09-23 23:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
|
|
|
|
super.scheduledTick(state, world, pos, random);
|
2023-05-24 20:18:19 +02:00
|
|
|
tryAdvanceStage(state, world, pos, random);
|
|
|
|
world.scheduleBlockTick(pos, this, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void tryAdvanceStage(BlockState state, ServerWorld world, BlockPos pos, Random random) {
|
2023-05-23 20:30:21 +02:00
|
|
|
if (state.get(PERSISTENT)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-23 23:25:00 +02:00
|
|
|
|
|
|
|
ZapAppleStageStore store = ZapAppleStageStore.get(world);
|
|
|
|
ZapAppleStageStore.Stage newStage = store.getStage();
|
|
|
|
if (!world.isDay() && state.get(STAGE).mustChangeIntoInstantly(newStage)) {
|
2023-05-23 20:30:21 +02:00
|
|
|
world.setBlockState(pos, newStage == ZapAppleStageStore.Stage.HIBERNATING ? UBlocks.ZAP_LEAVES_PLACEHOLDER.getDefaultState() : state.with(STAGE, newStage));
|
2022-09-23 23:25:00 +02:00
|
|
|
onStageChanged(store, newStage, world, state, pos, random);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean shouldDecay(BlockState state) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-24 20:18:19 +02:00
|
|
|
@Override
|
|
|
|
public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) {
|
|
|
|
ItemStack stack = super.getPickStack(world, pos, state);
|
|
|
|
stack.setDamage(Math.max(0, state.get(STAGE).ordinal() - 1));
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2022-09-23 23:25:00 +02:00
|
|
|
@Override
|
|
|
|
public BlockState getPlacementState(ItemPlacementContext ctx) {
|
2023-05-24 20:18:19 +02:00
|
|
|
return super.getPlacementState(ctx).with(STAGE, ZapAppleStageStore.Stage.byId(ctx.getStack().getDamage() + 1));
|
2022-09-23 23:25:00 +02:00
|
|
|
}
|
|
|
|
|
2023-05-23 20:30:21 +02:00
|
|
|
static void onStageChanged(ZapAppleStageStore store, ZapAppleStageStore.Stage stage, ServerWorld world, BlockState state, BlockPos pos, Random random) {
|
2022-09-23 23:25:00 +02:00
|
|
|
boolean mustFruit = Random.create(state.getRenderingSeed(pos)).nextInt(5) < 2;
|
|
|
|
BlockState below = world.getBlockState(pos.down());
|
|
|
|
|
|
|
|
if (world.isAir(pos.down())) {
|
|
|
|
if (stage == ZapAppleStageStore.Stage.FRUITING && mustFruit) {
|
|
|
|
world.setBlockState(pos.down(), UBlocks.ZAP_BULB.getDefaultState(), Block.NOTIFY_ALL);
|
|
|
|
store.triggerLightningStrike(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stage != ZapAppleStageStore.Stage.HIBERNATING && world.getRandom().nextInt(10) == 0) {
|
|
|
|
store.triggerLightningStrike(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stage == ZapAppleStageStore.Stage.RIPE) {
|
|
|
|
if (below.isOf(UBlocks.ZAP_BULB)) {
|
|
|
|
world.setBlockState(pos.down(), UBlocks.ZAP_APPLE.getDefaultState(), Block.NOTIFY_ALL);
|
2022-10-07 00:30:26 +02:00
|
|
|
store.playMoonEffect(pos);
|
2022-09-23 23:25:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mustFruit && stage == ZapAppleStageStore.Stage.HIBERNATING) {
|
|
|
|
if (below.isOf(UBlocks.ZAP_APPLE) || below.isOf(UBlocks.ZAP_BULB)) {
|
|
|
|
world.setBlockState(pos.down(), Blocks.AIR.getDefaultState());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBlockBreakStart(BlockState state, World world, BlockPos pos, PlayerEntity player) {
|
2022-09-25 00:14:29 +02:00
|
|
|
ZapBlock.triggerLightning(state, world, pos, player);
|
2022-09-23 23:25:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 00:14:29 +02:00
|
|
|
@Override
|
|
|
|
public int getTint(BlockState state, @Nullable BlockRenderView world, @Nullable BlockPos pos, int foliageColor) {
|
|
|
|
|
|
|
|
if (pos == null) {
|
|
|
|
return 0x4C7EFA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TintedBlock.blend(TintedBlock.rotate(foliageColor, 2), 0x0000FF, 0.3F);
|
|
|
|
}
|
|
|
|
|
2022-09-23 23:25:00 +02:00
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public float calcBlockBreakingDelta(BlockState state, PlayerEntity player, BlockView world, BlockPos pos) {
|
2022-09-25 00:14:29 +02:00
|
|
|
if (state.get(PERSISTENT)) {
|
|
|
|
return Blocks.OAK_LEAVES.calcBlockBreakingDelta(Blocks.OAK_LEAVES.getDefaultState(), player, world, pos);
|
|
|
|
}
|
|
|
|
|
2022-09-23 23:25:00 +02:00
|
|
|
float delta = super.calcBlockBreakingDelta(state, player, world, pos);
|
|
|
|
|
|
|
|
if (Pony.of(player).getSpecies().canUseEarth()) {
|
|
|
|
delta *= 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.get(STAGE) == ZapAppleStageStore.Stage.RIPE) {
|
|
|
|
delta *= 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MathHelper.clamp(delta, 0, 0.9F);
|
|
|
|
}
|
|
|
|
}
|