Looking for the right event to work with. I want to check if a player right-clicked another player.
This is what i have so far (doesn't work. Not getting into the if or the else statement:
public void onPlayerRightClicks(PlayerInteractEntityEvent e) {
Player p=e.getPlayer();
if(e.getRightClicked() instanceof Player) p.sendMessage("You have rightclicked a player.");
else p.sendMessage("You didn't hit anyone with your spell");
}
Found the answer to the question by combining my own code with codes from another answer on a similar question:
#SuppressWarnings("deprecation")
#EventHandler
public void onPlayerClick(PlayerInteractEvent e) {
Player p=e.getPlayer();
NBTItem item = new NBTItem(p.getItemInHand());
Entity en=getNearestEntityInSight(p,5);
if(e.getAction()==Action.RIGHT_CLICK_AIR && en instanceof Player) p.sendMessage("You have rightclicked a player.");
else p.sendMessage("You didn't hit anyone with your spell");
}
public static Entity getNearestEntityInSight(Player player, int range) {
ArrayList<Entity> entities = (ArrayList<Entity>) player.getNearbyEntities(range, range, range);
ArrayList<Block> sightBlock = (ArrayList<Block>) player.getLineOfSight(null, range);
ArrayList<Location> sight = new ArrayList<Location>();
for (int i = 0;i<sightBlock.size();i++)
sight.add(sightBlock.get(i).getLocation());
for (int i = 0;i<sight.size();i++) {
for (int k = 0;k<entities.size();k++) {
if (Math.abs(entities.get(k).getLocation().getX()-sight.get(i).getX())<1.3) {
if (Math.abs(entities.get(k).getLocation().getY()-sight.get(i).getY())<1.5) {
if (Math.abs(entities.get(k).getLocation().getZ()-sight.get(i).getZ())<1.3) {
return entities.get(k);
}
}
}
}
}
return null;
}
Even though it works, i don't think it's an optimal answer, as there should be an event that can check whether or not a player rightclicked or at least leftclicked another player. The getNearestEntitySight method finds the closest entity in the player's sight. I combined it with my code which runs when a player rightclicks, using the PlayerInteractEvent.
Try this:
#EventHandler
public void onPlayerInteractEntity(final PlayerInteractAtEntityEvent e) {
final Player p = e.getPlayer();
if (e.getRightClicked() instanceof Player) {
final Player clicked = (Player) e.getRightClicked();
// DO STUFF
}
}
Related
Here is an example of what I'm working with...
Player p = (Player) sender;
ArrayList<Player> nofalldmg = new ArrayList<Player>();
if (p.hasPermission("custome.fly")) {
if (p.isFlying()) {
p.setAllowFlight(false);
p.setFlying(false);
nofalldmg.add(p);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
nofalldmg.remove(p);
}
}, 60);
So as you can see I have two lines of code 1 "nofalldmg.add(p);" that adds the player to an array list, and 2 "nofalldmg.remove(p);" that removes the player from the array list
When they are added to the array list their fall damage is canceled, what I want to know is once they are removed from that array list how do I re enable their fall damage?
This is the full class that can help you (without import, with comment to explain how it works) :
private final ArrayList<Player> nofalldmg = new ArrayList<Player>(); // declare list
#EventHandler
public void onDamageDisabler(EntityDamageEvent e) { // method to disable fall damage
if (e.getCause() == EntityDamageEvent.DamageCause.FALL) { // only if it's fall
if(e.getEntity() instanceof Player && nofalldmg.contains((Player) e.getEntity()) { // only if it's in list
e.setCancelled(true); // cancel
}
}
}
public void yourMethod(CommandSender sender) {
Player p = (Player) sender;
if (p.hasPermission("custome.fly")) { // your conditions
if (p.isFlying()) {
p.setAllowFlight(false);
p.setFlying(false);
nofalldmg.add(p); // here we add it to the list
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
nofalldmg.remove(p);
// here you can resetting everything that you want
}, 60); // no we remove it, so can take damage
}
}
}
#EventHandler
public void playerInteraction(PlayerInteractEvent event)
{
Action action = event.getAction();
Player player = event.getPlayer();
Block block = event.getClickedBlock();
if (action.equals(Action.RIGHT_CLICK_BLOCK))
{
if (block.getType().equals(Material.NETHER_WART_BLOCK))
{
player.setHealth(player.getHealth() -1);
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_HURT, 10, 1);
}
else if (block.getType().equals(Material.DIAMOND_BLOCK))
{
player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 2));
player.playSound(player.getLocation(), Sound.ENTITY_SPLASH_POTION_BREAK, 10, 1);
}
else if(block.getType().equals(Material.EMERALD_BLOCK))
{
if (player.getHealth() != 20)
{
player.setHealth(player.getHealth() + 1);
player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 10, 1);;
}
if (player.getHealth() == 20)
{
player.sendMessage(ChatColor.DARK_RED + "You are already at full health!");
}
}
}
}
For some reason, all of these things happen twice whenever I right click the designated blocks. Anyone know why? I have posted the entire method, it's a player interaction event.
Thanks :)
First of all, make sure yo haven't registered the Listener class containing the event handler twice.
If that's not the case, according to this thread on the spigot forums, since Mojang added the left hand slot to Minecraft some events like PlayerInteractEvent or InventoryClickEvent will be called twice (once for each hand).
One possible fix is to "disable" the left hand on the event handler:
#EventHandler
public void onPlayerInteraction(PlayerInteractEvent event) {
if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getHand() == EquipmentSlot.HAND) {
//Do something once
}
}
If you require that both hands could be used to trigger the event you could do the following:
First time the code gets executed you add the player to a list.
Before executing the code you check if the player is in the list. If it's in the list it means the code was executed once so you can skip it.
Schedule a task to remove the player from the list some ticks later.
The code could be as follows:
public class Foo implements Listener {
//An instance of the main plugin class
private MainClass plugin;
private List<UUID> playerBlacklist = new ArrayList<>();
#EventHandler
public void onPlayerInteractEvent(PlayerInteractEvent event) {
if(playerBlacklist.contains(event.getPlayer().getUniqueId)) {
return;
} else {
blacklistPlayer(event.getPlayer());
}
//Do something
}
private void blacklistPlayer(UUID uuid) {
playerBlacklist.add(uuid);
BukkitRunnable runnable = new BukkitRunnable(){
#Override
public void run() {
playerBlacklist.remove(uuid);
}
}
runnable.runTaskLaterAsynchronously(plugin, 5L);
}
}
Let me know if this solved your issue.
Currently trying to create a potion effect that once it runs out of time, applies other potion effects to the player. Seemed simple enough yet I found a few errors and bugs trying to accomplish this,
Directly trying to add the effect
#Override
public void performEffect(EntityLivingBase entity, int amplifier){
if (entity instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer)entity;
if(player != null){
if(player.getActivePotionEffect(PotionRegistry.effectBuzz) != null){
int duraction = player.getActivePotionEffect(PotionRegistry.effectBuzz).getDuration();
if(duration <= 2){
player.addPotionEffect(new PotionEffect(MobEffects.WEAKNESS, 1200));
}
}
}
}
}
Needless to say this produces this error
[16:10:04] [Server thread/ERROR]: Encountered an unexpected exception
net.minecraft.util.ReportedException: Ticking player
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:212)
~[NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:807)
~[MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:688)
~[MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156)
~[IntegratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:537)
[MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
Caused by: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(Unknown Source) ~[?:1.8.0_161]
at java.util.HashMap$KeyIterator.next(Unknown Source) ~[?:1.8.0_161]
at net.minecraft.entity.EntityLivingBase.updatePotionEffects(EntityLivingBase.java:650)
~[EntityLivingBase.class:?]
at net.minecraft.entity.EntityLivingBase.onEntityUpdate(EntityLivingBase.java:383)
~[EntityLivingBase.class:?]
at net.minecraft.entity.Entity.onUpdate(Entity.java:436) ~[Entity.class:?]
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2144)
~[EntityLivingBase.class:?]
at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:260)
~[EntityPlayer.class:?]
at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:345)
~[EntityPlayerMP.class:?]
at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:174)
~[NetHandlerPlayServer.class:?]
at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:216)
~[NetworkDispatcher$1.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:309)
~[NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:197)
~[NetworkSystem.class:?]
... 5 more
Where as if I run this in a tick event
In CommonProxy
MinecraftForge.EVENT_BUS.register(new EventManager());
And then For the EventManager itself
public class EventManager {
public static PotionEffect potion = new PotionEffect(MobEffects.WEAKNESS, 1200);
public static PotionEffect potion2 = new PotionEffect(MobEffects.HUNGER, 600);
public static PotionEffect potion3 = new PotionEffect(MobEffects.UNLUCK, 1200);
#SubscribeEvent
public void onTick(WorldTickEvent event){
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
World world = Minecraft.getMinecraft().theWorld;
if(player != null){
boolean hasEffect = player.isPotionActive(PotionRegistry.effectBuzz);
int applyIt = 0;
if(hasEffect){
applyIt = 1;
} else if(!player.isPotionActive(potion.getPotion()) && applyIt == 1){
applyIt = 2;
} else {
applyIt = 0;
}
if(player != null && applyIt == 2){
player.addPotionEffect(potion);
}
}
}
}
This works, yet the effects are infinite.
You're performing your action whilst the potion effects are being looped. This is akin to modifying an array whilst iterating it. Don't do that.
Also, don't perform actions like potion effects client side.
The ONLY thing to do client side is graphics and user input/output.
Things like potions must be handled on the server, otherwise the server will overwrite your actions on the next update packet.
Just set a flag in your ExtendPlayer entity, and then onTick or player update event check for that flag existance and then add the potions.
#Override
public void performEffect(EntityLivingBase entity, int amplifier){
if (entity instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer)entity;
if(player != null){
if(player.getActivePotionEffect(PotionRegistry.effectBuzz) != null){
int duraction = player.getActivePotionEffect(PotionRegistry.effectBuzz).getDuration();
if(duration <= 2){
ExtendedPlayer ePlayer = ExtendedPlayer.get(player);
ePlayer.enableBuzz();
}
}
}
}
}
Something akin to your Extended player
public class ExtendedPlayer implements IExtendedEntityProperties {
... Extended player setup here
protected boolean startBuzz = false;
public void enableBuzz()
{
this.startBuzz = true;
}
public static final ExtendedPlayer get(EntityPlayer player) {
return (ExtendedPlayer) player.getExtendedProperties("MuddymansExtendedPlayer");
}
public EntityPlayer getPlayer() {
return this.player;
}
/**
* Updates anything that needs to be updated each tick
* NOT called automatically, so you must call it yourself from LivingUpdateEvent or a TickHandler
*/
public void onUpdate() {
if(!player.worldObj.isRemote) {
if(this.enableBuzz) {
Player player = this.getPlayer()
player.addPotionEffect(new PotionEffect(MobEffects.WEAKNESS, 1200));
player.addPotionEffect(new PotionEffect(MobEffects.HUNGER, 600));
player.addPotionEffect(new PotionEffect(MobEffects.UNLUCK, 1200));
this.startBuzz = false;
}
}
}
}
Call the exteded player update event from an event handler
#SubscribeEvent
public void livingTick(final LivingUpdateEvent event) {
if (event.entity != null && event.entity instanceof EntityPlayer) {
if(!event.entity.isDead) {
ExtendedPlayer.get((EntityPlayer)event.entity).onUpdate();
}
}
When I click the Item in the Inventory, it simply does nothing and I can drag it anywhere I want. Then I re-open the inventory and the Item is back. I want the item a click (The Notify item in this case) to toggle the notify boolean and close the Inventory. Please help. It may be a stupid solution but sometimes the simple stuff can escape me. Thanks.
This is the Inventory:
public class GUI extends JavaPlugin implements Listener
{
private API api;
AlphaCommand command = new AlphaCommand();
public static Inventory inv = Bukkit.createInventory(null, 9, ChatColor.AQUA + "Alpha Config");
public static boolean isNotifyEnabled()
{
return AlphaCommand.notify;
}
protected String getCheckName()
{
return this.getCheckName();
}
public static void openGUI(Player player, Inventory inv)
{
ItemStack checks = new ItemStack(Material.DIAMOND_SWORD);
ItemStack banPlayer = new ItemStack(Material.GOLD_AXE);
ItemStack autoBan = new ItemStack(Material.DIAMOND_AXE);
ItemStack notify = new ItemStack(Material.SLIME_BALL);
ItemMeta checksMeta = checks.getItemMeta();
ItemMeta autoBanMeta = autoBan.getItemMeta();
ItemMeta notifyMeta = notify.getItemMeta();
ItemMeta banPlayerMeta = banPlayer.getItemMeta();
checksMeta.setDisplayName(ChatColor.BLUE + "Checks");
autoBanMeta.setDisplayName(ChatColor.BLUE + "AutoBan");
notifyMeta.setDisplayName(ChatColor.BLUE + "Notify");
banPlayerMeta.setDisplayName(ChatColor.BLUE + "Ban Player");
checks.setItemMeta(checksMeta);
autoBan.setItemMeta(autoBanMeta);
notify.setItemMeta(notifyMeta);
banPlayer.setItemMeta(banPlayerMeta);
inv.setItem(7, banPlayer);
inv.setItem(1, checks);
inv.setItem(3, autoBan);
inv.setItem(5, notify);
player.openInventory(inv);
}
public static void open(Player player, Inventory inv)
{
if(player instanceof Player)
{
player.openInventory(inv);
}
}
public static Inventory getGUI(){
return inv;
}
#EventHandler
public void onInventoryClick(InventoryClickEvent e)
{
Bukkit.getPluginManager().registerEvents(this, Bukkit.getPluginManager().getPlugins()[0]);
String invName = ChatColor.stripColor(e.getClickedInventory().getName());
Player player = (Player) e.getWhoClicked(); // The player that clicked the item
ItemStack clicked = e.getCurrentItem(); // The item that was clicked
#SuppressWarnings("unused")
Inventory inventory = e.getInventory(); // The inventory that was clicked in
if(invName.equalsIgnoreCase(inv.getName()))
{
if((clicked == null) || (clicked.getType() == Material.AIR) || (!clicked.hasItemMeta()))
{
player.closeInventory();
return;
}
if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("AutoBan"))
{
Configuration config = this.api.getConfiguration();
boolean autoBan = config.readBoolean("checks." + getCheckName() + ".AutoBan");
if (config.readBoolean("checks." + getCheckName() + ".AutoBan") == true)
{
autoBan = false;
e.setCancelled(true);
player.closeInventory();
return;
}
else if(autoBan == false)
{
autoBan = true;
e.setCancelled(true);
player.closeInventory();
return;
}
else
{
e.setCancelled(true);
player.closeInventory();
return;
}
}
if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("Notify"))
{
boolean notify = isNotifyEnabled();
if(notify == true)
{
notify = false;
e.setCancelled(true);
player.closeInventory();
return;
}
else if(notify == false)
{
notify = true;
e.setCancelled(true);
player.closeInventory();
return;
}
}
if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("Checks"))
{
player.sendMessage(ChatColor.RED + "This Feature is coming soon to Alpha.");
e.setCancelled(true);
player.closeInventory();
return;
}
if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("Ban Player"))
{
player.sendMessage("This Feature is coming soon to Alpha V.4");
e.setCancelled(true);
player.closeInventory();
return;
}else
{
player.closeInventory();
e.setCancelled(true);
return;
}
}
}
}
There seems to be multiple issues in the code that you have provided above that will cause the expected result to not be seen.
#1
Firstly you register the events for this class, inside the event itself, which means it'll never get registered unless the event get's called, which will never happen unless you register it... See the problem? So what you need to do is move the following line of code into your onEnable method.
Bukkit.getPluginManager().registerEvents(this, Bukkit.getPluginManager().getPlugins()[0]);
You need to make a few changes to this line of code. You need to replace this with a new instance of the listener object, new GUI(). It's also recommended that you register the listener with the plugin that your calling it from, but fortunately, if you put this in your onEnable, your onEnable method is inside your plugin class. This means that you can change your plugin to this. You new line of code will look like this.
Bukkit.getPluginManager().registerEvents(new GUI(), this);
Put that in your onEnable and that issue here should be resolved.
#2
The second issue is that when you're checking your item names and you're not stripping colour. This get's rid of an colours in the string. This is because your item names are ChatColor.BLUE + "AutoBan" however you're checking if it equals "AutoBan" which is different. All you need to do to fix this issue is exactly the same with what you did with the inventory name, and use ChatColor.stripColor(itemName) to get a string you can check against.
For example:
String itemName = ChatColor.stripColor(clicked.getItemMeta().getDisplayName());
if (itemName.equals("AutoBan")){
Note: I also replaced equalsIgnoreCase with equals as the cases always matched, so was an unnecessary check.
Thanks in advance for your help!
I am building an Arkanoid/Breakout style game, and am running into some issues on account of body removal (at least that's where I suspect that the issue is). When I run the game, most of the time everything works just fine, but every now and then, the game crashes. during this crash, all of the sprites attached to bodies disappear, and any sprite without a body is left on the screen. There are no LogCat errors, but there is a drastic slowdown in FPS. Also, I noticed that sometime the contacts are registered twice before the code in the contact listener is fired (you can see where I implement the log file output in the ContactListener below).
In the game, instead of bricks, I am using bolts, and the code that I used to generate the bolts is listed below. After that, I show how I am using a ContactListener, and after that I give the code for removing the body/sprite combination. Any help or suggestions are greatly appreciated!
Thanks,
Adam
Creating the body/sprite items that will be removed when they are contacted.
//BOLTS
final FixtureDef boltFixtureDef = PhysicsFactory.createFixtureDef(1, 1.0f, 0.1f);
final Body[] boltBody=new Body[BOLT_NUMBER];
final Sprite[] boltSprite=new Sprite[BOLT_NUMBER];
final PhysicsConnector[] facePhysicsConnector= new PhysicsConnector[BOLT_NUMBER];
String[] bodyNames=new String[BOLT_NUMBER];
for(int i=0; i<BOLT_NUMBER; i++){
boltSprite[i] = new Sprite(-100,-100, sceneManager.mBolt, sceneManager.activity.getVertexBufferObjectManager());
boltSprite[i].setCullingEnabled(true);
boltBody[i] = PhysicsFactory.createBoxBody(mPhysicsWorld, boltSprite[i], BodyType.KinematicBody, boltFixtureDef);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(boltSprite[i], boltBody[i], true, true));
bodyNames[i]="bolt"+Integer.toString(i);
boltBody[i].setUserData(bodyNames[i]);
facePhysicsConnector[i] = mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(boltSprite[i]);
boltBody[i].setTransform(xLocations[i]/PMR, yLocations[i]/PMR, 0);}
for(int i=0; i<BOLT_NUMBER; i++){
scene.attachChild(boltSprite[i]);}
The ContactListener:
final ContactListener contact = new ContactListener(){
#Override
public void postSolve(Contact contact, ContactImpulse impulse) {
String a = (String) contact.getFixtureA().getBody().getUserData();
String b = (String) contact.getFixtureB().getBody().getUserData();
if(a != null && b != null) {
for(int i=0; i<BOLT_NUMBER; i++){
if(a.equals("wrench") && b.equals(facePhysicsConnector[i].getBody().getUserData()) ||
a.equals(facePhysicsConnector[i].getBody().getUserData()) && b.equals("wrench")) {
//boltCollision[i]=true;
Log.i("contact","bolt contact" + i);
myDestroyer(facePhysicsConnector[i], true);
Log.i("contact","bolt POSTcontact" + i);}
else if((a.equals("wrench") && b.equals("paddle")) ||
(a.equals("paddle") && b.equals("wrench"))) {
Log.i("contact","paddle contact");
SpringNoise=true;}
else if((a.equals("wrench") && b.equals("door")) ||
(a.equals("door") && b.equals("wrench"))) {
Log.i("contact","door contact");
WoodNoise=true;}
}}
}
#Override
public void endContact(final Contact contact) {}
#Override
public void beginContact(Contact contact) {}
#Override
public void preSolve(Contact contact, Manifold oldManifold) {}
};
And here is the code to remove the body/sprite combo:
private void myDestroyer(final PhysicsConnector facePhysicsConnector2, final Boolean bodyToo){
if(killingInProcess == false){
killingInProcess = true;
//final PhysicsConnector facePhysicsConnector2 = mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(myShape);
sceneValue.engine.registerUpdateHandler(new IUpdateHandler(){
#Override
public void onUpdate(float pSecondsElapsed) {
sceneValue.engine.unregisterUpdateHandler(this);
sceneValue.engine.runOnUpdateThread(new Runnable(){
#Override
public void run() {
Log.i("contact","removing bolt");
mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector2);
Vibrator v = (Vibrator) sceneValue.activity.getSystemService(Context.VIBRATOR_SERVICE);
//myFixture.getBody().destroyFixture(myFixture);
if(bodyToo == true){
mPhysicsWorld.destroyBody(facePhysicsConnector2.getBody());
}
facePhysicsConnector2.getShape().registerEntityModifier(new ParallelEntityModifier(new RotationModifier(3,180,0),
new ScaleModifier(3, 1, 0)));
sceneValue.mBlockSound.play();
v.vibrate(75);
//mScene.detachChild(mySprite);
System.gc();
killingInProcess = false;
}
});
}
#Override
public void reset() {
}
});
}}
I'm not sure about the crashing problem, but I do know why the removal of bodies happens too late.
The method postSolve is called from the UpdaeThread. But instead of removing the body there, you register another update handler (So it's code runs in the next game update). And in this code, you run another code in runOnUpdateThread so it runs again in the next game update. You skipped 2 updates for no reason :) This might create problems (It probably does)
I suggest you to fix this, this look up for another problems (This may serious and it's easily solved)
I'm not sure, but I think that the problem might be in your removing of sprites. I met the problem and now I'm trying to kill bodies in update handler like this(method onUpdate in IUpdateHandler):
final PhysicsConnector physicsConnector = physicsWorld
.getPhysicsConnectorManager().findPhysicsConnectorByShape(
body);
body.clearEntityModifiers();
physicsWorld.unregisterPhysicsConnector(physicsConnector );
physicsWorld.destroyBody(physicsConnector.getBody());
scene.detachChild(body);
activity.getEngine().unregisterUpdateHandler(this);