Skip to content

Commit

Permalink
Add wind turbine. Implement ability to lookup and efficiently store c…
Browse files Browse the repository at this point in the history
…ached positions of event driven wind speeds on the server side.
  • Loading branch information
Corosauce committed Nov 17, 2023
1 parent 8e7b37d commit c4d3b1a
Show file tree
Hide file tree
Showing 30 changed files with 541 additions and 97 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ dependencies {
minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"

implementation fg.deobf('com.corosus.coroutil:coroutil:1.20.1-1.3.0')
implementation fg.deobf('com.corosus.enderio:EnderIO:1.20.1-6.0.21-alpha')
implementation fg.deobf('com.corosus.pipez:pipez:1.20.1-1.1.5')
annotationProcessor 'org.spongepowered:mixin:0.8.5-SNAPSHOT:processor'
//implementation fg.deobf('com.lovetropics.ltweather:ltweather:1.20.1-1.0')
//implementation fg.deobf('com.lovetropics.minigames:LTMinigames:0.1.0-alpha+custom')
Expand Down
Binary file added libs/EnderIO-1.20.1-6.0.21-alpha.jar
Binary file not shown.
Binary file removed libs/LTMinigames-0.1.0-alpha+custom.jar
Binary file not shown.
Binary file removed libs/ltweather-1.20.1-1.0.jar
Binary file not shown.
Binary file added libs/pipez-1.20.1-1.1.5.jar
Binary file not shown.
4 changes: 4 additions & 0 deletions src/generated/resources/assets/minecraft/atlases/blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"type": "minecraft:single",
"resource": "weather2:blocks/wind_vane"
},
{
"type": "minecraft:single",
"resource": "weather2:blocks/wind_turbine"
},
{
"type": "minecraft:single",
"resource": "weather2:items/weather_item"
Expand Down
1 change: 1 addition & 0 deletions src/main/java/weather2/BlockAndItemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected void addSources()
addSpriteBlock("weather_machine");
addSpriteBlock("anemometer");
addSpriteBlock("wind_vane");
addSpriteBlock("wind_turbine");
addSpriteItem("weather_item");
addSpriteItem("sand_layer");
addSpriteItem("sand_layer_placeable");
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/weather2/ClientRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import weather2.client.AnemometerEntityRenderer;
import weather2.client.WindVaneEntityRenderer;
import weather2.client.tile.AnemometerEntityRenderer;
import weather2.client.tile.WindTurbineEntityRenderer;
import weather2.client.tile.WindVaneEntityRenderer;
import weather2.client.entity.model.AnemometerModel;
import weather2.client.entity.model.WindTurbineModel;
import weather2.client.entity.model.WindVaneModel;
import weather2.client.entity.render.LightningBoltWeatherNewRenderer;

Expand All @@ -27,13 +29,15 @@ public static void clientSetup(FMLClientSetupEvent event) {
public static void registerRenderers(EntityRenderersEvent.RegisterRenderers e) {
e.registerBlockEntityRenderer(WeatherBlocks.BLOCK_ENTITY_ANEMOMETER.get(), AnemometerEntityRenderer::new);
e.registerBlockEntityRenderer(WeatherBlocks.BLOCK_ENTITY_WIND_VANE.get(), WindVaneEntityRenderer::new);
e.registerBlockEntityRenderer(WeatherBlocks.BLOCK_ENTITY_WIND_TURBINE.get(), WindTurbineEntityRenderer::new);
}

@OnlyIn(Dist.CLIENT)
@SubscribeEvent
public static void registerLayerDefinitions(EntityRenderersEvent.RegisterLayerDefinitions event) {
event.registerLayerDefinition(WindVaneModel.LAYER_LOCATION, WindVaneModel::createBodyLayer);
event.registerLayerDefinition(AnemometerModel.LAYER_LOCATION, AnemometerModel::createBodyLayer);
event.registerLayerDefinition(WindTurbineModel.LAYER_LOCATION, WindTurbineModel::createBodyLayer);
}

}
5 changes: 3 additions & 2 deletions src/main/java/weather2/EventHandlerForge.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import weather2.client.SceneEnhancer;
Expand All @@ -39,14 +40,14 @@ public void worldRender(RenderLevelStageEvent event)
}
}

@SubscribeEvent
@SubscribeEvent(priority = EventPriority.LOWEST)
@OnlyIn(Dist.CLIENT)
public void onFogColors(ViewportEvent.ComputeFogColor event) {
SceneEnhancer.getFogAdjuster().onFogColors(event);

}

@SubscribeEvent
@SubscribeEvent(priority = EventPriority.LOWEST)
@OnlyIn(Dist.CLIENT)
public void onFogRender(ViewportEvent.RenderFog event) {
SceneEnhancer.getFogAdjuster().onFogRender(event);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/weather2/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class Weather
output.accept(WeatherItems.BLOCK_SAND_LAYER_ITEM.get());
output.accept(WeatherItems.BLOCK_ANEMOMETER_ITEM.get());
output.accept(WeatherItems.BLOCK_WIND_VANE_ITEM.get());
output.accept(WeatherItems.BLOCK_WIND_TURBINE_ITEM.get());
}).build());

public Weather() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/weather2/WeatherBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class WeatherBlocks {
public static final String SAND_LAYER_PLACEABLE = "sand_layer_placeable";
public static final String WEATHER_ITEM = "weather_item";
public static final String POCKET_SAND = "pocket_sand";
public static final String WIND_TURBINE = "wind_turbine";

private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Weather.MODID);
private static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITIES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, Weather.MODID);
Expand All @@ -46,6 +47,7 @@ public class WeatherBlocks {
public static final RegistryObject<AnemometerBlock> BLOCK_ANEMOMETER = BLOCKS.register(ANEMOMETER, () -> new AnemometerBlock(BlockBehaviour.Properties.of().mapColor(MapColor.STONE).randomTicks().strength(1.5F, 6F).requiresCorrectToolForDrops().sound(SoundType.STONE)));
public static final RegistryObject<WindVaneBlock> BLOCK_WIND_VANE = BLOCKS.register(WIND_VANE, () -> new WindVaneBlock(BlockBehaviour.Properties.of().mapColor(MapColor.STONE).randomTicks().strength(1.5F, 6F).requiresCorrectToolForDrops().sound(SoundType.STONE)));
public static final RegistryObject<SirenBlock> BLOCK_TORNADO_SIREN = BLOCKS.register(TORNADO_SIREN, () -> new SirenBlock(BlockBehaviour.Properties.of().mapColor(MapColor.STONE).randomTicks().strength(1.5F, 6F).requiresCorrectToolForDrops().sound(SoundType.STONE)));
public static final RegistryObject<WindTurbineBlock> BLOCK_WIND_TURBINE = BLOCKS.register(WIND_TURBINE, () -> new WindTurbineBlock(BlockBehaviour.Properties.of().mapColor(MapColor.STONE).randomTicks().strength(1.5F, 6F).requiresCorrectToolForDrops().sound(SoundType.STONE)));
//public static final RegistryObject<WeatherMachineBlock> BLOCK_WEATHER_MACHINE = BLOCKS.register(WEATHER_MACHINE, () -> new WeatherMachineBlock(BlockBehaviour.Properties.of(Material.STONE).randomTicks().strength(1.5F, 6F).requiresCorrectToolForDrops().sound(SoundType.STONE)));

@SuppressWarnings("ConstantConditions")
Expand All @@ -65,6 +67,9 @@ public class WeatherBlocks {
public static final RegistryObject<BlockEntityType<WindVaneBlockEntity>> BLOCK_ENTITY_WIND_VANE = BLOCK_ENTITIES.register(WIND_VANE, () ->
BlockEntityType.Builder.of(WindVaneBlockEntity::new, BLOCK_WIND_VANE.get()).build(null));

public static final RegistryObject<BlockEntityType<WindTurbineBlockEntity>> BLOCK_ENTITY_WIND_TURBINE = BLOCK_ENTITIES.register(WIND_TURBINE, () ->
BlockEntityType.Builder.of(WindTurbineBlockEntity::new, BLOCK_WIND_TURBINE.get()).build(null));

/*public static final RegistryObject<BlockEntityType<WeatherMachineBlockEntity>> BLOCK_ENTITY_WEATHER_MACHINE = BLOCK_ENTITIES.register(WEATHER_MACHINE, () ->
BlockEntityType.Builder.of(WeatherMachineBlockEntity::new, BLOCK_WEATHER_MACHINE.get()).build(null));*/

Expand Down
1 change: 1 addition & 0 deletions src/main/java/weather2/WeatherItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class WeatherItems {
public static final RegistryObject<BlockItem> BLOCK_FORECAST_ITEM = ITEMS.register(WeatherBlocks.WEATHER_FORECAST, () -> new BlockItem(WeatherBlocks.BLOCK_FORECAST.get(), new Item.Properties()));
public static final RegistryObject<BlockItem> BLOCK_ANEMOMETER_ITEM = ITEMS.register(WeatherBlocks.ANEMOMETER, () -> new BlockItem(WeatherBlocks.BLOCK_ANEMOMETER.get(), new Item.Properties()));
public static final RegistryObject<BlockItem> BLOCK_WIND_VANE_ITEM = ITEMS.register(WeatherBlocks.WIND_VANE, () -> new BlockItem(WeatherBlocks.BLOCK_WIND_VANE.get(), new Item.Properties()));
public static final RegistryObject<BlockItem> BLOCK_WIND_TURBINE_ITEM = ITEMS.register(WeatherBlocks.WIND_TURBINE, () -> new BlockItem(WeatherBlocks.BLOCK_WIND_TURBINE.get(), new Item.Properties()));

public static void registerHandlers(IEventBus modBus) {
ITEMS.register(modBus);
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/weather2/block/WindTurbineBlock.java
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 src/main/java/weather2/blockentity/WindTurbineBlockEntity.java
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();
}
}
Loading

0 comments on commit c4d3b1a

Please sign in to comment.