2018-10-21 12:51:13 +02:00
package de.ellpeck.naturesaura.events ;
2018-12-20 21:43:19 +01:00
import de.ellpeck.naturesaura.Helper ;
2018-11-12 00:32:35 +01:00
import de.ellpeck.naturesaura.ModConfig ;
2018-10-21 12:51:13 +02:00
import de.ellpeck.naturesaura.NaturesAura ;
2018-11-12 22:04:40 +01:00
import de.ellpeck.naturesaura.api.NaturesAuraAPI ;
2018-12-20 21:43:19 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk ;
2018-11-12 01:29:33 +01:00
import de.ellpeck.naturesaura.api.aura.type.IAuraType ;
2018-11-13 11:39:28 +01:00
import de.ellpeck.naturesaura.chunk.AuraChunk ;
2019-02-17 22:51:05 +01:00
import de.ellpeck.naturesaura.misc.WorldData ;
2018-10-21 13:12:03 +02:00
import de.ellpeck.naturesaura.packet.PacketHandler ;
2018-10-21 12:51:13 +02:00
import net.minecraft.util.ResourceLocation ;
2019-02-17 22:51:05 +01:00
import net.minecraft.world.World ;
2018-10-21 12:51:13 +02:00
import net.minecraft.world.WorldServer ;
import net.minecraft.world.chunk.Chunk ;
2018-10-22 20:18:54 +02:00
import net.minecraftforge.common.config.Config ;
import net.minecraftforge.common.config.ConfigManager ;
2018-10-21 12:51:13 +02:00
import net.minecraftforge.event.AttachCapabilitiesEvent ;
2018-10-21 13:12:03 +02:00
import net.minecraftforge.event.world.ChunkWatchEvent ;
2018-10-22 20:18:54 +02:00
import net.minecraftforge.fml.client.event.ConfigChangedEvent.OnConfigChangedEvent ;
2018-10-21 12:51:13 +02:00
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent ;
import net.minecraftforge.fml.common.gameevent.TickEvent ;
import java.util.Iterator ;
public class CommonEvents {
@SubscribeEvent
public void onChunkCapsAttach ( AttachCapabilitiesEvent < Chunk > event ) {
2018-11-07 23:42:13 +01:00
Chunk chunk = event . getObject ( ) ;
2019-04-13 13:28:39 +02:00
World world = chunk . getWorld ( ) ;
if ( world = = null ) {
NaturesAura . LOGGER . warn ( " Chunk with null world found. This is most likely due to OptiFine doing stuff that should really be disallowed. I don't know if this will cause any further issues, but hopefully not. " ) ;
return ;
}
IAuraType type = IAuraType . forWorld ( world ) ;
2018-11-07 23:42:13 +01:00
event . addCapability ( new ResourceLocation ( NaturesAura . MOD_ID , " aura " ) , new AuraChunk ( chunk , type ) ) ;
2018-10-21 12:51:13 +02:00
}
2019-02-17 22:51:05 +01:00
@SubscribeEvent
public void onWorldCapsAttach ( AttachCapabilitiesEvent < World > event ) {
2019-02-21 12:27:54 +01:00
event . addCapability ( new ResourceLocation ( NaturesAura . MOD_ID , " data " ) , new WorldData ( ) ) ;
2019-02-17 22:51:05 +01:00
}
2018-10-21 12:51:13 +02:00
@SubscribeEvent
public void onWorldTick ( TickEvent . WorldTickEvent event ) {
if ( ! event . world . isRemote & & event . phase = = TickEvent . Phase . END ) {
if ( event . world . getTotalWorldTime ( ) % 20 = = 0 ) {
2018-10-26 20:25:21 +02:00
event . world . profiler . func_194340_a ( ( ) - > NaturesAura . MOD_ID + " :onWorldTick " ) ;
2018-10-21 12:51:13 +02:00
Iterator < Chunk > chunks = event . world . getPersistentChunkIterable ( ( ( WorldServer ) event . world ) . getPlayerChunkMap ( ) . getChunkIterator ( ) ) ;
while ( chunks . hasNext ( ) ) {
Chunk chunk = chunks . next ( ) ;
2018-11-12 22:04:40 +01:00
if ( chunk . hasCapability ( NaturesAuraAPI . capAuraChunk , null ) ) {
AuraChunk auraChunk = ( AuraChunk ) chunk . getCapability ( NaturesAuraAPI . capAuraChunk , null ) ;
2018-10-21 12:51:13 +02:00
auraChunk . update ( ) ;
}
}
2018-10-26 20:25:21 +02:00
event . world . profiler . endSection ( ) ;
2018-10-21 12:51:13 +02:00
}
}
}
2018-10-21 13:12:03 +02:00
2018-12-20 21:43:19 +01:00
@SubscribeEvent
public void onPlayerTick ( TickEvent . PlayerTickEvent event ) {
if ( ! event . player . world . isRemote & & event . phase = = TickEvent . Phase . END ) {
if ( event . player . world . getTotalWorldTime ( ) % 200 ! = 0 )
return ;
int aura = IAuraChunk . triangulateAuraInArea ( event . player . world , event . player . getPosition ( ) , 25 ) ;
if ( aura < = 0 )
Helper . addAdvancement ( event . player , new ResourceLocation ( NaturesAura . MOD_ID , " negative_imbalance " ) , " triggered_in_code " ) ;
2019-02-03 22:05:56 +01:00
else if ( aura > = 1500000 )
2018-12-20 21:43:19 +01:00
Helper . addAdvancement ( event . player , new ResourceLocation ( NaturesAura . MOD_ID , " positive_imbalance " ) , " triggered_in_code " ) ;
}
}
2018-10-21 13:12:03 +02:00
@SubscribeEvent
public void onChunkWatch ( ChunkWatchEvent . Watch event ) {
Chunk chunk = event . getChunkInstance ( ) ;
2018-11-12 22:04:40 +01:00
if ( ! chunk . getWorld ( ) . isRemote & & chunk . hasCapability ( NaturesAuraAPI . capAuraChunk , null ) ) {
AuraChunk auraChunk = ( AuraChunk ) chunk . getCapability ( NaturesAuraAPI . capAuraChunk , null ) ;
2018-10-21 13:12:03 +02:00
PacketHandler . sendTo ( event . getPlayer ( ) , auraChunk . makePacket ( ) ) ;
}
}
2018-10-22 20:18:54 +02:00
@SubscribeEvent
public void onConfigChanged ( OnConfigChangedEvent event ) {
if ( NaturesAura . MOD_ID . equals ( event . getModID ( ) ) ) {
ConfigManager . sync ( NaturesAura . MOD_ID , Config . Type . INSTANCE ) ;
2018-11-20 19:59:18 +01:00
ModConfig . initOrReload ( true ) ;
2018-10-22 20:18:54 +02:00
}
}
2018-10-21 12:51:13 +02:00
}