2018-09-20 22:49:10 +02:00
|
|
|
package com.minelittlepony.unicopia.item;
|
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.Predicates;
|
2018-09-21 17:53:33 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.EntitySpell;
|
2018-09-20 22:49:10 +02:00
|
|
|
import com.minelittlepony.unicopia.spell.IMagicEffect;
|
|
|
|
import com.minelittlepony.unicopia.spell.IUseAction;
|
|
|
|
import com.minelittlepony.unicopia.spell.SpellCastResult;
|
|
|
|
import com.minelittlepony.unicopia.spell.IDispenceable;
|
|
|
|
import com.minelittlepony.unicopia.spell.SpellRegistry;
|
|
|
|
import com.minelittlepony.util.vector.VecHelper;
|
|
|
|
|
|
|
|
import net.minecraft.block.BlockDispenser;
|
|
|
|
import net.minecraft.creativetab.CreativeTabs;
|
|
|
|
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
|
|
|
|
import net.minecraft.dispenser.IBehaviorDispenseItem;
|
|
|
|
import net.minecraft.dispenser.IBlockSource;
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.EnumActionResult;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.EnumHand;
|
|
|
|
import net.minecraft.util.NonNullList;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
|
|
public class ItemSpell extends Item implements ICastable {
|
2019-01-20 00:08:13 +01:00
|
|
|
private static final IBehaviorDispenseItem dispenserBehavior = new BehaviorDefaultDispenseItem() {
|
|
|
|
@Override
|
|
|
|
protected ItemStack dispenseStack(IBlockSource source, ItemStack stack) {
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
IDispenceable effect = SpellRegistry.instance().getDispenseActionFrom(stack);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
if (effect == null) {
|
|
|
|
return super.dispenseStack(source, stack);
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
SpellCastResult dispenceResult = ((ICastable)stack.getItem()).onDispenseSpell(source, stack, effect);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
|
|
|
if (dispenceResult == SpellCastResult.DEFAULT) {
|
|
|
|
return super.dispenseStack(source, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dispenceResult == SpellCastResult.PLACE) {
|
2018-09-21 17:53:33 +02:00
|
|
|
BlockPos pos = source.getBlockPos();
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
castContainedSpell(source.getWorld(), pos, stack, effect);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
|
|
|
stack.shrink(1);
|
|
|
|
}
|
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
};
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
public ItemSpell(String domain, String name) {
|
|
|
|
super();
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
setHasSubtypes(true);
|
|
|
|
setMaxDamage(0);
|
|
|
|
setTranslationKey(name);
|
|
|
|
setRegistryName(domain, name);
|
|
|
|
setMaxStackSize(16);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
|
|
|
setCreativeTab(CreativeTabs.BREWING);
|
|
|
|
|
|
|
|
BlockDispenser.DISPENSE_BEHAVIOR_REGISTRY.putObject(this, dispenserBehavior);
|
2019-01-20 00:08:13 +01:00
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
@Override
|
|
|
|
public boolean hasEffect(ItemStack stack) {
|
|
|
|
return SpellRegistry.stackHasEnchantment(stack);
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SpellCastResult onDispenseSpell(IBlockSource source, ItemStack stack, IDispenceable effect) {
|
|
|
|
EnumFacing facing = source.getBlockState().getValue(BlockDispenser.FACING);
|
|
|
|
BlockPos pos = source.getBlockPos().offset(facing);
|
|
|
|
|
|
|
|
return effect.onDispenced(pos, facing, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SpellCastResult onCastSpell(EntityPlayer player, World world, BlockPos pos, ItemStack stack, IMagicEffect effect, EnumFacing side, float hitX, float hitY, float hitZ) {
|
|
|
|
if (effect instanceof IUseAction) {
|
|
|
|
return ((IUseAction)effect).onUse(stack, player, world, pos, side, hitX, hitY, hitZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SpellCastResult.PLACE;
|
|
|
|
}
|
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
@Override
|
|
|
|
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
if (hand != EnumHand.MAIN_HAND || !Predicates.MAGI.test(player)) {
|
|
|
|
return EnumActionResult.PASS;
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
ItemStack stack = player.getHeldItem(hand);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
if (!SpellRegistry.stackHasEnchantment(stack)) {
|
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
IMagicEffect effect = SpellRegistry.instance().getSpellFromItemStack(stack);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
|
|
|
if (effect == null) {
|
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpellCastResult result = ((ICastable)stack.getItem()).onCastSpell(player, world, pos, stack, effect, side, hitX, hitY, hitZ);
|
|
|
|
|
|
|
|
if (!world.isRemote) {
|
|
|
|
pos = pos.offset(side);
|
|
|
|
|
|
|
|
if (result == SpellCastResult.PLACE) {
|
2018-09-21 17:53:33 +02:00
|
|
|
castContainedSpell(world, pos, stack, effect).setOwner(player);
|
2018-09-20 22:49:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != SpellCastResult.NONE) {
|
|
|
|
if (!player.capabilities.isCreativeMode) {
|
|
|
|
stack.shrink(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EnumActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
return EnumActionResult.FAIL;
|
2018-09-20 22:49:10 +02:00
|
|
|
}
|
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
@Override
|
|
|
|
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
Entity target = VecHelper.getLookedAtEntity(player, 5);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
ItemStack stack = player.getHeldItem(hand);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
if (target == null) {
|
|
|
|
return new ActionResult<ItemStack>(EnumActionResult.PASS, stack);
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
if (!SpellRegistry.stackHasEnchantment(stack)) {
|
|
|
|
return new ActionResult<ItemStack>(EnumActionResult.FAIL, stack);
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:07:59 +01:00
|
|
|
IMagicEffect effect = SpellRegistry.instance().getSpellFromItemStack(stack);
|
|
|
|
|
|
|
|
if (effect instanceof IUseAction) {
|
|
|
|
SpellCastResult result = ((IUseAction)effect).onUse(stack, player, world, target);
|
|
|
|
|
|
|
|
if (result != SpellCastResult.NONE) {
|
|
|
|
if (result == SpellCastResult.PLACE && !player.capabilities.isCreativeMode) {
|
|
|
|
stack.shrink(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:49:10 +02:00
|
|
|
return new ActionResult<ItemStack>(EnumActionResult.PASS, stack);
|
|
|
|
}
|
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
@Override
|
|
|
|
public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> subItems) {
|
|
|
|
super.getSubItems(tab, subItems);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
if (isInCreativeTab(tab)) {
|
|
|
|
for (String name : SpellRegistry.instance().getAllNames()) {
|
|
|
|
subItems.add(SpellRegistry.instance().enchantStack(new ItemStack(this, 1), name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
@Override
|
|
|
|
public String getTranslationKey(ItemStack stack) {
|
|
|
|
String result = super.getTranslationKey(stack);
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
if (SpellRegistry.stackHasEnchantment(stack)) {
|
|
|
|
result += "." + stack.getTagCompound().getString("spell");
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
return result;
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
protected static EntitySpell castContainedSpell(World world, BlockPos pos, ItemStack stack, IMagicEffect effect) {
|
|
|
|
EntitySpell spell = new EntitySpell(world);
|
2018-09-21 17:53:33 +02:00
|
|
|
|
2018-09-20 22:49:10 +02:00
|
|
|
spell.setEffect(effect);
|
2019-01-20 00:08:13 +01:00
|
|
|
spell.setLocationAndAngles(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, 0, 0);
|
|
|
|
world.spawnEntity(spell);
|
2018-09-21 17:53:33 +02:00
|
|
|
|
2019-01-20 00:08:13 +01:00
|
|
|
return spell;
|
|
|
|
}
|
2018-09-24 21:37:16 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canFeed(EntitySpell entity, ItemStack stack) {
|
|
|
|
|
|
|
|
IMagicEffect effect = entity.getEffect();
|
|
|
|
|
|
|
|
return effect != null && effect.getName().equals(SpellRegistry.getKeyFromStack(stack));
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
}
|