2020-05-20 15:41:55 +02:00
|
|
|
package de.ellpeck.naturesaura.items;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.ElytraItem;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2020-09-22 03:17:02 +02:00
|
|
|
import net.minecraft.util.text.*;
|
2020-05-22 12:26:35 +02:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2020-05-20 15:41:55 +02:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.AnvilUpdateEvent;
|
|
|
|
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerEvent;
|
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class ItemBreakPrevention extends ItemImpl {
|
|
|
|
public ItemBreakPrevention() {
|
|
|
|
super("break_prevention");
|
|
|
|
MinecraftForge.EVENT_BUS.register(new Events());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Events {
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onAnvilUpdate(AnvilUpdateEvent event) {
|
|
|
|
ItemStack stack = event.getLeft();
|
|
|
|
if (stack.getToolTypes().isEmpty() || !stack.isDamageable())
|
|
|
|
return;
|
|
|
|
ItemStack second = event.getRight();
|
|
|
|
if (second.getItem() != ModItems.BREAK_PREVENTION)
|
|
|
|
return;
|
|
|
|
ItemStack output = stack.copy();
|
|
|
|
output.getOrCreateTag().putBoolean(NaturesAura.MOD_ID + ":break_prevention", true);
|
|
|
|
event.setOutput(output);
|
|
|
|
event.setMaterialCost(1);
|
|
|
|
event.setCost(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onBreakSpeed(PlayerEvent.BreakSpeed event) {
|
|
|
|
PlayerEntity player = event.getPlayer();
|
|
|
|
if (player == null)
|
|
|
|
return;
|
|
|
|
ItemStack stack = player.getHeldItemMainhand();
|
|
|
|
if (!stack.hasTag() || !stack.getTag().getBoolean(NaturesAura.MOD_ID + ":break_prevention"))
|
|
|
|
return;
|
|
|
|
if (ElytraItem.isUsable(stack))
|
|
|
|
return;
|
|
|
|
event.setNewSpeed(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2020-05-22 12:26:35 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2020-05-20 15:41:55 +02:00
|
|
|
public void onTooltip(ItemTooltipEvent event) {
|
|
|
|
ItemStack stack = event.getItemStack();
|
|
|
|
if (!stack.hasTag() || !stack.getTag().getBoolean(NaturesAura.MOD_ID + ":break_prevention"))
|
|
|
|
return;
|
|
|
|
List<ITextComponent> tooltip = event.getToolTip();
|
2020-09-22 03:17:02 +02:00
|
|
|
tooltip.add(new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".break_prevention").setStyle(Style.EMPTY.setFormatting(TextFormatting.GRAY)));
|
2020-05-20 15:41:55 +02:00
|
|
|
if (ElytraItem.isUsable(stack))
|
|
|
|
return;
|
|
|
|
if (tooltip.size() < 1)
|
|
|
|
return;
|
2020-09-22 03:17:02 +02:00
|
|
|
ITextComponent head = tooltip.get(0);
|
|
|
|
if (head instanceof TextComponent)
|
|
|
|
((TextComponent) head).append(new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".broken").setStyle(Style.EMPTY.setFormatting(TextFormatting.GRAY)));
|
2020-05-20 15:41:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|