Skip to content

Commit

Permalink
Add soul recipe framework & handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ACGaming committed Jun 9, 2024
1 parent 26aabc8 commit b4dc940
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/main/java/mod/emt/harkenscythe/HarkenScythe.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class HarkenScythe
public void init(FMLInitializationEvent event)
{
HSRegistry.registerTileEntities();
HSRegistry.registerRecipes();
LOGGER.info(NAME + " initialized");
}
}
38 changes: 32 additions & 6 deletions src/main/java/mod/emt/harkenscythe/blocks/HSSoulAltar.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package mod.emt.harkenscythe.blocks;

import mod.emt.harkenscythe.init.HSItems;
import mod.emt.harkenscythe.init.HSSoulAltarRecipes;
import mod.emt.harkenscythe.init.HSSoundEvents;
import mod.emt.harkenscythe.tileentities.HSSoulAltarTE;
import net.minecraft.block.BlockEnchantmentTable;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
Expand Down Expand Up @@ -53,12 +59,32 @@ public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, En
HSSoulAltarTE altar = (HSSoulAltarTE) te;
ItemStack heldItem = player.getHeldItem(hand);

if (!heldItem.isEmpty())
if (heldItem.getItem() == HSItems.harken_athame)
{
ItemStack altarItem = altar.getItem();
if (altar.isValidRecipe())
{
Item item = altar.getItemStack().getItem();
int requiredSouls = HSSoulAltarRecipes.getRequiredSouls(altar.getItemStack().getItem());
altar.decreaseCrucibleLevel(requiredSouls / 10);
altar.getItemStack().shrink(1);
if (!player.world.isRemote) player.world.spawnEntity(new EntityItem(player.world, altar.getPos().getX() + 0.5D, altar.getPos().getY() + 1.5D, altar.getPos().getZ() + 0.5D, new ItemStack(HSSoulAltarRecipes.getOutput(item))));
player.world.playSound(altar.getPos().getX(), altar.getPos().getY(), altar.getPos().getZ(), HSSoundEvents.BLOCK_SOUL_ALTAR_ENCHANT, SoundCategory.BLOCKS, 0.8F, 1.5F / (altar.getWorld().rand.nextFloat() * 0.4F + 1.2F), false);
player.world.playSound(altar.getPos().getX(), altar.getPos().getY(), altar.getPos().getZ(), SoundEvents.ENTITY_ENDEREYE_DEATH, SoundCategory.BLOCKS, 1.0F, 1.5F / (altar.getWorld().rand.nextFloat() * 0.4F + 1.2F), false);
for (int i = 0; i < requiredSouls; i++)
{
player.world.spawnParticle(EnumParticleTypes.SPELL_WITCH, altar.getPos().getX() + 0.5D, altar.getPos().getY() + 2.0D, altar.getPos().getZ() + 0.5D, 0.0D, 0.0D, 0.0D);
}
altar.updateSoulCount();
altar.updateRecipe();
return true;
}
}
else if (!heldItem.isEmpty())
{
ItemStack altarItem = altar.getItemStack();
if (altarItem.isEmpty())
{
altar.setItem(heldItem.splitStack(1));
altar.setItemStack(heldItem.splitStack(1));
player.world.playSound(altar.getPos().getX(), altar.getPos().getY(), altar.getPos().getZ(), SoundEvents.BLOCK_END_PORTAL_FRAME_FILL, SoundCategory.BLOCKS, 1.0F, 1.5F / (altar.getWorld().rand.nextFloat() * 0.4F + 1.2F), false);
return true;
}
Expand All @@ -72,16 +98,16 @@ else if (altarItem.getMaxStackSize() > altarItem.getCount() && ItemStack.areItem
}
else
{
ItemStack itemStack = altar.getItem();
ItemStack itemStack = altar.getItemStack();
if (!itemStack.isEmpty())
{
player.addItemStackToInventory(itemStack);
player.world.playSound(altar.getPos().getX(), altar.getPos().getY(), altar.getPos().getZ(), SoundEvents.ENTITY_ENDEREYE_DEATH, SoundCategory.BLOCKS, 1.0F, 1.5F / (altar.getWorld().rand.nextFloat() * 0.4F + 1.2F), false);
altar.setItem(ItemStack.EMPTY);
altar.setItemStack(ItemStack.EMPTY);
return true;
}
}
}
return true;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void render(HSSoulAltarTE te, double x, double y, double z, float partial
RenderHelper.enableStandardItemLighting();
GlStateManager.pushAttrib();

ItemStack stack = te.getItem();
ItemStack stack = te.getItemStack();
if (!stack.isEmpty())
{
GlStateManager.pushMatrix();
Expand All @@ -57,33 +57,6 @@ public void render(HSSoulAltarTE te, double x, double y, double z, float partial
GlStateManager.popAttrib();
GlStateManager.popMatrix();

if (!stack.isEmpty())
{
// Display soul count
String soulsText = "Souls: " + te.soulCount;
GlStateManager.pushMatrix();
GlStateManager.translate(x + 0.5, y + 1.5, z + 0.5);
GlStateManager.rotate(-Minecraft.getMinecraft().getRenderManager().playerViewY, 0, 1, 0);
GlStateManager.rotate(Minecraft.getMinecraft().getRenderManager().playerViewX, 1, 0, 0);
GlStateManager.scale(-0.025f, -0.025f, 0.025f);
GlStateManager.disableLighting();
Minecraft.getMinecraft().fontRenderer.drawString(soulsText, -Minecraft.getMinecraft().fontRenderer.getStringWidth(soulsText) / 2, 0, 0xFFFFFF);
GlStateManager.enableLighting();
GlStateManager.popMatrix();

// Display input stack count
String stackText = String.valueOf(stack.getCount());
GlStateManager.pushMatrix();
GlStateManager.translate(x + 0.5, y + 2.7, z + 0.5);
GlStateManager.rotate(-Minecraft.getMinecraft().getRenderManager().playerViewY, 0, 1, 0);
GlStateManager.rotate(Minecraft.getMinecraft().getRenderManager().playerViewX, 1, 0, 0);
GlStateManager.scale(-0.025f, -0.025f, 0.025f);
GlStateManager.disableLighting();
Minecraft.getMinecraft().fontRenderer.drawString(stackText, -Minecraft.getMinecraft().fontRenderer.getStringWidth(stackText) / 2, 0, 0xFFFFFF);
GlStateManager.enableLighting();
GlStateManager.popMatrix();
}

GlStateManager.pushMatrix();
GlStateManager.translate((float) x + 0.5F, (float) y + 0.75F, (float) z + 0.5F);
float f = (float) te.tickCount + partialTicks;
Expand Down Expand Up @@ -130,5 +103,34 @@ public void render(HSSoulAltarTE te, double x, double y, double z, float partial
GlStateManager.enableCull();
this.modelBook.render(null, f, f3, f4, f5, 0.0F, 0.0625F);
GlStateManager.popMatrix();

if (!stack.isEmpty())
{
int textColor = te.isValidRecipe() ? 0x00FF00 : 0xFFFFFF;

// Display soul count
String soulsText = "Souls: " + te.getSoulCount();
GlStateManager.pushMatrix();
GlStateManager.translate(x + 0.5, y + 1.5, z + 0.5);
GlStateManager.rotate(-Minecraft.getMinecraft().getRenderManager().playerViewY, 0, 1, 0);
GlStateManager.rotate(Minecraft.getMinecraft().getRenderManager().playerViewX, 1, 0, 0);
GlStateManager.scale(-0.025f, -0.025f, 0.025f);
GlStateManager.disableLighting();
Minecraft.getMinecraft().fontRenderer.drawString(soulsText, -Minecraft.getMinecraft().fontRenderer.getStringWidth(soulsText) / 2, 0, textColor);
GlStateManager.enableLighting();
GlStateManager.popMatrix();

// Display input stack count
String stackText = String.valueOf(stack.getCount());
GlStateManager.pushMatrix();
GlStateManager.translate(x + 0.5, y + 2.7, z + 0.5);
GlStateManager.rotate(-Minecraft.getMinecraft().getRenderManager().playerViewY, 0, 1, 0);
GlStateManager.rotate(Minecraft.getMinecraft().getRenderManager().playerViewX, 1, 0, 0);
GlStateManager.scale(-0.025f, -0.025f, 0.025f);
GlStateManager.disableLighting();
Minecraft.getMinecraft().fontRenderer.drawString(stackText, -Minecraft.getMinecraft().fontRenderer.getStringWidth(stackText) / 2, 0, textColor);
GlStateManager.enableLighting();
GlStateManager.popMatrix();
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/mod/emt/harkenscythe/init/HSRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import mod.emt.harkenscythe.entities.render.HSSoulRender;
import mod.emt.harkenscythe.tileentities.HSSoulAltarTE;
import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.ModelRegistryEvent;
Expand Down Expand Up @@ -63,6 +64,11 @@ public static void registerTileEntities()
GameRegistry.registerTileEntity(HSSoulAltarTE.class, new ResourceLocation(HarkenScythe.MOD_ID, "soul_altar"));
}

public static void registerRecipes()
{
HSSoulAltarRecipes.addRecipe(Items.IRON_INGOT, HSItems.livingmetal_ingot, 10);
}

@SideOnly(Side.CLIENT)
@SubscribeEvent
public static void registerEntityRenderers(ModelRegistryEvent event)
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/mod/emt/harkenscythe/init/HSSoulAltarRecipes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mod.emt.harkenscythe.init;

import java.util.Map;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.item.Item;

public class HSSoulAltarRecipes
{
private static final Map<Item, Item> SOUL_INPUT_OUTPUT_MAP = new Object2ObjectOpenHashMap<>();
private static final Map<Item, Integer> SOUL_INPUT_SOULS_MAP = new Object2IntOpenHashMap<>();

public static void addRecipe(Item input, Item output, int requiredSouls)
{
SOUL_INPUT_OUTPUT_MAP.put(input, output);
SOUL_INPUT_SOULS_MAP.put(input, Math.max(requiredSouls, 10));
}

public static boolean isValidInput(Item input)
{
return SOUL_INPUT_OUTPUT_MAP.containsKey(input) && SOUL_INPUT_SOULS_MAP.containsKey(input);
}

public static Item getOutput(Item input)
{
return SOUL_INPUT_OUTPUT_MAP.get(input);
}

public static int getRequiredSouls(Item input)
{
return SOUL_INPUT_SOULS_MAP.get(input);
}
}
2 changes: 2 additions & 0 deletions src/main/java/mod/emt/harkenscythe/init/HSSoundEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
@GameRegistry.ObjectHolder(HarkenScythe.MOD_ID)
public class HSSoundEvents
{
public static final SoundEvent BLOCK_BLOOD_ALTAR_ENCHANT = new SoundEvent(new ResourceLocation(HarkenScythe.MOD_ID, "block.soul_altar.enchant"));
public static final SoundEvent BLOCK_SOUL_ALTAR_ENCHANT = new SoundEvent(new ResourceLocation(HarkenScythe.MOD_ID, "block.blood_altar.enchant"));
public static final SoundEvent ITEM_ATHAME_CREATE = new SoundEvent(new ResourceLocation(HarkenScythe.MOD_ID, "item.athame.create"));
}
105 changes: 91 additions & 14 deletions src/main/java/mod/emt/harkenscythe/tileentities/HSSoulAltarTE.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package mod.emt.harkenscythe.tileentities;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import mod.emt.harkenscythe.blocks.HSSoulCrucible;
import mod.emt.harkenscythe.init.HSSoulAltarRecipes;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -29,28 +32,39 @@ public class HSSoulAltarTE extends TileEntity implements ITickable
public float bookRotation;
public float bookRotationPrev;
public float tRot;
public int soulCount;
private ItemStack item = ItemStack.EMPTY;
private int soulCount;
private boolean validRecipe;
private ItemStack itemStack = ItemStack.EMPTY;

public ItemStack getItem()
public int getSoulCount()
{
return item;
return soulCount;
}

public void setItem(ItemStack item)
public boolean isValidRecipe()
{
this.item = item;
return validRecipe;
}

public ItemStack getItemStack()
{
return itemStack;
}

public void setItemStack(ItemStack itemStack)
{
this.itemStack = itemStack;
markDirty();
}

public void dropItem()
{
if (!world.isRemote && !item.isEmpty())
if (!world.isRemote && !itemStack.isEmpty())
{
BlockPos pos = getPos();
EntityItem entityItem = new EntityItem(world, pos.getX(), pos.getY(), pos.getZ(), item);
EntityItem entityItem = new EntityItem(world, pos.getX(), pos.getY(), pos.getZ(), itemStack);
world.spawnEntity(entityItem);
setItem(ItemStack.EMPTY);
setItemStack(ItemStack.EMPTY);
}
}

Expand All @@ -60,18 +74,18 @@ public void readFromNBT(NBTTagCompound compound)
super.readFromNBT(compound);
if (compound.hasKey("Item"))
{
item = new ItemStack(compound.getCompoundTag("Item"));
itemStack = new ItemStack(compound.getCompoundTag("Item"));
}
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
{
super.writeToNBT(compound);
if (!item.isEmpty())
if (!itemStack.isEmpty())
{
NBTTagCompound itemTag = new NBTTagCompound();
item.writeToNBT(itemTag);
itemStack.writeToNBT(itemTag);
compound.setTag("Item", itemTag);
}
return compound;
Expand Down Expand Up @@ -174,12 +188,23 @@ public void update()
this.flipA += (f - this.flipA) * 0.9F;
this.pageFlip += this.flipA;

if (this.world.getWorldTime() % 20 == 19 && !this.getItem().isEmpty())
if (this.world.getWorldTime() % 20 == 19 && !this.getItemStack().isEmpty())
{
this.soulCount = scanCrucibleLevels() * 10;
updateSoulCount();
updateRecipe();
}
}

public void updateSoulCount()
{
this.soulCount = scanCrucibleLevels() * 10;
}

public void updateRecipe()
{
this.validRecipe = HSSoulAltarRecipes.isValidInput(this.getItemStack().getItem()) && HSSoulAltarRecipes.getRequiredSouls(this.getItemStack().getItem()) <= this.soulCount;
}

public int scanCrucibleLevels()
{
int totalLevel = 0;
Expand All @@ -206,4 +231,56 @@ public int scanCrucibleLevels()

return totalLevel;
}

public void decreaseCrucibleLevel(int levelToDecrease)
{
World world = this.getWorld();
if (world.isRemote) return;

BlockPos pos = this.getPos();
List<BlockPos> cruciblePositions = new ArrayList<>();

for (int dx = -RADIUS; dx <= RADIUS; dx++)
{
for (int dy = -RADIUS; dy <= RADIUS; dy++)
{
for (int dz = -RADIUS; dz <= RADIUS; dz++)
{
BlockPos checkPos = pos.add(dx, dy, dz);
IBlockState state = world.getBlockState(checkPos);

if (state.getBlock() instanceof HSSoulCrucible)
{
cruciblePositions.add(checkPos);
}
}
}
}

int remainingLevelToDecrease = levelToDecrease;

while (remainingLevelToDecrease > 0 && !cruciblePositions.isEmpty())
{
BlockPos selectedPos = cruciblePositions.get(world.rand.nextInt(cruciblePositions.size()));
IBlockState state = world.getBlockState(selectedPos);

if (state.getBlock() instanceof HSSoulCrucible)
{
int currentLevel = state.getValue(HSSoulCrucible.LEVEL);

if (currentLevel >= remainingLevelToDecrease)
{
world.setBlockState(selectedPos, state.withProperty(HSSoulCrucible.LEVEL, currentLevel - remainingLevelToDecrease));
remainingLevelToDecrease = 0;
}
else
{
world.setBlockState(selectedPos, state.withProperty(HSSoulCrucible.LEVEL, 0));
remainingLevelToDecrease -= currentLevel;
}
}

cruciblePositions.remove(selectedPos);
}
}
}
Loading

0 comments on commit b4dc940

Please sign in to comment.