ConstructionWand/src/main/java/thetadev/constructionwand/items/wand/ItemWand.java
2021-03-15 00:08:45 +01:00

141 lines
6.4 KiB
Java

package thetadev.constructionwand.items.wand;
import net.minecraft.block.BlockState;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import thetadev.constructionwand.ConstructionWand;
import thetadev.constructionwand.api.IWandCore;
import thetadev.constructionwand.basics.WandUtil;
import thetadev.constructionwand.basics.option.IOption;
import thetadev.constructionwand.basics.option.WandOptions;
import thetadev.constructionwand.items.ItemBase;
import thetadev.constructionwand.wand.WandJob;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public abstract class ItemWand extends ItemBase
{
public ItemWand(String name, Properties properties) {
super(name, properties);
addPropertyOverride(new ResourceLocation(ConstructionWand.MODID, "using_core"),
(stack, world, entity) -> entity == null || !(stack.getItem() instanceof ItemWand) ? 0 :
new WandOptions(stack).cores.get().getColor() > -1 ? 1 : 0);
}
@Nonnull
@Override
public ActionResultType onItemUse(ItemUseContext context) {
PlayerEntity player = context.getPlayer();
Hand hand = context.getHand();
World world = context.getWorld();
if(world.isRemote || player == null) return ActionResultType.FAIL;
ItemStack stack = player.getHeldItem(hand);
if(player.isSneaking() && ConstructionWand.instance.undoHistory.isUndoActive(player)) {
return ConstructionWand.instance.undoHistory.undo(player, world, context.getPos()) ? ActionResultType.SUCCESS : ActionResultType.FAIL;
}
else {
WandJob job = getWandJob(player, world, new BlockRayTraceResult(context.getHitVec(), context.getFace(), context.getPos(), false), stack);
return job.doIt() ? ActionResultType.SUCCESS : ActionResultType.FAIL;
}
}
@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(@Nonnull World world, PlayerEntity player, @Nonnull Hand hand) {
ItemStack stack = player.getHeldItem(hand);
if(!player.isSneaking()) {
if(world.isRemote) return new ActionResult<>(ActionResultType.FAIL, stack);
// Right click: Place angel block
WandJob job = getWandJob(player, world, BlockRayTraceResult.createMiss(player.getLookVec(),
WandUtil.fromVector(player.getLookVec()), WandUtil.playerPos(player)), stack);
return new ActionResult<>(job.doIt() ? ActionResultType.SUCCESS : ActionResultType.FAIL, stack);
}
return new ActionResult<>(ActionResultType.FAIL, stack);
}
public static WandJob getWandJob(PlayerEntity player, World world, @Nullable BlockRayTraceResult rayTraceResult, ItemStack wand) {
WandJob wandJob = new WandJob(player, world, rayTraceResult, wand);
wandJob.getSnapshots();
return wandJob;
}
@Override
public boolean canHarvestBlock(@Nonnull BlockState blockIn) {
return false;
}
@Override
public boolean getIsRepairable(@Nonnull ItemStack toRepair, @Nonnull ItemStack repair) {
return false;
}
public int remainingDurability(ItemStack stack) {
return Integer.MAX_VALUE;
}
@OnlyIn(Dist.CLIENT)
public void addInformation(@Nonnull ItemStack itemstack, World worldIn, @Nonnull List<ITextComponent> lines, @Nonnull ITooltipFlag extraInfo) {
WandOptions options = new WandOptions(itemstack);
int limit = options.cores.get().getWandAction().getLimit(itemstack);
String langTooltip = ConstructionWand.MODID + ".tooltip.";
// +SHIFT tooltip: show all options + installed cores
if(Screen.hasShiftDown()) {
for(int i = 1; i < options.allOptions.length; i++) {
IOption<?> opt = options.allOptions[i];
lines.add(new TranslationTextComponent(opt.getKeyTranslation()).applyTextStyle(TextFormatting.AQUA)
.appendSibling(new TranslationTextComponent(opt.getValueTranslation()).applyTextStyle(TextFormatting.GRAY))
);
}
if(!options.cores.getUpgrades().isEmpty()) {
lines.add(new StringTextComponent(""));
lines.add(new TranslationTextComponent(langTooltip + "cores").applyTextStyle(TextFormatting.GRAY));
for(IWandCore core : options.cores.getUpgrades()) {
lines.add(new TranslationTextComponent(options.cores.getKeyTranslation() + "." + core.getRegistryName().toString()));
}
}
}
// Default tooltip: show block limit + active wand core
else {
IOption<?> opt = options.allOptions[0];
lines.add(new TranslationTextComponent(langTooltip + "blocks", limit).applyTextStyle(TextFormatting.GRAY));
lines.add(new TranslationTextComponent(opt.getKeyTranslation()).applyTextStyle(TextFormatting.AQUA)
.appendSibling(new TranslationTextComponent(opt.getValueTranslation()).applyTextStyle(TextFormatting.WHITE)));
lines.add(new TranslationTextComponent(langTooltip + "shift").applyTextStyle(TextFormatting.AQUA));
}
}
public static void optionMessage(PlayerEntity player, IOption<?> option) {
player.sendStatusMessage(
new TranslationTextComponent(option.getKeyTranslation()).applyTextStyle(TextFormatting.AQUA)
.appendSibling(new TranslationTextComponent(option.getValueTranslation()).applyTextStyle(TextFormatting.WHITE))
.appendSibling(new StringTextComponent(" - ").applyTextStyle(TextFormatting.GRAY))
.appendSibling(new TranslationTextComponent(option.getDescTranslation()).applyTextStyle(TextFormatting.WHITE))
, true);
}
}