-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wind turbine. Implement ability to lookup and efficiently store c…
…ached positions of event driven wind speeds on the server side.
- Loading branch information
Showing
30 changed files
with
541 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package weather2.block; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.TooltipFlag; | ||
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.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.block.state.StateDefinition; | ||
import net.minecraft.world.phys.shapes.CollisionContext; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
import net.minecraftforge.api.distmarker.Dist; | ||
import net.minecraftforge.api.distmarker.OnlyIn; | ||
import org.jetbrains.annotations.Nullable; | ||
import weather2.WeatherBlocks; | ||
import weather2.blockentity.WindTurbineBlockEntity; | ||
import weather2.blockentity.WindVaneBlockEntity; | ||
|
||
import java.util.List; | ||
|
||
public class WindTurbineBlock extends BaseEntityBlock { | ||
|
||
public static final VoxelShape SHAPE = box(2.0, 0.0, 2.0, 14.0, 32.0, 14.0); | ||
|
||
public static final void register() {} | ||
|
||
public WindTurbineBlock(Properties properties) { | ||
super(properties); | ||
} | ||
|
||
@Override | ||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) { | ||
|
||
} | ||
|
||
@Override | ||
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) { | ||
return SHAPE; | ||
} | ||
|
||
@Override | ||
public RenderShape getRenderShape(BlockState p_49232_) { | ||
return RenderShape.INVISIBLE; | ||
} | ||
|
||
@Override | ||
@OnlyIn(Dist.CLIENT) | ||
public void appendHoverText(ItemStack stack, BlockGetter worldIn, List<Component> tooltip, TooltipFlag flagIn) { | ||
super.appendHoverText(stack, worldIn, tooltip, flagIn); | ||
//tooltip.add(Component.translatable(this.getDescriptionId() + ".desc").withStyle(ChatFormatting.GRAY)); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity newBlockEntity(BlockPos p_153215_, BlockState p_153216_) { | ||
return new WindTurbineBlockEntity(p_153215_, p_153216_); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level p_153212_, BlockState p_153213_, BlockEntityType<T> p_153214_) { | ||
return createTickerHelper(p_153214_, WeatherBlocks.BLOCK_ENTITY_WIND_TURBINE.get(), WindTurbineBlockEntity::tick); | ||
} | ||
|
||
@Nullable | ||
@SuppressWarnings("unchecked") | ||
private static <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A> createTicker(final BlockEntityType<A> type, final BlockEntityType<E> tickerType, final BlockEntityTicker<? super E> ticker) { | ||
return tickerType == type ? (BlockEntityTicker<A>) ticker : null; | ||
} | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
src/main/java/weather2/blockentity/WindTurbineBlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package weather2.blockentity; | ||
|
||
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.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.phys.Vec3; | ||
import net.minecraftforge.common.capabilities.Capability; | ||
import net.minecraftforge.common.capabilities.ForgeCapabilities; | ||
import net.minecraftforge.common.util.LazyOptional; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import weather2.Weather; | ||
import weather2.WeatherBlocks; | ||
import weather2.WeatherItems; | ||
import weather2.energy.EnergyManager; | ||
import weather2.util.WeatherUtilEntity; | ||
import weather2.util.WindReader; | ||
|
||
public class WindTurbineBlockEntity extends BlockEntity { | ||
|
||
public float smoothAngle = 0; | ||
public float smoothAnglePrev = 0; | ||
|
||
public float smoothAngleRotationalVel = 0; | ||
|
||
public boolean isOutsideCached = false; | ||
|
||
//private final EnergyManager energyManager; | ||
private LazyOptional<EnergyManager> energy; | ||
private EnergyManager energyManager; | ||
|
||
//amount generated at windspeed of 1, theoretical max windspeed is 2 when tornado right on top of it | ||
private int maxNormalGenerated = 10; | ||
private int capacity = maxNormalGenerated * 2; | ||
private int maxTransfer = capacity; | ||
|
||
private float lastWindSpeed = 0; | ||
|
||
public WindTurbineBlockEntity(BlockPos p_155229_, BlockState p_155230_) { | ||
super(WeatherBlocks.BLOCK_ENTITY_WIND_TURBINE.get(), p_155229_, p_155230_); | ||
|
||
this.energyManager = new EnergyManager(maxTransfer, capacity); | ||
this.energy = LazyOptional.of(() -> this.energyManager); | ||
} | ||
|
||
@Override | ||
public void setLevel(final Level level) { | ||
super.setLevel(level); | ||
} | ||
|
||
public static void tick(Level level, BlockPos pos, BlockState state, WindTurbineBlockEntity entity) { | ||
entity.tick(level, pos, state); | ||
} | ||
|
||
public void tick(Level level, BlockPos pos, BlockState state) { | ||
if (!level.isClientSide) { | ||
if (level.getGameTime() % 100 == 0) { | ||
lastWindSpeed = WindReader.getWindSpeed(level, getBlockPos()); | ||
} | ||
this.energyManager.addEnergy((int) (maxNormalGenerated * lastWindSpeed)); | ||
outputEnergy(); | ||
} else { | ||
if (level.getGameTime() % 40 == 0) { | ||
isOutsideCached = WeatherUtilEntity.isPosOutside(level, new Vec3(getBlockPos().getX()+0.5F, getBlockPos().getY()+0.5F, getBlockPos().getZ()+0.5F)); | ||
} | ||
|
||
if (isOutsideCached) { | ||
float windSpeed = WindReader.getWindSpeed(level); | ||
float rotMax = 100F; | ||
float maxSpeed = (windSpeed / 2F) * rotMax; | ||
if (smoothAngleRotationalVel < maxSpeed) { | ||
smoothAngleRotationalVel += windSpeed * 0.3F; | ||
} | ||
if (smoothAngleRotationalVel > rotMax) smoothAngleRotationalVel = rotMax; | ||
if (smoothAngle >= 180) smoothAngle -= 360; | ||
} | ||
|
||
smoothAnglePrev = smoothAngle; | ||
smoothAngle += smoothAngleRotationalVel; | ||
smoothAngleRotationalVel -= 0.01F; | ||
|
||
smoothAngleRotationalVel *= 0.99F; | ||
|
||
if (smoothAngleRotationalVel <= 0) smoothAngleRotationalVel = 0; | ||
} | ||
} | ||
|
||
public void outputEnergy() { | ||
//System.out.println(this.energyManager.getEnergyStored()); | ||
if (this.energyManager.getEnergyStored() >= this.energyManager.getMaxExtract() && this.energyManager.canExtract()) { | ||
for (final var direction : Direction.values()) { | ||
final BlockEntity be = this.level.getBlockEntity(this.worldPosition.relative(direction)); | ||
if (be == null) { | ||
continue; | ||
} | ||
|
||
be.getCapability(ForgeCapabilities.ENERGY, direction.getOpposite()).ifPresent(storage -> { | ||
if (be != this && storage.getEnergyStored() < storage.getMaxEnergyStored()) { | ||
this.energyManager.drainEnergy(this.energyManager.getMaxExtract()); | ||
//Weather.LOGGER.info("Send: {}", this.energyManager.getMaxExtract()); | ||
final int received = storage.receiveEnergy(this.energyManager.getMaxExtract(), false); | ||
//Weather.LOGGER.info("Final Received: {}", received); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void load(final CompoundTag tag) { | ||
super.load(tag); | ||
} | ||
|
||
@Override | ||
protected void saveAdditional(final CompoundTag tag) { | ||
super.saveAdditional(tag); | ||
} | ||
|
||
@Override | ||
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { | ||
LazyOptional<T> energyCapability = energyManager.getCapability(cap); | ||
if (energyCapability.isPresent()) { | ||
return energyCapability; | ||
} | ||
return super.getCapability(cap, side); | ||
} | ||
|
||
@Override | ||
public void invalidateCaps() { | ||
super.invalidateCaps(); | ||
this.energy.invalidate(); | ||
} | ||
} |
Oops, something went wrong.