Implemented the StrawTotemItem and StrawChargeItem.
This commit is contained in:
@ -5,13 +5,11 @@ import net.minecraft.item.Item;
|
||||
import net.minecraft.item.Rarity;
|
||||
|
||||
public class StrawChargeItem extends Item {
|
||||
private static final String NAME = "straw_charge";
|
||||
|
||||
public StrawChargeItem() {
|
||||
super(new Properties().tab(TotemOfReviving.ITEM_GROUP).stacksTo(64).rarity(Rarity.UNCOMMON));
|
||||
super(new Item.Properties().tab(TotemOfReviving.ITEM_GROUP).stacksTo(64).rarity(Rarity.COMMON));
|
||||
}
|
||||
|
||||
public static String getName() {
|
||||
return NAME;
|
||||
return "straw_charge";
|
||||
}
|
||||
}
|
||||
|
@ -1,119 +1,20 @@
|
||||
package dev.micle.totemofreviving.item;
|
||||
|
||||
import dev.micle.totemofreviving.TotemOfReviving;
|
||||
import dev.micle.totemofreviving.config.Config;
|
||||
import dev.micle.totemofreviving.network.ChangeTargetPacket;
|
||||
import dev.micle.totemofreviving.network.ChargeTotemPacket;
|
||||
import dev.micle.totemofreviving.network.Network;
|
||||
import dev.micle.totemofreviving.network.ReviveTargetPacket;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.util.ITooltipFlag;
|
||||
import net.minecraft.client.util.InputMappings;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.Rarity;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class StrawTotemItem extends Item {
|
||||
private static final String NAME = "straw_totem";
|
||||
|
||||
public static final String TAG_TARGET_INDEX = "target_index";
|
||||
public static final String TAG_TARGET_UUID = "target_uuid";
|
||||
public static final String TAG_TARGET_CHARGE = "target_cost";
|
||||
public static final String TAG_CHARGE= "charge";
|
||||
|
||||
public class StrawTotemItem extends TotemItem {
|
||||
public StrawTotemItem() {
|
||||
super(new Item.Properties().tab(TotemOfReviving.ITEM_GROUP).stacksTo(1).rarity(Rarity.UNCOMMON).defaultDurability(Config.Server.getStrawTotemConfig().getDurability()));
|
||||
super(Rarity.UNCOMMON, Config.Server.getStrawTotemConfig().getDurability());
|
||||
}
|
||||
|
||||
public static String getName() {
|
||||
return "straw_totem";
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
@ParametersAreNonnullByDefault
|
||||
public void appendHoverText(ItemStack itemStack, @Nullable World world, List<ITextComponent> tooltip, ITooltipFlag tooltipFlag) {
|
||||
if (world == null) { return; }
|
||||
String targetUUID = itemStack.getOrCreateTag().getString(TAG_TARGET_UUID);
|
||||
PlayerEntity targetPlayer = null;
|
||||
if (!targetUUID.isEmpty()) {
|
||||
targetPlayer = world.getPlayerByUUID(UUID.fromString(targetUUID));
|
||||
}
|
||||
String targetName = "Target not found";
|
||||
if (targetPlayer != null) {
|
||||
targetName = targetPlayer.getDisplayName().getString();
|
||||
}
|
||||
int targetCharge = itemStack.getOrCreateTag().getInt(TAG_TARGET_CHARGE);
|
||||
int charge = itemStack.getOrCreateTag().getInt(TAG_CHARGE);
|
||||
|
||||
if (Config.Server.getStrawTotemConfig().getIsEnabled()) {
|
||||
if (targetCharge > getMaxCharge() || targetPlayer == null) {
|
||||
tooltip.add(new StringTextComponent(TextFormatting.RED + "Target: " + TextFormatting.DARK_RED + targetName));
|
||||
tooltip.add(new StringTextComponent(TextFormatting.RED + "Required charges: + " + TextFormatting.DARK_RED + targetCharge));
|
||||
} else {
|
||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Target: " + TextFormatting.GRAY + targetName));
|
||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Required charges: " + TextFormatting.GRAY + targetCharge));
|
||||
}
|
||||
tooltip.add(new StringTextComponent(""));
|
||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Charges: " + TextFormatting.GRAY + "(" + charge + "/" + getMaxCharge() + ")"));
|
||||
tooltip.add(new StringTextComponent(""));
|
||||
if (InputMappings.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_KEY_LEFT_SHIFT)) {
|
||||
tooltip.add(new StringTextComponent(TextFormatting.GRAY + "Showing advanced tooltip."));
|
||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "[" + TextFormatting.GRAY + "R-CLICK" + TextFormatting.WHITE + "]"));
|
||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "When second hand is empty: revive target."));
|
||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "When second hand is holding a straw reviving charge: charge totem."));
|
||||
tooltip.add(new StringTextComponent(""));
|
||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "[" + TextFormatting.GRAY + "L-SHIFT + R-CLICK" + TextFormatting.WHITE + "]"));
|
||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Change target."));
|
||||
} else {
|
||||
tooltip.add(new StringTextComponent(TextFormatting.GRAY + "Hold [" + TextFormatting.DARK_GRAY + "L-SHIFT" + TextFormatting.GRAY + "] for advanced tooltip."));
|
||||
}
|
||||
} else {
|
||||
tooltip.add(new StringTextComponent(TextFormatting.RED + "Totem is disabled!"));
|
||||
}
|
||||
super.appendHoverText(itemStack, world, tooltip, tooltipFlag);
|
||||
public boolean isCharge(ItemStack stack) {
|
||||
return (stack.getItem() instanceof StrawChargeItem);
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
@ParametersAreNonnullByDefault
|
||||
public ActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
|
||||
if (!Config.Server.getStrawTotemConfig().getIsEnabled() || !world.isClientSide) { return super.use(world, playerEntity, hand); }
|
||||
ItemStack charge;
|
||||
if (hand.equals(Hand.MAIN_HAND)) {
|
||||
charge = playerEntity.getOffhandItem();
|
||||
} else {
|
||||
charge = playerEntity.getMainHandItem();
|
||||
}
|
||||
|
||||
if (playerEntity.isCrouching()) {
|
||||
Network.channel.sendToServer(new ChangeTargetPacket(hand));
|
||||
} else {
|
||||
if (charge.getItem() instanceof StrawChargeItem) {
|
||||
Network.channel.sendToServer(new ChargeTotemPacket(hand));
|
||||
}
|
||||
Network.channel.sendToServer(new ReviveTargetPacket(hand));
|
||||
}
|
||||
return super.use(world, playerEntity, hand);
|
||||
}
|
||||
|
||||
public static int getMaxCharge() {
|
||||
if (Config.Server.getStrawTotemConfig().getChargeCost() == -1) {
|
||||
return Config.Server.getStrawTotemConfig().getChargeCostLimit();
|
||||
}
|
||||
return Config.Server.getStrawTotemConfig().getChargeCost();
|
||||
}
|
||||
|
||||
public static String getName() { return NAME; }
|
||||
}
|
||||
|
Reference in New Issue
Block a user