Implemented target changing on totems.
This commit is contained in:
@ -2,6 +2,8 @@ package dev.micle.totemofreviving.item;
|
|||||||
|
|
||||||
import dev.micle.totemofreviving.TotemOfReviving;
|
import dev.micle.totemofreviving.TotemOfReviving;
|
||||||
import dev.micle.totemofreviving.config.Config;
|
import dev.micle.totemofreviving.config.Config;
|
||||||
|
import dev.micle.totemofreviving.network.ChangeTargetPacket;
|
||||||
|
import dev.micle.totemofreviving.network.Network;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.util.ITooltipFlag;
|
import net.minecraft.client.util.ITooltipFlag;
|
||||||
import net.minecraft.client.util.InputMappings;
|
import net.minecraft.client.util.InputMappings;
|
||||||
@ -22,13 +24,15 @@ import org.lwjgl.glfw.GLFW;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class StrawTotemItem extends Item {
|
public class StrawTotemItem extends Item {
|
||||||
private static final String NAME = "straw_totem";
|
private static final String NAME = "straw_totem";
|
||||||
|
|
||||||
private static final String TAG_TARGET_NAME = "target_uuid";
|
public static final String TAG_TARGET_INDEX = "target_index";
|
||||||
private static final String TAG_TARGET_CHARGE = "target_cost";
|
public static final String TAG_TARGET_UUID = "target_uuid";
|
||||||
private static final String TAG_CHARGE= "charge";
|
public static final String TAG_TARGET_CHARGE = "target_cost";
|
||||||
|
public static final String TAG_CHARGE= "charge";
|
||||||
|
|
||||||
public StrawTotemItem() {
|
public StrawTotemItem() {
|
||||||
super(new Item.Properties().tab(TotemOfReviving.ITEM_GROUP).stacksTo(1).rarity(Rarity.UNCOMMON).defaultDurability(Config.Server.getStrawTotemConfig().getDurability()));
|
super(new Item.Properties().tab(TotemOfReviving.ITEM_GROUP).stacksTo(1).rarity(Rarity.UNCOMMON).defaultDurability(Config.Server.getStrawTotemConfig().getDurability()));
|
||||||
@ -38,50 +42,70 @@ public class StrawTotemItem extends Item {
|
|||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
@ParametersAreNonnullByDefault
|
@ParametersAreNonnullByDefault
|
||||||
public void appendHoverText(ItemStack itemStack, @Nullable World world, List<ITextComponent> tooltip, ITooltipFlag tooltipFlag) {
|
public void appendHoverText(ItemStack itemStack, @Nullable World world, List<ITextComponent> tooltip, ITooltipFlag tooltipFlag) {
|
||||||
super.appendHoverText(itemStack, world, tooltip, tooltipFlag);
|
|
||||||
if (world == null) { return; }
|
if (world == null) { return; }
|
||||||
String target_uuid = itemStack.getOrCreateTag().getString(TAG_TARGET_NAME);
|
String targetUUID = itemStack.getOrCreateTag().getString(TAG_TARGET_UUID);
|
||||||
int target_charge = itemStack.getOrCreateTag().getInt(TAG_TARGET_CHARGE);
|
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);
|
int charge = itemStack.getOrCreateTag().getInt(TAG_CHARGE);
|
||||||
|
|
||||||
if (target_charge > getMaxCharge()) {
|
if (Config.Server.getStrawTotemConfig().getIsEnabled()) {
|
||||||
tooltip.add(new StringTextComponent(TextFormatting.RED + "Target: " + TextFormatting.DARK_RED + target_uuid));
|
if (targetCharge > getMaxCharge() || targetPlayer == null) {
|
||||||
tooltip.add(new StringTextComponent(TextFormatting.RED + "Required charges: + " + TextFormatting.DARK_RED + target_charge));
|
tooltip.add(new StringTextComponent(TextFormatting.RED + "Target: " + TextFormatting.DARK_RED + targetName));
|
||||||
} else {
|
tooltip.add(new StringTextComponent(TextFormatting.RED + "Required charges: + " + TextFormatting.DARK_RED + targetCharge));
|
||||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Target: " + TextFormatting.GRAY + target_uuid));
|
} else {
|
||||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Required charges: " + TextFormatting.GRAY + target_charge));
|
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(""));
|
||||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "[" + TextFormatting.GRAY + "L-SHIFT + R-CLICK" + TextFormatting.WHITE + "]"));
|
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Charges: " + TextFormatting.GRAY + "(" + charge + "/" + getMaxCharge() + ")"));
|
||||||
tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Change target."));
|
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 {
|
} else {
|
||||||
tooltip.add(new StringTextComponent(TextFormatting.GRAY + "Hold [" + TextFormatting.DARK_GRAY + "L-SHIFT" + TextFormatting.GRAY + "] for advanced tooltip."));
|
tooltip.add(new StringTextComponent(TextFormatting.RED + "Totem is disabled!"));
|
||||||
}
|
}
|
||||||
|
super.appendHoverText(itemStack, world, tooltip, tooltipFlag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
@ParametersAreNonnullByDefault
|
@ParametersAreNonnullByDefault
|
||||||
public ActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
|
public ActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
|
||||||
if (world.isClientSide) return super.use(world, playerEntity, hand);
|
if (!Config.Server.getStrawTotemConfig().getIsEnabled() || world.isClientSide) { return super.use(world, playerEntity, hand); }
|
||||||
if (playerEntity.isCrouching()) {
|
ItemStack totem;
|
||||||
ItemStack itemStack;
|
ItemStack charge;
|
||||||
if (hand.equals(Hand.MAIN_HAND)) {
|
if (hand.equals(Hand.MAIN_HAND)) {
|
||||||
itemStack = playerEntity.getMainHandItem();
|
totem = playerEntity.getMainHandItem();
|
||||||
} else {
|
charge = playerEntity.getOffhandItem();
|
||||||
itemStack = playerEntity.getOffhandItem();
|
|
||||||
}
|
|
||||||
itemStack.hurtAndBreak(1, playerEntity, e -> e.broadcastBreakEvent(hand));
|
|
||||||
} else {
|
} else {
|
||||||
|
totem = playerEntity.getOffhandItem();
|
||||||
|
charge = playerEntity.getMainHandItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (playerEntity.isCrouching()) {
|
||||||
|
Network.channel.sendToServer(new ChangeTargetPacket(hand));
|
||||||
|
} else {
|
||||||
|
if (charge.getItem() instanceof StrawChargeItem) {
|
||||||
|
// Charge totem
|
||||||
|
}
|
||||||
|
// Revive target
|
||||||
|
//totem.hurtAndBreak(1, sender, e -> e.broadcastBreakEvent(packet.hand));
|
||||||
}
|
}
|
||||||
return super.use(world, playerEntity, hand);
|
return super.use(world, playerEntity, hand);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,57 @@
|
|||||||
|
package dev.micle.totemofreviving.network;
|
||||||
|
|
||||||
|
import dev.micle.totemofreviving.TotemOfReviving;
|
||||||
|
import dev.micle.totemofreviving.item.StrawTotemItem;
|
||||||
|
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.network.PacketBuffer;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.server.management.PlayerList;
|
||||||
|
import net.minecraft.stats.Stats;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
|
import net.minecraftforge.fml.network.NetworkEvent;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import static dev.micle.totemofreviving.item.StrawTotemItem.*;
|
||||||
|
|
||||||
|
public class ChangeTargetPacket {
|
||||||
|
private final Hand hand;
|
||||||
|
|
||||||
|
public ChangeTargetPacket(final Hand hand) {
|
||||||
|
this.hand = hand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void encode(final ChangeTargetPacket packet, final PacketBuffer buffer) {
|
||||||
|
Network.writeVersionInfo(buffer);
|
||||||
|
buffer.writeEnum(packet.hand);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ChangeTargetPacket decode(final PacketBuffer buffer) {
|
||||||
|
Network.checkVersion(buffer);
|
||||||
|
return new ChangeTargetPacket(buffer.readEnum(Hand.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void handle(final ChangeTargetPacket packet, final Supplier<NetworkEvent.Context> contextSupplier) {
|
||||||
|
final NetworkEvent.Context context = contextSupplier.get();
|
||||||
|
context.enqueueWork(() -> {
|
||||||
|
ServerPlayerEntity sender = context.getSender();
|
||||||
|
if (sender == null) { return; }
|
||||||
|
ItemStack totem = sender.getItemInHand(packet.hand);
|
||||||
|
|
||||||
|
PlayerList playerList = TotemOfReviving.PROXY.getServer().getPlayerList();
|
||||||
|
|
||||||
|
int targetIndex = totem.getOrCreateTag().getInt(TAG_TARGET_INDEX) + 1;
|
||||||
|
if (targetIndex > playerList.getPlayerCount()-1) {
|
||||||
|
targetIndex = 0;
|
||||||
|
}
|
||||||
|
ServerPlayerEntity target = playerList.getPlayers().get(targetIndex);
|
||||||
|
|
||||||
|
totem.getOrCreateTag().putInt(TAG_TARGET_INDEX, targetIndex);
|
||||||
|
totem.getOrCreateTag().putString(TAG_TARGET_UUID, target.getStringUUID());
|
||||||
|
totem.getOrCreateTag().putInt(TAG_TARGET_CHARGE, target.getStats().getValue(Stats.CUSTOM.get(Stats.DEATHS)));
|
||||||
|
});
|
||||||
|
context.setPacketHandled(true);
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,11 @@ public class Network {
|
|||||||
.simpleChannel();
|
.simpleChannel();
|
||||||
|
|
||||||
int id = 0;
|
int id = 0;
|
||||||
|
channel.messageBuilder(ChangeTargetPacket.class, id++)
|
||||||
|
.encoder(ChangeTargetPacket::encode)
|
||||||
|
.decoder(ChangeTargetPacket::decode)
|
||||||
|
.consumer(ChangeTargetPacket::handle)
|
||||||
|
.add();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeVersionInfo(PacketBuffer buffer) {
|
public static void writeVersionInfo(PacketBuffer buffer) {
|
||||||
|
Reference in New Issue
Block a user