Make controller block a capprovider for energy. Pass energy value to block on place.
This commit is contained in:
@@ -5,16 +5,22 @@ import com.raoulvdberge.refinedstorage.tile.ControllerTile;
|
|||||||
import com.raoulvdberge.refinedstorage.util.BlockUtils;
|
import com.raoulvdberge.refinedstorage.util.BlockUtils;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.state.EnumProperty;
|
import net.minecraft.state.EnumProperty;
|
||||||
import net.minecraft.state.StateContainer;
|
import net.minecraft.state.StateContainer;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.util.BlockRenderLayer;
|
import net.minecraft.util.BlockRenderLayer;
|
||||||
import net.minecraft.util.IStringSerializable;
|
import net.minecraft.util.IStringSerializable;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.IBlockReader;
|
import net.minecraft.world.IBlockReader;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import net.minecraftforge.energy.CapabilityEnergy;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
// TODO - Fullbright models
|
// TODO - Fullbright models
|
||||||
|
// TODO DROPS
|
||||||
public class ControllerBlock extends Block {
|
public class ControllerBlock extends Block {
|
||||||
public enum Type {
|
public enum Type {
|
||||||
NORMAL,
|
NORMAL,
|
||||||
@@ -83,27 +89,27 @@ public class ControllerBlock extends Block {
|
|||||||
return new ControllerTile(type);
|
return new ControllerTile(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBlockPlacedBy(World world, BlockPos pos, BlockState state, @Nullable LivingEntity entity, ItemStack stack) {
|
||||||
|
super.onBlockPlacedBy(world, pos, state, entity, stack);
|
||||||
|
|
||||||
|
if (!world.isRemote) {
|
||||||
|
stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(energyFromStack -> {
|
||||||
|
TileEntity tile = world.getTileEntity(pos);
|
||||||
|
|
||||||
|
if (tile != null) {
|
||||||
|
tile.getCapability(CapabilityEnergy.ENERGY).ifPresent(energyFromTile -> energyFromTile.receiveEnergy(energyFromStack.getEnergyStored(), false));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@Override
|
@Override
|
||||||
public boolean onBlockActivated(World world, BlockPos pos, BlockState state, PlayerEntity player, EnumHand hand, Direction side, float hitX, float hitY, float hitZ) {
|
public boolean onBlockActivated(World world, BlockPos pos, BlockState state, PlayerEntity player, EnumHand hand, Direction side, float hitX, float hitY, float hitZ) {
|
||||||
return openNetworkGui(RSGui.CONTROLLER, player, world, pos, side);
|
return openNetworkGui(RSGui.CONTROLLER, player, world, pos, side);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBlockPlacedBy(World world, BlockPos pos, BlockState state, EntityLivingBase player, ItemStack stack) {
|
|
||||||
if (!world.isRemote) {
|
|
||||||
TileController controller = (TileController) world.getTileEntity(pos);
|
|
||||||
|
|
||||||
CompoundNBT tag = stack.getTagCompound();
|
|
||||||
|
|
||||||
if (tag != null && tag.hasKey(TileController.NBT_ENERGY)) {
|
|
||||||
controller.getEnergy().setStored(tag.getInteger(TileController.NBT_ENERGY));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
super.onBlockPlacedBy(world, pos, state, player, stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, BlockState state, int fortune) {
|
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, BlockState state, int fortune) {
|
||||||
ItemStack stack = new ItemStack(this, 1, getMetaFromState(state));
|
ItemStack stack = new ItemStack(this, 1, getMetaFromState(state));
|
||||||
|
@@ -4,6 +4,7 @@ import com.raoulvdberge.refinedstorage.RS;
|
|||||||
import com.raoulvdberge.refinedstorage.util.BlockUtils;
|
import com.raoulvdberge.refinedstorage.util.BlockUtils;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
|
|
||||||
|
// TODO DROPS
|
||||||
public class MachineCasingBlock extends Block {
|
public class MachineCasingBlock extends Block {
|
||||||
public MachineCasingBlock() {
|
public MachineCasingBlock() {
|
||||||
super(BlockUtils.DEFAULT_ROCK_PROPERTIES);
|
super(BlockUtils.DEFAULT_ROCK_PROPERTIES);
|
||||||
|
@@ -4,6 +4,7 @@ import com.raoulvdberge.refinedstorage.RS;
|
|||||||
import com.raoulvdberge.refinedstorage.util.BlockUtils;
|
import com.raoulvdberge.refinedstorage.util.BlockUtils;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
|
|
||||||
|
// TODO DROPS
|
||||||
public class QuartzEnrichedIronBlock extends Block {
|
public class QuartzEnrichedIronBlock extends Block {
|
||||||
public QuartzEnrichedIronBlock() {
|
public QuartzEnrichedIronBlock() {
|
||||||
super(BlockUtils.DEFAULT_ROCK_PROPERTIES);
|
super(BlockUtils.DEFAULT_ROCK_PROPERTIES);
|
||||||
|
@@ -23,7 +23,7 @@ public abstract class EnergyItem extends Item {
|
|||||||
private final boolean creative;
|
private final boolean creative;
|
||||||
|
|
||||||
public EnergyItem(Item.Properties properties, boolean creative, int energyCapacity) {
|
public EnergyItem(Item.Properties properties, boolean creative, int energyCapacity) {
|
||||||
super(properties.maxDamage(energyCapacity).maxStackSize(1));
|
super(properties);
|
||||||
|
|
||||||
this.creative = creative;
|
this.creative = creative;
|
||||||
this.energyCapacity = energyCapacity;
|
this.energyCapacity = energyCapacity;
|
||||||
@@ -35,13 +35,8 @@ public abstract class EnergyItem extends Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isDamageable() {
|
public boolean showDurabilityBar(ItemStack stack) {
|
||||||
return true;
|
return !creative;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean getIsRepairable(ItemStack a, ItemStack b) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,24 +59,12 @@ public abstract class EnergyItem extends Item {
|
|||||||
return MathHelper.hsvToRGB(Math.max(0.0F, (float) energy.getEnergyStored() / (float) energy.getMaxEnergyStored()) / 3.0F, 1.0F, 1.0F);
|
return MathHelper.hsvToRGB(Math.max(0.0F, (float) energy.getEnergyStored() / (float) energy.getMaxEnergyStored()) / 3.0F, 1.0F, 1.0F);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isDamaged(ItemStack stack) {
|
|
||||||
return !creative;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setDamage(ItemStack stack, int damage) {
|
|
||||||
// NO OP
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInformation(ItemStack stack, @Nullable World world, List<ITextComponent> tooltip, ITooltipFlag flag) {
|
public void addInformation(ItemStack stack, @Nullable World world, List<ITextComponent> tooltip, ITooltipFlag flag) {
|
||||||
super.addInformation(stack, world, tooltip, flag);
|
super.addInformation(stack, world, tooltip, flag);
|
||||||
|
|
||||||
if (!creative) {
|
if (!creative) {
|
||||||
stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(energy -> {
|
stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(energy -> tooltip.add(new TranslationTextComponent("misc.refinedstorage.energy_stored", energy.getEnergyStored(), energy.getMaxEnergyStored()).setStyle(new Style().setColor(TextFormatting.GRAY))));
|
||||||
tooltip.add(new TranslationTextComponent("misc.refinedstorage.energy_stored", energy.getEnergyStored(), energy.getMaxEnergyStored()).setStyle(new Style().setColor(TextFormatting.GRAY)));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,74 +2,12 @@ package com.raoulvdberge.refinedstorage.item.blockitem;
|
|||||||
|
|
||||||
import com.raoulvdberge.refinedstorage.RS;
|
import com.raoulvdberge.refinedstorage.RS;
|
||||||
import com.raoulvdberge.refinedstorage.block.ControllerBlock;
|
import com.raoulvdberge.refinedstorage.block.ControllerBlock;
|
||||||
import com.raoulvdberge.refinedstorage.tile.ControllerTile;
|
|
||||||
import net.minecraft.client.util.ITooltipFlag;
|
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
|
||||||
import net.minecraft.item.BlockItem;
|
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
import net.minecraft.nbt.CompoundNBT;
|
|
||||||
import net.minecraft.util.math.MathHelper;
|
|
||||||
import net.minecraft.util.text.ITextComponent;
|
|
||||||
import net.minecraft.util.text.Style;
|
|
||||||
import net.minecraft.util.text.TextFormatting;
|
|
||||||
import net.minecraft.util.text.TranslationTextComponent;
|
|
||||||
import net.minecraft.world.World;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
// TODO: Convert to energy cap.
|
|
||||||
public class ControllerBlockItem extends BlockItem {
|
|
||||||
private ControllerBlock controller;
|
|
||||||
|
|
||||||
|
public class ControllerBlockItem extends EnergyBlockItem {
|
||||||
public ControllerBlockItem(ControllerBlock block) {
|
public ControllerBlockItem(ControllerBlock block) {
|
||||||
super(block, new Item.Properties().group(RS.MAIN_GROUP).maxStackSize(1));
|
super(block, new Item.Properties().group(RS.MAIN_GROUP).maxStackSize(1), block.getType() == ControllerBlock.Type.CREATIVE, () -> RS.CONFIG.getController().getCapacity());
|
||||||
|
|
||||||
this.controller = block;
|
|
||||||
this.setRegistryName(block.getRegistryName());
|
this.setRegistryName(block.getRegistryName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getDurabilityForDisplay(ItemStack stack) {
|
|
||||||
return 1D - ((double) getEnergyStored(stack) / (double) RS.CONFIG.getController().getCapacity());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getRGBDurabilityForDisplay(ItemStack stack) {
|
|
||||||
return MathHelper.hsvToRGB(Math.max(0.0F, (float) getEnergyStored(stack) / (float) RS.CONFIG.getController().getCapacity()) / 3.0F, 1.0F, 1.0F);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean showDurabilityBar(ItemStack stack) {
|
|
||||||
return controller.getType() != ControllerBlock.Type.CREATIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addInformation(ItemStack stack, @Nullable World world, List<ITextComponent> tooltip, ITooltipFlag flag) {
|
|
||||||
super.addInformation(stack, world, tooltip, flag);
|
|
||||||
|
|
||||||
if (controller.getType() != ControllerBlock.Type.CREATIVE) {
|
|
||||||
tooltip.add(new TranslationTextComponent("misc.refinedstorage.energy_stored", getEnergyStored(stack), RS.CONFIG.getController().getCapacity()).setStyle(new Style().setColor(TextFormatting.GRAY)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getEnergyStored(ItemStack stack) {
|
|
||||||
return (stack.hasTag() && stack.getTag().contains(ControllerTile.NBT_ENERGY)) ? stack.getTag().getInt(ControllerTile.NBT_ENERGY) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreated(ItemStack stack, World world, PlayerEntity player) {
|
|
||||||
super.onCreated(stack, world, player);
|
|
||||||
|
|
||||||
CompoundNBT tag = stack.getTag();
|
|
||||||
|
|
||||||
if (tag == null) {
|
|
||||||
tag = new CompoundNBT();
|
|
||||||
}
|
|
||||||
|
|
||||||
tag.putInt(ControllerTile.NBT_ENERGY, 0);
|
|
||||||
|
|
||||||
stack.setTag(tag);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,73 @@
|
|||||||
|
package com.raoulvdberge.refinedstorage.item.blockitem;
|
||||||
|
|
||||||
|
import com.raoulvdberge.refinedstorage.item.capabilityprovider.CapabilityProviderEnergy;
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.client.util.ITooltipFlag;
|
||||||
|
import net.minecraft.item.BlockItem;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.nbt.CompoundNBT;
|
||||||
|
import net.minecraft.util.math.MathHelper;
|
||||||
|
import net.minecraft.util.text.ITextComponent;
|
||||||
|
import net.minecraft.util.text.Style;
|
||||||
|
import net.minecraft.util.text.TextFormatting;
|
||||||
|
import net.minecraft.util.text.TranslationTextComponent;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||||
|
import net.minecraftforge.energy.CapabilityEnergy;
|
||||||
|
import net.minecraftforge.energy.IEnergyStorage;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public abstract class EnergyBlockItem extends BlockItem {
|
||||||
|
private final Supplier<Integer> energyCapacity;
|
||||||
|
private final boolean creative;
|
||||||
|
|
||||||
|
public EnergyBlockItem(Block block, Item.Properties properties, boolean creative, Supplier<Integer> energyCapacity) {
|
||||||
|
super(block, properties);
|
||||||
|
|
||||||
|
this.creative = creative;
|
||||||
|
this.energyCapacity = energyCapacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT tag) {
|
||||||
|
return new CapabilityProviderEnergy(stack, energyCapacity.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean showDurabilityBar(ItemStack stack) {
|
||||||
|
return !creative;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getDurabilityForDisplay(ItemStack stack) {
|
||||||
|
IEnergyStorage energy = stack.getCapability(CapabilityEnergy.ENERGY).orElse(null);
|
||||||
|
if (energy == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1D - ((double) energy.getEnergyStored() / (double) energy.getMaxEnergyStored());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getRGBDurabilityForDisplay(ItemStack stack) {
|
||||||
|
IEnergyStorage energy = stack.getCapability(CapabilityEnergy.ENERGY, null).orElse(null);
|
||||||
|
if (energy == null) {
|
||||||
|
return super.getRGBDurabilityForDisplay(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
return MathHelper.hsvToRGB(Math.max(0.0F, (float) energy.getEnergyStored() / (float) energy.getMaxEnergyStored()) / 3.0F, 1.0F, 1.0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInformation(ItemStack stack, @Nullable World world, List<ITextComponent> tooltip, ITooltipFlag flag) {
|
||||||
|
super.addInformation(stack, world, tooltip, flag);
|
||||||
|
|
||||||
|
if (!creative) {
|
||||||
|
stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(energy -> tooltip.add(new TranslationTextComponent("misc.refinedstorage.energy_stored", energy.getEnergyStored(), energy.getMaxEnergyStored()).setStyle(new Style().setColor(TextFormatting.GRAY))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user