Skip to content

Commit

Permalink
Make a lot of the requested changes. Some more still need to be made.
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Sep 7, 2019
1 parent 5bd68fb commit 7097401
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 75 deletions.
32 changes: 18 additions & 14 deletions src/api/java/com/teamacronymcoders/epos/api/locks/LockRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;
import javax.annotation.Nonnull;

//TODO: JavaDoc all methods once the RequirementCombiner system is developed, as some of the methods here may change slightly
public class LockRegistry {

private static final List<IRequirement> EMPTY_REQUIREMENTS = Collections.emptyList();
Expand All @@ -23,11 +24,16 @@ public class LockRegistry {
private final Map<ILockKey, List<IRequirement>> locks = new HashMap<>();
private final Map<ILockKey, Set<IFuzzyLockKey>> fuzzyLockInfo = new HashMap<>();

/**
* Unlike the 1.12 system this has it so that each lock key has one registration for each type it can be created from Each one has to be checked when getting by type
* rather than knowing it "should" be good for that type already
*
* @param creator A Lock Key Creator that creates keys of type KEY given an object.
* @param <KEY> The type of key the given lock creator is for.
*/
//TODO: Evaluate if there is any performance impact of checking against all compared to directly getting the subset that works
public <KEY extends ILockKey> void registerLockType(@Nonnull ILockKeyCreator<KEY> creator) {
keyCreators.add(creator);
//Unlike the 1.12 system this has it so that each lock key has one registration for each type it can be created from
//Each one has to be checked when getting by type rather than knowing it "should" be good for that type already
//Does this have any negative performance impact
}

/**
Expand All @@ -39,17 +45,15 @@ public void clearLocks() {
}

public void addLockByKey(@Nonnull ILockKey key, @Nonnull List<IRequirement> requirements) {
if (key instanceof GenericLockKey || requirements.isEmpty()) {
//Don't allow having locks on the generic key that is just for when there isn't enough information about a fuzzy lock key
return;
}
locks.put(key, requirements);

if (key instanceof IFuzzyLockKey) {
IFuzzyLockKey fuzzy = (IFuzzyLockKey) key;
if (!fuzzy.isNotFuzzy()) {
//Store the fuzzy instance in a list for the specific item
fuzzyLockInfo.computeIfAbsent(fuzzy.getNotFuzzy(), k -> new HashSet<>()).add(fuzzy);
//Don't allow having locks on the generic key that is just for when there isn't enough information about a fuzzy lock key
if (!(key instanceof GenericLockKey) && !requirements.isEmpty()) {
locks.put(key, requirements);
if (key instanceof IFuzzyLockKey) {
IFuzzyLockKey fuzzy = (IFuzzyLockKey) key;
if (!fuzzy.isNotFuzzy()) {
//Store the fuzzy instance in a list for the specific item
fuzzyLockInfo.computeIfAbsent(fuzzy.getNotFuzzy(), k -> new HashSet<>()).add(fuzzy);
}
}
}
}
Expand Down
18 changes: 1 addition & 17 deletions src/main/java/com/teamacronymcoders/epos/locks/DefaultLocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,22 @@
public class DefaultLocks {

public static void registerKeyLookups() {
//ItemLockKey
EposAPI.LOCK_REGISTRY.registerLockType(object -> object instanceof ItemStack ? ItemLockKey.fromItemStack((ItemStack) object) : null);
//ModLockKey
EposAPI.LOCK_REGISTRY.registerLockType(ItemLockKey::fromObject);
EposAPI.LOCK_REGISTRY.registerLockType(ModLockKey::fromObject);
//Generic lock based entirely on NBT
EposAPI.LOCK_REGISTRY.registerLockType(GenericNBTLockKey::fromObject);

//Ones that probably will be in an addon but for now are from CompatSkills for more test cases of seeing how the system works

//DimensionTypeLockKey
EposAPI.LOCK_REGISTRY.registerLockType(DimensionTypeLockKey::fromObject);
//EntityDamageKey
EposAPI.LOCK_REGISTRY.registerLockType(EntityDamageKey::fromObject);
//EntityMountKey
EposAPI.LOCK_REGISTRY.registerLockType(EntityMountKey::fromObject);
//EntityTameKey
EposAPI.LOCK_REGISTRY.registerLockType(EntityTameKey::fromObject);
//ArmorLockKey
EposAPI.LOCK_REGISTRY.registerLockType(object -> object instanceof ItemStack ? ArmorLockKey.fromItemStack((ItemStack) object) : null);
//ArmorToughnessKey
EposAPI.LOCK_REGISTRY.registerLockType(object -> object instanceof ItemStack ? ArmorToughnessLockKey.fromItemStack((ItemStack) object) : null);
//AttackDamageLockKey
EposAPI.LOCK_REGISTRY.registerLockType(object -> object instanceof ItemStack ? AttackDamageLockKey.fromItemStack((ItemStack) object) : null);
//HungerLockKey
EposAPI.LOCK_REGISTRY.registerLockType(HungerLockKey::fromObject);
//SaturationLockKey
EposAPI.LOCK_REGISTRY.registerLockType(SaturationLockKey::fromObject);
//BlockHarvestLockKey
EposAPI.LOCK_REGISTRY.registerLockType(BlockHarvestLockKey::fromObject);
//ToolHarvestLockKey
EposAPI.LOCK_REGISTRY.registerLockType(object -> object instanceof ItemStack ? ToolHarvestLockKey.fromItemStack((ItemStack) object) : null);
//ParentTagLockKey
EposAPI.LOCK_REGISTRY.registerLockType(ParentTagLockKey::fromObject);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.teamacronymcoders.epos.locks.keys;

import com.google.common.collect.Multimap;
import com.teamacronymcoders.epos.api.locks.keys.GenericLockKey;
import com.teamacronymcoders.epos.api.locks.keys.IFuzzyLockKey;
import com.teamacronymcoders.epos.api.locks.keys.ILockKey;
import com.teamacronymcoders.epos.locks.FuzzyLockKeyTypes;
import java.util.Collection;
import com.teamacronymcoders.epos.utils.AttributeUtils;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -20,10 +21,10 @@ public class ArmorLockKey implements IFuzzyLockKey {

private final double armor;

/**
* @apiNote Ensure that the given armor value is positive.
*/
public ArmorLockKey(double armor) {
if (armor < 0) {
throw new IllegalArgumentException("Armor value must be greater than or equal to zero. Received: '" + armor + "'.");
}
this.armor = armor;
}

Expand All @@ -35,12 +36,10 @@ public static ArmorLockKey fromItemStack(@Nonnull ItemStack stack) {
Item item = stack.getItem();
if (item instanceof ArmorItem) {
ArmorItem armorItem = (ArmorItem) item;
double armor = armorItem.getDamageReduceAmount();
Collection<AttributeModifier> protection = armorItem.getAttributeModifiers(armorItem.getEquipmentSlot(), stack).get(SharedMonsterAttributes.ARMOR.getName());
if (!protection.isEmpty()) {
armor += protection.stream().findFirst().map(AttributeModifier::getAmount).orElse(0D);
}
return new ArmorLockKey(armor);
Multimap<String, AttributeModifier> attributeModifiers = armorItem.getAttributeModifiers(armorItem.getEquipmentSlot(), stack);
double armor = AttributeUtils.calculateValueFromModifiers(attributeModifiers, SharedMonsterAttributes.ARMOR, armorItem.getDamageReduceAmount());
//Ensure that the value is actually positive
return armor < 0 ? null : new ArmorLockKey(armor);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.teamacronymcoders.epos.locks.keys;

import com.google.common.collect.Multimap;
import com.teamacronymcoders.epos.api.locks.keys.GenericLockKey;
import com.teamacronymcoders.epos.api.locks.keys.IFuzzyLockKey;
import com.teamacronymcoders.epos.api.locks.keys.ILockKey;
import com.teamacronymcoders.epos.locks.FuzzyLockKeyTypes;
import java.util.Collection;
import com.teamacronymcoders.epos.utils.AttributeUtils;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -20,10 +21,10 @@ public class ArmorToughnessLockKey implements IFuzzyLockKey {

private final double toughness;

/**
* @apiNote Ensure that the given armor toughness value is positive.
*/
public ArmorToughnessLockKey(double toughness) {
if (toughness < 0) {
throw new IllegalArgumentException("Armor toughness must be greater than or equal to zero. Received: '" + toughness + "'.");
}
this.toughness = toughness;
}

Expand All @@ -35,12 +36,10 @@ public static ArmorToughnessLockKey fromItemStack(@Nonnull ItemStack stack) {
Item item = stack.getItem();
if (item instanceof ArmorItem) {
ArmorItem armorItem = (ArmorItem) item;
double toughness = armorItem.getToughness();
Collection<AttributeModifier> protection = armorItem.getAttributeModifiers(armorItem.getEquipmentSlot(), stack).get(SharedMonsterAttributes.ARMOR_TOUGHNESS.getName());
if (!protection.isEmpty()) {
toughness += protection.stream().findFirst().map(AttributeModifier::getAmount).orElse(0D);
}
return new ArmorToughnessLockKey(toughness);
Multimap<String, AttributeModifier> attributeModifiers = armorItem.getAttributeModifiers(armorItem.getEquipmentSlot(), stack);
double toughness = AttributeUtils.calculateValueFromModifiers(attributeModifiers, SharedMonsterAttributes.ARMOR_TOUGHNESS, armorItem.getToughness());
//Ensure that the value is actually positive
return toughness < 0 ? null : new ArmorToughnessLockKey(toughness);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.teamacronymcoders.epos.api.locks.keys.IFuzzyLockKey;
import com.teamacronymcoders.epos.api.locks.keys.ILockKey;
import com.teamacronymcoders.epos.locks.FuzzyLockKeyTypes;
import java.util.Collection;
import com.teamacronymcoders.epos.utils.AttributeUtils;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -21,10 +21,10 @@ public class AttackDamageLockKey implements IFuzzyLockKey {

private final double attackDamage;

/**
* @apiNote Ensure that the given damage value is positive.
*/
public AttackDamageLockKey(double attackDamage) {
if (attackDamage < 0) {
throw new IllegalArgumentException("Damage value must be greater than or equal to zero. Received: '" + attackDamage + "'.");
}
this.attackDamage = attackDamage;
}

Expand All @@ -35,8 +35,9 @@ public static AttackDamageLockKey fromItemStack(@Nonnull ItemStack stack) {
}
Item item = stack.getItem();
Multimap<String, AttributeModifier> attributeModifiers = item.getAttributeModifiers(EquipmentSlotType.MAINHAND, stack);
Collection<AttributeModifier> damage = attributeModifiers.get(SharedMonsterAttributes.ATTACK_DAMAGE.getName());
return damage.isEmpty() ? null : new AttackDamageLockKey(damage.stream().findFirst().map(AttributeModifier::getAmount).orElse(0D));
double damage = AttributeUtils.calculateValueFromModifiers(attributeModifiers, SharedMonsterAttributes.ATTACK_DAMAGE);
//Ensure that the value is actually positive
return damage < 0 ? null : new AttackDamageLockKey(damage);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ public class GenericNBTLockKey extends NBTLockKey {

private static final GenericLockKey NOT_FUZZY = new GenericLockKey(FuzzyLockKeyTypes.GENERIC_NBT);

public GenericNBTLockKey(@Nullable CompoundNBT nbt) {
/**
* @apiNote Ensure that the given Compound is not empty.
*/
public GenericNBTLockKey(@Nonnull CompoundNBT nbt) {
super(nbt);
}

@Nullable
public static GenericNBTLockKey fromObject(@Nonnull Object object) {
if (object instanceof ItemStack) {
ItemStack stack = (ItemStack) object;
return stack.isEmpty() ? null : new GenericNBTLockKey(((ItemStack) object).getTag());
return stack.hasTag() ? fromCompound(stack.getTag()) : null;
} else if (object instanceof CompoundNBT) {
return new GenericNBTLockKey((CompoundNBT) object);
return fromCompound((CompoundNBT) object);
}
return null;
}

@Nullable
private static GenericNBTLockKey fromCompound(@Nonnull CompoundNBT nbt) {
return nbt.isEmpty() ? null : new GenericNBTLockKey(nbt);
}

@Override
@Nonnull
public ILockKey getNotFuzzy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class HungerLockKey implements IFuzzyLockKey {

private final int hunger;

/**
* @apiNote Ensure that the given hunger value is positive.
*/
public HungerLockKey(int hunger) {
if (hunger < 0) {
throw new IllegalArgumentException("Hunger value must be greater than or equal to zero. Received: '" + hunger + "'.");
}
this.hunger = hunger;
}

Expand All @@ -47,7 +47,12 @@ private static HungerLockKey fromItem(@Nonnull Item item) {

@Nullable
private static HungerLockKey fromFood(@Nullable Food food) {
return food == null ? null : new HungerLockKey(food.getHealing());
if (food == null) {
return null;
}
int healing = food.getHealing();
//Ensure that the value is actually positive
return healing < 0 ? null : new HungerLockKey(healing);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ public ItemLockKey(@Nonnull Item item, @Nullable CompoundNBT nbt) {
}

@Nullable
public static ItemLockKey fromItemStack(@Nonnull ItemStack stack) {
return stack.isEmpty() ? null : new ItemLockKey(stack.getItem(), stack.getTag());
public static ItemLockKey fromObject(@Nonnull Object object) {
if (object instanceof ItemStack) {
ItemStack stack = (ItemStack) object;
return stack.isEmpty() ? null : new ItemLockKey(stack.getItem(), stack.getTag());
} else if (object instanceof Item) {
return new ItemLockKey((Item) object);
}
//TODO: Add case for block and for blockstate (or maybe just add a BlockStateLockKey)
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class SaturationLockKey implements IFuzzyLockKey {

private final float saturation;

/**
* @apiNote Ensure that the given saturation value is positive.
*/
public SaturationLockKey(float saturation) {
if (saturation < 0) {
throw new IllegalArgumentException("Saturation value must be greater than or equal to zero. Received: '" + saturation + "'.");
}
this.saturation = saturation;
}

Expand All @@ -47,7 +47,12 @@ private static SaturationLockKey fromItem(@Nonnull Item item) {

@Nullable
private static SaturationLockKey fromFood(@Nullable Food food) {
return food == null ? null : new SaturationLockKey(food.getHealing() * food.getSaturation() * 2F);
if (food == null) {
return null;
}
float saturation = food.getHealing() * food.getSaturation() * 2F;
//Ensure that the value is actually positive
return saturation < 0 ? null : new SaturationLockKey(saturation);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class BlockHarvestLockKey extends HarvestLockKey {

private static final GenericLockKey NOT_FUZZY = new GenericLockKey(FuzzyLockKeyTypes.BLOCK_HARVEST);

/**
* @apiNote Ensure that the given harvest level is positive.
*/
public BlockHarvestLockKey(int harvestLevel) {
super(harvestLevel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public abstract class HarvestLockKey implements IFuzzyLockKey {

protected final int harvestLevel;

/**
* @apiNote Ensure that the given harvest level is positive.
*/
protected HarvestLockKey(int harvestLevel) {
if (harvestLevel < 0) {
throw new IllegalArgumentException("Harvest level must be greater than or equal to zero. Received: '" + harvestLevel + "'.");
}
this.harvestLevel = harvestLevel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ public class ToolHarvestLockKey extends HarvestLockKey {
@Nullable
private final ToolType toolType;

/**
* @apiNote Ensure that the given harvest level is positive.
*/
public ToolHarvestLockKey(@Nullable ToolType toolType, int harvestLevel) {
super(harvestLevel);
this.toolType = toolType;
this.typeLevels = new HashMap<>();
}

/**
* @apiNote Ensure that the given harvest level is positive.
*/
private ToolHarvestLockKey(@Nonnull Map<ToolType, Integer> typeLevels, int harvestLevel) {
super(harvestLevel);
this.toolType = null;
Expand All @@ -56,7 +62,7 @@ public static ToolHarvestLockKey fromItemStack(@Nonnull ItemStack stack) {
}
//TODO: Decide if it matches only one tool type if it should just be that type instead of it as a map
// Note: Would have to rewrite fuzzyEquals implementation
return highestLevel == -1 ? null : new ToolHarvestLockKey(typeLevels, highestLevel);
return highestLevel < 0 || typeLevels.isEmpty() ? null : new ToolHarvestLockKey(typeLevels, highestLevel);
}

@Override
Expand Down
Loading

0 comments on commit 7097401

Please sign in to comment.