Search in sources :

Example 36 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project Tropicraft by Tropicraft.

the class BongoDrumBlock method onBlockLeftClick.

@SubscribeEvent
public static void onBlockLeftClick(PlayerInteractEvent.LeftClickBlock event) {
    final Level world = event.getWorld();
    final BlockState state = world.getBlockState(event.getPos());
    final Block block = state.getBlock();
    if (state.getBlock() instanceof BongoDrumBlock && event.getFace() == Direction.UP) {
        ((BongoDrumBlock) block).playBongoSound(world, event.getPos(), state);
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) Block(net.minecraft.world.level.block.Block) Level(net.minecraft.world.level.Level) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 37 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project Tropicraft by Tropicraft.

the class JigarbovTorchPlacement method onPlaceBlock.

@SubscribeEvent
public static void onPlaceBlock(BlockEvent.EntityPlaceEvent event) {
    BlockState placedState = event.getPlacedBlock();
    Block placedBlock = placedState.getBlock();
    if (placedBlock == Blocks.REDSTONE_WALL_TORCH) {
        RegistryObject<RedstoneWallTorchBlock> jigarbovTorchBlock = getJigarbovTorchFor(event.getPlacedAgainst().getBlock());
        if (jigarbovTorchBlock != null) {
            BlockState jigarbovTorch = jigarbovTorchBlock.get().defaultBlockState();
            jigarbovTorch = copyPropertiesTo(jigarbovTorch, placedState);
            event.getWorld().setBlock(event.getPos(), jigarbovTorch, Block.UPDATE_ALL);
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) RedstoneWallTorchBlock(net.minecraft.world.level.block.RedstoneWallTorchBlock) Block(net.minecraft.world.level.block.Block) RedstoneWallTorchBlock(net.minecraft.world.level.block.RedstoneWallTorchBlock) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 38 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project Tropicraft by Tropicraft.

the class ScubaData method onPlayerTick.

@SubscribeEvent
public static void onPlayerTick(PlayerTickEvent event) {
    Level world = event.player.level;
    if (event.phase == Phase.END) {
        // TODO support more than chest slot?
        ItemStack chestStack = event.player.getItemBySlot(EquipmentSlot.CHEST);
        Item chestItem = chestStack.getItem();
        if (chestItem instanceof ScubaArmorItem) {
            LazyOptional<ScubaData> data = event.player.getCapability(CAPABILITY);
            if (!world.isClientSide) {
                underwaterPlayers.add((ServerPlayer) event.player);
            }
            if (isUnderWater(event.player)) {
                data.ifPresent(d -> {
                    d.tick(event.player);
                    if (!world.isClientSide) {
                        d.updateClient((ServerPlayer) event.player, false);
                    }
                });
                ((ScubaArmorItem) chestItem).tickAir(event.player, EquipmentSlot.CHEST, chestStack);
                if (!world.isClientSide && world.getGameTime() % 60 == 0) {
                    // TODO this effect could be better, custom packet?
                    Vec3 eyePos = event.player.getEyePosition(0);
                    Vec3 motion = event.player.getDeltaMovement();
                    Vec3 particlePos = eyePos.add(motion.reverse());
                    ((ServerLevel) world).sendParticles(ParticleTypes.BUBBLE, particlePos.x(), particlePos.y(), particlePos.z(), 4 + world.random.nextInt(3), 0.25, 0.25, 0.25, motion.length());
                }
            } else if (!world.isClientSide && underwaterPlayers.remove(event.player)) {
                // Update client state as they leave the water
                data.ifPresent(d -> d.updateClient((ServerPlayer) event.player, false));
            }
        }
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Direction(net.minecraft.core.Direction) Item(net.minecraft.world.item.Item) CapabilityManager(net.minecraftforge.common.capabilities.CapabilityManager) EventBusSubscriber(net.minecraftforge.fml.common.Mod.EventBusSubscriber) PacketDistributor(net.minecraftforge.fmllegacy.network.PacketDistributor) TropicraftDimension(net.tropicraft.core.common.dimension.TropicraftDimension) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) ServerLevel(net.minecraft.server.level.ServerLevel) LazyOptional(net.minecraftforge.common.util.LazyOptional) ServerPlayer(net.minecraft.server.level.ServerPlayer) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) Nonnull(javax.annotation.Nonnull) Phase(net.minecraftforge.event.TickEvent.Phase) MessageUpdateScubaData(net.tropicraft.core.common.network.message.MessageUpdateScubaData) Nullable(javax.annotation.Nullable) WeakHashMap(java.util.WeakHashMap) PlayerEvent(net.minecraftforge.event.entity.player.PlayerEvent) PlayerTickEvent(net.minecraftforge.event.TickEvent.PlayerTickEvent) PlayerChangedDimensionEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerChangedDimensionEvent) FluidTags(net.minecraft.tags.FluidTags) Set(java.util.Set) AttachCapabilitiesEvent(net.minecraftforge.event.AttachCapabilitiesEvent) Capability(net.minecraftforge.common.capabilities.Capability) Player(net.minecraft.world.entity.player.Player) PlayerRespawnEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerRespawnEvent) TropicraftPackets(net.tropicraft.core.common.network.TropicraftPackets) ICapabilitySerializable(net.minecraftforge.common.capabilities.ICapabilitySerializable) CompoundTag(net.minecraft.nbt.CompoundTag) Entity(net.minecraft.world.entity.Entity) BlockPos(net.minecraft.core.BlockPos) Vec3(net.minecraft.world.phys.Vec3) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Constants(net.tropicraft.Constants) PlayerLoggedInEvent(net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) CapabilityToken(net.minecraftforge.common.capabilities.CapabilityToken) ParticleTypes(net.minecraft.core.particles.ParticleTypes) Bus(net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus) Collections(java.util.Collections) INBTSerializable(net.minecraftforge.common.util.INBTSerializable) Item(net.minecraft.world.item.Item) MessageUpdateScubaData(net.tropicraft.core.common.network.message.MessageUpdateScubaData) ServerLevel(net.minecraft.server.level.ServerLevel) Vec3(net.minecraft.world.phys.Vec3) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 39 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project MC-Prefab by Brian-Wuest.

the class StructureEventHandler method onServerTick.

/**
 * This event is primarily used to build 100 blocks for any queued structures for all players.
 *
 * @param event The event object.
 */
@SubscribeEvent
public static void onServerTick(ServerTickEvent event) {
    if (event.phase == TickEvent.Phase.START) {
        ArrayList<Player> playersToRemove = new ArrayList<>();
        if (StructureEventHandler.entitiesToGenerate.size() > 0) {
            StructureEventHandler.ticksSinceLastEntitiesGenerated++;
            if (StructureEventHandler.ticksSinceLastEntitiesGenerated > 40) {
                // Process any entities.
                StructureEventHandler.processStructureEntities();
                StructureEventHandler.ticksSinceLastEntitiesGenerated = 0;
            }
        }
        if (StructureEventHandler.structuresToBuild.size() > 0) {
            for (Entry<Player, ArrayList<Structure>> entry : StructureEventHandler.structuresToBuild.entrySet()) {
                ArrayList<Structure> structuresToRemove = new ArrayList<>();
                // Build the first 100 blocks of each structure for this player.
                for (Structure structure : entry.getValue()) {
                    if (!structure.entitiesRemoved) {
                        // If there is a player there...they will probably die anyways.....
                        for (BlockPos clearedPos : structure.clearedBlockPos) {
                            AABB axisPos = Shapes.block().bounds().move(clearedPos);
                            List<Entity> list = structure.world.getEntities(null, axisPos);
                            if (!list.isEmpty()) {
                                for (Entity entity : list) {
                                    // Don't kill living entities.
                                    if (!(entity instanceof LivingEntity)) {
                                        if (entity instanceof HangingEntity) {
                                            structure.BeforeHangingEntityRemoved((HangingEntity) entity);
                                        }
                                        structure.world.removeEntity(entity, false);
                                    }
                                }
                            }
                        }
                        structure.entitiesRemoved = true;
                    }
                    if (structure.airBlocks.size() > 0) {
                        structure.hasAirBlocks = true;
                    }
                    for (int i = 0; i < 10; i++) {
                        i = StructureEventHandler.setBlock(i, structure, structuresToRemove);
                    }
                    // After building the blocks for this tick, find waterlogged blocks and remove them.
                    StructureEventHandler.removeWaterLogging(structure);
                }
                // Update the list of structures to remove this structure since it's done building.
                StructureEventHandler.removeStructuresFromList(structuresToRemove, entry);
                if (entry.getValue().size() == 0) {
                    playersToRemove.add(entry.getKey());
                }
            }
        }
        // Remove each player that has their structure's built.
        for (Player player : playersToRemove) {
            StructureEventHandler.structuresToBuild.remove(player);
        }
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Entity(net.minecraft.world.entity.Entity) BuildEntity(com.wuest.prefab.structures.base.BuildEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) ServerPlayer(net.minecraft.server.level.ServerPlayer) Player(net.minecraft.world.entity.player.Player) LivingEntity(net.minecraft.world.entity.LivingEntity) BlockPos(net.minecraft.core.BlockPos) Structure(com.wuest.prefab.structures.base.Structure) AABB(net.minecraft.world.phys.AABB) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 40 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project MC-Prefab by Brian-Wuest.

the class StructureEventHandler method onClone.

/**
 * This occurs when a player dies and is used to make sure that a player does not get a duplicate starting house.
 *
 * @param event The player clone event.
 */
@SubscribeEvent
public static void onClone(PlayerEvent.Clone event) {
    if (event.getPlayer() instanceof ServerPlayer) {
        // Don't add the tag unless the house item was added. This way it can be added if the feature is turned on.
        // When the player is cloned, make sure to copy the tag. If this is not done the item can be given to the
        // player again if they die before the log out and log back in.
        CompoundTag originalTag = event.getOriginal().getPersistentData();
        // Use the server configuration to determine if the house should be added for this player.
        String startingItem = CommonProxy.proxyConfiguration.serverConfiguration.startingItem;
        if (startingItem != null && !startingItem.equalsIgnoreCase("Nothing")) {
            if (originalTag.contains(EntityPlayerConfiguration.PLAYER_ENTITY_TAG)) {
                CompoundTag newPlayerTag = event.getPlayer().getPersistentData();
                newPlayerTag.put(EntityPlayerConfiguration.PLAYER_ENTITY_TAG, originalTag.get(EntityPlayerConfiguration.PLAYER_ENTITY_TAG));
                // Send the persist tag to the client.
                Prefab.network.sendTo(new PlayerEntityTagMessage(originalTag.getCompound(EntityPlayerConfiguration.PLAYER_ENTITY_TAG)), ((ServerPlayer) event.getPlayer()).connection.connection, NetworkDirection.PLAY_TO_CLIENT);
            }
        }
    }
}
Also used : PlayerEntityTagMessage(com.wuest.prefab.proxy.messages.PlayerEntityTagMessage) ServerPlayer(net.minecraft.server.level.ServerPlayer) CompoundTag(net.minecraft.nbt.CompoundTag) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)87 PlayerEntity (net.minecraft.entity.player.PlayerEntity)18 ItemStack (net.minecraft.item.ItemStack)17 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)11 ResourceLocation (net.minecraft.util.ResourceLocation)10 World (net.minecraft.world.World)8 Player (net.minecraft.world.entity.player.Player)8 BlockPos (net.minecraft.util.math.BlockPos)7 Entity (net.minecraft.world.entity.Entity)7 ItemStack (net.minecraft.world.item.ItemStack)7 CompoundNBT (net.minecraft.nbt.CompoundNBT)6 Minecraft (net.minecraft.client.Minecraft)5 BlockPos (net.minecraft.core.BlockPos)5 ServerPlayer (net.minecraft.server.level.ServerPlayer)5 StringTextComponent (net.minecraft.util.text.StringTextComponent)5 Item (net.minecraft.world.item.Item)5 BlockItem (net.minecraft.item.BlockItem)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4 BlockState (net.minecraft.world.level.block.state.BlockState)4 OnlyIn (net.minecraftforge.api.distmarker.OnlyIn)4