gpt4 book ai didi

java - 如何使可变出血与这两个事件一起工作

转载 作者:行者123 更新时间:2023-12-01 12:39:50 24 4
gpt4 key购买 nike

@EventHandler
public void onEntityDamageByEntityEvent(EntityDamageEvent event)
{

if(Math.random()<=0.02)
{
Entity e = event.getEntity();

int bleed = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
{
public void run()
{
//Two percent chance you will bleed

if(e instanceof Player)
{
Player player = (Player) e;

player.damage(1.0D);
player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 2));
player.sendMessage(ChatColor.RED + "You are bleeding, find a bandage and heal yourself");

public void onPlayerEvent(PlayerInteractEvent event) { //ERROR HAPPENS ON THIS LINE ON THE PARENTHESES

Player player = event.getPlayer();
Action act = event.getAction();

if(event.getAction() == act.RIGHT_CLICK_AIR) {
if(player.getItemInHand().getType() == Material.PAPER) {
plugin.getServer().getScheduler().cancelTask(bleed);
}
}
}
}
}
}, 20, 400);
}
}

我完全意识到我不允许在方法中包含方法,但我不知道有任何其他方法可以实现此目的。

第 7 行的 bleed 变量应该与第 22 行的相同变量相对应

第二个事件应该取消第一个事件中的任务。

我收到的错误是:“(”,;预期的语法错误

编辑:

@AndrewG 所以,我测试了你的代码,它不能正常工作,这是我所拥有的类的全部源代码:`

package com.aidan.TeamHeal;


import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;



public class BukkitListener implements Listener {

Map<UUID, Integer> bleed = new HashMap<UUID, Integer>();

public static Bukkit plugin;

public BukkitListener(Bukkit instance) {
plugin = instance;
}



@EventHandler
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
if(Math.random() <1) {
Entity e = event.getEntity();
// Entity z = event.getDamager();

if(e instanceof Player) {
Player player = (Player) e;

player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 1));
}
}
}



@EventHandler
public void onEntityDamageByEntityEvent(EntityDamageEvent event) {

if (Math.random() <= 1) {
final Entity e = event.getEntity();

int playersBleed = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {

public void run() {
// Two percent chance you will bleed

if (e instanceof Player) {
Player player = (Player) e;

player.damage(1.0D);
player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 2));
player.sendMessage(ChatColor.RED + "You are bleeding, find a bandage and heal yourself");

}
}
}, 20, 400);
bleed.put(e.getUniqueId(), new Integer(playersBleed));
}
}

// Don't forget the @EventHandler annotation here too!
@EventHandler
public void onPlayerEvent(PlayerInteractEvent event) {

Player player = event.getPlayer();
if (event.getAction() == Action.RIGHT_CLICK_AIR) {
if (player.getItemInHand().getType() == Material.PAPER) {

if(bleed.containsKey(player.getUniqueId())) {
Integer playersBleed = bleed.get(player.getUniqueId());
int playersBleedInt = playersBleed.intValue();
plugin.getServer().getScheduler().cancelTask(playersBleedInt);
player.sendMessage("YOu have been healed");



}
}
}
}
}

`

最佳答案

这是 Bukkit 吗?如果您完全意识到方法中不能包含方法,那么为什么还要有一个方法呢?

您的问题是您没有在方法之外使用变量。例如:

public class Bleed extends JavaPlugin {

// Put the variable here, so it can be accessed by all the methods in the class.

int bleed = 0; // Declare it here, use it elsewhere. We'll just have 0 as the default value.


@EventHandler
public void onEntityDamageByEntityEvent(EntityDamageEvent event) {

if (Math.random() <= 0.02) {
Entity e = event.getEntity();

// Don't declare it again, just assign a value to it.
bleed = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {

public void run() {
// Two percent chance you will bleed

if (e instanceof Player) {
Player player = (Player) e;

player.damage(1.0D);
player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 2));
player.sendMessage(ChatColor.RED + "You are bleeding, find a bandage and heal yourself");

}
}
}, 20, 400);
}
}

// Don't forget the @EventHandler annotation here too!
@EventHandler
public void onPlayerEvent(PlayerInteractEvent event) {

Player player = event.getPlayer();
Action act = event.getAction();

if (event.getAction() == act.RIGHT_CLICK_AIR) {
if (player.getItemInHand().getType() == Material.PAPER) {
// Assuming that the onEntityDamageByEntityEvent is called before this, bleed will have the value set in that method.
// Let's make sure that actually happened, and bleed isn't still 0.
if(bleed != 0) {
plugin.getServer().getScheduler().cancelTask(bleed);
}
}
}
}
}

我所做的更改主要是在类范围内声明一个出血变量。 (在这里停下来,阅读有关 Java 中变量作用域的所有内容,这非常重要。)现在我们有了一个两个方法都可以访问的变量,我们取出另一个方法,并将其放在应该的位置。我做的最后一件事就是确保我们只在实际设置了任务时才调用 cancelTask​​()(这意味着 bleed 中确实有东西)。 if 语句。

现在这就引入了另一个问题,如果玩家 A 开始流血,然后玩家 B 在玩家 A 停止之前就开始流血了怎么办?现在玩家 B 的任务存储在 bleed 中,而玩家 A 的任务消失了。我们需要的是为每个流血的玩家存储不同的bleed变量。为此,我们可以使用 HashMap。更改此:

int bleed = 0;

对此:

Map<UUID, Integer> bleed = new HashMap<UUID, Integer>();

这会创建一个列表,因此我们可以将玩家的 ID 存储在代表其流血任务的 Integer 对象旁边(因为我们无法在 HashMap 中使用基元,所以我们使用 Integer 对象)。

然后,我们改变这个:

bleed = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {

public void run() {
// Two percent chance you will bleed

if (e instanceof Player) {
Player player = (Player) e;

player.damage(1.0D);
player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 2));
player.sendMessage(ChatColor.RED + "You are bleeding, find a bandage and heal yourself");

}
}
}, 20, 400);

对此:

int playersBleed = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {

public void run() {
// Two percent chance you will bleed

if (e instanceof Player) {
Player player = (Player) e;

player.damage(1.0D);
player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 2));
player.sendMessage(ChatColor.RED + "You are bleeding, find a bandage and heal yourself");

}
}
}, 20, 400);
bleed.put(player.getUniqueId(), new Integer(playersBleed));

它的作用是从任务中获取 int,将其放入一个新的 Integer 中,并将其放在玩家 ID 旁边。然后我们改变这个:

if(bleed != 0) {
plugin.getServer().getScheduler().cancelTask(bleed);
}

至:

if(bleed.containsKey(player.getUniqueId()) { // If that player is bleeding.
Integer playersBleed = bleed.get(player.getUniqueId());
int playersBleedInt = playersBleed.intValue();
plugin.getServer().getScheduler().cancelTask(playersBleedInt);
}

最后(呵呵),您需要更改此:

Entity e = event.getEntity();

对此:

final Entity e = event.getEntity();

这仅允许可运行对象内的代码获取 e 变量。

这一切都有意义吗?我知道我走得有点快..我很乐意在评论中解释更多。希望对您有所帮助,如有任何问题,请随时在评论中提出。

关于java - 如何使可变出血与这两个事件一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25224454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com