Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Bulk Digestion in Gastric Acid #150

Merged
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.github.elenterius.biomancy.init;

import com.github.elenterius.biomancy.block.digester.DigesterBlockEntity;
import com.github.elenterius.biomancy.crafting.recipe.DigestingRecipe;
import com.github.elenterius.biomancy.init.tags.ModBlockTags;
import com.github.elenterius.biomancy.inventory.BehavioralInventory;
import com.github.elenterius.biomancy.util.CombatUtil;
import net.minecraft.core.Direction;
import net.minecraft.core.cauldron.CauldronInteraction;
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ItemUtils;
Expand All @@ -29,6 +36,7 @@

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public final class AcidInteractions {

Expand Down Expand Up @@ -157,4 +165,55 @@ else if (livingEntity.tickCount % 10 == 0 && livingEntity.getRandom().nextFloat(
}
}

private static final String TIMER_KEY = "biomancy:digestion_timer";
private static final float EFFICIENCY = 0.8f;
public static void tryDigest(ItemEntity itemEntity) {
kd8lvt marked this conversation as resolved.
Show resolved Hide resolved
if (!digestible(itemEntity)) return;
CompoundTag data = itemEntity.getPersistentData();
if (data.contains(TIMER_KEY) && data.getInt(TIMER_KEY) < 100) {
data.putInt(TIMER_KEY,data.getInt(TIMER_KEY)+1);
//TODO: Bubble particles? Couldn't get it working for some reason.
// Copied the addParticle call from handleEntityInsideAcid and it said no.
Elenterius marked this conversation as resolved.
Show resolved Hide resolved
} else if (data.contains(TIMER_KEY) && data.getInt(TIMER_KEY) >= 100){
digestIntoNutrientPasteStacks(itemEntity);
itemEntity.getPersistentData().remove(TIMER_KEY);
} else {
data.putInt(TIMER_KEY,1);
}
}

private static void digestIntoNutrientPasteStacks(ItemEntity itemEntity) {
if (getDigestionRecipe(itemEntity).isEmpty()) return;
kd8lvt marked this conversation as resolved.
Show resolved Hide resolved
DigestingRecipe recipe = getDigestionRecipe(itemEntity).get();
BehavioralInventory<?> inv = BehavioralInventory.createServerContents(1,player->false,()->{});
Elenterius marked this conversation as resolved.
Show resolved Hide resolved
inv.insertItemStack(itemEntity.getItem());
ItemStack resultStack = recipe.assemble(inv,itemEntity.level().registryAccess());
int amt = (int)Math.floor(resultStack.getCount()*itemEntity.getItem().getCount()*EFFICIENCY);
kd8lvt marked this conversation as resolved.
Show resolved Hide resolved
if (amt > 64) amt = splitIntoStacks(itemEntity,resultStack);
itemEntity.setItem(new ItemStack(ModItems.NUTRIENT_PASTE.get(), amt));
itemEntity.playSound(SoundEvents.PLAYER_BURP);
}

private static int splitIntoStacks(ItemEntity itemEntity, ItemStack resultStack) {
int numItems = (int)Math.floor(resultStack.getCount()*itemEntity.getItem().getCount()*EFFICIENCY);
Level level = itemEntity.level();
while (numItems > 64) {
DefaultDispenseItemBehavior.spawnItem(level,new ItemStack(ModItems.NUTRIENT_PASTE.get(),64),1, Direction.UP, itemEntity.position());
numItems -= 64;
}
return numItems;
}

@SuppressWarnings("RedundantIfStatement")
private static boolean digestible(ItemEntity itemEntity) {
if (!itemEntity.isInFluidType(ModFluids.ACID_TYPE.get()) && !itemEntity.level().getBlockState(itemEntity.blockPosition()).is(ModBlocks.ACID_CAULDRON.get())) return false;
if (itemEntity.getItem().is(ModItems.NUTRIENT_PASTE.get())) return false;
kd8lvt marked this conversation as resolved.
Show resolved Hide resolved
if (getDigestionRecipe(itemEntity).isEmpty()) return false;
return true;
}

private static Optional<DigestingRecipe> getDigestionRecipe(ItemEntity itemEntity) {
return DigesterBlockEntity.RECIPE_TYPE.get().getRecipeForIngredient(itemEntity.level(), itemEntity.getItem());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.elenterius.biomancy.mixin;

import com.github.elenterius.biomancy.init.AcidInteractions;
import net.minecraft.world.entity.item.ItemEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemEntity.class)
public class ItemEntityMixin {
kd8lvt marked this conversation as resolved.
Show resolved Hide resolved
@Unique
@Inject(at=@At("TAIL"),method={"tick()V"})
public void biomancy$tick(CallbackInfo ci) {
kd8lvt marked this conversation as resolved.
Show resolved Hide resolved
ItemEntity self = (ItemEntity)((Object)this); //I hate casting like this on so many levels
if (self.level().isClientSide()) return;
AcidInteractions.tryDigest(self);
kd8lvt marked this conversation as resolved.
Show resolved Hide resolved
}
}
53 changes: 45 additions & 8 deletions src/main/resources/mixins.biomancy.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,53 @@
"defaultRequire": 1
},
"mixins": [
"accessor.AgeableMobAccessor", "accessor.ArmorStandAccessor", "accessor.BiomeAccessor", "accessor.CreeperAccessor", "accessor.DamageSourceAccessor",
"accessor.EntityAccessor", "accessor.IntegerPropertyAccessor", "accessor.LivingEntityAccessor", "accessor.MobEffectInstanceAccessor",
"accessor.MobEntityAccessor", "accessor.ServerLevelAccessor", "accessor.SheepAccessor", "accessor.SlimeAccessor", "accessor.SuspiciousStewItemAccessor",
"accessor.SwordItemMixinAccessor", "accessor.TadpoleAccessor", "accessor.TextureSlotAccessor", "accessor.ZombieVillagerMixinAccessor",
"AnimalMixin", "AreaEffectCloudMixin", "ArrowMixin", "ChickenMixin", "CowMixin", "FlowingFluidMixin", "LivingEntityMixin", "MobEffectInstanceMixin",
"PhantomMixin", "PigMixin", "PlayerMixin", "ServerLevelMixin", "SheepMixin", "ThrownPotionMixin", "UnbreakingEnchantmentMixin", "WallBlockMixin",
"BlockMixin", "NodeEvaluatorMixin", "SignBlockEntityMixin", "ServerGamePacketListenerImplMixin", "VillagerMixin", "VillagerMakeLoveMixin"
"AnimalMixin",
"AreaEffectCloudMixin",
"ArrowMixin",
"BlockMixin",
"ChickenMixin",
"CowMixin",
"FlowingFluidMixin",
"ItemEntityMixin",
"LivingEntityMixin",
"MobEffectInstanceMixin",
"NodeEvaluatorMixin",
"PhantomMixin",
"PigMixin",
"PlayerMixin",
"ServerGamePacketListenerImplMixin",
"ServerLevelMixin",
"SheepMixin",
"SignBlockEntityMixin",
"ThrownPotionMixin",
"UnbreakingEnchantmentMixin",
"VillagerMakeLoveMixin",
"VillagerMixin",
"WallBlockMixin",
"accessor.AgeableMobAccessor",
"accessor.ArmorStandAccessor",
"accessor.BiomeAccessor",
"accessor.CreeperAccessor",
"accessor.DamageSourceAccessor",
"accessor.EntityAccessor",
"accessor.IntegerPropertyAccessor",
"accessor.LivingEntityAccessor",
"accessor.MobEffectInstanceAccessor",
"accessor.MobEntityAccessor",
"accessor.ServerLevelAccessor",
"accessor.SheepAccessor",
"accessor.SlimeAccessor",
"accessor.SuspiciousStewItemAccessor",
"accessor.SwordItemMixinAccessor",
"accessor.TadpoleAccessor",
"accessor.TextureSlotAccessor",
"accessor.ZombieVillagerMixinAccessor"
],
"client": [
"accessor.RecipeCollectionAccessor", "client.ClientPackListenerMixin", "client.ClientRecipeBookMixin", "client.GuiGraphicsMixin",
"accessor.RecipeCollectionAccessor",
"client.ClientPackListenerMixin",
"client.ClientRecipeBookMixin",
"client.GuiGraphicsMixin",
"client.PlayerRendererMixin"
],
"server": [ ]
Expand Down