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;
}
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));
}
}
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();
}
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;
}
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);
}
Aggregations