首页 > 解决方案 > Java - 对象类型的方法 getValue() 未定义

问题描述

我有这个错误:

方法 getValue() 未为 Object 类型定义

*这是错误代码: 在行中:

final String materialName = BasePlugin.getPlugin().getItemDb().getName(new ItemStack(((Object) ((Map)dataMap).entrySet().iterator().next()).getValue().getItemType(), 1));

完整代码

public class LandMap
{
    private static final int FACTION_MAP_RADIUS_BLOCKS = 22;
    private static final Material[] BLACKLISK;

    @SuppressWarnings("rawtypes")
    public static boolean updateMap(final Player player, final HCF plugin, final VisualType visualType, final boolean inform) {
        final Location location = player.getLocation();
        final World world = player.getWorld();
        final int locationX = location.getBlockX();
        final int locationZ = location.getBlockZ();
        final int minimumX = locationX - 22;
        final int minimumZ = locationZ - 22;
        final int maximumX = locationX + 22;
        final int maximumZ = locationZ + 22;
        final Set<Claim> board = new LinkedHashSet<Claim>();
        boolean subclaimBased;
        if (visualType == VisualType.SUBCLAIM_MAP) {
            subclaimBased = true;
        }
        else {
            if (visualType != VisualType.CLAIM_MAP) {
                player.sendMessage(ConfigurationService.RED + "Not supported: " + visualType.name().toLowerCase() + '.');
                return false;
            }
            subclaimBased = false;
        }
        for (int x = minimumX; x <= maximumX; ++x) {
            for (int z = minimumZ; z <= maximumZ; ++z) {
                final Claim claim = plugin.getFactionManager().getClaimAt(world, x, z);
                if (claim != null) {
                    if (subclaimBased) {
                        board.addAll(claim.getSubclaims());
                    }
                    else {
                        board.add(claim);
                    }
                }
            }
        }
        if (board.isEmpty()) {
            player.sendMessage(ConfigurationService.RED + "No claims are in your visual range to display.");
            return false;
        }
        for (final Claim claim2 : board) {
            if (claim2 == null) {
                continue;
            }
            final int maxHeight = Math.min(world.getMaxHeight(), 256);
            final Location[] corners = claim2.getCornerLocations();
            final List<MemoryBlockLocation> shown = new ArrayList<MemoryBlockLocation>(maxHeight * corners.length);
            for (final Location corner : corners) {
                for (int y = 0; y < maxHeight; ++y) {
                    shown.add(new MemoryBlockLocation(world, corner.getBlockX(), y, corner.getBlockZ()));
                }
            }
            final Object dataMap = plugin.getVisualiseHandler().generate(player, shown, visualType, true);
            if (((Map)dataMap).isEmpty()) {
                continue;
            }
            final String materialName = BasePlugin.getPlugin().getItemDb().getName(new ItemStack(((Object) ((Map)dataMap).entrySet().iterator().next()).getValue().getItemType(), 1));
            if (!inform || claim2.getFaction() == null) {
                continue;
            }
            player.sendMessage(ConfigurationService.YELLOW + claim2.getFaction().getDisplayName((CommandSender)player) + ConfigurationService.YELLOW + " owns land " + ConfigurationService.GRAY + " (displayed with " + materialName + ")" + ConfigurationService.YELLOW + '.');
        }
        return true;
    }

    public static Location getNearestSafePosition(final Player player, final Location origin, final int searchRadius) {
        return getNearestSafePosition(player, origin, searchRadius, false);
    }

    public static Location getNearestSafePosition(final Player player, final Location origin, final int searchRadius, final boolean stuck) {
        final FactionManager factionManager = HCF.getPlugin().getFactionManager();
        final Faction playerFaction = factionManager.getPlayerFaction(player.getUniqueId());
        final int max = ConfigurationService.BORDER_SIZES.get(origin.getWorld().getEnvironment());
        final int originalX = Math.max(Math.min(origin.getBlockX(), max), -max);
        final int originalZ = Math.max(Math.min(origin.getBlockZ(), max), -max);
        final int minX = Math.max(originalX - searchRadius, -max) - originalX;
        final int maxX = Math.min(originalX + searchRadius, max) - originalX;
        final int minZ = Math.max(originalZ - searchRadius, -max) - originalZ;
        final int maxZ = Math.min(originalZ + searchRadius, max) - originalZ;
        for (int x = 0; x < searchRadius; ++x) {
            if (x <= maxX) {
                if (-x >= minX) {
                    for (int z = 0; z < searchRadius; ++z) {
                        if (z <= maxZ) {
                            if (-z >= minZ) {
                                final Location atPos = origin.clone().add((double)x, 0.0, (double)z);
                                final Faction factionAtPos = factionManager.getFactionAt(atPos);
                                if (factionAtPos == null || (!stuck && playerFaction != null && playerFaction.equals(factionAtPos)) || !(factionAtPos instanceof PlayerFaction)) {
                                    final Location safe = getSafeLocation(origin.getWorld(), atPos.getBlockX(), atPos.getBlockZ());
                                    if (safe != null) {
                                        return safe.add(0.5, 0.5, 0.5);
                                    }
                                }
                                final Location atNeg = origin.clone().add((double)x, 0.0, (double)z);
                                final Faction factionAtNeg = factionManager.getFactionAt(atNeg);
                                if (factionAtNeg == null || (!stuck && playerFaction != null && playerFaction.equals(factionAtNeg)) || !(factionAtNeg instanceof PlayerFaction)) {
                                    final Location safe2 = getSafeLocation(origin.getWorld(), atNeg.getBlockX(), atNeg.getBlockZ());
                                    if (safe2 != null) {
                                        return safe2.add(0.5, 0.5, 0.5);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

    private static Location getSafeLocation(final World world, final int x, final int z) {
        Block highest = world.getHighestBlockAt(x, z);
        Material type = highest.getType();
        if (Arrays.asList(LandMap.BLACKLISK).contains(type)) {
            return null;
        }
        while (!type.isSolid()) {
            if (highest.getY() <= 1 || Arrays.asList(LandMap.BLACKLISK).contains(type)) {
                return null;
            }
            highest = highest.getRelative(BlockFace.DOWN);
            type = highest.getType();
        }
        return highest.getRelative(BlockFace.UP).getLocation();
    }

    static {
        BLACKLISK = new Material[] { Material.LEAVES, Material.LEAVES_2, Material.FENCE_GATE, Material.WATER, Material.LAVA, Material.STATIONARY_LAVA, Material.STATIONARY_WATER };
    }
}

进口:

import org.bukkit.entity.*;
import net.tutorialesaful.hardcorefactions.faction.claim.*;
import net.tutorialesaful.hardcorefactions.*;
import net.tutorialesaful.hardcorefactions.util.location.*;
import net.tutorialesaful.framework.*;
import net.tutorialesaful.hardcorefactions.visualise.*;
import org.bukkit.inventory.*;
import org.bukkit.command.*;
import org.bukkit.*;
import net.tutorialesaful.hardcorefactions.faction.type.*;
import java.util.*;
import org.bukkit.block.*;

这是一类 bukkit

标签: java

解决方案


什么对象generate()方法返回?你能在你的问题中添加generate()方法吗?

您尝试将其强制转换为Object

((Object) ((Map)dataMap).entrySet().iterator().next()).getValue()...

哪个(正如@MadProgrammer 所说)没有getValue()方法。如果您需要,Map您应该将其转换为Map并使用该get(Object key)方法

final String materialName = BasePlugin.getPlugin().getItemDb().getName(new ItemStack(((Map) ((Map)dataMap).entrySet().iterator().next()).get(**the key**).getItemType(), 1));

但在这种情况下,您必须知道key.


推荐阅读