Created
January 6, 2022 15:52
-
-
Save Lanse505/74b4ae4acb5bb48862fb1f7a801fbb49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // See Line: 122 | |
| package com.teamacronymcoders.essence.common.block.infusion.tile; | |
| import com.hrznstudio.titanium.annotation.Save; | |
| import com.hrznstudio.titanium.block.tile.ActiveTile; | |
| import com.hrznstudio.titanium.component.inventory.InventoryComponent; | |
| import com.teamacronymcoders.essence.Essence; | |
| import com.teamacronymcoders.essence.api.recipe.infusion.ExtendableInfusionRecipe; | |
| import com.teamacronymcoders.essence.common.block.infusion.InfusionPedestalBlock; | |
| import com.teamacronymcoders.essence.common.item.tome.TomeOfKnowledgeItem; | |
| import com.teamacronymcoders.essence.common.util.helper.EssenceWorldHelper; | |
| import com.teamacronymcoders.essence.compat.registrate.EssenceBlockRegistrate; | |
| import net.minecraft.client.multiplayer.ClientLevel; | |
| import net.minecraft.core.BlockPos; | |
| import net.minecraft.core.NonNullList; | |
| import net.minecraft.core.particles.ItemParticleOption; | |
| import net.minecraft.core.particles.ParticleTypes; | |
| import net.minecraft.resources.ResourceLocation; | |
| import net.minecraft.util.Mth; | |
| import net.minecraft.world.entity.player.Player; | |
| import net.minecraft.world.item.ItemStack; | |
| import net.minecraft.world.level.Level; | |
| import net.minecraft.world.level.block.state.BlockState; | |
| import net.minecraft.world.phys.Vec3; | |
| import javax.annotation.Nonnull; | |
| public class InfusionTableBlockEntity extends ActiveTile<InfusionTableBlockEntity> { | |
| private static final BlockPos[] pedestal_positions = new BlockPos[]{ | |
| new BlockPos(-3, 0, 0), | |
| new BlockPos(+3, 0, 0), | |
| new BlockPos(0, 0, +3), | |
| new BlockPos(0, 0, -3), | |
| new BlockPos(+2, 0, -2), | |
| new BlockPos(+2, 0, +2), | |
| new BlockPos(-2, 0, +2), | |
| new BlockPos(-2, 0, -2) | |
| }; | |
| @Save | |
| private String recipe; | |
| @Save | |
| private Boolean shouldBeWorking = false; | |
| @Save | |
| private Boolean isWorking = false; | |
| @Save | |
| private Integer workDuration = 0; | |
| @Save | |
| private Integer totalWorkDuration = 0; | |
| @Save | |
| private Boolean hasFiredSound = false; | |
| @Save | |
| private Integer ticksExisted = 0; | |
| @Save | |
| private final InventoryComponent<InfusionTableBlockEntity> infusable; | |
| @Save | |
| private final InventoryComponent<InfusionTableBlockEntity> tome; | |
| // Book Rendering Variables | |
| public int ticks; | |
| public float flip; | |
| public float oldFlip; | |
| public float flipT; | |
| public float flipA; | |
| public float open; | |
| public float oldOpen; | |
| public float rot; | |
| public float oldRot; | |
| public float tRot; | |
| public int particleTick = 0; | |
| @Save | |
| public Long pageSoundLastPlayed = 0L; | |
| public InfusionTableBlockEntity(BlockPos pos, BlockState state) { | |
| super(EssenceBlockRegistrate.INFUSION_TABLE.get(), pos, state); | |
| addInventory(infusable = new InventoryComponent<InfusionTableBlockEntity>("input", 80, 20, 1) | |
| .setComponentHarness(this) | |
| .setOutputFilter(this::canExtractInfusable) | |
| .setSlotLimit(1) | |
| ); | |
| addInventory(tome = new InventoryComponent<InfusionTableBlockEntity>("tome", 9, 10, 1) | |
| .setComponentHarness(this) | |
| .setOnSlotChanged((stack, integer) -> markComponentForUpdate(false)) | |
| .setInputFilter((stack, integer) -> false) | |
| .setOutputFilter((stack, integer) -> false) | |
| .setSlotLimit(1) | |
| ); | |
| } | |
| public static void tick(Level level, BlockPos pos, BlockState state, InfusionTableBlockEntity blockEntity) { | |
| blockEntity.ticksExisted++; | |
| if (level.isClientSide()) { | |
| runAnimationTickCycle(pos, blockEntity); | |
| } else { | |
| runLogicTickCycle(blockEntity); | |
| } | |
| } | |
| public static void runLogicTickCycle(InfusionTableBlockEntity be) { | |
| NonNullList<ItemStack> stacks = be.getPedestalStacks(); | |
| if (be.shouldBeWorking && !be.isWorking && be.recipe == null) be.getInfusionRecipe(be.getInfusable().getStackInSlot(0), stacks); | |
| be.markComponentForUpdate(false); | |
| if (be.recipe != null && (be.shouldBeWorking || be.isWorking)) { | |
| ExtendableInfusionRecipe recipe = (ExtendableInfusionRecipe) be.getLevel().getRecipeManager().byKey(new ResourceLocation(be.recipe)).get(); | |
| be.markComponentDirty(); | |
| if (be.totalWorkDuration == 0) { | |
| be.totalWorkDuration = recipe.duration; | |
| be.markComponentForUpdate(true); | |
| } | |
| if (!be.isWorking) { | |
| be.isWorking = true; | |
| be.markComponentForUpdate(true); | |
| } | |
| if (be.workDuration >= be.totalWorkDuration) { | |
| ItemStack infusable = be.infusable.getStackInSlot(0); | |
| ItemStack result = recipe.resolveRecipe(be, infusable); | |
| be.infusable.setStackInSlot(0, result); | |
| be.shouldBeWorking = false; | |
| be.isWorking = false; | |
| be.hasFiredSound = false; | |
| be.workDuration = 0; | |
| be.totalWorkDuration = 0; | |
| be.recipe = null; | |
| be.markComponentForUpdate(false); | |
| } else { | |
| be.workDuration++; | |
| } | |
| if (be.isWorking && !be.hasFiredSound) { | |
| EssenceWorldHelper.playInfusionSound(be, true); | |
| be.hasFiredSound = true; | |
| } | |
| } | |
| } | |
| public static void runAnimationTickCycle(BlockPos pos, InfusionTableBlockEntity blockEntity) { | |
| blockEntity.oldOpen = blockEntity.open; | |
| blockEntity.oldRot = blockEntity.rot; | |
| Player player = blockEntity.getLevel().getNearestPlayer((double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, 3.0D, false); | |
| if (player != null) { | |
| double d0 = player.getX() - ((double) pos.getX() + 0.5D); | |
| double d1 = player.getZ() - ((double) pos.getZ() + 0.5D); | |
| blockEntity.tRot = (float) Mth.atan2(d1, d0); | |
| blockEntity.open += 0.1F; | |
| if (blockEntity.open < 0.5F || Essence.RANDOM.nextInt(40) == 0) { | |
| float f1 = blockEntity.flipT; | |
| do { | |
| blockEntity.flipT += (float) (Essence.RANDOM.nextInt(4) - Essence.RANDOM.nextInt(4)); | |
| } while (f1 == blockEntity.flipT); | |
| } | |
| } else { | |
| blockEntity.tRot += 0.02F; | |
| blockEntity.open -= 0.1F; | |
| } | |
| while (blockEntity.rot >= (float) Math.PI) { | |
| blockEntity.rot -= ((float) Math.PI * 2F); | |
| } | |
| while (blockEntity.rot < -(float) Math.PI) { | |
| blockEntity.rot += ((float) Math.PI * 2F); | |
| } | |
| while (blockEntity.tRot >= (float) Math.PI) { | |
| blockEntity.tRot -= ((float) Math.PI * 2F); | |
| } | |
| while (blockEntity.tRot < -(float) Math.PI) { | |
| blockEntity.tRot += ((float) Math.PI * 2F); | |
| } | |
| float f2; | |
| for (f2 = blockEntity.tRot - blockEntity.rot; f2 >= (float) Math.PI; f2 -= ((float) Math.PI * 2F)) {} | |
| while (f2 < -(float) Math.PI) { | |
| f2 += ((float) Math.PI * 2F); | |
| } | |
| blockEntity.rot += f2 * 0.4F; | |
| blockEntity.open = Mth.clamp(blockEntity.open, 0.0F, 1.0F); | |
| ++blockEntity.ticks; | |
| blockEntity.oldFlip = blockEntity.flip; | |
| float f = (blockEntity.flipT - blockEntity.flip) * 0.4F; | |
| float f3 = 0.2F; | |
| f = Mth.clamp(f, -f3, f3); | |
| blockEntity.flipA += (f - blockEntity.flipA) * 0.9F; | |
| blockEntity.flip += blockEntity.flipA; | |
| if (blockEntity.isWorking) { | |
| if (blockEntity.particleTick >= 3) { | |
| for (InfusionPedestalBlockEntity pedestal : blockEntity.getPedestals()) { | |
| ClientLevel level = (ClientLevel) blockEntity.getLevel(); | |
| BlockPos targetPosition = blockEntity.getBlockPos().above(2); | |
| BlockPos pedestalPosition = pedestal.getBlockPos(); | |
| Vec3 targetVec = new Vec3(targetPosition.getX(), targetPosition.getY(), targetPosition.getZ()); | |
| Vec3 pedestalVec = new Vec3(pedestalPosition.getX(), pedestalPosition.getY(), pedestalPosition.getZ()); | |
| Vec3 subtractedVec = targetVec.subtract(pedestalVec); | |
| float delta = blockEntity.workDuration > 0 && blockEntity.totalWorkDuration > 0 ? Mth.clamp((blockEntity.workDuration / blockEntity.totalWorkDuration), 0, 1) : 0; | |
| Vec3 lerpedVec = subtractedVec.lerp(new Vec3(0,0,0), delta); | |
| level.addParticle( | |
| new ItemParticleOption(ParticleTypes.ITEM, pedestal.getStack()), | |
| pedestalPosition.getX() + 0.5, pedestalPosition.getY() + 1.25, pedestalPosition.getZ() + 0.5, | |
| lerpedVec.x * 0.1, lerpedVec.y * 0.15, lerpedVec.z * 0.1 | |
| ); | |
| blockEntity.particleTick = 0; | |
| } | |
| } else { | |
| blockEntity.particleTick++; | |
| } | |
| } | |
| } | |
| @Nonnull | |
| @Override | |
| public InfusionTableBlockEntity getSelf() { | |
| return this; | |
| } | |
| private boolean canExtractInfusable(ItemStack stack, int slot) { | |
| return !isWorking; | |
| } | |
| public NonNullList<InfusionPedestalBlockEntity> getPedestals() { | |
| BlockPos tablePosition = this.getBlockPos(); | |
| NonNullList<InfusionPedestalBlockEntity> pedestals = NonNullList.create(); | |
| if (getLevel() != null) { | |
| for (BlockPos pos : pedestal_positions) { | |
| BlockPos pedestalPosition = tablePosition.offset(pos.getX(), pos.getY(), pos.getZ()); | |
| if (getLevel().getBlockState(pedestalPosition).getBlock() instanceof InfusionPedestalBlock && getLevel().getBlockEntity(pedestalPosition) instanceof InfusionPedestalBlockEntity pedestal) { | |
| pedestals.add(pedestal); | |
| } | |
| } | |
| } | |
| return pedestals; | |
| } | |
| public NonNullList<ItemStack> getPedestalStacks() { | |
| BlockPos tablePosition = this.getBlockPos(); | |
| NonNullList<ItemStack> stacks = NonNullList.create(); | |
| if (getLevel() != null) { | |
| for (BlockPos pos : pedestal_positions) { | |
| BlockPos pedestalPosition = tablePosition.offset(pos.getX(), pos.getY(), pos.getZ()); | |
| if (getLevel().getBlockState(pedestalPosition).getBlock() instanceof InfusionPedestalBlock && getLevel().getBlockEntity(pedestalPosition) instanceof InfusionPedestalBlockEntity pedestal) { | |
| stacks.add(pedestal.getStack()); | |
| } | |
| } | |
| } | |
| return stacks; | |
| } | |
| private void getInfusionRecipe(ItemStack infusable, NonNullList<ItemStack> stacks) { | |
| if (getLevel() != null) { | |
| getLevel().getRecipeManager().getRecipes() | |
| .stream() | |
| .filter(iRecipe -> iRecipe instanceof ExtendableInfusionRecipe) | |
| .map(iRecipe -> (ExtendableInfusionRecipe) iRecipe) | |
| .filter(recipes -> recipes.isValid(infusable, stacks)) | |
| .findFirst().ifPresent(recipe -> this.recipe = recipe.getId().toString()); | |
| } | |
| } | |
| public Boolean getWorking() { | |
| return isWorking; | |
| } | |
| public InventoryComponent<InfusionTableBlockEntity> getTome() { | |
| return tome; | |
| } | |
| public InventoryComponent<InfusionTableBlockEntity> getInfusable() { | |
| return infusable; | |
| } | |
| public void setShouldBeWorking(Boolean shouldBeWorking) { | |
| this.shouldBeWorking = shouldBeWorking; | |
| } | |
| public boolean hasTome() { | |
| return !tome.getStackInSlot(0).isEmpty() && tome.getStackInSlot(0).getItem() instanceof TomeOfKnowledgeItem; | |
| } | |
| public Integer getTicksExisted() { | |
| return ticksExisted; | |
| } | |
| public Long getPageSoundLastPlayed() { | |
| return pageSoundLastPlayed; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // See Line: 86 | |
| package com.teamacronymcoders.essence.api.recipe.infusion; | |
| import com.hrznstudio.titanium.recipe.serializer.GenericSerializer; | |
| import com.hrznstudio.titanium.recipe.serializer.SerializableRecipe; | |
| import com.teamacronymcoders.essence.Essence; | |
| import com.teamacronymcoders.essence.common.block.infusion.tile.InfusionTableBlockEntity; | |
| import com.teamacronymcoders.essence.common.util.EssenceTags; | |
| import com.teamacronymcoders.essence.common.util.helper.EssenceBowHelper; | |
| import com.teamacronymcoders.essence.common.util.helper.EssenceJsonHelper; | |
| import com.teamacronymcoders.essence.common.util.helper.recipe.EssenceModifierRecipeHelper; | |
| import com.teamacronymcoders.essence.common.util.tier.EssenceToolTiers; | |
| import com.teamacronymcoders.essence.compat.registrate.EssenceModifierRegistrate; | |
| import com.teamacronymcoders.essence.data.ingredient.TierIngredient; | |
| import net.minecraft.core.NonNullList; | |
| import net.minecraft.resources.ResourceLocation; | |
| import net.minecraft.world.effect.MobEffectInstance; | |
| import net.minecraft.world.effect.MobEffects; | |
| import net.minecraft.world.item.ItemStack; | |
| import net.minecraft.world.item.Items; | |
| import net.minecraft.world.item.alchemy.PotionUtils; | |
| import net.minecraft.world.item.alchemy.Potions; | |
| import net.minecraft.world.item.crafting.Ingredient; | |
| import net.minecraft.world.item.crafting.RecipeType; | |
| import net.minecraftforge.common.Tags; | |
| import net.minecraftforge.data.loading.DatagenModLoader; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Optional; | |
| public class InfusionRecipeModifier extends ExtendableInfusionRecipe { | |
| public static GenericSerializer<InfusionRecipeModifier> SERIALIZER = new GenericSerializer<>(new ResourceLocation(Essence.MOD_ID, "infusion/infusion_modifier"), InfusionRecipeModifier.class); | |
| public static List<InfusionRecipeModifier> RECIPES = new ArrayList<>(); | |
| static { | |
| if (DatagenModLoader.isRunningDataGen()) { | |
| RECIPES.add( | |
| new InfusionRecipeModifier( | |
| new ResourceLocation(Essence.MOD_ID, "test_modifier_add"), | |
| TierIngredient.of(EssenceTags.EssenceItemTags.ESSENCE_SWORD, EssenceToolTiers.ESSENCE, Optional.empty()), | |
| new Ingredient[]{ | |
| Ingredient.of(Tags.Items.GEMS_QUARTZ), Ingredient.of(Tags.Items.GEMS_QUARTZ), | |
| Ingredient.of(Tags.Items.GEMS_QUARTZ), Ingredient.of(Tags.Items.GEMS_QUARTZ), | |
| Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL), | |
| Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL) | |
| }, | |
| SerializableModifier.getSerializableModifiers( | |
| new SerializableModifier( | |
| EssenceModifierRegistrate.STRENGTHENED_SHARPNESS_MODIFIER.get(), | |
| 1, | |
| null, | |
| InfusionOperation.ADD | |
| ) | |
| ), | |
| 100 | |
| ) | |
| ); | |
| RECIPES.add(new InfusionRecipeModifier(new ResourceLocation(Essence.MOD_ID, "test_modifier_remove"), TierIngredient.of(EssenceTags.EssenceItemTags.ESSENCE_SWORD, EssenceToolTiers.ESSENCE, Optional.empty()), new Ingredient[]{Ingredient.of(Tags.Items.GEMS_PRISMARINE), Ingredient.of(Tags.Items.GEMS_PRISMARINE), Ingredient.of(Tags.Items.GEMS_PRISMARINE), Ingredient.of(Tags.Items.GEMS_PRISMARINE), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL)}, SerializableModifier.getSerializableModifiers(new SerializableModifier(EssenceModifierRegistrate.STRENGTHENED_SHARPNESS_MODIFIER.get(), 1, null, InfusionOperation.REMOVE)), 100)); | |
| RECIPES.add(new InfusionRecipeModifier(new ResourceLocation(Essence.MOD_ID, "test_modifier_increment"), TierIngredient.of(EssenceTags.EssenceItemTags.ESSENCE_SWORD, EssenceToolTiers.ESSENCE, Optional.empty()), new Ingredient[]{Ingredient.of(Tags.Items.GEMS_QUARTZ), Ingredient.of(Tags.Items.GEMS_QUARTZ), Ingredient.of(Tags.Items.GEMS_QUARTZ), Ingredient.of(Tags.Items.GEMS_QUARTZ), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED)}, SerializableModifier.getSerializableModifiers(new SerializableModifier(EssenceModifierRegistrate.STRENGTHENED_SHARPNESS_MODIFIER.get(), 1, null, InfusionOperation.INCREMENT)), 100)); | |
| RECIPES.add(new InfusionRecipeModifier(new ResourceLocation(Essence.MOD_ID, "test_modifier_decrement"), TierIngredient.of(EssenceTags.EssenceItemTags.ESSENCE_SWORD, EssenceToolTiers.ESSENCE, Optional.empty()), new Ingredient[]{Ingredient.of(Tags.Items.GEMS_PRISMARINE), Ingredient.of(Tags.Items.GEMS_PRISMARINE), Ingredient.of(Tags.Items.GEMS_PRISMARINE), Ingredient.of(Tags.Items.GEMS_PRISMARINE), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED)}, SerializableModifier.getSerializableModifiers(new SerializableModifier(EssenceModifierRegistrate.STRENGTHENED_SHARPNESS_MODIFIER.get(), 1, null, InfusionOperation.INCREMENT)), 100)); | |
| RECIPES.add(new InfusionRecipeModifier(new ResourceLocation(Essence.MOD_ID, "test_modifier_merge_tags"), TierIngredient.of(EssenceTags.EssenceItemTags.ESSENCE_BOW, EssenceToolTiers.ESSENCE, Optional.empty()), new Ingredient[]{EssenceJsonHelper.getNBTIngredient(PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.FIRE_RESISTANCE)), EssenceJsonHelper.getNBTIngredient(PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.FIRE_RESISTANCE)), EssenceJsonHelper.getNBTIngredient(PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.FIRE_RESISTANCE)), EssenceJsonHelper.getNBTIngredient(PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.FIRE_RESISTANCE)), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED), Ingredient.of(EssenceTags.EssenceItemTags.ESSENCE_INFUSED_METAL_EMPOWERED)}, SerializableModifier.getSerializableModifiers(new SerializableModifier(EssenceModifierRegistrate.BREWED_MODIFIER.get(), 1, EssenceBowHelper.createEffectInstanceNBT(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, 600, 5)), InfusionOperation.MERGE_TAGS)), 100)); | |
| } | |
| } | |
| public SerializableModifier[] modifiers; | |
| public InfusionRecipeModifier(ResourceLocation resourceLocation) { | |
| super(resourceLocation); | |
| } | |
| public InfusionRecipeModifier(ResourceLocation id, TierIngredient infusable, Ingredient[] inputIngredients, SerializableModifier[] modifiers, int duration) { | |
| super(id, infusable, inputIngredients, duration); | |
| this.modifiers = modifiers; | |
| } | |
| @Override | |
| public boolean isValid(ItemStack infusable, NonNullList<ItemStack> stacks) { | |
| return super.isValid(infusable, stacks); | |
| } | |
| @Override | |
| public ItemStack resolveRecipe(InfusionTableBlockEntity be, ItemStack stack) { | |
| EssenceModifierRecipeHelper.resolveRecipe(stack, modifiers); | |
| //be.getPedestals().forEach(pedestal -> pedestal.markComponentForUpdate(false)); | |
| //be.getPedestalStacks().forEach(pedestalItem -> pedestalItem.shrink(1)); | |
| return stack; | |
| } | |
| @Override | |
| public GenericSerializer<? extends SerializableRecipe> getSerializer() { | |
| return SERIALIZER; | |
| } | |
| @Override | |
| public RecipeType<?> getType() { | |
| return SERIALIZER.getRecipeType(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // See Lines: 13-22 | |
| package com.teamacronymcoders.essence.common.util.helper.recipe; | |
| import com.teamacronymcoders.essence.api.holder.ModifierInstance; | |
| import com.teamacronymcoders.essence.api.recipe.infusion.SerializableModifier; | |
| import com.teamacronymcoders.essence.common.util.helper.EssenceItemstackModifierHelpers; | |
| import net.minecraft.world.item.ItemStack; | |
| public class EssenceModifierRecipeHelper { | |
| public static ItemStack resolveRecipe(ItemStack stack, SerializableModifier[] modifiers) { | |
| for (SerializableModifier modifier : modifiers) { | |
| resolveOperationBehaviour(stack, modifier); | |
| } | |
| return stack; | |
| } | |
| public static void resolveOperationBehaviour(ItemStack stack, SerializableModifier modifier) { | |
| switch (modifier.getOperation()) { | |
| case ADD -> EssenceItemstackModifierHelpers.addModifier(stack, modifier.getModifier(), modifier.getLevel(), modifier.getModifierData()); | |
| case REMOVE -> EssenceItemstackModifierHelpers.removeModifiers(stack, modifier.getModifier()); | |
| case INCREMENT -> EssenceItemstackModifierHelpers.increaseModifierLevel(stack, new ModifierInstance(modifier::getModifier, modifier.getLevel(), modifier.getModifierData()), modifier.getLevel()); | |
| case DECREMENT -> EssenceItemstackModifierHelpers.decreaseModifierLevel(stack, new ModifierInstance(modifier::getModifier, modifier.getLevel(), modifier.getModifierData()), modifier.getLevel()); | |
| case MERGE_TAGS -> EssenceItemstackModifierHelpers.mergeModifierTags(stack, new ModifierInstance(modifier::getModifier, modifier.getLevel(), modifier.getModifierData())); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // See Lines: 155-160 | |
| package com.teamacronymcoders.essence.common.util.helper; | |
| import com.teamacronymcoders.essence.Essence; | |
| import com.teamacronymcoders.essence.api.holder.IModifierHolder; | |
| import com.teamacronymcoders.essence.api.holder.ModifierInstance; | |
| import com.teamacronymcoders.essence.api.modified.IModified; | |
| import com.teamacronymcoders.essence.api.modifier.core.Modifier; | |
| import com.teamacronymcoders.essence.api.modifier.item.ItemCoreModifier; | |
| import com.teamacronymcoders.essence.common.capability.EssenceCoreCapability; | |
| import com.teamacronymcoders.essence.common.capability.itemstack.modifier.ItemStackModifierHolder; | |
| import com.teamacronymcoders.essence.common.modifier.item.arrow.SoakedModifier; | |
| import com.teamacronymcoders.essence.common.modifier.item.cosmetic.EnchantedModifier; | |
| import com.teamacronymcoders.essence.common.modifier.item.interaction.RainbowModifier; | |
| import com.teamacronymcoders.essence.compat.registrate.EssenceModifierRegistrate; | |
| import net.minecraft.nbt.CompoundTag; | |
| import net.minecraft.nbt.ListTag; | |
| import net.minecraft.nbt.Tag; | |
| import net.minecraft.resources.ResourceLocation; | |
| import net.minecraft.world.item.ItemStack; | |
| import net.minecraftforge.common.util.LazyOptional; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.Objects; | |
| public class EssenceItemstackModifierHelpers { | |
| public static final String TAG_MODIFIERS = "ModifierInstances"; | |
| /** | |
| * @param name The ResourceLocation name of the Modifier stored in NBT. | |
| * @return Returns the Modifier matching the ResourceLocation name from the Modifier Registry. | |
| */ | |
| public static Modifier getModifierByRegistryName(String name) { | |
| return EssenceModifierRegistrate.REGISTRY.get().getValue(new ResourceLocation(name)); | |
| } | |
| public static boolean canApplyModifier(Modifier input, ItemStack stack, Modifier modifier) { | |
| if (input instanceof ItemCoreModifier) { | |
| return input.isCompatibleWith(modifier) && ((ItemCoreModifier) input).canApplyOnObject(stack); | |
| } | |
| return input.isCompatibleWith(modifier); | |
| } | |
| public static boolean canApplyModifiers(Modifier input, ItemStack stack, Modifier[] modifiers) { | |
| if (input instanceof ItemCoreModifier) { | |
| return Arrays.stream(modifiers).allMatch(input::isCompatibleWith) && Arrays.stream(modifiers).allMatch(modifier -> ((ItemCoreModifier) modifier).canApplyOnObject(stack)); | |
| } | |
| return Arrays.stream(modifiers).allMatch(input::isCompatibleWith); | |
| } | |
| /** | |
| * @param stack The ItemStack holding the Modifiers. | |
| * @param modifier The Modifier to get the level off. | |
| * @return Returns the level of the modifier on the tool. | |
| */ | |
| public static ModifierInstance getModifierInstance(ItemStack stack, Modifier modifier) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| if (holderLazyOptional.isPresent()) { | |
| holderLazyOptional.ifPresent(holder -> holder.deserializeNBT(stack.getOrCreateTag().getList(TAG_MODIFIERS, Tag.TAG_COMPOUND))); | |
| return holderLazyOptional.map(holder -> Objects.requireNonNull(holder.getModifierInstances().stream().filter(instance -> instance.getModifier() == modifier).findAny().orElse(null))).orElse(null); | |
| } | |
| return null; | |
| } | |
| public static void addModifier(ItemStack stack, Modifier modifier, int level, CompoundTag modifierData) { | |
| final LazyOptional<ItemStackModifierHolder> instances = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| if (stack.getItem() instanceof IModified && instances.isPresent()) { | |
| instances.ifPresent(holder -> { | |
| if (modifier != null && holder.addModifierInstance(true, stack, new ModifierInstance(() -> modifier, level, modifierData))) { | |
| holder.addModifierInstance(false, stack, new ModifierInstance(() -> modifier, level, modifierData)); | |
| stack.getOrCreateTag().put(TAG_MODIFIERS, holder.serializeNBT()); | |
| } | |
| }); | |
| } | |
| } | |
| /** | |
| * This adds the specified Modifiers to the Tool. | |
| * | |
| * @param stack The ItemStack holding the Modifiers. | |
| * @param modifiers The Modifier to remove. | |
| */ | |
| public static void addModifiers(ItemStack stack, Modifier... modifiers) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| if (stack.getItem() instanceof IModified && holderLazyOptional.isPresent()) { | |
| holderLazyOptional.ifPresent(holder -> { | |
| List<ModifierInstance> instances = new ArrayList<>(holder.getModifierInstances()); | |
| boolean compatible = true; | |
| for (ModifierInstance instance : instances) { | |
| boolean incompatible = false; | |
| for (Modifier modifier : modifiers) { | |
| if (!instance.getModifier().isCompatibleWith(modifier)) { | |
| incompatible = true; | |
| break; | |
| } | |
| } | |
| if (incompatible) { | |
| compatible = false; | |
| break; | |
| } | |
| } | |
| if (compatible) { | |
| for (Modifier modifier : modifiers) { | |
| if (instances.contains(new ModifierInstance(() -> modifier, 1, null))) { | |
| addModifier(stack, modifier, 1, null); | |
| } | |
| } | |
| } | |
| }); | |
| } | |
| } | |
| public static void removeModifiers(ItemStack stack, Modifier... modifiersToRemove) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| holderLazyOptional.ifPresent(holder -> { | |
| Modifier foundModifier = null; | |
| for (ModifierInstance instance : holder.getModifierInstances()) { | |
| for (Modifier modifier : modifiersToRemove) { | |
| if (instance.getModifier() == modifier) { | |
| foundModifier = instance.getModifier(); | |
| break; | |
| } | |
| } | |
| } | |
| if (foundModifier != null) { | |
| if (holder.removeModifierInstance(true, stack, foundModifier)) { | |
| holder.removeModifierInstance(false, stack, foundModifier); | |
| stack.getOrCreateTag().put(TAG_MODIFIERS, holder.serializeNBT()); | |
| } | |
| } | |
| }); | |
| } | |
| public static void setModifierLevel(ItemStack stack, ModifierInstance replacement) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| holderLazyOptional.ifPresent(holder -> { | |
| for (ModifierInstance instance : holder.getModifierInstances()) { | |
| if (instance.getModifier() == replacement.getModifier() && instance.getModifierData() == replacement.getModifierData()) { | |
| instance.setLevel(replacement.getLevel()); | |
| break; | |
| } | |
| } | |
| stack.getOrCreateTag().put(TAG_MODIFIERS, holder.serializeNBT()); | |
| }); | |
| } | |
| public static void increaseModifierLevel(ItemStack stack, ModifierInstance checkInstance) { | |
| increaseModifierLevel(stack, checkInstance, 1); | |
| } | |
| public static void increaseModifierLevel(ItemStack stack, ModifierInstance checkInstance, int increase) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| holderLazyOptional.ifPresent(holder -> { | |
| if (holder.levelUpModifier(true, increase, stack, checkInstance)) { | |
| holder.levelUpModifier(false, increase, stack, checkInstance); | |
| stack.getOrCreateTag().put(TAG_MODIFIERS, holder.serializeNBT()); | |
| } | |
| }); | |
| } | |
| public static void decreaseModifierLevel(ItemStack stack, ModifierInstance modifier) { | |
| decreaseModifierLevel(stack, modifier, 1); | |
| } | |
| public static void decreaseModifierLevel(ItemStack stack, ModifierInstance checkInstance, int decrease) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| holderLazyOptional.ifPresent(holder -> { | |
| if (holder.levelDownModifier(true, decrease, stack, checkInstance)) { | |
| holder.levelDownModifier(false, decrease, stack, checkInstance); | |
| stack.getOrCreateTag().put(TAG_MODIFIERS, holder.serializeNBT()); | |
| } | |
| }); | |
| } | |
| public static void mergeModifierTags(ItemStack stack, ModifierInstance mergeInstance) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| holderLazyOptional.ifPresent(holder -> { | |
| for (ModifierInstance modifierInstance : holder.getModifierInstances()) { | |
| if (modifierInstance.getModifier() == mergeInstance.getModifier()) { | |
| CompoundTag tagOnItem = modifierInstance.getModifierData(); | |
| modifierInstance.getModifier().mergeTags(tagOnItem, mergeInstance.getModifierData()); | |
| modifierInstance.setModifierData(tagOnItem); | |
| break; | |
| } | |
| } | |
| stack.getOrCreateTag().put(TAG_MODIFIERS, holder.serializeNBT()); | |
| }); | |
| } | |
| /** | |
| * @param stack The ItemStack holding the Modifiers. | |
| * @return Returns an random modifier from the modifiers on the ItemStack | |
| */ | |
| public static ModifierInstance getRandomModifierInstance(ItemStack stack) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| return holderLazyOptional.map(holder -> holder.getModifierInstances().stream().skip(Essence.RANDOM.nextInt(holder.getModifierInstances().size())).findFirst()).orElse(null).orElse(null); | |
| } | |
| /** | |
| * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU'RE DOING! | |
| * I'm looking at you Future Simon >_> | |
| * | |
| * @param stack The ItemStack to be cleared of Modifiers | |
| */ | |
| public static void clearModifiers(ItemStack stack) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| holderLazyOptional.ifPresent(IModifierHolder::clearModifiers); | |
| } | |
| public static boolean hasRainbowModifier(ItemStack stack) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| return holderLazyOptional.map(holder -> holder.getModifierInstances().stream().anyMatch(instance -> instance.getModifier() instanceof RainbowModifier)).orElse(false); | |
| } | |
| public static boolean hasEnchantedModifier(ItemStack stack) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| return holderLazyOptional.map(holder -> holder.getModifierInstances().stream().anyMatch(instance -> instance.getModifier() instanceof EnchantedModifier)).orElse(false); | |
| } | |
| public static boolean hasSoakedModifier(ItemStack stack) { | |
| final LazyOptional<ItemStackModifierHolder> holderLazyOptional = stack.getCapability(EssenceCoreCapability.ITEMSTACK_MODIFIER_HOLDER); | |
| return holderLazyOptional.map(holder -> holder.getModifierInstances().stream().anyMatch(instance -> instance.getModifier() instanceof SoakedModifier)).orElse(false); | |
| } | |
| /** | |
| * @param instances Modifier Instances | |
| * @return Returns CompoundNBT for Serialization/Deserialization | |
| * Do note that using this method DOES NOT add or remove from the free modifier amount. | |
| */ | |
| public static CompoundTag getStackNBTForFillGroup(ModifierInstance... instances) { | |
| final CompoundTag nbt = new CompoundTag(); | |
| final ListTag listNBT = new ListTag(); | |
| for (ModifierInstance instance : instances) { | |
| listNBT.add(instance.serializeNBT()); | |
| } | |
| nbt.put(TAG_MODIFIERS, listNBT); | |
| return nbt; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // See Lines: 79-163 | |
| package com.teamacronymcoders.essence.common.capability.itemstack.modifier; | |
| import com.google.common.collect.Sets; | |
| import com.teamacronymcoders.essence.api.holder.ModifierHolder; | |
| import com.teamacronymcoders.essence.api.holder.ModifierInstance; | |
| import com.teamacronymcoders.essence.api.modified.IModified; | |
| import com.teamacronymcoders.essence.api.modifier.core.Modifier; | |
| import com.teamacronymcoders.essence.common.util.helper.EssenceItemstackModifierHelpers; | |
| import net.minecraft.nbt.ListTag; | |
| import net.minecraft.nbt.Tag; | |
| import net.minecraft.world.item.ItemStack; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Set; | |
| public class ItemStackModifierHolder extends ModifierHolder<ItemStack> { | |
| private ItemStack stack; | |
| public ItemStackModifierHolder() { | |
| super(); | |
| } | |
| public ItemStackModifierHolder(ItemStack stack) { | |
| super(); | |
| this.stack = stack; | |
| } | |
| @Override | |
| public boolean addModifierInstance(boolean simulate, ItemStack object, ModifierInstance... instances) { | |
| if (simulate && object.getItem() instanceof IModified modified) { | |
| List<ModifierInstance> sim = new ArrayList<>(getModifierInstances()); | |
| for (ModifierInstance instance : instances) { | |
| if (!sim.contains(instance)) { | |
| sim.add(instance); | |
| } | |
| } | |
| return modified.recheck(sim); | |
| } | |
| if (object.getItem() instanceof IModified modified) { | |
| for (ModifierInstance instance : instances) { | |
| if (!getModifierInstances().contains(instance)) { | |
| getModifierInstances().add(instance); | |
| modified.decreaseFreeModifiers(instance.getModifier().getModifierCountValue(instance.getLevel())); | |
| } | |
| } | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public boolean removeModifierInstance(boolean simulate, ItemStack object, Modifier... modifiers) { | |
| Set<Modifier> mods = Sets.newHashSet(modifiers); | |
| if (simulate && object.getItem() instanceof IModified modified) { | |
| List<ModifierInstance> sim = new ArrayList<>(getModifierInstances()); | |
| sim.removeIf(instance -> mods.contains(instance.getModifier())); | |
| return modified.recheck(sim); | |
| } | |
| if (object.getItem() instanceof IModified modified) { | |
| if (modifiers.length > 0) { | |
| List<ModifierInstance> instances = this.getModifierInstances(); | |
| int[] value = new int[]{0}; | |
| instances.removeIf(instance -> { | |
| if (!mods.contains(instance.getModifier())) return false; | |
| value[0] += instance.getModifier().getModifierCountValue(instance.getLevel()); | |
| return true; | |
| }); | |
| modified.addModifierWithoutIncreasingAdditional(value[0]); | |
| return true; | |
| } | |
| return false; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public boolean levelUpModifier(boolean simulate, int increase, ItemStack object, Modifier... modifiers) { | |
| if (simulate && object.getItem() instanceof IModified modified) { | |
| List<ModifierInstance> sim = new ArrayList<>(getModifierInstances()); | |
| for (ModifierInstance instance : sim) { | |
| boolean matched = false; | |
| for (Modifier modifier : modifiers) { | |
| if (instance.getModifier() == modifier) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| Modifier stackCoreModifier = instance.getModifier(); | |
| instance.setLevel(Math.min(instance.getLevel() + increase, stackCoreModifier.getMaxLevel())); | |
| } | |
| } | |
| return modified.recheck(sim); | |
| } | |
| if (object.getItem() instanceof IModified modified) { | |
| for (ModifierInstance instance : getModifierInstances()) { | |
| boolean matched = false; | |
| for (Modifier modifier : modifiers) { | |
| if (instance.getModifier() == modifier) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| Modifier stackCoreModifier = instance.getModifier(); | |
| int x = stackCoreModifier.getModifierCountValue(instance.getLevel()); | |
| int y = stackCoreModifier.getModifierCountValue(Math.min(instance.getLevel() + increase, stackCoreModifier.getMaxLevel())); | |
| instance.setLevel(Math.min(instance.getLevel() + increase, stackCoreModifier.getMaxLevel())); | |
| if (x < y) { | |
| modified.decreaseFreeModifiers(y - x); | |
| } | |
| } | |
| } | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public boolean levelUpModifier(boolean simulate, int increase, ItemStack object, ModifierInstance... modifiersWithData) { | |
| if (simulate && object.getItem() instanceof IModified modified) { | |
| List<ModifierInstance> sim = new ArrayList<>(getModifierInstances()); | |
| for (ModifierInstance instance : sim) { | |
| boolean matched = false; | |
| for (ModifierInstance modifier : modifiersWithData) { | |
| if (instance.getModifier().equals(modifier.getModifier()) && instance.getModifierData().equals(modifier.getModifierData())) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| Modifier stackCoreModifier = instance.getModifier(); | |
| instance.setLevel(Math.min(instance.getLevel() + increase, stackCoreModifier.getMaxLevel())); | |
| } | |
| } | |
| return modified.recheck(sim); | |
| } | |
| if (object.getItem() instanceof IModified modified) { | |
| for (ModifierInstance instance : getModifierInstances()) { | |
| boolean matched = false; | |
| for (ModifierInstance modifier : modifiersWithData) { | |
| if (instance.getModifier().equals(modifier.getModifier()) && instance.getModifierData().equals(modifier.getModifierData())) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| Modifier stackCoreModifier = instance.getModifier(); | |
| int x = stackCoreModifier.getModifierCountValue(instance.getLevel()); | |
| int y = stackCoreModifier.getModifierCountValue(Math.min(instance.getLevel() + increase, stackCoreModifier.getMaxLevel())); | |
| instance.setLevel(Math.min(instance.getLevel() + increase, stackCoreModifier.getMaxLevel())); | |
| if (x < y) { | |
| modified.decreaseFreeModifiers(y - x); | |
| } | |
| } | |
| } | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public boolean levelDownModifier(boolean simulate, int decrease, ItemStack object, Modifier... modifiers) { | |
| if (simulate && object.getItem() instanceof IModified modified) { | |
| List<ModifierInstance> sim = new ArrayList<>(getModifierInstances()); | |
| for (ModifierInstance instance : sim) { | |
| boolean matched = false; | |
| for (Modifier modifier : modifiers) { | |
| if (instance.getModifier() == modifier) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| int level = instance.getLevel(); | |
| if (level - decrease < instance.getModifier().getMinLevel()) { | |
| sim.remove(instance); | |
| continue; | |
| } | |
| instance.setLevel(instance.getLevel() - decrease); | |
| } | |
| } | |
| return modified.recheck(sim); | |
| } | |
| if (object.getItem() instanceof IModified modified) { | |
| for (ModifierInstance instance : getModifierInstances()) { | |
| boolean matched = false; | |
| for (Modifier modifier : modifiers) { | |
| if (modifier == instance.getModifier()) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| int level = instance.getLevel(); | |
| if (level - decrease < instance.getModifier().getMinLevel()) { | |
| removeModifierInstance(false, object, instance.getModifier()); | |
| continue; | |
| } | |
| int x = instance.getModifier().getModifierCountValue(instance.getLevel()) - instance.getModifier().getModifierCountValue(instance.getLevel() - decrease); | |
| instance.setLevel(instance.getLevel() - decrease); | |
| if (x > 0) { | |
| modified.addModifierWithoutIncreasingAdditional(x); | |
| } | |
| } | |
| } | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public boolean levelDownModifier(boolean simulate, int decrease, ItemStack object, ModifierInstance... modifiersWithData) { | |
| boolean result = false; | |
| if (simulate && object.getItem() instanceof IModified modified) { | |
| List<ModifierInstance> sim = new ArrayList<>(getModifierInstances()); | |
| for (ModifierInstance instance : sim) { | |
| boolean matched = false; | |
| for (ModifierInstance modifier : modifiersWithData) { | |
| if (instance.getModifier() == modifier.getModifier() && instance.getModifierData() == modifier.getModifierData()) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| int level = instance.getLevel(); | |
| if (level - decrease < instance.getModifier().getMinLevel()) { | |
| sim.remove(instance); | |
| continue; | |
| } | |
| instance.setLevel(instance.getLevel() - decrease); | |
| } | |
| } | |
| result = modified.recheck(sim); | |
| } else if (object.getItem() instanceof IModified modified) { | |
| for (ModifierInstance instance : getModifierInstances()) { | |
| boolean matched = false; | |
| for (ModifierInstance modifier : modifiersWithData) { | |
| if (instance.getModifier() == modifier.getModifier() && instance.getModifierData() == modifier.getModifierData()) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| int level = instance.getLevel(); | |
| if (level - decrease < instance.getModifier().getMinLevel()) { | |
| removeModifierInstance(false, object, instance.getModifier()); | |
| continue; | |
| } | |
| int x = instance.getModifier().getModifierCountValue(instance.getLevel()) - instance.getModifier().getModifierCountValue(instance.getLevel() - decrease); | |
| instance.setLevel(instance.getLevel() - decrease); | |
| if (x > 0) { | |
| modified.addModifierWithoutIncreasingAdditional(x); | |
| } | |
| } | |
| } | |
| result = true; | |
| } | |
| return result; | |
| } | |
| @Override | |
| public boolean levelSetModifier(boolean simulate, int level, ItemStack object, Modifier... modifiers) { | |
| if (simulate && object.getItem() instanceof IModified modified) { | |
| List<ModifierInstance> sim = new ArrayList<>(getModifierInstances()); | |
| for (ModifierInstance instance : sim) { | |
| for (Modifier modifier : modifiers) { | |
| if (instance.getModifier() == modifier) { | |
| instance.setLevel(level); | |
| break; | |
| } | |
| } | |
| } | |
| return modified.recheck(sim); | |
| } | |
| if (object.getItem() instanceof IModified modified) { | |
| for (ModifierInstance instance : getModifierInstances()) { | |
| boolean matched = false; | |
| for (Modifier modifier : modifiers) { | |
| if (instance.getModifier() == modifier) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| int x = instance.getModifier().getModifierCountValue(instance.getLevel()) - instance.getModifier().getModifierCountValue(level); | |
| instance.setLevel(level); | |
| if (x > 0) { | |
| modified.addModifierWithoutIncreasingAdditional(x); | |
| } | |
| if (x < 0) { | |
| modified.decreaseFreeModifiers(x); | |
| } | |
| } | |
| } | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public boolean levelSetModifier(boolean simulate, int level, ItemStack object, ModifierInstance... modifiersWithData) { | |
| if (simulate && object.getItem() instanceof IModified modified) { | |
| List<ModifierInstance> sim = new ArrayList<>(getModifierInstances()); | |
| for (ModifierInstance instance : sim) { | |
| for (ModifierInstance modifier : modifiersWithData) { | |
| if (instance.getModifier() == modifier.getModifier() && instance.getModifierData() == modifier.getModifierData()) { | |
| instance.setLevel(level); | |
| break; | |
| } | |
| } | |
| } | |
| return modified.recheck(sim); | |
| } | |
| if (object.getItem() instanceof IModified modified) { | |
| for (ModifierInstance instance : getModifierInstances()) { | |
| boolean matched = false; | |
| for (ModifierInstance modifier : modifiersWithData) { | |
| if (instance.getModifier() == modifier.getModifier() && instance.getModifierData() == modifier.getModifierData()) { | |
| matched = true; | |
| break; | |
| } | |
| } | |
| if (matched) { | |
| int x = instance.getModifier().getModifierCountValue(instance.getLevel()) - instance.getModifier().getModifierCountValue(level); | |
| instance.setLevel(level); | |
| if (x > 0) { | |
| modified.addModifierWithoutIncreasingAdditional(x); | |
| } | |
| if (x < 0) { | |
| modified.decreaseFreeModifiers(x); | |
| } | |
| } | |
| } | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public ListTag serializeNBT() { | |
| final ListTag listNBT = new ListTag(); | |
| for (ModifierInstance instance : getModifierInstances()) { | |
| listNBT.add(instance.serializeNBT()); | |
| } | |
| stack.getOrCreateTag().put(EssenceItemstackModifierHelpers.TAG_MODIFIERS, listNBT); | |
| return listNBT; | |
| } | |
| @Override | |
| public void deserializeNBT(ListTag nbt) { | |
| super.deserializeNBT(nbt.size() > 0 ? nbt : stack.getOrCreateTag().getList(EssenceItemstackModifierHelpers.TAG_MODIFIERS, Tag.TAG_COMPOUND)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // See Lines: 68-70 & 64-66 | |
| package com.teamacronymcoders.essence.api.modifier.item; | |
| import com.google.common.collect.HashMultimap; | |
| import com.google.common.collect.Multimap; | |
| import com.teamacronymcoders.essence.api.holder.ModifierInstance; | |
| import com.teamacronymcoders.essence.api.modifier.core.Modifier; | |
| import net.minecraft.world.entity.LivingEntity; | |
| import net.minecraft.world.entity.ai.attributes.Attribute; | |
| import net.minecraft.world.entity.ai.attributes.AttributeModifier; | |
| import net.minecraft.world.item.ItemStack; | |
| public abstract class ItemCoreModifier extends Modifier { | |
| private static final Multimap<Attribute, AttributeModifier> EMPTY_ATTRIBUTE_MAP = HashMultimap.create(); | |
| public ItemCoreModifier() { | |
| super(); | |
| } | |
| public ItemCoreModifier(int maxLevel) { | |
| super(maxLevel); | |
| } | |
| public ItemCoreModifier(int minLevel, int maxLevel) { | |
| super(minLevel, maxLevel); | |
| } | |
| public Multimap<Attribute, AttributeModifier> getAttributeModifiers(ItemStack stack, LivingEntity wielder, ModifierInstance instance) { | |
| return EMPTY_ATTRIBUTE_MAP; | |
| } | |
| public int getModifiedDurability(ItemStack stack, ModifierInstance instance, int base) { | |
| return 0; | |
| } | |
| public float getModifiedEfficiency(ItemStack stack, ModifierInstance instance, float base) { | |
| return 0; | |
| } | |
| public int getModifiedHarvestLevel(ItemStack stack, ModifierInstance instance, int base) { | |
| return 0; | |
| } | |
| @Override | |
| public boolean canApplyOnObject() { | |
| return false; | |
| } | |
| public boolean canApplyOnObject(ItemStack stack) { | |
| return canApplyOnObject(); | |
| } | |
| @Override | |
| public boolean countsTowardsLimit(int level) { | |
| return true; | |
| } | |
| public boolean countsTowardsLimit(int level, ItemStack stack) { | |
| return countsTowardsLimit(level); | |
| } | |
| @Override | |
| public int getModifierCountValue(int level) { | |
| return 1; | |
| } | |
| public int getModifierCountValue(int level, ItemStack stack) { | |
| return getModifierCountValue(level); | |
| } | |
| public int getMinLevel(ItemStack stack) { | |
| return getMinLevel(); | |
| } | |
| public int getMaxLevel(ItemStack stack) { | |
| return getMaxLevel(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // See Lines: 63-65 | |
| package com.teamacronymcoders.essence.api.holder; | |
| import com.teamacronymcoders.essence.api.modifier.core.Modifier; | |
| import com.teamacronymcoders.essence.common.util.helper.EssenceItemstackModifierHelpers; | |
| import net.minecraft.nbt.CompoundTag; | |
| import net.minecraftforge.common.util.INBTSerializable; | |
| import java.util.Objects; | |
| import java.util.function.Supplier; | |
| public class ModifierInstance implements INBTSerializable<CompoundTag> { | |
| public static final String TAG_MODIFIER = "Modifier"; | |
| public static final String TAG_INFO = "ModifierInfo"; | |
| public static final String TAG_LEVEL = "ModifierLevel"; | |
| public static final String TAG_COMPOUND = "ModifierCompound"; | |
| private Supplier<Modifier> modifier; | |
| private int level; | |
| private CompoundTag modifierData; | |
| public ModifierInstance() { | |
| } | |
| public ModifierInstance(Supplier<Modifier> modifier, int level, CompoundTag modifierData) { | |
| this.modifier = modifier; | |
| this.level = level; | |
| this.modifierData = modifierData; | |
| } | |
| @Override | |
| public CompoundTag serializeNBT() { | |
| final CompoundTag compoundNBT = new CompoundTag(); | |
| compoundNBT.putString(TAG_MODIFIER, modifier.get().getRegistryName().toString()); | |
| final CompoundTag info = new CompoundTag(); | |
| info.putInt(TAG_LEVEL, level); | |
| info.put(TAG_COMPOUND, Objects.requireNonNullElseGet(modifierData, CompoundTag::new)); | |
| compoundNBT.put(TAG_INFO, info); | |
| return compoundNBT; | |
| } | |
| @Override | |
| public void deserializeNBT(CompoundTag nbt) { | |
| final Modifier modifier = EssenceItemstackModifierHelpers.getModifierByRegistryName(nbt.getString(TAG_MODIFIER)); | |
| final CompoundTag info = nbt.getCompound(TAG_INFO); | |
| final int level = info.getInt(TAG_LEVEL); | |
| final CompoundTag modifierData = info.getCompound(TAG_COMPOUND); | |
| this.modifier = () -> modifier; | |
| this.level = level; | |
| this.modifierData = modifierData; | |
| this.modifier.get().update(this.modifierData); | |
| } | |
| public Modifier getModifier() { | |
| return modifier.get(); | |
| } | |
| public int getLevel() { | |
| return level; | |
| } | |
| public void setLevel(int level) { | |
| this.level = level; | |
| } | |
| public CompoundTag getModifierData() { | |
| return modifierData; | |
| } | |
| public void setModifierData(CompoundTag modifierData) { | |
| this.modifierData = modifierData; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment