From f0e7e077c82569899ec853bb98fa523c0d4d3c0a Mon Sep 17 00:00:00 2001 From: ktpatient Date: Sun, 22 Dec 2024 22:17:18 +0000 Subject: [PATCH 1/2] Add Creative Energy (FE) Source Block --- .../CreativeEnergySourceBlockEntity.java | 45 +++++++++++++++++++ .../blocks/CreativeEnergySourceBlock.java | 41 +++++++++++++++++ .../nautec/registries/NTBlockEntityTypes.java | 3 ++ .../nautec/registries/NTBlocks.java | 2 + 4 files changed, 91 insertions(+) create mode 100644 src/main/java/com/portingdeadmods/nautec/content/blockentities/CreativeEnergySourceBlockEntity.java create mode 100644 src/main/java/com/portingdeadmods/nautec/content/blocks/CreativeEnergySourceBlock.java diff --git a/src/main/java/com/portingdeadmods/nautec/content/blockentities/CreativeEnergySourceBlockEntity.java b/src/main/java/com/portingdeadmods/nautec/content/blockentities/CreativeEnergySourceBlockEntity.java new file mode 100644 index 0000000..140206c --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/content/blockentities/CreativeEnergySourceBlockEntity.java @@ -0,0 +1,45 @@ +package com.portingdeadmods.nautec.content.blockentities; + +import com.portingdeadmods.nautec.registries.NTBlockEntityTypes; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.neoforged.neoforge.energy.IEnergyStorage; + +public class CreativeEnergySourceBlockEntity extends BlockEntity implements IEnergyStorage { + private static final int MAX_ENERGY = Integer.MAX_VALUE; + + public CreativeEnergySourceBlockEntity(BlockPos pos, BlockState blockState) { + super(NTBlockEntityTypes.CREATIVE_ENERGY_SOURCE.get(), pos, blockState); + } + + @Override + public int receiveEnergy(int toReceive, boolean simulate) { + return 0; + } + + @Override + public int extractEnergy(int toExtract, boolean simulate) { + return toExtract; + } + + @Override + public int getEnergyStored() { + return MAX_ENERGY; + } + + @Override + public int getMaxEnergyStored() { + return MAX_ENERGY; + } + + @Override + public boolean canExtract() { + return true; + } + + @Override + public boolean canReceive() { + return false; + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/content/blocks/CreativeEnergySourceBlock.java b/src/main/java/com/portingdeadmods/nautec/content/blocks/CreativeEnergySourceBlock.java new file mode 100644 index 0000000..b090fd1 --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/content/blocks/CreativeEnergySourceBlock.java @@ -0,0 +1,41 @@ +package com.portingdeadmods.nautec.content.blocks; + +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.util.RandomSource; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.EntityBlock; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import org.jetbrains.annotations.Nullable; + +public class CreativeEnergySourceBlock extends Block implements EntityBlock { + public CreativeEnergySourceBlock(Properties properties) { + super(properties); + } + + @Override + public @Nullable BlockEntity newBlockEntity(BlockPos pos, BlockState state) { + return null; + } + + @Override + protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) { + super.onPlace(state, level, pos, oldState, movedByPiston); + level.scheduleTick(pos, this, 1); + } + + @Override + protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) { + super.tick(state, level, pos, random); + level.scheduleTick(pos,this,1); + } + + @Override + protected boolean triggerEvent(BlockState state, Level level, BlockPos pos, int id, int param) { + super.triggerEvent(state, level, pos, id, param); + BlockEntity be = level.getBlockEntity(pos); + return be != null && be.triggerEvent(id, param); + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTBlockEntityTypes.java b/src/main/java/com/portingdeadmods/nautec/registries/NTBlockEntityTypes.java index 4393ade..136d2fd 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTBlockEntityTypes.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTBlockEntityTypes.java @@ -71,6 +71,9 @@ public final class NTBlockEntityTypes { public static final Supplier> CREATIVE_POWER_SOURCE = BLOCK_ENTITIES.register("creative_power_source", () -> BlockEntityType.Builder.of(CreativePowerSourceBlockEntity::new, NTBlocks.CREATIVE_POWER_SOURCE.get()).build(null)); + public static final Supplier> CREATIVE_ENERGY_SOURCE = BLOCK_ENTITIES.register("creative_energy_source", + () -> BlockEntityType.Builder.of(CreativeEnergySourceBlockEntity::new, + NTBlocks.CREATIVE_ENERGY_SOURCE.get()).build(null)); public static final Supplier> CHARGER = BLOCK_ENTITIES.register("charger", () -> BlockEntityType.Builder.of(ChargerBlockEntity::new, NTBlocks.CHARGER.get()).build(null)); diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTBlocks.java b/src/main/java/com/portingdeadmods/nautec/registries/NTBlocks.java index 520fd49..c1668d5 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTBlocks.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTBlocks.java @@ -112,6 +112,8 @@ public final class NTBlocks { public static final DeferredBlock CREATIVE_POWER_SOURCE = registerBlockAndItem("creative_power_source", CreativePowerSourceBlock::new, BlockBehaviour.Properties.ofFullCopy(Blocks.IRON_BLOCK), false, true); + public static final DeferredBlock CREATIVE_ENERGY_SOURCE = registerBlockAndItem("creative_energy_source", CreativeEnergySourceBlock::new, + BlockBehaviour.Properties.ofFullCopy(Blocks.IRON_BLOCK), false, true); private static DeferredBlock registerBlockAndItem(String name, Function blockConstructor, BlockBehaviour.Properties properties) { return registerBlockAndItem(name, blockConstructor, properties, true, true); From b2f967aa28f974a9a6c4fad19aca4206d5227f7f Mon Sep 17 00:00:00 2001 From: ktpatient Date: Sun, 22 Dec 2024 22:33:58 +0000 Subject: [PATCH 2/2] Energy Converter --- .../EnergyConverterBlockEntity.java | 92 +++++++++++++++++++ .../content/blocks/EnergyConverterBlock.java | 20 ++++ .../nautec/registries/NTBlockEntityTypes.java | 3 + .../nautec/registries/NTBlocks.java | 2 + 4 files changed, 117 insertions(+) create mode 100644 src/main/java/com/portingdeadmods/nautec/content/blockentities/EnergyConverterBlockEntity.java create mode 100644 src/main/java/com/portingdeadmods/nautec/content/blocks/EnergyConverterBlock.java diff --git a/src/main/java/com/portingdeadmods/nautec/content/blockentities/EnergyConverterBlockEntity.java b/src/main/java/com/portingdeadmods/nautec/content/blockentities/EnergyConverterBlockEntity.java new file mode 100644 index 0000000..98afe9a --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/content/blockentities/EnergyConverterBlockEntity.java @@ -0,0 +1,92 @@ +package com.portingdeadmods.nautec.content.blockentities; + +import com.portingdeadmods.nautec.api.blockentities.LaserBlockEntity; +import com.portingdeadmods.nautec.capabilities.IOActions; +import com.portingdeadmods.nautec.registries.NTBlockEntityTypes; +import it.unimi.dsi.fastutil.Pair; +import it.unimi.dsi.fastutil.objects.ObjectSet; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.world.level.block.state.BlockState; +import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.energy.IEnergyStorage; +import org.jetbrains.annotations.Nullable; + +import java.util.Map; + +public class EnergyConverterBlockEntity extends LaserBlockEntity implements IEnergyStorage { + private static final int FE_CONVERSION_RATE = 100; + private int feStored = 0; + private static final int MAX_FE = 100000; + + public EnergyConverterBlockEntity(BlockPos blockPos, BlockState blockState) { + super(NTBlockEntityTypes.ENERGY_CONVERTER.get(), blockPos, blockState); + } + + @Override + public ObjectSet getLaserInputs() { + return ObjectSet.of(); + } + + @Override + public ObjectSet getLaserOutputs() { + return ObjectSet.of(Direction.values()); + } + + @Override + public Map> getSidedInteractions(BlockCapability capability) { + return Map.of(); + } + + @Override + public void commonTick() { + if (!level.isClientSide) { + Direction[] directions = Direction.values(); + for (Direction direction : directions) { + BlockPos pos = worldPosition.relative(direction); + if (level.getBlockEntity(pos) instanceof LaserBlockEntity laserBlockEntity) { + if (laserBlockEntity.getLaserInputs().contains(direction.getOpposite())) { + int energyToConvert = Math.min(FE_CONVERSION_RATE, feStored); + if (energyToConvert > 0) { + transmitPower(energyToConvert); + feStored -= energyToConvert; + } + } + } + } + } + } + @Override + public int receiveEnergy(int maxReceive, boolean simulate) { + int energyReceived = Math.min(MAX_FE - feStored, maxReceive); + if (!simulate) { + feStored += energyReceived; + } + return energyReceived; + } + + @Override + public int extractEnergy(int maxExtract, boolean simulate) { + return 0; + } + + @Override + public int getEnergyStored() { + return feStored; + } + + @Override + public int getMaxEnergyStored() { + return MAX_FE; + } + + @Override + public boolean canExtract() { + return false; + } + + @Override + public boolean canReceive() { + return true; + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/content/blocks/EnergyConverterBlock.java b/src/main/java/com/portingdeadmods/nautec/content/blocks/EnergyConverterBlock.java new file mode 100644 index 0000000..5c77886 --- /dev/null +++ b/src/main/java/com/portingdeadmods/nautec/content/blocks/EnergyConverterBlock.java @@ -0,0 +1,20 @@ +package com.portingdeadmods.nautec.content.blocks; + +import com.portingdeadmods.nautec.content.blockentities.EnergyConverterBlockEntity; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.EntityBlock; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import org.jetbrains.annotations.Nullable; + +public class EnergyConverterBlock extends Block implements EntityBlock { + public EnergyConverterBlock(Properties properties) { + super(properties); + } + + @Override + public @Nullable BlockEntity newBlockEntity(BlockPos pos, BlockState state) { + return new EnergyConverterBlockEntity(pos,state); + } +} diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTBlockEntityTypes.java b/src/main/java/com/portingdeadmods/nautec/registries/NTBlockEntityTypes.java index 136d2fd..91ae690 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTBlockEntityTypes.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTBlockEntityTypes.java @@ -74,6 +74,9 @@ public final class NTBlockEntityTypes { public static final Supplier> CREATIVE_ENERGY_SOURCE = BLOCK_ENTITIES.register("creative_energy_source", () -> BlockEntityType.Builder.of(CreativeEnergySourceBlockEntity::new, NTBlocks.CREATIVE_ENERGY_SOURCE.get()).build(null)); + public static final Supplier> ENERGY_CONVERTER = BLOCK_ENTITIES.register("energy_converter", + () -> BlockEntityType.Builder.of(EnergyConverterBlockEntity::new, + NTBlocks.ENERGY_CONVERTER.get()).build(null)); public static final Supplier> CHARGER = BLOCK_ENTITIES.register("charger", () -> BlockEntityType.Builder.of(ChargerBlockEntity::new, NTBlocks.CHARGER.get()).build(null)); diff --git a/src/main/java/com/portingdeadmods/nautec/registries/NTBlocks.java b/src/main/java/com/portingdeadmods/nautec/registries/NTBlocks.java index c1668d5..e9f4cf9 100644 --- a/src/main/java/com/portingdeadmods/nautec/registries/NTBlocks.java +++ b/src/main/java/com/portingdeadmods/nautec/registries/NTBlocks.java @@ -114,6 +114,8 @@ public final class NTBlocks { BlockBehaviour.Properties.ofFullCopy(Blocks.IRON_BLOCK), false, true); public static final DeferredBlock CREATIVE_ENERGY_SOURCE = registerBlockAndItem("creative_energy_source", CreativeEnergySourceBlock::new, BlockBehaviour.Properties.ofFullCopy(Blocks.IRON_BLOCK), false, true); + public static final DeferredBlock ENERGY_CONVERTER = registerBlockAndItem("energy_converter", EnergyConverterBlock::new, + BlockBehaviour.Properties.ofFullCopy(Blocks.IRON_BLOCK), false, true); private static DeferredBlock registerBlockAndItem(String name, Function blockConstructor, BlockBehaviour.Properties properties) { return registerBlockAndItem(name, blockConstructor, properties, true, true);