fortune enchantable

This commit is contained in:
serenibyss
2024-01-03 23:21:32 -06:00
parent b252d1c891
commit 27a3f70f0b
2 changed files with 53 additions and 16 deletions
@@ -140,6 +140,27 @@ public final class ItemPart extends AEBaseItem implements IPartItem, IItemGroup
return AEApi.instance().partHelper().placeBus(player.getHeldItem(hand), pos, side, player, hand, w); return AEApi.instance().partHelper().placeBus(player.getHeldItem(hand), pos, side, player, hand, w);
} }
@Override
public boolean isBookEnchantable(ItemStack itemstack1, ItemStack itemstack2) {
return true;
}
@Override
public boolean isEnchantable(ItemStack stack) {
if (getTypeByStack(stack) == PartType.IDENTITY_ANNIHILATION_PLANE) {
return true;
}
return super.isEnchantable(stack);
}
@Override
public int getItemEnchantability(ItemStack stack) {
if (getTypeByStack(stack) == PartType.IDENTITY_ANNIHILATION_PLANE) {
return 10;
}
return super.getItemEnchantability(stack);
}
@Override @Override
@Nonnull @Nonnull
public String getTranslationKey(final ItemStack is) { public String getTranslationKey(final ItemStack is) {
@@ -19,23 +19,27 @@
package appeng.parts.automation; package appeng.parts.automation;
import appeng.api.parts.IPart;
import appeng.api.parts.IPartHost;
import appeng.api.parts.IPartModel; import appeng.api.parts.IPartModel;
import appeng.api.util.AEPartLocation;
import appeng.items.parts.PartModels; import appeng.items.parts.PartModels;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.init.Enchantments;
import net.minecraft.init.Items; import net.minecraft.init.Items;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer; import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayer; import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.FakePlayerFactory; import net.minecraftforge.common.util.FakePlayerFactory;
import net.minecraftforge.event.ForgeEventFactory;
import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class PartIdentityAnnihilationPlane extends PartAnnihilationPlane { public class PartIdentityAnnihilationPlane extends PartAnnihilationPlane {
@@ -53,15 +57,6 @@ public class PartIdentityAnnihilationPlane extends PartAnnihilationPlane {
super(is); super(is);
} }
@Override
protected boolean isAnnihilationPlane(final TileEntity blockTileEntity, final AEPartLocation side) {
if (blockTileEntity instanceof IPartHost) {
final IPart p = ((IPartHost) blockTileEntity).getPart(side);
return p != null && p.getClass() == this.getClass();
}
return false;
}
@Override @Override
protected float calculateEnergyUsage(final WorldServer w, final BlockPos pos, final List<ItemStack> items) { protected float calculateEnergyUsage(final WorldServer w, final BlockPos pos, final List<ItemStack> items) {
final float requiredEnergy = super.calculateEnergyUsage(w, pos, items); final float requiredEnergy = super.calculateEnergyUsage(w, pos, items);
@@ -74,7 +69,11 @@ public class PartIdentityAnnihilationPlane extends PartAnnihilationPlane {
final FakePlayer fakePlayer = FakePlayerFactory.getMinecraft(w); final FakePlayer fakePlayer = FakePlayerFactory.getMinecraft(w);
final IBlockState state = w.getBlockState(pos); final IBlockState state = w.getBlockState(pos);
if (state.getBlock().canSilkHarvest(w, pos, state, fakePlayer)) { final ItemStack prevItem = fakePlayer.getHeldItem(EnumHand.MAIN_HAND);
final ItemStack stack = getItemStack();
fakePlayer.setHeldItem(EnumHand.MAIN_HAND, stack);
if (state.getBlock().canSilkHarvest(w, pos, state, fakePlayer) && getEnchantLevel(Enchantments.FORTUNE, stack) <= 0) {
final List<ItemStack> out = new ArrayList<>(1); final List<ItemStack> out = new ArrayList<>(1);
final Item item = Item.getItemFromBlock(state.getBlock()); final Item item = Item.getItemFromBlock(state.getBlock());
@@ -85,16 +84,33 @@ public class PartIdentityAnnihilationPlane extends PartAnnihilationPlane {
} }
final ItemStack itemstack = new ItemStack(item, 1, meta); final ItemStack itemstack = new ItemStack(item, 1, meta);
out.add(itemstack); out.add(itemstack);
final float chance = ForgeEventFactory.fireBlockHarvesting(out, w, pos, state, 0, 1.0F, true, fakePlayer);
fakePlayer.setHeldItem(EnumHand.MAIN_HAND, prevItem);
if (chance == 1.0F) return out;
return out.stream().filter($ -> w.rand.nextFloat() <= chance).collect(Collectors.toList());
} }
fakePlayer.setHeldItem(EnumHand.MAIN_HAND, prevItem);
return out; return out;
} else {
return super.obtainBlockDrops(w, pos);
} }
final int fortune = getEnchantLevel(Enchantments.FORTUNE, stack);
final NonNullList<ItemStack> drops = NonNullList.create();
state.getBlock().getDrops(drops, w, pos, state, fortune);
final float chance = ForgeEventFactory.fireBlockHarvesting(drops, w, pos, state, fortune, 1.0F, false, fakePlayer);
fakePlayer.setHeldItem(EnumHand.MAIN_HAND, prevItem);
if (chance == 1.0F) return drops;
return drops.stream().filter($ -> w.rand.nextFloat() <= chance).collect(Collectors.toList());
} }
@Nonnull
@Override @Override
public IPartModel getStaticModels() { public IPartModel getStaticModels() {
return MODELS.getModel(this.getConnections(), this.isPowered(), this.isActive()); return MODELS.getModel(this.getConnections(), this.isPowered(), this.isActive());
} }
private static int getEnchantLevel(final Enchantment enchantment, final ItemStack stack) {
if (enchantment == null) return 0;
return EnchantmentHelper.getEnchantmentLevel(enchantment, stack);
}
} }