Search in sources :

Example 41 with SubscribeEvent

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

the class StructureEventHandler method PlayerLoggedIn.

/**
 * This event is used to determine if the player should be given the starting house item when they log in.
 *
 * @param event The event object.
 */
@SubscribeEvent
public static void PlayerLoggedIn(PlayerLoggedInEvent event) {
    if (!event.getPlayer().level.isClientSide() && event.getPlayer() instanceof ServerPlayer) {
        ServerPlayer player = (ServerPlayer) event.getPlayer();
        EntityPlayerConfiguration playerConfig = EntityPlayerConfiguration.loadFromEntityData(player);
        String startingItem = CommonProxy.proxyConfiguration.serverConfiguration.startingItem;
        if (!playerConfig.givenHouseBuilder && startingItem != null) {
            ItemStack stack = ItemStack.EMPTY;
            switch(startingItem.toLowerCase()) {
                case "starting house":
                    {
                        stack = new ItemStack(ModRegistry.StartHouse.get());
                        break;
                    }
                case "moderate house":
                    {
                        stack = new ItemStack(ModRegistry.ModerateHouse.get());
                        break;
                    }
            }
            if (!stack.isEmpty()) {
                System.out.println(player.getDisplayName().getString() + " joined the game for the first time. Giving them starting item.");
                player.getInventory().add(stack);
                player.containerMenu.broadcastChanges();
                // Make sure to set the tag for this player so they don't get the item again.
                playerConfig.givenHouseBuilder = true;
                playerConfig.saveToPlayer(player);
            }
        }
        // Send the tag to the client.
        Prefab.network.sendTo(new PlayerEntityTagMessage(playerConfig.getModIsPlayerNewTag(player)), ((ServerPlayer) event.getPlayer()).connection.connection, NetworkDirection.PLAY_TO_CLIENT);
    }
}
Also used : PlayerEntityTagMessage(com.wuest.prefab.proxy.messages.PlayerEntityTagMessage) EntityPlayerConfiguration(com.wuest.prefab.config.EntityPlayerConfiguration) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemStack(net.minecraft.world.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 42 with SubscribeEvent

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

the class ClientEventHandler method KeyInput.

@SubscribeEvent(priority = EventPriority.NORMAL)
@OnlyIn(Dist.CLIENT)
public static void KeyInput(InputEvent.KeyInputEvent event) {
    for (KeyMapping binding : ClientEventHandler.keyBindings) {
        if (binding.isDown()) {
            if (StructureRenderHandler.currentStructure != null) {
                ItemStack mainHandStack = Minecraft.getInstance().player.getMainHandItem();
                ItemStack offHandStack = Minecraft.getInstance().player.getOffhandItem();
                boolean foundCorrectStructureItem = false;
                if (mainHandStack != ItemStack.EMPTY || offHandStack != ItemStack.EMPTY) {
                    StructureTagMessage.EnumStructureConfiguration structureConfigurationEnum = StructureTagMessage.EnumStructureConfiguration.getByConfigurationInstance(StructureRenderHandler.currentConfiguration);
                    if (mainHandStack != ItemStack.EMPTY && mainHandStack.getItem() instanceof StructureItem) {
                        // Check main hand.
                        foundCorrectStructureItem = ClientEventHandler.checkIfStackIsCorrectGui(structureConfigurationEnum, mainHandStack);
                    }
                    if (!foundCorrectStructureItem && offHandStack != ItemStack.EMPTY && offHandStack.getItem() instanceof StructureItem) {
                        // Main hand is not correct item; check off-hand
                        foundCorrectStructureItem = ClientEventHandler.checkIfStackIsCorrectGui(structureConfigurationEnum, offHandStack);
                    }
                }
                if (foundCorrectStructureItem) {
                    Prefab.network.sendToServer(new StructureTagMessage(StructureRenderHandler.currentConfiguration.WriteToCompoundTag(), StructureTagMessage.EnumStructureConfiguration.getByConfigurationInstance(StructureRenderHandler.currentConfiguration)));
                }
                StructureRenderHandler.currentStructure = null;
            }
            break;
        }
    }
}
Also used : StructureItem(com.wuest.prefab.structures.items.StructureItem) KeyMapping(net.minecraft.client.KeyMapping) ItemStack(net.minecraft.world.item.ItemStack) StructureTagMessage(com.wuest.prefab.structures.messages.StructureTagMessage) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 43 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project RealisticArmorTiers by IsakViste.

the class EventEquipmentSets method onServerTick.

@SubscribeEvent
public void onServerTick(TickEvent.ServerTickEvent evt) {
    MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
    List<ServerPlayerEntity> playerList = Lists.newArrayList(server.getPlayerList().getPlayers());
    IArmor armors = null;
    List<PotionEffect> setEffects = new ArrayList<>();
    boolean foundWhole = false;
    for (ServerPlayerEntity serverPlayerEntity : playerList) {
        int m;
        List<ItemStack> stacks;
        if (serverPlayerEntity.getCapability(ArmorProvider.Armor).isPresent()) {
            armors = (IArmor) serverPlayerEntity.getCapability(ArmorProvider.Armor);
            stacks = (List<ItemStack>) serverPlayerEntity.getArmorSlots();
            int numberOfStack = 0;
            for (ItemStack stack : stacks) {
                if (!stack.isEmpty()) {
                    numberOfStack++;
                }
            }
            if (numberOfStack == armors.getItems().size()) {
                foundWhole = true;
                for (ItemStack stack : stacks) {
                    boolean found = false;
                    if (!stack.isEmpty()) {
                        for (int l = 0; l < armors.getItems().size(); l++) {
                            if (stack.getItem().equals(armors.getItems().get(l).getItem())) {
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            foundWhole = false;
                            break;
                        }
                    }
                }
            }
        }
        if (foundWhole) {
            setEffects = armors.getPotionEffects();
        } else {
            if (serverPlayerEntity.getCapability(ArmorProvider.Armor).isPresent()) {
                if (armors != null) {
                    m = 0;
                    setEffects = armors.getPotionEffects();
                    while (m < setEffects.size()) {
                        RegistryObject<Effect> potionEffect = RegistryObject.of(new ResourceLocation(setEffects.get(m).id), ForgeRegistries.POTIONS);
                        if (potionEffect.isPresent()) {
                            serverPlayerEntity.removeEffect(potionEffect.get());
                        }
                        m++;
                    }
                    armors.removeAllItems();
                    Collection<EffectInstance> potionEffectsPlayer = serverPlayerEntity.getActiveEffects();
                    Iterator<EffectInstance> potionEffects = potionEffectsPlayer.iterator();
                    while (potionEffects.hasNext()) {
                        EffectInstance o = potionEffects.next();
                        ResourceLocation effectResLoc = o.getEffect().getRegistryName();
                        if (effectResLoc == null) {
                            RealisticArmorTiers.LOGGER.warn("Could not find ResourceLocation of " + o.getDescriptionId());
                            continue;
                        }
                        PotionEffect usedPotion = new PotionEffect(effectResLoc.getNamespace() + ":" + o.getEffect().getRegistryName().getPath(), o.getAmplifier(), o.getDuration());
                        armors.addUsedPotionEffect(usedPotion);
                        potionEffects.remove();
                    }
                    stacks = (List<ItemStack>) serverPlayerEntity.getArmorSlots();
                    for (ItemStack stack : stacks) {
                        if (!stack.isEmpty()) {
                            armors.addItem(stack.copy());
                        }
                    }
                }
            } else {
                serverPlayerEntity.removeAllEffects();
            }
            int setNumber = sets.armors.checkIfSet(serverPlayerEntity);
            if (setNumber != -1) {
                setEffects = sets.armors.getPotionEffects(setNumber);
            }
            if (serverPlayerEntity.getCapability(ArmorProvider.Armor).isPresent()) {
                if (setEffects != null && armors != null) {
                    armors.addPotionEffectList(setEffects);
                }
            }
        }
        if (armors != null) {
            if (setEffects != null) {
                m = 0;
                while (m < setEffects.size()) {
                    Equiped.addPotionEffect(serverPlayerEntity, armors.getPotionEffects().get(m));
                    m++;
                }
            }
            if (!foundWhole) {
                setEffects = armors.getUsedPotionEffects();
                Equiped.addUsedPotionEffect(serverPlayerEntity, setEffects, armors);
            }
        }
    }
}
Also used : PotionEffect(com.viste.realisticarmortiers.data.PotionEffect) ArrayList(java.util.ArrayList) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) IArmor(com.viste.realisticarmortiers.capability.IArmor) MinecraftServer(net.minecraft.server.MinecraftServer) ResourceLocation(net.minecraft.util.ResourceLocation) Effect(net.minecraft.potion.Effect) PotionEffect(com.viste.realisticarmortiers.data.PotionEffect) ItemStack(net.minecraft.item.ItemStack) EffectInstance(net.minecraft.potion.EffectInstance) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 44 with SubscribeEvent

use of net.minecraftforge.eventbus.api.SubscribeEvent in project Overloaded by CJ-MC-Mods.

the class RenderMultiToolAssist method onMouseEvent.

@SubscribeEvent
public static void onMouseEvent(InputEvent.MouseScrollEvent event) {
    ClientPlayerEntity player = Minecraft.getInstance().player;
    if (event.getScrollDelta() != 0 && player != null && player.isShiftKeyDown()) {
        ItemStack stack = player.getMainHandItem();
        if (player.isShiftKeyDown() && !stack.isEmpty() && stack.getItem() == ModItems.multiTool) {
            changeHelpMode((int) Math.round(event.getScrollDelta()));
            player.displayClientMessage(new StringTextComponent("Assist Mode: " + getAssistMode().getName()), true);
            event.setCanceled(true);
        }
    }
}
Also used : StringTextComponent(net.minecraft.util.text.StringTextComponent) ClientPlayerEntity(net.minecraft.client.entity.player.ClientPlayerEntity) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 45 with SubscribeEvent

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

the class ForgeMod method registerLootData.

// ModBus
@SubscribeEvent
// automatically called by Forge
@SuppressWarnings("unused")
public void registerLootData(RegistryEvent.Register<GlobalLootModifierSerializer<?>> event) {
    // Ignore the event itself: this is done only not to statically initialize our custom LootConditionType
    Registry.register(Registry.LOOT_CONDITION_TYPE, new ResourceLocation("forge:loot_table_id"), LootTableIdCondition.LOOT_TABLE_ID);
    Registry.register(Registry.LOOT_CONDITION_TYPE, new ResourceLocation("forge:can_tool_perform_action"), CanToolPerformAction.LOOT_CONDITION_TYPE);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) 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