2016-06-29 50 views
3

因此,我有一個叫做HitDistanceRatios的類,它實現了CommandExecutor,它在MainClass中被調用,它是唯一一個調用JavaPlugin的類,除了ConfigGets。我還有另一個叫HitEvent的課程,這是一個EventHandler,每次玩家被擊中時,傷害者的UUID被添加到Multimap,hitMap,其中命中距離被存儲爲值Double。還有一個應該知道的類是HitDistance,它實際上可以接收hitMapHitDistanceHitDistanceRatios之間的差異是在MainClass中調用HitDistanceRatios來執行命令。Bukkit - 爲什麼我的Multimap不被傳遞?

基本上我遇到的問題是HitDistanceRatios沒有采取任何其他類的任何東西。我嘗試了多種方法從HitEvent獲取hitMap,甚至通過HitDistance嘗試獲取它,但我無法找到有效的方法。

這裏是HitDistanceRatios

package yt.Kaelinator.commands; 
 

 
import java.text.DecimalFormat; 
 

 
import org.bukkit.Bukkit; 
 
import org.bukkit.ChatColor; 
 
import org.bukkit.command.Command; 
 
import org.bukkit.command.CommandExecutor; 
 
import org.bukkit.command.CommandSender; 
 
import org.bukkit.entity.Player; 
 

 
import yt.Kaelinator.ConfigGets; 
 
import yt.Kaelinator.MainClass; 
 
import yt.Kaelinator.calculations.HitDistance; 
 

 
public class HitDistanceRatios implements CommandExecutor { 
 

 
    public HitDistanceRatios(MainClass instance) {} 
 

 
    @ 
 
    Override 
 
    public boolean onCommand(CommandSender sender, Command command, String cmd, String[] args) { 
 
    if (!(sender instanceof Player)) 
 
     return false; 
 
    Player player = (Player) sender; 
 

 
    if (args.length == 0) { 
 

 
     DecimalFormat numberFormat = new DecimalFormat("#.00"); 
 
     HitDistance hd = new HitDistance(null); 
 

 
     player.sendMessage(ConfigGets.prefix + ChatColor.BLUE + player.getName() + ": " + ChatColor.RED + numberFormat.format(hd.getFourMap(player.getUniqueId())) + ChatColor.BLUE + "% of hits are over 4 blocks, " + ChatColor.RED + numberFormat.format(hd.getThreeHalfMap(player.getUniqueId())) + ChatColor.BLUE + "% are over 3.5 blocks."); 
 
     return true; 
 
    } else {@ 
 
     SuppressWarnings("deprecation") 
 
     Player target = Bukkit.getPlayer(args[0]); 
 

 
     if (target == null) { 
 
     player.sendMessage(ConfigGets.prefix + "Player " + args[0] + " couldn't be found."); 
 
     return true; 
 

 
     } else { 
 

 
     DecimalFormat numberFormat = new DecimalFormat("#.00"); 
 
     HitDistance hd = new HitDistance(null); 
 

 
     player.sendMessage(ConfigGets.prefix + ChatColor.BLUE + target.getName() + ": " + ChatColor.RED + numberFormat.format(hd.getFourMap(target.getUniqueId())) + ChatColor.BLUE + "% of hits are over 4 blocks, " + ChatColor.RED + numberFormat.format(hd.getThreeHalfMap(target.getUniqueId())) + ChatColor.BLUE + "% are over 3.5 blocks."); 
 
     } 
 
     return true; 
 
    } 
 
    } 
 
}
這裏,類正在採取 HitDistance並試圖從那裏一對夫婦HashMaps這樣,但我得到了同樣的結果,如果我在 HitEvent使用一個getter類。

這裏是HitEvent

package yt.Kaelinator.events; 
 

 
import java.util.UUID; 
 

 
import org.bukkit.Bukkit; 
 
import org.bukkit.ChatColor; 
 
import org.bukkit.Location; 
 
import org.bukkit.entity.Player; 
 
import org.bukkit.event.EventHandler; 
 
import org.bukkit.event.Listener; 
 
import org.bukkit.event.entity.EntityDamageByEntityEvent; 
 

 
import com.google.common.collect.ArrayListMultimap; 
 
import com.google.common.collect.Multimap; 
 

 
import yt.Kaelinator.ConfigGets; 
 
import yt.Kaelinator.MainClass; 
 
import yt.Kaelinator.calculations.HitDistance; 
 
import yt.Kaelinator.calculations.Lag; 
 

 
public class HitEvent implements Listener { 
 

 
    Multimap < UUID, Double > hitMap = ArrayListMultimap.create(); 
 

 
    public HitEvent(MainClass instance) {} 
 

 
    @ 
 
    EventHandler 
 
    public void onPlayerHit(EntityDamageByEntityEvent event) { 
 

 
    // make sure they are all players 
 
    if (!(event.getEntity() instanceof Player) | !(event.getDamager() instanceof Player)) 
 
     return; 
 

 
    // get the event's players 
 
    Player damaged = (Player) event.getEntity(); 
 
    Player damager = (Player) event.getDamager(); 
 

 
    // get the locations 
 
    Location damagedLocation = damaged.getLocation(); 
 
    Location damagerLocation = damager.getLocation(); 
 

 
    // calculate the distance 
 
    double distance = damagedLocation.distance(damagerLocation); 
 

 
    // broadcast it to everyone 
 
    ClickEvent ce = new ClickEvent(); 
 
    boolean displayHits = ConfigGets.DisplayDistances; 
 
    if (displayHits) { 
 
     for (Player p: Bukkit.getOnlinePlayers()) { 
 
     p.sendMessage(ChatColor.translateAlternateColorCodes('&', 
 
      ConfigGets.prefix + 
 
      "&f" + damager.getDisplayName() + "&3 damaged &f" + damaged.getDisplayName() + "&3 Distance: &4" + distance + " &7&l" + ce.checkForLag(damager.getUniqueId()) + " " + ce.checkForLag(damaged.getUniqueId()) + " " + Math.ceil(Lag.getTPS()))); 
 
     } 
 
    } 
 

 
    if (!ce.checkForLag(damager.getUniqueId()) & !ce.checkForLag(damaged.getUniqueId()) & !(Math.ceil(Lag.getTPS()) < 20)) { 
 
     hitMap.put(damager.getUniqueId(), distance); 
 
    } 
 

 
    UUID playerUUID = damager.getUniqueId(); 
 
    int hitAmount = hitMap.get(playerUUID).size(); 
 

 
    if ((hitAmount % 5 == 0) & hitAmount >= 20) { 
 

 
     HitDistance hd = new HitDistance(null); 
 
     hd.hasReach(hitMap.get(playerUUID), playerUUID); 
 
    } 
 
    } 
 

 
    public Multimap < UUID, Double > getHitMap() { 
 
    return hitMap; 
 
    } 
 
}

正如你所看到的,我有我會用一個getter,此getter曾經是一個集合,將返回命中距離的集合給出一個UUID。從另一個課程獲得的任何內容返回爲null,導致錯誤。

這裏的HitDistance

package yt.Kaelinator.calculations; 
 

 
import java.util.Collection; 
 
import java.util.HashMap; 
 
import java.util.UUID; 
 

 
import org.bukkit.Bukkit; 
 

 
import yt.Kaelinator.ConfigGets; 
 
import yt.Kaelinator.MainClass; 
 
import yt.Kaelinator.events.HitEvent; 
 

 
public class HitDistance { 
 

 
    public HitDistance(MainClass instance) {} 
 

 
    public HashMap < UUID, Double > mapFourOverTotal = new HashMap < UUID, Double >(); 
 
    public HashMap < UUID, Double > mapThreeHalfOverTotal = new HashMap < UUID, Double >(); 
 

 
    HitEvent he = new HitEvent(null); 
 

 
    public void hasReach(Collection <Double> collection, UUID uuid) { 
 

 
    int length = collection.size(); 
 

 
    Double[] distances = collection.toArray(new Double[length]); 
 

 
    int overThreeHalf = 0; 
 
    int overFour = 0; 
 
    int rep = 0; 
 
    for (int i = 0; i < distances.length; i++) { 
 
     if (distances[i] > 4.0) { 
 
     overFour++; 
 
     if (i >= 5) { 
 
      if (distances[i - 1] > 4.0) 
 
      rep++; 
 
      if (distances[i - 2] > 4.05) 
 
      rep += 2; 
 
      if (distances[i - 3] > 4.1) 
 
      rep += 3; 
 
     } 
 
     } else if (distances[i] > 3.5) { 
 
     overThreeHalf++; 
 
     if (i >= 5) { 
 
      if (distances[i - 1] > 3.6) 
 
      rep++; 
 
      if (distances[i - 2] > 3.7) 
 
      rep += 2; 
 
      if (distances[i - 3] > 3.8) 
 
      rep += 3; 
 
     } 
 
     } 
 
    } 
 

 
    double fourOverTotal = overFour/distances.length; 
 
    double threeHalfOverTotal = overThreeHalf/distances.length; 
 

 
    mapFourOverTotal.put(uuid, fourOverTotal); 
 
    mapThreeHalfOverTotal.put(uuid, threeHalfOverTotal); 
 

 
    if (fourOverTotal >= 1/3) { 
 
     rep += 10; 
 
    } 
 

 
    if (threeHalfOverTotal/distances.length >= 2/5) { 
 
     rep += 10; 
 
    } 
 

 

 

 
    if (rep >= 30) { 
 
     Bukkit.getPlayer(uuid).kickPlayer(ConfigGets.prefix + 
 
     ConfigGets.kickForReach); 
 
    } 
 
    } 
 

 
    public double getFourMap(UUID uuid) { 
 
    return mapFourOverTotal.get(uuid); 
 
    } 
 

 
    public double getThreeHalfMap(UUID uuid) { 
 
    return mapThreeHalfOverTotal.get(uuid); 
 
    } 
 
}

同樣,你可以看到我有幾個干將,從給定UUID一個HashMap獲取值。但是,不要驚慌,一切都會返回null。這對我來說非常混亂,因爲HitDistance可以訪問hitMap,但HitDistanceRatios不能訪問任何內容。我覺得這事做與他們的MainClass內如何相互作用:

package yt.Kaelinator; 
 

 
import org.bukkit.Bukkit; 
 
import org.bukkit.plugin.PluginManager; 
 
import org.bukkit.plugin.java.JavaPlugin; 
 

 
import yt.Kaelinator.calculations.Lag; 
 
import yt.Kaelinator.commands.HitDistanceRatios; 
 
import yt.Kaelinator.events.ClickEvent; 
 
import yt.Kaelinator.events.HitEvent; 
 

 
public class MainClass extends JavaPlugin { 
 

 
    public static MainClass instance = null; 
 

 
    public void onEnable() { 
 
    registerEvents(); 
 
    registerConfig(); 
 
    registerCommands(); 
 
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Lag(), 100L, 1L); 
 
    } 
 

 
    public void registerEvents() { 
 
    PluginManager pm = getServer().getPluginManager(); 
 
    pm.registerEvents(new HitEvent(null), this); 
 
    pm.registerEvents(new ClickEvent(), this); 
 
    } 
 

 
    private void registerConfig() { 
 
    getConfig().options().copyDefaults(true); 
 
    saveConfig(); 
 
    } 
 

 
    private void registerCommands() { 
 
    getCommand("hitdistanceratio").setExecutor(new HitDistanceRatios(this)); 
 
    } 
 

 
}
有什麼建議?

+0

minecraft類型的問題通常是StackOverflow的焦點話題。不知道爲什麼標籤仍然在這裏。他們幾乎不可能複製。 – redFIVE

+0

@redFIVE我知道:/我已經試過去那裏。我只是希望這是一個人們用java來解決的問題。 – Kaelinator

+0

想要做什麼的代碼?或者,相反,你想要代碼做什麼? –

回答

0

這是一個複雜的。從一點閱讀中,我看到如果你沒有指定訪問修飾符,它默認爲「私有包」。我不完全知道這意味着什麼,但如果你在HitEvent行更改爲這個,我認爲:

public Multimap < UUID, Double > hitMap = ArrayListMultimap.create(); 

它應該工作,如果它是這樣的。原因是訪問修飾符「私有包」只允許特定包中的類訪問它。如果試圖訪問它的類不在其包中,則它無法訪問。

相關問題