Search in sources :

Example 1 with RegistryKey

use of net.minecraft.util.RegistryKey in project AgriCraft by AgriCraft.

the class BlockUpdateHandler method addListener.

public void addListener(World world, BlockPos pos, IListener listener) {
    if (world instanceof ServerWorld) {
        RegistryKey<World> dimension = world.getDimensionKey();
        this.listeners.computeIfAbsent(dimension, key -> Maps.newHashMap()).computeIfAbsent(new ChunkPos(pos), chunkPos -> Maps.newHashMap()).computeIfAbsent(pos, aPos -> Sets.newIdentityHashSet()).add(listener);
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ServerWorld(net.minecraft.world.server.ServerWorld) BlockEvent(net.minecraftforge.event.world.BlockEvent) WorldEvent(net.minecraftforge.event.world.WorldEvent) ChunkEvent(net.minecraftforge.event.world.ChunkEvent) World(net.minecraft.world.World) ChunkPos(net.minecraft.util.math.ChunkPos) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) RegistryKey(net.minecraft.util.RegistryKey) Map(java.util.Map) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) ChunkPos(net.minecraft.util.math.ChunkPos) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World)

Example 2 with RegistryKey

use of net.minecraft.util.RegistryKey in project AgriCraft by AgriCraft.

the class BlockUpdateHandler method onChunkUnloaded.

@SubscribeEvent
@SuppressWarnings("unused")
public void onChunkUnloaded(ChunkEvent.Unload event) {
    if (event.getWorld() instanceof ServerWorld) {
        ServerWorld world = (ServerWorld) event.getWorld();
        RegistryKey<World> dimension = world.getDimensionKey();
        if (listeners.containsKey(dimension)) {
            listeners.computeIfPresent(dimension, (dim, chunkMap) -> {
                if (chunkMap.containsKey(event.getChunk().getPos())) {
                    chunkMap.remove(event.getChunk().getPos()).forEach((pos, set) -> set.forEach(listener -> listener.onChunkUnloaded(world, pos)));
                }
                return chunkMap;
            });
            listeners.get(dimension).remove(event.getChunk().getPos());
        }
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ServerWorld(net.minecraft.world.server.ServerWorld) BlockEvent(net.minecraftforge.event.world.BlockEvent) WorldEvent(net.minecraftforge.event.world.WorldEvent) ChunkEvent(net.minecraftforge.event.world.ChunkEvent) World(net.minecraft.world.World) ChunkPos(net.minecraft.util.math.ChunkPos) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) RegistryKey(net.minecraft.util.RegistryKey) Map(java.util.Map) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

Maps (com.google.common.collect.Maps)2 Sets (com.google.common.collect.Sets)2 Map (java.util.Map)2 Set (java.util.Set)2 RegistryKey (net.minecraft.util.RegistryKey)2 BlockPos (net.minecraft.util.math.BlockPos)2 ChunkPos (net.minecraft.util.math.ChunkPos)2 World (net.minecraft.world.World)2 ServerWorld (net.minecraft.world.server.ServerWorld)2 BlockEvent (net.minecraftforge.event.world.BlockEvent)2 ChunkEvent (net.minecraftforge.event.world.ChunkEvent)2 WorldEvent (net.minecraftforge.event.world.WorldEvent)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2