2018-10-21 12:51:13 +02:00
|
|
|
package de.ellpeck.naturesaura.events;
|
|
|
|
|
2020-03-01 19:25:39 +01:00
|
|
|
import com.google.common.collect.ArrayListMultimap;
|
|
|
|
import com.google.common.collect.ListMultimap;
|
2018-12-20 21:43:19 +01:00
|
|
|
import de.ellpeck.naturesaura.Helper;
|
2020-09-22 15:49:06 +02: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;
|
2020-03-14 03:10:56 +01:00
|
|
|
import de.ellpeck.naturesaura.api.misc.IWorldData;
|
2018-11-13 11:39:28 +01:00
|
|
|
import de.ellpeck.naturesaura.chunk.AuraChunk;
|
2019-04-14 11:22:11 +02:00
|
|
|
import de.ellpeck.naturesaura.chunk.AuraChunkProvider;
|
2020-02-26 20:01:57 +01:00
|
|
|
import de.ellpeck.naturesaura.commands.CommandAura;
|
2020-09-22 15:49:06 +02:00
|
|
|
import de.ellpeck.naturesaura.gen.ModFeatures;
|
2019-02-17 22:51:05 +01:00
|
|
|
import de.ellpeck.naturesaura.misc.WorldData;
|
2020-01-22 23:21:52 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
2020-03-14 13:54:31 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
2020-03-01 19:25:39 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2020-03-14 03:10:56 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2018-10-21 12:51:13 +02:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2020-03-01 19:25:39 +01:00
|
|
|
import net.minecraft.util.math.ChunkPos;
|
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.chunk.Chunk;
|
2020-10-19 03:05:13 +02:00
|
|
|
import net.minecraft.world.gen.GenerationStage.Decoration;
|
2020-01-22 23:21:52 +01:00
|
|
|
import net.minecraft.world.server.ChunkHolder;
|
|
|
|
import net.minecraft.world.server.ChunkManager;
|
|
|
|
import net.minecraft.world.server.ServerChunkProvider;
|
2018-10-21 12:51:13 +02:00
|
|
|
import net.minecraftforge.event.AttachCapabilitiesEvent;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraftforge.event.TickEvent;
|
2020-03-14 03:10:56 +01:00
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
2020-09-22 15:49:06 +02:00
|
|
|
import net.minecraftforge.event.world.BiomeLoadingEvent;
|
2018-10-21 13:12:03 +02:00
|
|
|
import net.minecraftforge.event.world.ChunkWatchEvent;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
2020-01-22 23:21:52 +01:00
|
|
|
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
|
2020-02-26 20:01:57 +01:00
|
|
|
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
|
2020-01-22 23:21:52 +01:00
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2020-03-01 19:25:39 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
2018-10-21 12:51:13 +02:00
|
|
|
|
|
|
|
public class CommonEvents {
|
|
|
|
|
2020-01-22 23:21:52 +01:00
|
|
|
private static final Method GET_LOADED_CHUNKS_METHOD = ObfuscationReflectionHelper.findMethod(ChunkManager.class, "func_223491_f");
|
2020-03-01 19:25:39 +01:00
|
|
|
private static final ListMultimap<UUID, ChunkPos> PENDING_AURA_CHUNKS = ArrayListMultimap.create();
|
2020-01-22 23:21:52 +01:00
|
|
|
|
2020-09-22 15:49:06 +02:00
|
|
|
@SubscribeEvent
|
|
|
|
public void onBiomeLoad(BiomeLoadingEvent event) {
|
|
|
|
if (ModConfig.instance.auraBlooms.get()) {
|
2020-12-02 19:53:14 +01:00
|
|
|
event.getGeneration().func_242513_a(Decoration.VEGETAL_DECORATION, ModFeatures.Configured.AURA_BLOOM);
|
2020-10-19 03:05:13 +02:00
|
|
|
switch (event.getCategory()) {
|
|
|
|
case DESERT:
|
2020-12-02 19:53:14 +01:00
|
|
|
event.getGeneration().func_242513_a(Decoration.VEGETAL_DECORATION, ModFeatures.Configured.AURA_CACTUS);
|
2020-10-19 03:05:13 +02:00
|
|
|
break;
|
|
|
|
case NETHER:
|
2020-12-02 19:53:14 +01:00
|
|
|
event.getGeneration().func_242513_a(Decoration.VEGETAL_DECORATION, ModFeatures.Configured.CRIMSON_AURA_MUSHROOM);
|
|
|
|
event.getGeneration().func_242513_a(Decoration.VEGETAL_DECORATION, ModFeatures.Configured.WARPED_AURA_MUSHROOM);
|
2020-10-19 03:05:13 +02:00
|
|
|
break;
|
|
|
|
case MUSHROOM:
|
2020-12-02 19:53:14 +01:00
|
|
|
event.getGeneration().func_242513_a(Decoration.VEGETAL_DECORATION, ModFeatures.Configured.AURA_MUSHROOM);
|
2020-10-19 03:05:13 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-09-22 15:49:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:51:13 +02:00
|
|
|
@SubscribeEvent
|
|
|
|
public void onChunkCapsAttach(AttachCapabilitiesEvent<Chunk> event) {
|
2018-11-07 23:42:13 +01:00
|
|
|
Chunk chunk = event.getObject();
|
2019-04-14 11:22:11 +02:00
|
|
|
event.addCapability(new ResourceLocation(NaturesAura.MOD_ID, "aura"), new AuraChunkProvider(chunk));
|
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
|
|
|
}
|
|
|
|
|
2020-03-14 03:10:56 +01:00
|
|
|
@SubscribeEvent
|
|
|
|
public void onItemUse(PlayerInteractEvent.RightClickBlock event) {
|
|
|
|
PlayerEntity player = event.getPlayer();
|
|
|
|
if (player.world.isRemote)
|
|
|
|
return;
|
|
|
|
ItemStack held = event.getItemStack();
|
|
|
|
if (!held.isEmpty() && held.getItem().getRegistryName().getPath().contains("chisel")) {
|
2020-03-14 13:54:31 +01:00
|
|
|
BlockState state = player.world.getBlockState(event.getPos());
|
|
|
|
if (NaturesAuraAPI.BOTANIST_PICKAXE_CONVERSIONS.containsKey(state)) {
|
|
|
|
WorldData data = (WorldData) IWorldData.getWorldData(player.world);
|
|
|
|
data.addMossStone(event.getPos());
|
|
|
|
}
|
2020-03-14 03:10:56 +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) {
|
2020-01-21 21:04:44 +01:00
|
|
|
if (event.world.getGameTime() % 20 == 0) {
|
2020-01-22 23:21:52 +01:00
|
|
|
event.world.getProfiler().startSection(NaturesAura.MOD_ID + ":onWorldTick");
|
|
|
|
try {
|
|
|
|
ChunkManager manager = ((ServerChunkProvider) event.world.getChunkProvider()).chunkManager;
|
|
|
|
Iterable<ChunkHolder> chunks = (Iterable<ChunkHolder>) GET_LOADED_CHUNKS_METHOD.invoke(manager);
|
|
|
|
for (ChunkHolder holder : chunks) {
|
2020-01-28 18:08:56 +01:00
|
|
|
Chunk chunk = holder.getChunkIfComplete();
|
2020-01-23 02:20:02 +01:00
|
|
|
if (chunk == null)
|
2020-01-22 23:21:52 +01:00
|
|
|
continue;
|
2020-01-23 02:20:02 +01:00
|
|
|
AuraChunk auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.capAuraChunk, null).orElse(null);
|
2020-01-22 23:21:52 +01:00
|
|
|
if (auraChunk != null)
|
|
|
|
auraChunk.update();
|
|
|
|
}
|
|
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
|
|
NaturesAura.LOGGER.fatal(e);
|
|
|
|
}
|
|
|
|
event.world.getProfiler().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) {
|
2020-03-01 19:25:39 +01:00
|
|
|
if (event.player.world.getGameTime() % 10 == 0) {
|
|
|
|
List<ChunkPos> pending = PENDING_AURA_CHUNKS.get(event.player.getUniqueID());
|
|
|
|
pending.removeIf(p -> this.handleChunkWatchDeferred(event.player, p));
|
|
|
|
}
|
|
|
|
|
2020-01-21 21:04:44 +01:00
|
|
|
if (event.player.world.getGameTime() % 200 != 0)
|
2018-12-20 21:43:19 +01:00
|
|
|
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) {
|
2020-03-01 19:25:39 +01:00
|
|
|
PENDING_AURA_CHUNKS.put(event.getPlayer().getUniqueID(), event.getPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean handleChunkWatchDeferred(PlayerEntity player, ChunkPos pos) {
|
|
|
|
Chunk chunk = Helper.getLoadedChunk(player.world, pos.x, pos.z);
|
|
|
|
if (chunk == null)
|
|
|
|
return false;
|
|
|
|
AuraChunk auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.capAuraChunk, null).orElse(null);
|
|
|
|
if (auraChunk == null)
|
|
|
|
return false;
|
|
|
|
PacketHandler.sendTo(player, auraChunk.makePacket());
|
|
|
|
return true;
|
2018-10-21 13:12:03 +02:00
|
|
|
}
|
2020-02-26 20:01:57 +01:00
|
|
|
|
|
|
|
@SubscribeEvent
|
2020-04-26 19:37:15 +02:00
|
|
|
public void onServerStarting(FMLServerStartingEvent event) {
|
2020-09-22 03:17:02 +02:00
|
|
|
CommandAura.register(event.getServer().getCommandManager().getDispatcher());
|
2020-02-26 20:01:57 +01:00
|
|
|
}
|
2018-10-21 12:51:13 +02:00
|
|
|
}
|