You are not logged in.

  • Login

[Bukkit-Plugin] Asynchronous entity add!

Bitte besucht unser neues Forum unter https://forum.nitrado.net | Please visit our new Forum at https://forum.nitrado.net

Dear visitor, welcome to Nitrado.net Prepaid Gameserver Community-Support - Archiv. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Master_Timpa

User / Kunde

  • "Master_Timpa" is male
  • "Master_Timpa" started this thread

Posts: 437

Location: mCyber-Lounge - an der Bar

Occupation: Arbeitslos durch den Tag ... bekomme eh HartzIV vom Staat...

  • Send private message

1

Sunday, January 31st 2016, 9:26pm

Asynchronous entity add!

Hallo,

Ich habe gerade anhand verschiedener Tutorials versucht beim Ausloggen einen NPC zu erstellen, der das Inventar droppt wenn er angegriffen wird. Der NPC spawnt auch und wird beim Angriff(Linksklick) auch 'zerstört'. Allerdings werde ich gekickt wenn ich die Items droppen will. In der Konsole steht nur:

Source code

1
[21:21:17] [Server thread/INFO]: Timpa lost connection: Internal Exception: io.netty.handler.codec.DecoderException: java.lang.IllegalStateException: Asynchronous entity add!


Dieser Error entsteht aber nur wenn ich die Items droppe. Das mache ich allerdings einfach nur mit:

Java source code

1
		location.getWorld().dropItem(location, is);


Die location ist die Variable die ich beim spawnen des NPCs setze...

Danke schonmal im Vorraus!

MfG Timpa

Gamerplay33

User / Kunde

  • "Gamerplay33" is male

Posts: 72

Location: Irgentwo

Occupation: Schüler

  • Send private message

2

Sunday, January 31st 2016, 9:55pm

Du darfst keinen Asyncronen Scheduler benutzen. Das ist bei Spigot eine "sicherung".

Master_Timpa

User / Kunde

  • "Master_Timpa" is male
  • "Master_Timpa" started this thread

Posts: 437

Location: mCyber-Lounge - an der Bar

Occupation: Arbeitslos durch den Tag ... bekomme eh HartzIV vom Staat...

  • Send private message

3

Sunday, January 31st 2016, 9:58pm

Naja leider vewende ich hier keinen Scheduler... Kann es auch etwas anderes sein?

Handtuchstaender

User / Kunde

  • "Handtuchstaender" is male

Posts: 686

Location: Localhost

Occupation: Schule :c

  • Send private message

4

Sunday, January 31st 2016, 9:59pm

Falls es etwas anderes sein sollte, wäre der Code nützlich.

Master_Timpa

User / Kunde

  • "Master_Timpa" is male
  • "Master_Timpa" started this thread

Posts: 437

Location: mCyber-Lounge - an der Bar

Occupation: Arbeitslos durch den Tag ... bekomme eh HartzIV vom Staat...

  • Send private message

5

Sunday, January 31st 2016, 10:03pm

FakePlayer.java

Spoiler Spoiler

Java source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.Arrays;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.mojang.authlib.GameProfile;

import de.Timpa.mCyber.Annihilation.Main;
import net.minecraft.server.v1_8_R3.ChatComponentText;
import net.minecraft.server.v1_8_R3.DataWatcher;
import net.minecraft.server.v1_8_R3.MathHelper;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy;
import net.minecraft.server.v1_8_R3.PacketPlayOutNamedEntitySpawn;
import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerInfo.EnumPlayerInfoAction;
import net.minecraft.server.v1_8_R3.WorldSettings.EnumGamemode;

public class FakePlayer extends Reflections {
	
	Main main = Main.getInstance();
	
	int entityID;
	Location location;
	GameProfile gameprofile;
	String name;
	
	public FakePlayer(Player p) {
		entityID = (int) Math.ceil(Math.random() * 10000)+ 10000;
		gameprofile = new GameProfile(p.getUniqueId(), p.getName());
		this.location = p.getLocation();
		this.name = p.getName();
		spawn();
	}
	
	public void spawn() {
		PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn();
		setValue(packet, "a", entityID);
		setValue(packet, "b", gameprofile.getId());
		setValue(packet, "c", (int) MathHelper.floor(location.getX() * 32.0D));
		setValue(packet, "d", (int) MathHelper.floor(location.getY() * 32.0D));
		setValue(packet, "e", (int) MathHelper.floor(location.getZ() * 32.0D));
		setValue(packet, "f", (byte) ((int) (location.getYaw() * 256.0F * 360.0F)));
		setValue(packet, "g", (byte) ((int) (location.getPitch() * 256.0F * 360.0F)));
		setValue(packet, "h", 0);
		DataWatcher w = new DataWatcher(null);
		w.a(6, 1F);
		w.a(7, (int)3);
		w.a(10, (byte)127);
		setValue(packet, "i", w);
		addToTabList();
		sendPacket(packet);
	}
	
	public void addToTabList() {
		PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER);
		setValue(packet, "b", Arrays.asList(packet.new PlayerInfoData(gameprofile, 0, EnumGamemode.NOT_SET, new ChatComponentText(this.gameprofile.getName()))));
		sendPacket(packet);
	}
	
	public void remove() {
		PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[] {entityID});
		sendPacket(packet);
	}
	
	public int getID() {
		return entityID;
	}
	
	public String getName() {
		return name;
	}
	
	public void dropItem(ItemStack is) {
		location.getWorld().dropItem(location, is);
	}
}

FakePlayerManager.java

Spoiler Spoiler

Java source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class FakePlayerManager {

	Map<Integer, FakePlayer> npcs = new HashMap<Integer, FakePlayer>();
	Map<Integer, ItemStack[]> items = new HashMap<Integer, ItemStack[]>();
	Map<Integer, ItemStack[]> armor = new HashMap<Integer, ItemStack[]>();
	
	public void add(FakePlayer fp, Player p) {
		npcs.put(fp.getID(), fp);
		items.put(fp.getID(), p.getInventory().getContents());
		armor.put(fp.getID(), p.getInventory().getArmorContents());
	}
	
	public void kill(FakePlayer fp) {
		for(ItemStack is : items.get(fp.getID())) if(is != null) fp.dropItem(is);
		npcs.remove(fp.getID());
		items.remove(fp.getID());
		fp.remove();
	}
	
	public FakePlayer get(int id) {
		return npcs.get(id);
	}

NPCList.java

Spoiler Spoiler

Java source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	Main main = Main.getInstance();
	
	Map<Player, PacketReader> prs = new HashMap<Player, PacketReader>();
	
	@EventHandler
	public void onLogin(PlayerJoinEvent e) {
		PacketReader pr = new PacketReader(e.getPlayer());
		prs.put(e.getPlayer(), pr);
		pr.inject();
	}
	
	@EventHandler
	public void onLeave(PlayerQuitEvent e) {
		if(prs.get(e.getPlayer()) != null) prs.get(e.getPlayer()).uninject();
		final Player p = e.getPlayer();
		FakePlayer fp = new FakePlayer(p);
		main.fpm.add(fp, p);
	}
}

und PacketReader.java

Spoiler Spoiler

Java source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.lang.reflect.Field;
import java.util.List;

import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;

import de.Timpa.mCyber.Annihilation.Main;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import net.minecraft.server.v1_8_R3.Packet;

public class PacketReader {

	Main main = Main.getInstance();
	
	Player player;
	Channel channel;

	public PacketReader(Player player) {
		this.player = player;
	}

	public void inject(){
		CraftPlayer cPlayer = (CraftPlayer)this.player;
		channel = cPlayer.getHandle().playerConnection.networkManager.channel;
		channel.pipeline().addAfter("decoder", "PacketInjector", new MessageToMessageDecoder<Packet<?>>() {
			@Override protected void decode(ChannelHandlerContext arg0, Packet<?> packet,List<Object> arg2) throws Exception {
				arg2.add(packet);
				readPacket(packet);
			}
		});
	}

	public void uninject(){
		if(channel.pipeline().get("PacketInjector") != null){
			channel.pipeline().remove("PacketInjector");
		}
	}


	public void readPacket(Packet<?> packet){
		if(packet.getClass().getSimpleName().equalsIgnoreCase("PacketPlayInUseEntity")){
			int id = (Integer)getValue(packet, "a");
			if(id < 10000) return;
			if(getValue(packet, "action").toString().equalsIgnoreCase("ATTACK")) {
				if(main.fpm.get(id) != null) {
					main.fpm.kill(main.fpm.get(id));
				}
			}
		}
	}


	public void setValue(Object obj,String name,Object value){
		try{
			Field field = obj.getClass().getDeclaredField(name);
			field.setAccessible(true);
			field.set(obj, value);
		}catch(Exception e){}
	}

	public Object getValue(Object obj,String name){
		try{
			Field field = obj.getClass().getDeclaredField(name);
			field.setAccessible(true);
			return field.get(obj);
		}catch(Exception e){}
		return null;
	}

}

Gamerplay33

User / Kunde

  • "Gamerplay33" is male

Posts: 72

Location: Irgentwo

Occupation: Schüler

  • Send private message

6

Sunday, January 31st 2016, 10:05pm

In welcher Zeile ist der Fehler?

Master_Timpa

User / Kunde

  • "Master_Timpa" is male
  • "Master_Timpa" started this thread

Posts: 437

Location: mCyber-Lounge - an der Bar

Occupation: Arbeitslos durch den Tag ... bekomme eh HartzIV vom Staat...

  • Send private message

7

Sunday, January 31st 2016, 10:09pm

Es gibt keien Zeilenangabe im Fehler, allerdings kommt der Fehler nur wenn ich im FakePlayerManager Zeile 14(die for() Schleife) drinnen habe...

Gamerplay33

User / Kunde

  • "Gamerplay33" is male

Posts: 72

Location: Irgentwo

Occupation: Schüler

  • Send private message

8

Sunday, January 31st 2016, 10:14pm

Wird der FakePlayer gespawnt?

Master_Timpa

User / Kunde

  • "Master_Timpa" is male
  • "Master_Timpa" started this thread

Posts: 437

Location: mCyber-Lounge - an der Bar

Occupation: Arbeitslos durch den Tag ... bekomme eh HartzIV vom Staat...

  • Send private message

9

Sunday, January 31st 2016, 10:16pm

Ja der ist da und wenn ich die Zeile 14 raus lasse wird er beim Schlag auch wird removet ... nur das Itemdroppen kickt mich eben

Gamerplay33

User / Kunde

  • "Gamerplay33" is male

Posts: 72

Location: Irgentwo

Occupation: Schüler

  • Send private message

10

Sunday, January 31st 2016, 10:26pm

Ich verstehe gerade nicht was da falsch ist...

Master_Timpa

User / Kunde

  • "Master_Timpa" is male
  • "Master_Timpa" started this thread

Posts: 437

Location: mCyber-Lounge - an der Bar

Occupation: Arbeitslos durch den Tag ... bekomme eh HartzIV vom Staat...

  • Send private message

11

Sunday, January 31st 2016, 10:29pm

Das ich nicht gekickt werden soll wen ich versuche den NPC zu schlagen... :P

Gamerplay33

User / Kunde

  • "Gamerplay33" is male

Posts: 72

Location: Irgentwo

Occupation: Schüler

  • Send private message

12

Sunday, January 31st 2016, 10:33pm

Führ mal die dropItem und die remove Methoden in nem SyncDelayed scheduler aus. Bukkit.getScheduler ().scheduleSyncDelayedTask (deine Main instance, und new Runnable ()) aber lass den Integer am schluss weg! (Heißt ohne verzögerung ausführen den Scheduler)

Master_Timpa

User / Kunde

  • "Master_Timpa" is male
  • "Master_Timpa" started this thread

Posts: 437

Location: mCyber-Lounge - an der Bar

Occupation: Arbeitslos durch den Tag ... bekomme eh HartzIV vom Staat...

  • Send private message

13

Sunday, January 31st 2016, 10:40pm

Danke, es funktioniert :D
hab jetzt die dropMethode nur verändert:

Java source code

1
2
3
4
5
6
7
8
	public void dropItem(final ItemStack is) {
		Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
			@Override
			public void run() {
				location.getWorld().dropItem(location, is);
			}
		});
	}

Gamerplay33

User / Kunde

  • "Gamerplay33" is male

Posts: 72

Location: Irgentwo

Occupation: Schüler

  • Send private message

14

Sunday, January 31st 2016, 10:42pm

Dann ist ja gut xD

Bitte besucht unser neues Forum unter https://forum.nitrado.net| Please visit our new Forum at https://forum.nitrado.net