2020-04-15 14:22:03 +02:00
|
|
|
package com.minelittlepony.unicopia.block;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2020-04-15 17:11:37 +02:00
|
|
|
import com.minelittlepony.unicopia.ducks.Colourful;
|
2020-04-15 18:12:00 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.LeavesBlock;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.DyeItem;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2020-04-22 16:28:20 +02:00
|
|
|
import net.minecraft.server.world.ServerWorld;
|
2020-01-16 12:35:46 +01:00
|
|
|
import net.minecraft.sound.SoundCategory;
|
|
|
|
import net.minecraft.sound.SoundEvents;
|
2020-04-22 16:28:20 +02:00
|
|
|
import net.minecraft.state.StateManager;
|
2020-01-16 12:35:46 +01:00
|
|
|
import net.minecraft.state.property.BooleanProperty;
|
2020-04-22 16:28:20 +02:00
|
|
|
import net.minecraft.util.ActionResult;
|
2020-01-16 12:35:46 +01:00
|
|
|
import net.minecraft.util.DyeColor;
|
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraft.world.biome.Biome.TemperatureGroup;
|
|
|
|
|
2020-04-15 17:11:37 +02:00
|
|
|
public class FruitLeavesBlock extends LeavesBlock implements Colourful {
|
2020-01-16 12:35:46 +01:00
|
|
|
|
|
|
|
public static final BooleanProperty HEAVY = BooleanProperty.of("heavy");
|
|
|
|
|
|
|
|
private boolean hardy;
|
|
|
|
|
|
|
|
private int baseGrowthChance;
|
|
|
|
private int customTint;
|
|
|
|
|
|
|
|
private Function<World, ItemStack> fruitProducer = w -> ItemStack.EMPTY;
|
|
|
|
private Function<World, ItemStack> compostProducer = w -> ItemStack.EMPTY;
|
|
|
|
|
2020-04-27 17:28:22 +02:00
|
|
|
public FruitLeavesBlock(Settings settings) {
|
|
|
|
super(settings);
|
2020-04-22 16:28:20 +02:00
|
|
|
setDefaultState(stateManager.getDefaultState()
|
2020-01-16 12:35:46 +01:00
|
|
|
.with(HEAVY, false)
|
|
|
|
.with(DISTANCE, 7)
|
|
|
|
.with(PERSISTENT, false)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-22 16:28:20 +02:00
|
|
|
@Override
|
|
|
|
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
|
|
|
|
super.appendProperties(builder);
|
|
|
|
builder.add(HEAVY);
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:35:46 +01:00
|
|
|
public FruitLeavesBlock hardy(boolean value) {
|
|
|
|
hardy = value;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FruitLeavesBlock fruit(@Nonnull Function<World, ItemStack> producer) {
|
|
|
|
fruitProducer = producer;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FruitLeavesBlock compost(@Nonnull Function<World, ItemStack> producer) {
|
|
|
|
compostProducer = producer;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FruitLeavesBlock growthChance(int chance) {
|
|
|
|
baseGrowthChance = chance;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FruitLeavesBlock tint(int tint) {
|
|
|
|
customTint = tint;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-22 16:28:20 +02:00
|
|
|
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
|
2020-01-16 12:35:46 +01:00
|
|
|
ItemStack stack = player.getStackInHand(hand);
|
|
|
|
|
2020-04-15 18:12:00 +02:00
|
|
|
if (Pony.of(player).getSpecies().canUseEarth()) {
|
2020-01-16 12:35:46 +01:00
|
|
|
if (stack.isEmpty()) {
|
|
|
|
|
|
|
|
if (state.get(HEAVY)) {
|
|
|
|
dropContents(world, pos, state, 0);
|
|
|
|
}
|
|
|
|
} else if (stack.getItem() instanceof DyeItem && ((DyeItem)stack.getItem()).getColor() == DyeColor.WHITE) {
|
|
|
|
if (!state.get(HEAVY)) {
|
|
|
|
world.setBlockState(pos, state.with(HEAVY, true));
|
|
|
|
|
|
|
|
if (!world.isClient) {
|
|
|
|
world.playGlobalEvent(2005, pos, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!player.abilities.creativeMode) {
|
|
|
|
stack.decrement(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 16:28:20 +02:00
|
|
|
return ActionResult.SUCCESS;
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 16:28:20 +02:00
|
|
|
return ActionResult.PASS;
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-22 16:28:20 +02:00
|
|
|
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random rand) {
|
|
|
|
if (!world.isClient && world.isChunkLoaded(pos) && !state.get(PERSISTENT)) {
|
2020-01-16 12:35:46 +01:00
|
|
|
int growthChance = getGrowthChance(world, pos, state);
|
|
|
|
|
|
|
|
if (!state.get(HEAVY) && (growthChance <= 0 || rand.nextInt(growthChance) == 0)) {
|
|
|
|
world.setBlockState(pos, state.with(HEAVY, true));
|
|
|
|
} else {
|
|
|
|
growthChance /= 10;
|
|
|
|
|
|
|
|
if (state.get(HEAVY) && (growthChance <= 0 || rand.nextInt(growthChance) == 0)) {
|
|
|
|
dropContents(world, pos, state, 0);
|
|
|
|
} else {
|
2020-04-22 16:28:20 +02:00
|
|
|
super.scheduledTick(state, world, pos, rand);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int getGrowthChance(World world, BlockPos pos, BlockState state) {
|
|
|
|
int chance = baseGrowthChance;
|
|
|
|
|
2020-04-22 16:28:20 +02:00
|
|
|
if (!hardy && !world.isDay()) {
|
2020-01-16 12:35:46 +01:00
|
|
|
chance *= 40;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (world.getLightLevel(pos) >= 4) {
|
|
|
|
chance /= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
TemperatureGroup temp = world.getBiome(pos).getTemperatureGroup();
|
|
|
|
|
|
|
|
if (!hardy && temp == TemperatureGroup.COLD) {
|
|
|
|
chance *= 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp == TemperatureGroup.WARM) {
|
|
|
|
chance /= 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp == TemperatureGroup.MEDIUM) {
|
|
|
|
chance /= 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
return chance;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCustomTint(BlockState state, int tint) {
|
|
|
|
return customTint;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void dropContents(World world, BlockPos pos, BlockState state, int chance) {
|
|
|
|
Function<World, ItemStack> fruit = world.random.nextInt(40) == 0 ? compostProducer : fruitProducer;
|
|
|
|
dropStack(world, pos, fruit.apply(world));
|
|
|
|
|
|
|
|
world.playSound(null, pos, SoundEvents.ENTITY_ITEM_FRAME_PLACE, SoundCategory.BLOCKS, 0.3F, 1);
|
|
|
|
world.setBlockState(pos, state.with(HEAVY, false));
|
|
|
|
}
|
|
|
|
}
|