Files
Botania/src/main/java/vazkii/botania/api/subtile/TileEntityGeneratingFlower.java
T

334 lines
9.6 KiB
Java

/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*
* File Created @ [Jan 24, 2014, 8:03:36 PM (GMT)]
*/
package vazkii.botania.api.subtile;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.block.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.registries.ForgeRegistries;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.internal.IManaNetwork;
import vazkii.botania.api.mana.IManaCollector;
import java.awt.Color;
import java.util.List;
/**
* The basic class for a Generating Flower.
*/
public class TileEntityGeneratingFlower extends TileEntitySpecialFlower {
public static final int LINK_RANGE = 6;
private static final String TAG_MANA = "mana";
private static final String TAG_COLLECTOR_X = "collectorX";
private static final String TAG_COLLECTOR_Y = "collectorY";
private static final String TAG_COLLECTOR_Z = "collectorZ";
private static final String TAG_PASSIVE_DECAY_TICKS = "passiveDecayTicks";
protected int mana;
public int redstoneSignal = 0;
int sizeLastCheck = -1;
protected TileEntity linkedCollector = null;
public int knownMana = -1;
public int passiveDecayTicks;
BlockPos cachedCollectorCoordinates = null;
public TileEntityGeneratingFlower(TileEntityType<?> type) {
super(type);
}
/**
* If set to true, redstoneSignal will be updated every tick.
*/
public boolean acceptsRedstone() {
return false;
}
@Override
public void tickFlower() {
super.tickFlower();
linkCollector();
if(canGeneratePassively()) {
int delay = getDelayBetweenPassiveGeneration();
if(delay > 0 && ticksExisted % delay == 0 && !getWorld().isRemote) {
if(shouldSyncPassiveGeneration())
sync();
addMana(getValueForPassiveGeneration());
}
}
emptyManaIntoCollector();
if(acceptsRedstone()) {
redstoneSignal = 0;
for(Direction dir : Direction.values()) {
int redstoneSide = getWorld().getRedstonePower(getPos().offset(dir), dir);
redstoneSignal = Math.max(redstoneSignal, redstoneSide);
}
}
if(getWorld().isRemote) {
double particleChance = 1F - (double) mana / (double) getMaxMana() / 3.5F;
Color color = new Color(getColor());
if(Math.random() > particleChance) {
Vec3d offset = getWorld().getBlockState(getPos()).getOffset(getWorld(), getPos());
double x = getPos().getX() + offset.x;
double y = getPos().getY() + offset.y;
double z = getPos().getZ() + offset.z;
BotaniaAPI.internalHandler.sparkleFX(getWorld(), x + 0.3 + Math.random() * 0.5, y + 0.5 + Math.random() * 0.5, z + 0.3 + Math.random() * 0.5, color.getRed() / 255F, color.getGreen() / 255F, color.getBlue() / 255F, (float) Math.random(), 5);
}
}
boolean passive = isPassiveFlower();
if(!getWorld().isRemote) {
int muhBalance = BotaniaAPI.internalHandler.getPassiveFlowerDecay();
if(passive && muhBalance > 0 && passiveDecayTicks > muhBalance) {
getWorld().destroyBlock(getPos(), false);
if(Blocks.DEAD_BUSH.getDefaultState().isValidPosition(getWorld(), getPos()))
getWorld().setBlockState(getPos(), Blocks.DEAD_BUSH.getDefaultState());
}
}
if(passive)
passiveDecayTicks++;
}
public void linkCollector() {
boolean needsNew = false;
if(linkedCollector == null) {
needsNew = true;
if(cachedCollectorCoordinates != null) {
needsNew = false;
if(getWorld().isBlockLoaded(cachedCollectorCoordinates)) {
needsNew = true;
TileEntity tileAt = getWorld().getTileEntity(cachedCollectorCoordinates);
if(tileAt instanceof IManaCollector && !tileAt.isRemoved()) {
linkedCollector = tileAt;
needsNew = false;
}
cachedCollectorCoordinates = null;
}
}
} else {
TileEntity tileAt = getWorld().getTileEntity(linkedCollector.getPos());
if(tileAt instanceof IManaCollector)
linkedCollector = tileAt;
}
if(needsNew && ticksExisted == 1) { // New flowers only
IManaNetwork network = BotaniaAPI.internalHandler.getManaNetworkInstance();
int size = network.getAllCollectorsInWorld(getWorld()).size();
if(BotaniaAPI.internalHandler.shouldForceCheck() || size != sizeLastCheck) {
linkedCollector = network.getClosestCollector(getPos(), getWorld(), LINK_RANGE);
sizeLastCheck = size;
}
}
}
public void linkToForcefully(TileEntity collector) {
linkedCollector = collector;
}
public void addMana(int mana) {
this.mana = Math.min(getMaxMana(), this.mana + mana);
}
public void emptyManaIntoCollector() {
if(linkedCollector != null && isValidBinding()) {
IManaCollector collector = (IManaCollector) linkedCollector;
if(!collector.isFull() && mana > 0) {
int manaval = Math.min(mana, collector.getMaxMana() - collector.getCurrentMana());
mana -= manaval;
collector.recieveMana(manaval);
}
}
}
public boolean isPassiveFlower() {
return false;
}
public boolean shouldSyncPassiveGeneration() {
return false;
}
public boolean canGeneratePassively() {
return false;
}
public int getDelayBetweenPassiveGeneration() {
return 20;
}
public int getValueForPassiveGeneration() {
return 1;
}
@Override
public List<ItemStack> getDrops(List<ItemStack> list, LootContext.Builder ctx) {
List<ItemStack> drops = super.getDrops(list, ctx);
populateDropStackNBTs(drops);
return drops;
}
public void populateDropStackNBTs(List<ItemStack> drops) {
if(isPassiveFlower() && ticksExisted > 0 && BotaniaAPI.internalHandler.getPassiveFlowerDecay() > 0) {
ItemStack drop = drops.get(0);
if(!drop.isEmpty()) {
drop.getOrCreateTag().putInt(TAG_PASSIVE_DECAY_TICKS, passiveDecayTicks);
}
}
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, BlockState state, LivingEntity entity, ItemStack stack) {
super.onBlockPlacedBy(world, pos, state, entity, stack);
if(isPassiveFlower()) {
CompoundNBT cmp = stack.getOrCreateTag();
passiveDecayTicks = cmp.getInt(TAG_PASSIVE_DECAY_TICKS);
}
}
@Override
public boolean onWanded(PlayerEntity player, ItemStack wand) {
if(player == null)
return false;
if(!player.world.isRemote)
sync();
knownMana = mana;
SoundEvent evt = ForgeRegistries.SOUND_EVENTS.getValue(DING_SOUND_EVENT);
if(evt != null)
player.playSound(evt, 0.1F, 1F);
return super.onWanded(player, wand);
}
public int getMaxMana() {
return 20;
}
public int getColor() {
return 0xFFFFFF;
}
@Override
public void readFromPacketNBT(CompoundNBT cmp) {
mana = cmp.getInt(TAG_MANA);
passiveDecayTicks = cmp.getInt(TAG_PASSIVE_DECAY_TICKS);
int x = cmp.getInt(TAG_COLLECTOR_X);
int y = cmp.getInt(TAG_COLLECTOR_Y);
int z = cmp.getInt(TAG_COLLECTOR_Z);
cachedCollectorCoordinates = y < 0 ? null : new BlockPos(x, y, z);
}
@Override
public void writeToPacketNBT(CompoundNBT cmp) {
cmp.putInt(TAG_MANA, mana);
cmp.putInt(TAG_TICKS_EXISTED, ticksExisted);
cmp.putInt(TAG_PASSIVE_DECAY_TICKS, passiveDecayTicks);
if(cachedCollectorCoordinates != null) {
cmp.putInt(TAG_COLLECTOR_X, cachedCollectorCoordinates.getX());
cmp.putInt(TAG_COLLECTOR_Y, cachedCollectorCoordinates.getY());
cmp.putInt(TAG_COLLECTOR_Z, cachedCollectorCoordinates.getZ());
} else {
int x = linkedCollector == null ? 0 : linkedCollector.getPos().getX();
int y = linkedCollector == null ? -1 : linkedCollector.getPos().getY();
int z = linkedCollector == null ? 0 : linkedCollector.getPos().getZ();
cmp.putInt(TAG_COLLECTOR_X, x);
cmp.putInt(TAG_COLLECTOR_Y, y);
cmp.putInt(TAG_COLLECTOR_Z, z);
}
}
@Override
public BlockPos getBinding() {
if(linkedCollector == null)
return null;
return linkedCollector.getPos();
}
@Override
public boolean canSelect(PlayerEntity player, ItemStack wand, BlockPos pos, Direction side) {
return true;
}
@Override
public boolean bindTo(PlayerEntity player, ItemStack wand, BlockPos pos, Direction side) {
int range = 6;
range *= range;
double dist = pos.distanceSq(getPos());
if(range >= dist) {
TileEntity tile = player.world.getTileEntity(pos);
if(tile instanceof IManaCollector) {
linkedCollector = tile;
return true;
}
}
return false;
}
public boolean isValidBinding() {
return linkedCollector != null && !linkedCollector.isRemoved() && getWorld().getTileEntity(linkedCollector.getPos()) == linkedCollector;
}
@OnlyIn(Dist.CLIENT)
@Override
public void renderHUD(Minecraft mc) {
String name = I18n.format(getBlockState().getBlock().getTranslationKey());
int color = getColor();
BotaniaAPI.internalHandler.drawComplexManaHUD(color, knownMana, getMaxMana(), name, BotaniaAPI.internalHandler.getBindDisplayForFlowerType(this), isValidBinding());
}
@Override
public boolean isOvergrowthAffected() {
return !isPassiveFlower();
}
}