@SetupDLL:
Yes, you can. For example, this is a event to detect when a player changed his armed weapon:
PHP код:
//PlayerEvent contains a Player variable already. You can also create you all own type of event, when you just implement Event. Or if you want t make some Vehicle Event you can use VehicleEvent.
public class PlayerChangedWeapon extends PlayerEvent {
private WeaponModel oldWeapon;
private WeaponModel newWeapon;
protected PlayerChangedWeapon(Player player, WeaponModel oldWeapon, WeaponModel newWeapon) {
super(player);
this.oldWeapon = oldWeapon;
this.newWeapon = newWeapon;
}
public WeaponModel getNewWeapon() {
return newWeapon;
}
public WeaponModel getOldWeapon() {
return oldWeapon;
}
}
To register it, you can use it like this:
PHP код:
eventManager.registerHandler(PlayerChangedWeapon.class, (e) -> {
Player player = e.getPlayer();
player.sendMessage(Color.RED, "* Your old weapon was " + e.getOldWeapon().getName() + " and your new one is " + e.getNewWeapon().getName());
});
But it won't trigger itself, so you need to recognize somehow if the player changed his weapon. In this case,
we can use the PlayerUpdate event and compare his old and current weapon:
PHP код:
//We need to save his old weapon, so create a global hashmap:
private WeakHashMap<Player, WeaponModel> oldWeapons;
//Initzialize it in constructor
oldWeapons = new WeakHashMap<>();
//Register PlayerUpdateEvent
eventManager.registerHandler(PlayerUpdateEvent.class, (e) -> {
if(oldWeapons.containsKey(e.getPlayer())) {
WeaponModel old = oldWeapons.get(e.getPlayer());
WeaponModel newModel = e.getPlayer().getArmedWeapon();
if(!old.equals(newModel)) {
oldWeapons.put(e.getPlayer(), newModel);
//Trigger the PlayerChangedWeapon
eventManager.dispatchEvent(new PlayerChangedWeapon(e.getPlayer(), old, newModel),
e.getPlayer());
}
} else oldWeapons.put(e.getPlayer(), e.getPlayer().getArmedWeapon());
});