Skip to content

Commit

Permalink
Enchantment sheep improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AtomicBlom committed Oct 11, 2016
1 parent 88091ee commit e3fc14c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private ItemStackHelper() {}

public static boolean isStackForBlock(ItemStack itemStack, Block block)
{
if (itemStack == null) return false;
final Item item = itemStack.getItem();
if (item instanceof ItemBlock) {
if (Objects.equals(((ItemBlock) item).block, block)) {
Expand All @@ -27,6 +28,7 @@ public static boolean isStackForBlock(ItemStack itemStack, Block block)

public static boolean isStackForBlockSubclassOf(ItemStack itemStack, Class<? extends Block> blockClass)
{
if (itemStack == null) return false;
final Item item = itemStack.getItem();
if (item instanceof ItemBlock) {
if (blockClass.isAssignableFrom(((ItemBlock) item).block.getClass())) {
Expand All @@ -45,6 +47,7 @@ public static boolean isStackForBlock(ItemStack itemStack, Block... blocks) {
}

private static boolean isStackForBlock(ItemStack itemStack, Iterator<Block> blocks) {
if (itemStack == null) return false;
final Item item = itemStack.getItem();
if (item instanceof ItemBlock) {
final Block block = ((ItemBlock) item).block;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
package com.github.atomicblom.shearmadness.variations.vanilla.container;

import com.github.atomicblom.shearmadness.api.Capability;
import com.github.atomicblom.shearmadness.api.ItemStackHelper;
import com.github.atomicblom.shearmadness.api.capability.IChiseledSheepCapability;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentData;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.ContainerEnchantment;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;

import java.util.List;
import java.util.Random;

public class ContainerEnchantmentSheep extends ContainerEnchantment
{
private final EntityLiving entity;
private final Random rand;
private final World world;

public ContainerEnchantmentSheep(InventoryPlayer playerInventory, World worldIn, EntityLiving entity)
{
super(playerInventory, worldIn, entity.getPosition());
this.entity = entity;
rand = new Random();
world = worldIn;
}

@Override
Expand All @@ -34,4 +52,98 @@ public boolean canInteractWith(EntityPlayer playerIn) {

return playerIn.getDistanceSq(entity.getPosition()) <= 64.0D;
}

/**
* Callback for when the crafting matrix is changed.
*/
public void onCraftMatrixChanged(IInventory inventoryIn)
{
if (inventoryIn == tableInventory)
{
final ItemStack itemstack = inventoryIn.getStackInSlot(0);

if (itemstack != null && itemstack.isItemEnchantable())
{
if (!world.isRemote)
{
float power = 0;
final BlockPos position = entity.getPosition();

for (int j = -1; j <= 1; ++j)
{
for (int k = -1; k <= 1; ++k)
{
if ((j != 0 || k != 0) && world.isAirBlock(position.add(k, 0, j)) && world.isAirBlock(position.add(k, 1, j)))
{
power += ForgeHooks.getEnchantPower(world, position.add(k * 2, 0, j * 2));
power += ForgeHooks.getEnchantPower(world, position.add(k * 2, 1, j * 2));
if (k != 0 && j != 0)
{
power += ForgeHooks.getEnchantPower(world, position.add(k * 2, 0, j));
power += ForgeHooks.getEnchantPower(world, position.add(k * 2, 1, j));
power += ForgeHooks.getEnchantPower(world, position.add(k, 0, j * 2));
power += ForgeHooks.getEnchantPower(world, position.add(k, 1, j * 2));
}
}
}
}

final AxisAlignedBB searchBox = new AxisAlignedBB(position.add(-5, -2, -5), position.add(5, 2, 5));
for (final Entity nearbyEntity : entity.worldObj.getEntitiesWithinAABBExcludingEntity(entity, searchBox))
{
final double distance = entity.getDistanceSqToEntity(nearbyEntity);

//5^2
final IChiseledSheepCapability capability = nearbyEntity.getCapability(Capability.CHISELED_SHEEP, null);
if (distance < 25 && capability != null) {
final ItemStack chiselItemStack = capability.getChiselItemStack();
if (chiselItemStack != null && chiselItemStack.getItem() instanceof ItemBlock) {
power += ((ItemBlock) chiselItemStack.getItem()).block.getEnchantPowerBonus(world, nearbyEntity.getPosition());
}
}
}

rand.setSeed(xpSeed);

for (int i1 = 0; i1 < 3; ++i1)
{
enchantLevels[i1] = EnchantmentHelper.calcItemStackEnchantability(rand, i1, (int)power, itemstack);
enchantClue[i1] = -1;
worldClue[i1] = -1;

if (enchantLevels[i1] < i1 + 1)
{
enchantLevels[i1] = 0;
}
}

for (int j1 = 0; j1 < 3; ++j1)
{
if (enchantLevels[j1] > 0)
{
final List<EnchantmentData> list = getEnchantmentList(itemstack, j1, enchantLevels[j1]);

if (list != null && !list.isEmpty())
{
final EnchantmentData enchantmentdata = list.get(rand.nextInt(list.size()));
enchantClue[j1] = Enchantment.getEnchantmentID(enchantmentdata.enchantmentobj);
worldClue[j1] = enchantmentdata.enchantmentLevel;
}
}
}

detectAndSendChanges();
}
}
else
{
for (int i = 0; i < 3; ++i)
{
enchantLevels[i] = 0;
enchantClue[i] = -1;
worldClue[i] = -1;
}
}
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/ShearMadness_at.cfg
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
public net.minecraft.block.BlockNote field_176434_a # INSTRUMENTS
public net.minecraft.block.BlockNote field_176434_a # INSTRUMENTS
protected net.minecraft.inventory.ContainerEnchantment func_178148_a(Lnet/minecraft/item/ItemStack;II)Ljava/util/List; # getEnchantmentList

0 comments on commit e3fc14c

Please sign in to comment.