Solar Panel re-implementation

This commit is contained in:
2022-09-24 13:25:29 +02:00
parent 5697847d74
commit 6c22a10e93
12 changed files with 218 additions and 5 deletions

View File

@@ -0,0 +1,69 @@
package me.hypherionmc.hyperlighting.common.blockentities;
import me.hypherionmc.craterlib.api.blockentities.ITickable;
import me.hypherionmc.craterlib.api.blockentities.caps.ForgeCapability;
import me.hypherionmc.craterlib.common.blockentity.CraterBlockEntity;
import me.hypherionmc.craterlib.systems.energy.CustomEnergyStorage;
import me.hypherionmc.hyperlighting.common.blocks.SolarPanel;
import me.hypherionmc.hyperlighting.common.init.HLBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Mth;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Optional;
/**
* @author HypherionSA
* @date 18/09/2022
*/
public class SolarPanelBlockEntity extends CraterBlockEntity implements ITickable {
private CustomEnergyStorage energyStorage = new CustomEnergyStorage(2000, 50, 1000);
public SolarPanelBlockEntity(BlockPos pos, BlockState state) {
super(HLBlockEntities.SOLAR_PANEL.get(), pos, state);
}
@Override
public void tick(Level level, BlockPos blockPos, BlockState blockState, BlockEntity blockEntity) {
if (this.getBlockState().getBlock() instanceof SolarPanel && level.isDay() && level.canSeeSky(blockPos)) {
int i = level.getBrightness(LightLayer.SKY, blockPos) - level.getSkyDarken();
float f = level.getSunAngle(1.0F);
if (i > 8 && this.energyStorage.getPowerLevel() < this.energyStorage.getPowerCapacity()) {
float f1 = f < (float) Math.PI ? 0.0F : ((float) Math.PI * 2F);
f = f + (f1 - f) * 0.2F;
i = Math.round((float) i * Mth.cos(f));
i = Mth.clamp(i, 0, 15);
this.energyStorage.receiveEnergyInternal(i, false);
}
this.sendUpdates();
}
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
this.energyStorage.readNBT(tag);
}
@Override
public void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
this.energyStorage.writeNBT(tag);
}
@Override
public <T> Optional<T> getForgeCapability(ForgeCapability forgeCapability, Direction direction) {
if (forgeCapability == ForgeCapability.ENERGY && (direction == Direction.DOWN || direction == null)) {
return (Optional<T>) Optional.of(energyStorage);
}
return Optional.empty();
}
}

View File

@@ -0,0 +1,63 @@
package me.hypherionmc.hyperlighting.common.blocks;
import me.hypherionmc.hyperlighting.common.blockentities.SolarPanelBlockEntity;
import me.hypherionmc.hyperlighting.common.init.CommonRegistration;
import me.hypherionmc.hyperlighting.common.init.HLItems;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
/**
* @author HypherionSA
* @date 18/09/2022
*/
public class SolarPanel extends BaseEntityBlock {
private final VoxelShape BOUNDS = Block.box(0, 0, 0, 16, 1.92, 16);
public SolarPanel(String name) {
super(Properties.of(Material.GLASS).sound(SoundType.GLASS).noCollission().noOcclusion());
HLItems.register(name, () -> new BlockItem(this, new Item.Properties().tab(CommonRegistration.MACHINES_TAB)));
}
@Override
public VoxelShape getShape(BlockState state, BlockGetter blockGetter, BlockPos pos, CollisionContext context) {
return BOUNDS;
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> blockEntityType) {
return (level1, blockPos, blockState1, t) -> {
if (!level1.isClientSide() && t instanceof SolarPanelBlockEntity be) {
be.tick(level1, blockPos, blockState1, be);
}
};
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return new SolarPanelBlockEntity(blockPos, blockState);
}
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
}

View File

@@ -12,6 +12,8 @@ public class CommonRegistration {
public static HyperLightingClientConfig config = new HyperLightingClientConfig();
public static final CreativeModeTab LIGHTS_TAB = CreativeTabBuilder.builder(MOD_ID, "lighting").setIcon(() -> new ItemStack(HLBlocks.ADVANCED_LANTERN)).build();
public static final CreativeModeTab MACHINES_TAB = CreativeTabBuilder.builder(MOD_ID, "machines").setIcon(() -> new ItemStack(HLBlocks.ADVANCED_TORCH)).build();
public static void registerAll() {
HLSounds.loadAll();

View File

@@ -4,6 +4,7 @@ import me.hypherionmc.craterlib.systems.reg.RegistrationProvider;
import me.hypherionmc.craterlib.systems.reg.RegistryObject;
import me.hypherionmc.hyperlighting.Constants;
import me.hypherionmc.hyperlighting.common.blockentities.AdvancedCampfireBlockEntity;
import me.hypherionmc.hyperlighting.common.blockentities.SolarPanelBlockEntity;
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.entity.BlockEntityType;
@@ -17,5 +18,7 @@ public class HLBlockEntities {
public static RegistryObject<BlockEntityType<AdvancedCampfireBlockEntity>> CAMPFIRE = BE.register("campfire", () -> BlockEntityType.Builder.of(AdvancedCampfireBlockEntity::new, HLBlocks.ADVANCED_CAMPFIRE.get()).build(null));
public static RegistryObject<BlockEntityType<SolarPanelBlockEntity>> SOLAR_PANEL = BE.register("solar_panel", () -> BlockEntityType.Builder.of(SolarPanelBlockEntity::new, HLBlocks.SOLAR_PANEL.get()).build(null));
public static void loadAll() {}
}

View File

@@ -3,10 +3,7 @@ package me.hypherionmc.hyperlighting.common.init;
import me.hypherionmc.craterlib.systems.reg.BlockRegistryObject;
import me.hypherionmc.craterlib.systems.reg.RegistrationProvider;
import me.hypherionmc.hyperlighting.Constants;
import me.hypherionmc.hyperlighting.common.blocks.AdvancedCandleBlock;
import me.hypherionmc.hyperlighting.common.blocks.AdvancedCampfire;
import me.hypherionmc.hyperlighting.common.blocks.AdvancedLanternBlock;
import me.hypherionmc.hyperlighting.common.blocks.AdvancedTorchBlock;
import me.hypherionmc.hyperlighting.common.blocks.*;
import net.minecraft.core.Registry;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.Block;
@@ -30,6 +27,10 @@ public class HLBlocks {
/* Candles */
public static BlockRegistryObject<Block> ADVANCED_CANDLE = register("advanced_candle", () -> new AdvancedCandleBlock("advanced_candle", DyeColor.ORANGE, CommonRegistration.LIGHTS_TAB));
/* Machines */
public static BlockRegistryObject<Block> SOLAR_PANEL = register("solar_panel", () -> new SolarPanel("solar_panel"));
public static void loadAll() {}
public static <B extends Block> BlockRegistryObject<B> register(String name, Supplier<? extends B> block) {

View File

@@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "hyperlighting:block/solar_panel" }
}
}

View File

@@ -0,0 +1,22 @@
{
"credit": "Made with Blockbench",
"texture_size": [16, 32],
"textures": {
"0": "hyperlighting:block/solar",
"particle": "hyperlighting:block/solar"
},
"elements": [
{
"from": [0, 0, 0],
"to": [16, 2, 16],
"faces": {
"north": {"uv": [0, 8, 16, 10], "texture": "#0"},
"east": {"uv": [0, 8, 16, 10], "texture": "#0"},
"south": {"uv": [0, 8, 16, 10], "texture": "#0"},
"west": {"uv": [0, 8, 16, 10], "texture": "#0"},
"up": {"uv": [0, 0, 16, 8], "texture": "#0"},
"down": {"uv": [0, 8, 16, 16], "texture": "#0"}
}
}
]
}

View File

@@ -0,0 +1,3 @@
{
"parent": "hyperlighting:block/solar_panel"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 781 B