Danke, sehr gut erklärt, ich habe es verstanden, dank der Beispiele und Erläuterungen
In meinem Gamemode funktioniert das jetzt, nur ganz durchblicke ich es doch noch nicht
Das Problem nun besteht darin, dass ich mich in einem EventManager (playerEventManager) befinde, welcher zB das PlayerDeathEvent,... regelt.
Wie kann ich dort eine Instanz kreieren? Ist ja sozusagen dann eine Instanz in etwas was eine Instanz erstellen soll
Ziel: Ich will den Killer von PlayerDeathEvent nach PlayerSpawnEvent bringen/vererben:
package com.samp.lvdm;import java.util.Random;import net.gtaun.shoebill.Shoebill;import net.gtaun.shoebill.common.command.CommandGroup;import net.gtaun.shoebill.common.command.PlayerCommandManager;import net.gtaun.shoebill.common.player.PlayerLifecycleHolder;import net.gtaun.shoebill.constant.WeaponModel;import net.gtaun.shoebill.data.Color;import net.gtaun.shoebill.data.Vector3D;import net.gtaun.shoebill.event.player.PlayerCommandEvent;import net.gtaun.shoebill.event.player.PlayerConnectEvent;import net.gtaun.shoebill.event.player.PlayerDeathEvent;import net.gtaun.shoebill.event.player.PlayerDisconnectEvent;import net.gtaun.shoebill.event.player.PlayerRequestClassEvent;import net.gtaun.shoebill.event.player.PlayerSpawnEvent;import net.gtaun.shoebill.event.player.PlayerUpdateEvent;import net.gtaun.shoebill.event.player.PlayerWeaponShotEvent;import net.gtaun.shoebill.object.Player;import net.gtaun.util.event.EventManager;import net.gtaun.util.event.EventManagerNode;import net.gtaun.util.event.HandlerPriority;public class PlayerManager{ public static final int MAX_PLAYERS = LvdmGamemode.MAX_PLAYERS; private boolean suicid = false; private boolean killed = false; private Player[] lastKiller = new Player[MAX_PLAYERS]; private static final int INITIAL_MONEY = 50000; private static final Vector3D[] RANDOM_SPAWNS = { new Vector3D(1958.3783f, 1343.1572f, 15.3746f), new Vector3D(2199.6531f, 1393.3678f, 10.8203f), new Vector3D(2483.5977f, 1222.0825f, 10.8203f), new Vector3D(2637.2712f, 1129.2743f, 11.1797f), new Vector3D(2000.0106f, 1521.1111f, 17.0625f), new Vector3D(2024.8190f, 1917.9425f, 12.3386f), new Vector3D(2261.9048f, 2035.9547f, 10.8203f), new Vector3D(2262.0986f, 2398.6572f, 10.8203f), new Vector3D(2244.2566f, 2523.7280f, 10.8203f), new Vector3D(2335.3228f, 2786.4478f, 10.8203f), new Vector3D(2150.0186f, 2734.2297f, 11.1763f), new Vector3D(2158.0811f, 2797.5488f, 10.8203f), new Vector3D(1969.8301f, 2722.8564f, 10.8203f), new Vector3D(1652.0555f, 2709.4072f, 10.8265f), new Vector3D(1564.0052f, 2756.9463f, 10.8203f), new Vector3D(1271.5452f, 2554.0227f, 10.8203f), new Vector3D(1441.5894f, 2567.9099f, 10.8203f), new Vector3D(1480.6473f, 2213.5718f, 11.0234f), new Vector3D(1400.5906f, 2225.6960f, 11.0234f), new Vector3D(1598.8419f, 2221.5676f, 11.0625f), new Vector3D(1318.7759f, 1251.3580f, 10.8203f), new Vector3D(1558.0731f, 1007.8292f, 10.8125f), new Vector3D(-857.0551f, 1536.6832f, 22.5870f), new Vector3D(817.3494f, 856.5039f, 12.7891f), new Vector3D(116.9315f, 1110.1823f, 13.6094f), new Vector3D(-18.8529f, 1176.0159f, 19.5634f), new Vector3D(-315.0575f, 1774.0636f, 43.6406f), new Vector3D(1705.2347f, 1025.6808f, 10.8203f) }; private EventManagerNode eventManagerNode; private PlayerCommandManager commandManager; private Random random; public PlayerManager(EventManager rootEventManager) { random = new Random(); eventManagerNode = rootEventManager.createChildNode(); commandManager = new PlayerCommandManager(eventManagerNode); commandManager.installCommandHandler(HandlerPriority.NORMAL); commandManager.registerCommands(new LvdmCommands()); commandManager.registerCommands(new TestCommands()); commandManager.registerCommands(new AdminCommands()); // Example: register /test [command] ... CommandGroup testGroup = new CommandGroup(); testGroup.registerCommands(new TestCommands()); commandManager.registerChildGroup(testGroup, "test"); // Example: register /admin [command] ... CommandGroup adminCommands = new CommandGroup(); adminCommands.registerCommands(new AdminCommands()); commandManager.registerChildGroup(adminCommands, "admin"); commandManager.setUsageMessageSupplier((player, command, prefix, params, help) -> { String message = prefix + command; for (String param : params) { message += " [" + param + "]"; } return message; }); //-- eventManagerNode.registerHandler(PlayerUpdateEvent.class, (e) -> { Player player = e.getPlayer(); // getUpdateCount() Example if (player.getUpdateCount() % 100 == 0) { player.setScore(player.getMoney()); } if(suicid) { Shoebill.get().runOnSampThread(() -> { LvdmGamemode.queue.addPoints(player, "SELBSTMORD", -100, DynamicActionLabel.DynamicItem.TYPE_BIG); }); suicid = false; } if(killed) { Shoebill.get().runOnSampThread(() -> { LvdmGamemode.functions.showKiller(player, lastKiller[player.getId()]); }); killed = false; } }); eventManagerNode.registerHandler(PlayerWeaponShotEvent.class, (e) -> { // e.getPlayer().sendMessage(Color.LIGHTBLUE, String.format("WeaponShot: hittype: %1$s, weapon: %2$s, pos: %3$s", e.getHitType(), e.getWeapon(), e.getPosition())); }); eventManagerNode.registerHandler(PlayerConnectEvent.class, (e) -> { Player player = e.getPlayer(); player.sendGameText(5000, 5, "~w~SA-MP: ~r~Las Venturas ~g~MoneyGrub"); Player.sendMessageToAll(Color.GREEN,player.getName()+" Connected!"); player.sendMessage(Color.PURPLE, "Welcome to Las Venturas MoneyGrub, For help type /help."); Player.sendDeathMessageToAll(player, null, WeaponModel.CONNECT); Color color = new Color(); do color.setValue(random.nextInt()); while (color.getY() < 128); player.setColor(color); }); eventManagerNode.registerHandler(PlayerDisconnectEvent.class, (e) -> { Player player = e.getPlayer(); Player.sendMessageToAll(Color.RED,player.getName()+" Disconnected!"); Player.sendDeathMessageToAll(player, null, WeaponModel.DISCONNECT); }); eventManagerNode.registerHandler(PlayerSpawnEvent.class, (e) -> { Player player = e.getPlayer(); player.giveMoney(INITIAL_MONEY); player.toggleClock(true); setRandomSpawnPos(player); if(suicid) { Shoebill.get().runOnSampThread(() -> { LvdmGamemode.queue.addPoints(player, "SELBSTMORD", -100, DynamicActionLabel.DynamicItem.TYPE_BIG); }); suicid = false; } if(killed) { Shoebill.get().runOnSampThread(() -> { LvdmGamemode.functions.showKiller(player, lastKiller[player.getId()]); }); killed = false; } }); eventManagerNode.registerHandler(PlayerDeathEvent.class, (e) -> { Player player = e.getPlayer(); Player killer = e.getKiller(); player.setMoney(0); Player.sendDeathMessageToAll(killer, player, e.getReason()); if (killer != null) { killer.giveMoney(player.getMoney()); } if(killer != null && killer.getName() != player.getName()) { LvdmGamemode.queue.addPoints(killer, "GEGNER ELIMINIERT", 100, DynamicActionLabel.DynamicItem.TYPE_BIG); if(LvdmGamemode.MessageOnRespawnAfterDead) { killed = true; lastKiller[player.getId()] = killer; } else { LvdmGamemode.functions.showKiller(player, killer); } } else if(killer == null && player != null || killer.getName() == player.getName()) { if(LvdmGamemode.MessageOnRespawnAfterDead) { suicid = true; } else { LvdmGamemode.queue.addPoints(player, "SELBSTMORD", -100, DynamicActionLabel.DynamicItem.TYPE_BIG); } } }); eventManagerNode.registerHandler(PlayerRequestClassEvent.class, (e) -> { Player player = e.getPlayer(); setupForClassSelection(player); }); eventManagerNode.registerHandler(PlayerCommandEvent.class, HandlerPriority.BOTTOM, (e) -> { Player player = e.getPlayer(); player.sendMessage(Color.RED, "Unknown command. Type /help to see help."); e.setProcessed(); }); } public void uninitialize() { commandManager.destroy(); eventManagerNode.destroy(); } private void setRandomSpawnPos(Player player) { int rand = random.nextInt(RANDOM_SPAWNS.length); player.setLocation(RANDOM_SPAWNS[rand]); player.setInterior(0); } private void setupForClassSelection(Player player) { player.setInterior(14); player.setLocation(258.4893f, -41.4008f, 1002.0234f); player.setAngle(270.0f); player.setCameraPosition(256.0815f, -43.0475f, 1004.0234f); player.setCameraLookAt(258.4893f, -41.4008f, 1002.0234f); }}
Thx und danke für die Hilfe
Musst dir das mit den Instanzen merken, für nen Tutorial oder so, war gut erklärt und würde bst noch anderen helfen, falls sie es nicht wussten wie ich
(Ich nutze normalerweise sehr viele Variablen :/)
------------------------
>>edit:
Habe es nun im EventHandler/Manager über folgendes gelöst:
PlayerLifecycleHolder playerLifecycleHolder = new PlayerLifecycleHolder(rootEventManager.createChildNode());
playerLifecycleHolder.registerClass(Lifecycle.class, (lifecycle, p) -> new Lifecycle(lifecycle, p));
Das doofe ist nur immer ein neuen Lifecycle.getObjects in jedem Event zu erstellen... Kann man nicht statt dessen ein globalen Array erstellen mit MAX_PLAYERS, welcher dann beim PlayerConnectEvent bzw PlayerRequestClassEvent auf die playerid abgestimmt wird und so dann immer wieder aufrufbar ist, ohne neu zu definieren?
Bsp.:
//Vars deklarieren
public static final int MAX_PLAYERS = 500;
private static PlayerLifecycleHolder playerLifecycleHolder;
private Lifecycle[] playerLifecycle = new Lifecycle[MAX_PLAYERS];
//Vars setzen
eventManagerNode.registerHandler(PlayerRequestClassEvent.class, (e) ->
{
playerLifecycle[e.getPlayer().getId()] = playerLifecycleHolder.getObject(e.getPlayer(), Lifecycle.class);
Player player = e.getPlayer();
setupForClassSelection(player);
});
//Vars nutzen
//in ieinem Event:
Player killer = playerLifecycle[e.getPlayer().getId()].getLastKiller();
Alles anzeigen
Kann man das auch anders aber einfacher als meine Methode machen oder ist deine wirklich die beste in dem Fall?
Immer getObject in jeder Methode ist auch nicht soo toll, aber wäre auch nicht schlimm, hilft ja sehr
Nur ich finde es bei meiner Methode nervig, dass man nach jeder Abfrage dann immer ''playerLifecycle[e.getPlayer().getId()]."... schreiben muss
Und wäre meine nicht sicherer? Könnte ja sein, dass sich die playerLifecycle überschreiben, wenn zB 2 gleichzeitig sterben (Explosion) ?