Jump to content

How to spawn lightning with forge 1.14


Tryhard

Recommended Posts

Well, just like the title says, i'm having trouble spawning a new lightning into the world.

 

How about a little bit of background first. I'm new to modding, and i really REALLY want to get into it. I already know Java and object oriented programming, not on an expert lvl but i do think i'm ready for this. The main difficulty for me is, understanding how minecraft works, and what each class and methods do... i still fail to grasp the uses for ItemStack, BlockStates, DataTables, ItemGroups, ItemProperties  etc... I am currently trying to understand events, since they seem to be quite simple.

 

With that said, i am messing around with a simple idea involving mouse input events. An item that summons lightning... after a short while of researching this topic, i realized this is quite the common idea among new modders.

 

Anyway, i'm just trying to get anything to do something ingame, the code will NOT be the best, i do hope to get some informative feedback from u guys :D So far i managed to get a custom item into the game (by following a tutorial on youtube, not a very good one, "just do what i do" type of tutorial, so i still don't understand the entirety of the process of adding blocks/items).

 

NOTE: The original idea was to summon lightning wherever the player was looking at the moment of swinging said item. But since i just can't manage to get the target block, i decided to sumon lightning when and where a block is broken by said item.

 

So i added an event listener to this item (inside the class itself) and linked it to the porper event bus. The problem is, it "kinda" works. What i mean by that is, the lightning strike does happen, but the actual "beam" doesn't show up. I don't really know how to attach code in here but, eh, here it goes:

 

 

public class TutorialItem extends Item {

    public static final String REGISTRY_NAME = "tutorial_item";
    public static final String DISPLAY_NAME = "Tutorial Item";

    public TutorialItem(){
        super(new Item.Properties().maxStackSize(2).group(ModSetup.itemGroup));
        MinecraftForge.EVENT_BUS.register(this);
    }


    @SubscribeEvent
    public void summonLightning(InputEvent.MouseInputEvent event){
        //nothing here yet
    }

    @SubscribeEvent
    public void test(BlockEvent.BreakEvent event){

            World world = (World)event.getWorld();
            BlockPos pos = event.getPos();
            Iterable<ItemStack> items = event.getPlayer().getHeldEquipment();
            Iterator<ItemStack> it = items.iterator();
            Item item;
            Boolean playerHasItem = false;

            while(it.hasNext()){
                item = it.next().getItem();
                if(item instanceof TutorialItem){
                    System.out.println("ITEM: " +this.DISPLAY_NAME);
                    playerHasItem = true;
                    break;
                }
            }

            if(playerHasItem) {
                LightningBoltEntity new_lightning = new LightningBoltEntity(world, pos.getX(), pos.getY(), pos.getZ(), false);
                new_lightning.setLocationAndAngles(pos.getX(), pos.getY(), pos.getZ(), 0, 0.0F);
                world.addEntity(new_lightning);

            }
    }
}

 

I'm sure i do NOT need to loop through that "items" collection thingy... bu i just don't know how to handle that Object.

 

Then, when i am spawning the lightning, i saw that for older versions they used the methods World#addWeatherEffect and World#spawnInWorld, but they are nowhere to be found now, i'm sure i'm just not spawning that lightningbolt correctly.

Edited by Tryhard
Link to comment
Share on other sites

Lightning is summoned with World#addWeatherEffect.

Pass in an instance of LightningBoltEntity.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

15 minutes ago, Tryhard said:

Well, the World instance i'm curently using doesn't seem to have such a method. 

What version of forge do you have. If you can update it might not have been deobfuscated in your version. You can also update your mappings by editing the value in the build.gradle file. 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

3 minutes ago, Animefan8888 said:

What version of forge do you have. If you can update it might not have been deobfuscated in your version. You can also update your mappings by editing the value in the build.gradle file. 

I think my current version is: 1.14.3-27.0.21. I'm adding two images showing what version shows up in the actual game.  

Is there a better way of knowing what version i'm using? And if i were using a wrong version, how should i go about updating it?

 

I'm currently trying to update the mappings (which i also don't fully comprehend what they are)

x.PNG

t.PNG

Link to comment
Share on other sites

4 hours ago, diesieben07 said:

Show your build.gradle.

Here it is:

buildscript {
    repositories {
        maven { url = 'https://files.minecraftforge.net/maven' }
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true
    }
}
apply plugin: 'net.minecraftforge.gradle'
// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '1.14.4-0.1.0'
group = 'mod.joanalbert.tutorial_mod' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'tutorial_mod'

sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' // Need this here so eclipse task generates correctly.

minecraft {
    // The mappings can be changed at any time, and must be in the following format.
    // snapshot_YYYYMMDD   Snapshot are built nightly.
    // stable_#            Stables are built at the discretion of the MCP team.
    // Use non-default mappings at your own risk. they may not always work.
    // Simply re-run your setup task after changing the mappings to update your workspace.
    //mappings channel: 'snapshot', version: '20190719-1.14.3'
    mappings channel: 'snapshot', version: '20190917-1.14.3';
    // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
    
    // accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')

    // Default run configurations.
    // These can be tweaked, removed, or duplicated as needed.
    runs {
        client {
            workingDirectory project.file('run')

            // Recommended logging data for a userdev environment
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'

            // Recommended logging level for the console
            property 'forge.logging.console.level', 'debug'

            mods {
                examplemod {
                    source sourceSets.main
                }
            }
        }

        server {
            workingDirectory project.file('run')

            // Recommended logging data for a userdev environment
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'

            // Recommended logging level for the console
            property 'forge.logging.console.level', 'debug'

            mods {
                examplemod {
                    source sourceSets.main
                }
            }
        }

        data {
            workingDirectory project.file('run')

            // Recommended logging data for a userdev environment
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'

            // Recommended logging level for the console
            property 'forge.logging.console.level', 'debug'

            args '--mod', 'examplemod', '--all', '--output', file('src/generated/resources/')

            mods {
                examplemod {
                    source sourceSets.main
                }
            }
        }
    }
}

dependencies {
    // Specify the version of Minecraft to use, If this is any group other then 'net.minecraft' it is assumed
    // that the dep is a ForgeGradle 'patcher' dependency. And it's patches will be applied.
    // The userdev artifact is a special name and will get all sorts of transformations applied to it.
    minecraft 'net.minecraftforge:forge:1.14.4-28.1.0'

    // You may put jars on which you depend on in ./libs or you may define them like so..
    // compile "some.group:artifact:version:classifier"
    // compile "some.group:artifact:version"

    // Real examples
    // compile 'com.mod-buildcraft:buildcraft:6.0.8:dev'  // adds buildcraft to the dev env
    // compile 'com.googlecode.efficient-java-matrix-library:ejml:0.24' // adds ejml to the dev env

    // The 'provided' configuration is for optional dependencies that exist at compile-time but might not at runtime.
    // provided 'com.mod-buildcraft:buildcraft:6.0.8:dev'

    // These dependencies get remapped to your current MCP mappings
    // deobf 'com.mod-buildcraft:buildcraft:6.0.8:dev'

    // For more info...
    // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
    // http://www.gradle.org/docs/current/userguide/dependency_management.html

}

// Example for how to get properties into the manifest for reading by the runtime..
jar {
    manifest {
        attributes([
            "Specification-Title": "examplemod",
            "Specification-Vendor": "examplemodsareus",
            "Specification-Version": "1", // We are version 1 of ourselves
            "Implementation-Title": project.name,
            "Implementation-Version": "${version}",
            "Implementation-Vendor" :"examplemodsareus",
            "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
        ])
    }
}

// Example configuration to allow publishing using the maven-publish task
// we define a custom artifact that is sourced from the reobfJar output task
// and then declare that to be published
// Note you'll need to add a repository here
def reobfFile = file("$buildDir/reobfJar/output.jar")
def reobfArtifact = artifacts.add('default', reobfFile) {
    type 'jar'
    builtBy 'reobfJar'
}
publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact reobfArtifact
        }
    }
    repositories {
        maven {
            url "file:///${project.projectDir}/mcmodsrepo"
        }
    }
}


///////////////////////
task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}
build.dependsOn sourcesJar

artifacts {
    archives sourcesJar
}




// Process resources on build
processResources {
    // This will ensure that this task is redone when the versions change.
    inputs.property 'version', project.version

    // Replace stuff in mods.toml, nothing else
    from(sourceSets.main.resources.srcDirs) {
        include 'META-INF/mods.toml'

        // Replace version
        expand 'version':project.version
    }

    // Copy everything else except the mods.toml
    from(sourceSets.main.resources.srcDirs) {
        exclude 'META-INF/mods.toml'
    }
}
///////////////////////////////////////////
Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...

This worked for me (1.16.5)

    @Override
    public ActionResult<ItemStack> use(World p_77659_1_, PlayerEntity p_77659_2_, Hand p_77659_3_) {
        LightningBoltEntity lightning = new LightningBoltEntity(EntityType.LIGHTNING_BOLT, p_77659_1_);
        lightning.setPos(p_77659_2_.getX(), p_77659_2_.getY(), p_77659_2_.getZ());
        p_77659_1_.addFreshEntity(lightning);
        return super.use(p_77659_1_, p_77659_2_, p_77659_3_);
    }
Link to comment
Share on other sites

  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.