Search in sources :

Example 71 with SubscribeEvent

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

the class AddPackFinderEventTest method addPackFinders.

@SubscribeEvent
public static void addPackFinders(AddPackFindersEvent event) {
    try {
        if (event.getPackType() == PackType.CLIENT_RESOURCES) {
            var resourcePath = ModList.get().getModFileById(MODID).getFile().findResource("test_nested_resource_pack");
            var pack = new PathResourcePack(ModList.get().getModFileById(MODID).getFile().getFileName() + ":" + resourcePath, resourcePath);
            var metadataSection = pack.getMetadataSection(PackMetadataSection.SERIALIZER);
            if (metadataSection != null) {
                event.addRepositorySource((packConsumer, packConstructor) -> packConsumer.accept(packConstructor.create("builtin/add_pack_finders_test", new TextComponent("display name"), false, () -> pack, metadataSection, Pack.Position.BOTTOM, PackSource.BUILT_IN, false)));
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) IOException(java.io.IOException) PathResourcePack(net.minecraftforge.resource.PathResourcePack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 72 with SubscribeEvent

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

the class DataGeneratorTest method gatherData.

@SubscribeEvent
public static void gatherData(GatherDataEvent event) {
    GSON = new GsonBuilder().registerTypeAdapter(Variant.class, new Variant.Deserializer()).registerTypeAdapter(ItemTransforms.class, new ItemTransforms.Deserializer()).registerTypeAdapter(ItemTransform.class, new ItemTransform.Deserializer()).create();
    DataGenerator gen = event.getGenerator();
    if (event.includeClient()) {
        gen.addProvider(new Lang(gen));
        // Let blockstate provider see generated item models by passing its existing file helper
        ItemModelProvider itemModels = new ItemModels(gen, event.getExistingFileHelper());
        gen.addProvider(itemModels);
        gen.addProvider(new BlockStates(gen, itemModels.existingFileHelper));
        gen.addProvider(new SoundDefinitions(gen, event.getExistingFileHelper()));
    }
    if (event.includeServer()) {
        gen.addProvider(new Recipes(gen));
        gen.addProvider(new Tags(gen, event.getExistingFileHelper()));
        gen.addProvider(new Advancements(gen, event.getExistingFileHelper()));
    }
}
Also used : ItemModelProvider(net.minecraftforge.client.model.generators.ItemModelProvider) GsonBuilder(com.google.gson.GsonBuilder) ItemTransforms(net.minecraft.client.renderer.block.model.ItemTransforms) Variant(net.minecraft.client.renderer.block.model.Variant) ItemTransform(net.minecraft.client.renderer.block.model.ItemTransform) DataGenerator(net.minecraft.data.DataGenerator) BlockTags(net.minecraft.tags.BlockTags) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 73 with SubscribeEvent

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

the class RenderLocalPlayerTest method onItemRightClick.

@SubscribeEvent
public static void onItemRightClick(final PlayerInteractEvent.RightClickItem event) {
    if (ENABLED && event.getItemStack().getItem() == Items.STICK) {
        Minecraft mc = Minecraft.getInstance();
        if (mc.getCameraEntity() != mc.player) {
            mc.setCameraEntity(mc.player);
            event.setCancellationResult(InteractionResult.SUCCESS);
            event.setCanceled(true);
        }
    }
}
Also used : Minecraft(net.minecraft.client.Minecraft) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 74 with SubscribeEvent

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

the class RenderLocalPlayerTest method onItemRightClickEntity.

@SubscribeEvent
public static void onItemRightClickEntity(final PlayerInteractEvent.EntityInteract event) {
    if (ENABLED && event.getItemStack().getItem() == Items.STICK) {
        Minecraft mc = Minecraft.getInstance();
        if (mc.getCameraEntity() == mc.player) {
            mc.setCameraEntity(event.getTarget());
            event.setCancellationResult(InteractionResult.SUCCESS);
            event.setCanceled(true);
        }
    }
}
Also used : Minecraft(net.minecraft.client.Minecraft) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 75 with SubscribeEvent

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

the class CommandEventTest method onCommand.

@SubscribeEvent
public static void onCommand(CommandEvent event) {
    CommandDispatcher<CommandSourceStack> dispatcher = event.getParseResults().getContext().getDispatcher();
    List<ParsedCommandNode<CommandSourceStack>> nodes = event.getParseResults().getContext().getNodes();
    CommandSourceStack source = event.getParseResults().getContext().getSource();
    // test: when the /time command is used with no arguments, automatically add default arguments (/time set day)
    if (nodes.size() == 1 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("time")) {
        event.setParseResults(dispatcher.parse("time set day", source));
        return;
    }
    // test: whenever a player uses the /give command, let everyone on the server know
    if (nodes.size() > 0 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("give")) {
        String msg = source.getTextName() + " used the give command: " + event.getParseResults().getReader().getString();
        source.getServer().getPlayerList().getPlayers().forEach(player -> player.sendMessage(new TextComponent(msg), player.getUUID()));
        return;
    }
// this is annoying so I disabled it
// test: when the /kill command is used with no arguments, throw a custom exception
// if (nodes.size() == 1 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("kill"))
// {
// event.setException(new CommandRuntimeException(new TextComponent("You tried to use the /kill command with no arguments")));
// event.setCanceled(true);
// return;
// }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) ParsedCommandNode(com.mojang.brigadier.context.ParsedCommandNode) CommandSourceStack(net.minecraft.commands.CommandSourceStack) 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