Search in sources :

Example 6 with IArmor

use of net.silentchaos512.gems.api.IArmor in project SilentGems by SilentChaos512.

the class SoulSkill method selectSkillToLearn.

public static SoulSkill selectSkillToLearn(ToolSoul soul, ItemStack tool) {
    // if (soul.getLevel() == 5) {
    // return SUPER_SKILL;
    // }
    Map<SoulSkill, Double> candidates = new LinkedHashMap<>();
    // Create list of candidates
    for (SoulSkill skill : SKILL_LIST.values()) {
        if (skill.canLearn(soul, tool)) {
            boolean favorsElement = false;
            // Select a weight based on favored elements.
            double weight = skill.favoredElements.length < 1 ? 20 : 7;
            for (EnumSoulElement elem : skill.favoredElements) {
                if (elem == soul.getPrimaryElement()) {
                    weight = 20;
                    favorsElement = true;
                    break;
                } else if (elem == soul.getSecondaryElement()) {
                    weight = 15;
                    favorsElement = true;
                    break;
                }
            }
            // Favors certain tool types?
            if (skill.favoredType != EnumToolType.NONE) {
                EnumToolType toolType = tool.getItem() instanceof ITool ? ((ITool) tool.getItem()).getToolType() : tool.getItem() instanceof IArmor ? ((IArmor) tool.getItem()).getToolType() : EnumToolType.NONE;
                if (toolType == skill.favoredType) {
                    weight += 5;
                }
            }
            // If skill has a median level, apply that to the weight.
            if (skill.medianXpLevel > 0) {
                int diff = Math.abs(soul.level - skill.medianXpLevel);
                if (diff > 6) {
                    diff = 6;
                }
                weight -= 0.75 * diff;
            }
            // If a lower level of the skill is already known, reduce the weight.
            if (soul.skills.containsKey(skill)) {
                weight -= 2.5 * soul.skills.get(skill);
            }
            // Base weight diff, favors multiplier
            weight += skill.weightDiff;
            if (favorsElement) {
                weight *= skill.favorWeightMulti;
            }
            // Make sure weight is at least 1.
            if (weight < 1) {
                weight = 1;
            }
            candidates.put(skill, weight);
        }
    }
    // Seed based on soul elements, level, and tool UUID.
    Random rand = new Random(soul.getPrimaryElement().ordinal() + (soul.getSecondaryElement().ordinal() << 4) + (soul.getLevel() << 8) + (ToolHelper.getUUID(tool).getLeastSignificantBits() << 16));
    // Weighted random selection.
    SoulSkill selected = null;
    double bestValue = Double.MIN_VALUE;
    for (SoulSkill skill : candidates.keySet()) {
        double value = -Math.log(rand.nextFloat() / candidates.get(skill));
        SilentGems.logHelper.debug(skill.id, candidates.get(skill), value, bestValue);
        if (value > bestValue) {
            bestValue = value;
            selected = skill;
        }
    }
    return selected;
}
Also used : Random(java.util.Random) EnumToolType(net.silentchaos512.gems.lib.EnumToolType) EnumSoulElement(net.silentchaos512.gems.lib.soul.EnumSoulElement) ITool(net.silentchaos512.gems.api.ITool) IArmor(net.silentchaos512.gems.api.IArmor) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with IArmor

use of net.silentchaos512.gems.api.IArmor in project SilentGems by SilentChaos512.

the class ItemSkillOrb method clOnItemRightClick.

@Override
protected ActionResult<ItemStack> clOnItemRightClick(World world, EntityPlayer player, EnumHand hand) {
    ItemStack offhand = player.getHeldItemOffhand();
    // Orb in main hand, tool/armor in offhand.
    if (hand != EnumHand.MAIN_HAND || StackHelper.isEmpty(offhand) || !(offhand.getItem() instanceof ITool || offhand.getItem() instanceof IArmor)) {
        return new ActionResult<ItemStack>(EnumActionResult.PASS, player.getHeldItem(hand));
    }
    ItemStack orb = player.getHeldItemMainhand();
    // Check for tool soul.
    ToolSoul soul = SoulManager.getSoul(offhand);
    if (soul == null) {
        if (!world.isRemote) {
            ChatHelper.sendMessage(player, SilentGems.localizationHelper.getItemSubText(Names.SKILL_ORB, "no_soul"));
        }
        return new ActionResult<ItemStack>(EnumActionResult.PASS, orb);
    }
    // Check skill on orb.
    SoulSkill skill = getSkill(orb);
    if (skill == null) {
        if (!world.isRemote) {
            ChatHelper.sendMessage(player, SilentGems.localizationHelper.getItemSubText(Names.SKILL_ORB, "no_skill"));
        }
        return new ActionResult<ItemStack>(EnumActionResult.PASS, orb);
    }
    if (world.isRemote) {
        return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
    }
    // Try to add or level up the skill.
    if (soul.addOrLevelSkill(skill, offhand, player)) {
        ToolHelper.recalculateStats(offhand);
        StackHelper.shrink(orb, 1);
        return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, orb);
    } else {
        ChatHelper.sendMessage(player, SilentGems.localizationHelper.getItemSubText(Names.SKILL_ORB, "max_level"));
        return new ActionResult<ItemStack>(EnumActionResult.PASS, player.getHeldItem(hand));
    }
}
Also used : ToolSoul(net.silentchaos512.gems.lib.soul.ToolSoul) ActionResult(net.minecraft.util.ActionResult) EnumActionResult(net.minecraft.util.EnumActionResult) SoulSkill(net.silentchaos512.gems.lib.soul.SoulSkill) ItemStack(net.minecraft.item.ItemStack) ITool(net.silentchaos512.gems.api.ITool) IArmor(net.silentchaos512.gems.api.IArmor)

Example 8 with IArmor

use of net.silentchaos512.gems.api.IArmor in project SilentGems by SilentChaos512.

the class RecipeMixedMaterialItem method getCraftingResult.

@Override
public ItemStack getCraftingResult(InventoryCrafting inv) {
    if (!partTiersMatch(inv)) {
        return StackHelper.empty();
    }
    ItemStack rod = getRod(inv);
    ItemStack frame = getFrame(inv);
    ItemStackList materials = getMaterials(inv);
    ItemStack[] array = materials.toArray(new ItemStack[materials.size()]);
    if (toolItem instanceof ITool)
        return ((ITool) toolItem).constructTool(rod, array);
    if (toolItem instanceof IArmor)
        return ((IArmor) toolItem).constructArmor(frame, array);
    return StackHelper.empty();
}
Also used : ItemStackList(net.silentchaos512.lib.collection.ItemStackList) ItemStack(net.minecraft.item.ItemStack) ITool(net.silentchaos512.gems.api.ITool) IArmor(net.silentchaos512.gems.api.IArmor)

Example 9 with IArmor

use of net.silentchaos512.gems.api.IArmor in project SilentGems by SilentChaos512.

the class SoulManager method getSoul.

@Nullable
public static ToolSoul getSoul(ItemStack tool) {
    if (StackHelper.isEmpty(tool) || !(tool.getItem() instanceof ITool || tool.getItem() instanceof IArmor) || ToolHelper.isExampleItem(tool)) {
        return null;
    }
    UUID uuid = ToolHelper.getSoulUUID(tool);
    // Soul already in map?
    ToolSoul soul = map.get(uuid);
    if (soul != null) {
        return soul;
    }
    // Does soul exist in NBT?
    ToolHelper.initRootTag(tool);
    if (!tool.getTagCompound().hasKey(ToolHelper.NBT_ROOT_TOOL_SOUL)) {
        return null;
    }
    // Read from NBT, place in map for fast access.
    soul = ToolSoul.readFromNBT(ToolHelper.getRootTag(tool, ToolHelper.NBT_ROOT_TOOL_SOUL));
    if (uuid == null) {
        ToolHelper.setRandomSoulUUID(tool);
        uuid = ToolHelper.getSoulUUID(tool);
    }
    map.put(uuid, soul);
    // SilentGems.logHelper.debug("Put tool soul " + soul + " in the map! Total count: " + map.size());
    return soul;
}
Also used : ToolSoul(net.silentchaos512.gems.lib.soul.ToolSoul) UUID(java.util.UUID) ITool(net.silentchaos512.gems.api.ITool) IArmor(net.silentchaos512.gems.api.IArmor) Nullable(javax.annotation.Nullable)

Example 10 with IArmor

use of net.silentchaos512.gems.api.IArmor in project SilentGems by SilentChaos512.

the class ToolHelper method getUUID.

/**
 * Gets the tool's or armor's UUID. If it currently does not have a UUID, one will be created if appropriate.
 *
 * @param tool
 *          The tool or armor stack. The stack's item must implement either ITool or IArmor.
 * @return The UUID of the tool or armor, or null if the item is not allowed to have one.
 */
@Nullable
public static UUID getUUID(ItemStack tool) {
    if (!(tool.getItem() instanceof ITool || tool.getItem() instanceof IArmor)) {
        return null;
    }
    initRootTag(tool);
    if (!tool.getTagCompound().hasUniqueId(NBT_UUID)) {
        UUID uuid = UUID.randomUUID();
        tool.getTagCompound().setUniqueId(NBT_UUID, uuid);
        return uuid;
    }
    return tool.getTagCompound().getUniqueId(NBT_UUID);
}
Also used : UUID(java.util.UUID) ITool(net.silentchaos512.gems.api.ITool) IArmor(net.silentchaos512.gems.api.IArmor) Nullable(javax.annotation.Nullable)

Aggregations

IArmor (net.silentchaos512.gems.api.IArmor)14 ITool (net.silentchaos512.gems.api.ITool)10 ItemStack (net.minecraft.item.ItemStack)8 ToolSoul (net.silentchaos512.gems.lib.soul.ToolSoul)4 UUID (java.util.UUID)2 Nullable (javax.annotation.Nullable)2 Item (net.minecraft.item.Item)2 EnumMaterialTier (net.silentchaos512.gems.api.lib.EnumMaterialTier)2 ItemGemBow (net.silentchaos512.gems.item.tool.ItemGemBow)2 ItemGemShield (net.silentchaos512.gems.item.tool.ItemGemShield)2 EnumGem (net.silentchaos512.gems.lib.EnumGem)2 LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1 ActionResult (net.minecraft.util.ActionResult)1 EnumActionResult (net.minecraft.util.EnumActionResult)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1 EnumMaterialGrade (net.silentchaos512.gems.api.lib.EnumMaterialGrade)1 ToolPart (net.silentchaos512.gems.api.tool.part.ToolPart)1 ToolPartMain (net.silentchaos512.gems.api.tool.part.ToolPartMain)1 ItemToolSoul (net.silentchaos512.gems.item.ItemToolSoul)1