Jump to content

Invisible lightnings


Androm

Recommended Posts

Hello modders,

I have a problem with spawning EnitityLightningBolts in the world. When they spawn you can hear them, but not see them.. They didn´t do any damage!

Here is my class(The event is registered and the code gets executed in the right way)

 

 

package de.MhytRPG.www.events;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;

 

import net.minecraft.entity.effect.EntityLightningBolt;

import net.minecraft.entity.player.EntityPlayer;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.gameevent.TickEvent;

import de.MhytRPG.www.Karma.Blitzgewitter;

import de.MhytRPG.www.utils.PlayerUUID;

 

public class OnWorldTick {

 

private HashMap<EntityLightningBolt, String> blitz_velocity = new HashMap<EntityLightningBolt, String>();

 

@SubscribeEvent

public void onTickInWorld(TickEvent.WorldTickEvent event) {

// Zeus-lightning strike(Karma)

if(!Blitzgewitter.blitze.isEmpty()) {

/*Blitzgewitter is the class where the locations of the llightnings are created

* in a rectangle of 32*32 with the player in the middle

* the lightnings are randomly placed inside this rectangle

*/

for (String uuid : Blitzgewitter.blitze.keySet()) {

EntityPlayer player = new PlayerUUID().getPlayerFromUUID(uuid);

ArrayList<String> blitze = Blitzgewitter.blitze.get(uuid);

if (blitze.size() == 0)

new Blitzgewitter().setLightningList(player, blitze);

else {

String[] loc = blitze.get(blitze.size() - 1).split(";");

EntityLightningBolt elb = new EntityLightningBolt(

player.worldObj, Double.valueOf(loc[0]),

Double.valueOf(loc[1]), Double.valueOf(loc[2]));

player.worldObj.joinEntityInSurroundings(elb);

elb.setVelocity(player.posX, player.posY, player.posZ);

blitze.remove(blitze.size() - 1);

new Blitzgewitter().setLightningList(player, blitze);

blitz_velocity.put(elb, player.getGameProfile().getId()

.toString());

}

}

}

if(!blitz_velocity.isEmpty()) {

Iterator<Entry<EntityLightningBolt, String>> iterator = blitz_velocity.entrySet().iterator();

        while(iterator.hasNext()){

            Entry<EntityLightningBolt, String> entry = iterator.next();

            EntityLightningBolt elb = entry.getKey();

            if (elb.isDead)

            iterator.remove();

else {

EntityPlayer ep = new PlayerUUID()

.getPlayerFromUUID(blitz_velocity.get(elb));

elb.setVelocity(ep.posX, ep.posY, ep.posZ);

continue;

}

           

        }

}

}

}

 

 

 

Link to comment
Share on other sites

Updated code:

 

package de.MhytRPG.www.events;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import de.MhytRPG.www.Karma.Blitzgewitter;
import de.MhytRPG.www.utils.PlayerUUID;

public class OnWorldTick {

private HashMap<EntityLightningBolt, String> blitz_velocity = new HashMap<EntityLightningBolt, String>();

@SubscribeEvent
public void onTickInWorld(TickEvent.WorldTickEvent event) {
	// Zeus-lightning strike(Karma)
	if(!Blitzgewitter.blitze.isEmpty()) {
		/*Blitzgewitter is the class where the locations of the llightnings are created
		 * in a rectangle of 32*32 with the player in the middle
		 * the lightnings are randomly placed inside this rectangle
		 */
		for (String uuid : Blitzgewitter.blitze.keySet()) {
			EntityPlayer player = new PlayerUUID().getPlayerFromUUID(uuid);
			if(!player.worldObj.isRemote) {
				ArrayList<String> blitze = Blitzgewitter.blitze.get(uuid);
				if (blitze.size() == 0)
					new Blitzgewitter().setLightningList(player, blitze);
				else {
					String[] loc = blitze.get(blitze.size() - 1).split(";");
					EntityLightningBolt elb = new EntityLightningBolt(
							player.worldObj, Double.valueOf(loc[0]),
							Double.valueOf(loc[1]), Double.valueOf(loc[2]));
					//player.worldObj.joinEntityInSurroundings(elb);
					player.worldObj.addWeatherEffect(elb);
					elb.setVelocity(player.posX, player.posY, player.posZ);
					blitze.remove(blitze.size() - 1);
					new Blitzgewitter().setLightningList(player, blitze);
					blitz_velocity.put(elb, player.getGameProfile().getId()
							.toString());
				}
			} else {
				Blitzgewitter.blitze.clear();
			}
		}
	}
	if(!blitz_velocity.isEmpty()) {
		Iterator<Entry<EntityLightningBolt, String>> iterator = blitz_velocity.entrySet().iterator();
        while(iterator.hasNext()){
            Entry<EntityLightningBolt, String> entry = iterator.next();
            EntityLightningBolt elb = entry.getKey();
            if (elb.isDead)
            	iterator.remove();
			else {
				EntityPlayer ep = new PlayerUUID()
						.getPlayerFromUUID(blitz_velocity.get(elb));
				elb.setVelocity(ep.posX, ep.posY, ep.posZ);
				continue;
			}
            
        }
	}
}
}

 

I don´t need a test for the world for the method after

if(!blitz_velocity.isEmpty())

because blitz_velocity will be always empty if the world isn´t remote.

Link to comment
Share on other sites

@brandon3055

your using setVelocity where you should be using setPosition

I don´t want to that the position of the lightning to the player! I just want that the lightnings travel to the player with setting the velocity from the player to the lightnings.

@diesieben07

Also: You are accessing the collections from both threads (client & server). You cannot do that! If they are static fields they will be shared between the two. You have to check with world.isRemote.

Forgot what I said.. Now I updated the code. If there is something in blitz_velocity and world.isRemote return true (==singleplayer) the Hashmap will be cleared. Otherwise the code will be performed.

 

package de.MhytRPG.www.events;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import de.MhytRPG.www.Karma.Blitzgewitter;
import de.MhytRPG.www.utils.PlayerUUID;

public class OnWorldTick {

private HashMap<EntityLightningBolt, String> blitz_velocity = new HashMap<EntityLightningBolt, String>();

@SubscribeEvent
public void onTickInWorld(TickEvent.WorldTickEvent event) {
	// Zeus-lightning strike(Karma)
	if (!Blitzgewitter.blitze.isEmpty()) {
		/*
		 * Blitzgewitter is the class where the locations of the lightnings
		 * are created in a rectangle of 32*32 with the player in the middle
		 * the lightnings are randomly placed inside this rectangle
		 */
		for (String uuid : Blitzgewitter.blitze.keySet()) {
			EntityPlayer player = new PlayerUUID().getPlayerFromUUID(uuid);
			if (!player.worldObj.isRemote) {
				ArrayList<String> blitze = Blitzgewitter.blitze.get(uuid);
				if (blitze.size() == 0)
					new Blitzgewitter().setLightningList(player, blitze);
				else {
					String[] loc = blitze.get(blitze.size() - 1).split(";");
					EntityLightningBolt elb = new EntityLightningBolt(
							player.worldObj, Double.valueOf(loc[0]),
							Double.valueOf(loc[1]), Double.valueOf(loc[2]));
					// player.worldObj.joinEntityInSurroundings(elb);
					player.worldObj.addWeatherEffect(elb);
					elb.setVelocity(player.posX, player.posY, player.posZ);
					blitze.remove(blitze.size() - 1);
					new Blitzgewitter().setLightningList(player, blitze);
					blitz_velocity.put(elb, player.getGameProfile().getId()
							.toString());
				}
			} else {
				Blitzgewitter.blitze.clear();
			}
		}
	}
	if (!blitz_velocity.isEmpty()) {
		Iterator<Entry<EntityLightningBolt, String>> iterator = blitz_velocity
				.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<EntityLightningBolt, String> entry = iterator.next();
			EntityLightningBolt elb = entry.getKey();
			if (elb.isDead
					|| !new PlayerUUID().getPlayerFromUUID(blitz_velocity
							.get(elb)).worldObj.isRemote)
				iterator.remove();
			else {
				EntityPlayer ep = new PlayerUUID()
						.getPlayerFromUUID(blitz_velocity.get(elb));
				elb.setVelocity(ep.posX, ep.posY, ep.posZ);
				continue;
			}
		}
	}
}
}

 

But the problem still exists.

Link to comment
Share on other sites

I updated all

 

package de.MhytRPG.www.events;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import de.MhytRPG.www.Karma.Blitzgewitter;

public class OnWorldTick {

private HashMap<EntityLightningBolt, EntityPlayer> blitz_velocity = new HashMap<EntityLightningBolt, EntityPlayer>();

@SubscribeEvent
public void onTickInWorld(TickEvent.WorldTickEvent event) {
	if(!event.world.isRemote) {
		// Zeus-lightning strike(Karma)
		if (!Blitzgewitter.blitze.isEmpty()) {
			/*
			 * Blitzgewitter is the class where the locations of the lightnings
			 * are created in a rectangle of 32*32 with the player in the middle
			 * the lightnings are randomly placed inside this rectangle
			 */
			for (EntityPlayer player : Blitzgewitter.blitze.keySet()) {
					ArrayList<EntityLightningBolt> blitze = Blitzgewitter.blitze.get(player);
					if (blitze.size() == 0)
						new Blitzgewitter().setLightningList(player, blitze);
					else {
						EntityLightningBolt elb = blitze.get(blitze.size() - 1);
						// player.worldObj.joinEntityInSurroundings(elb);
						player.worldObj.addWeatherEffect(elb);
						elb.setVelocity(player.posX, player.posY, player.posZ);
						blitze.remove(blitze.size() - 1);
						new Blitzgewitter().setLightningList(player, blitze);
						blitz_velocity.put(elb, player);
					}
			}
		}
		if (!blitz_velocity.isEmpty()) {
			Iterator<Entry<EntityLightningBolt, EntityPlayer>> iterator = blitz_velocity.entrySet().iterator();
			while (iterator.hasNext()) {
				Entry<EntityLightningBolt, EntityPlayer> entry = iterator.next();
				EntityLightningBolt elb = entry.getKey();
				if (elb.isDead) {
					iterator.remove();
				}
				else {
					EntityPlayer ep = blitz_velocity.get(elb);
					elb.setVelocity(ep.posX, ep.posY, ep.posZ);
					continue;
				}
			}
		}
	}
}
}

 

I now formatted the next by myself instant of the eclipse one. I also added

if(!world.isRemote())

(I think that´s now right!).

I now changed the Objects to other Objects. Before I thought that was a problem with the Hashmap and that it doesn´t can save EntityPlayer or EntityLightningBolts...

You can hear the lightnings but you can´t see them and they do no damage!

Thanks for your patience diesieben07! Today it isn´t my day...

EDIT: I pasted the old code....

Link to comment
Share on other sites

@diesieben07

I am trying to spawn lightnings in the world around the player who has bad karma. The spawnpoints of the lightnings are randomly setted by the class called BlitzGewitter near the player. On the event Tickevent.WorldTick it gets the list and checks if it isn´t empty. Then it spawns the last EntityLightningBolt in the ArrayList<EntityLightningBolt>  in the world, deletes the entity from the list and adds him to another HashMap calles blitz_velocity. Next it checks if blitz_velocity is empty. If not it gets all the EntityLightningBolts in the Map and check if they are dead. If yes they will be removed from the list (I used iterator.remove() to prevent a java.util.ConcurrentModificationError). Else the velocity is setted to the EntityPlayer which is stored as value for the EntityLightningBolt (I want that the lightnings go straight to the player or near him). That´s all!

Link to comment
Share on other sites

Is this right?

 

package de.MhytRPG.www.events;

import java.util.Random;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;

public class PlayerTickEvent {

@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event) {
	if(!event.player.worldObj.isRemote) {
		if(event.player.hasBadKarma()) {
			if (Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage()
					.toString().contains("Deutsch")) {
				event.player.addChatComponentMessage(new ChatComponentText("Du hast "
						+ EnumChatFormatting.DARK_BLUE + "Zeus "
						+ EnumChatFormatting.WHITE + "verärgert! "
						+ EnumChatFormatting.DARK_RED + "SPÜRE SEINE RACHE!"));
			} else {
				event.player.addChatComponentMessage(new ChatComponentText("You got "
						+ EnumChatFormatting.DARK_BLUE + "Zeus "
						+ EnumChatFormatting.WHITE + "angry! "
						+ EnumChatFormatting.DARK_RED + "NOW FEEL HIS PAYBACK!"));
			}
			World w = event.player.worldObj;
			EntityLightningBolt elb;
			double x = event.player.posX;
			double z = event.player.posZ;
			for (int i = 0; i < 10; i++) {
				for (double cx = x - 16; cx < x + 16; cx++) {
					for (double cz = z - 16; cz < z + 16; cz++) {
						if (new Random().nextBoolean() == true) {
							elb = new EntityLightningBolt(w, cx, event.player.posY + 20D, cz);
							event.player.worldObj.addWeatherEffect(elb);
						}
					}
				}
			}
		}
	}
}

}

 

I change the 2 for-loops later to another method.

Link to comment
Share on other sites

I have already .lang files but I don´t know how to make that for chat messages. I also need to know if the player´s language is german or whatever(I have .lang files for german and english).

About this I looked in the Internet and found nothing. So can you explain how to get the player´s language and then send him the right translation (server side without minecraft code)?

Link to comment
Share on other sites

Thanks for your help. But now i have another problem:

event.player.addChatMessage(new ChatComponentTranslation("msg.badkarma.txt", new Object[0]));

works, but it isn´t colored anymore.

In the .lang file:

msg.badkarma.txt=You got §1Zeus §fangry! §4NOW FEEL HIS PAYBACK!

§1 = DARK_BLUE; §f = WHITE; §4 = DARK_RED.

In the chat is written:

You got ?1Zeus ?fangry! ?4NOW FEEL HIS PAYBACK!

 

EDIT: Solved the problem. Thread can be locked now!

Link to comment
Share on other sites

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.