From fecf3e58959054f1e199094af619fa7526392855 Mon Sep 17 00:00:00 2001 From: micle Date: Sat, 29 May 2021 21:09:49 +0100 Subject: [PATCH] Changed color of feedback messages from blue and aqua to gray and dark gray. Added a check in C2SRequestPlayerRevive and C2SRequestTotemCharge to check if the totem is a straw one and to do the correct fail chance check. Added functionality to the Straw Totem. Added an onCraftedBy override for the normal totem in case. Fixed a small error caused by right-clicking with any totem when no target was selected. Created extra Utils functions for random numbers in a range. --- .../items/RevivingChargeItem.java | 3 +- .../items/StrawChargeItem.java | 3 +- .../totemofreviving/items/StrawTotemItem.java | 78 ++++++++++++++++++- .../items/TotemOfRevivingItem.java | 6 ++ .../network/C2SRequestPlayerRevive.java | 25 ++++-- .../network/C2SRequestTotemCharge.java | 14 ++++ .../network/C2SRequestTotemTarget.java | 2 +- .../micle/totemofreviving/utils/Utils.java | 12 +++ 8 files changed, 134 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/micle/totemofreviving/items/RevivingChargeItem.java b/src/main/java/com/micle/totemofreviving/items/RevivingChargeItem.java index 6041f37..9560cc1 100755 --- a/src/main/java/com/micle/totemofreviving/items/RevivingChargeItem.java +++ b/src/main/java/com/micle/totemofreviving/items/RevivingChargeItem.java @@ -2,9 +2,10 @@ package com.micle.totemofreviving.items; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; +import net.minecraft.item.Rarity; public class RevivingChargeItem extends Item { public RevivingChargeItem() { - super(new Item.Properties().tab(ItemGroup.TAB_MISC)); + super(new Item.Properties().tab(ItemGroup.TAB_MISC).rarity(Rarity.RARE)); } } diff --git a/src/main/java/com/micle/totemofreviving/items/StrawChargeItem.java b/src/main/java/com/micle/totemofreviving/items/StrawChargeItem.java index 4211fe9..a84f715 100755 --- a/src/main/java/com/micle/totemofreviving/items/StrawChargeItem.java +++ b/src/main/java/com/micle/totemofreviving/items/StrawChargeItem.java @@ -2,9 +2,10 @@ package com.micle.totemofreviving.items; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; +import net.minecraft.item.Rarity; public class StrawChargeItem extends Item { public StrawChargeItem() { - super(new Properties().tab(ItemGroup.TAB_MISC)); + super(new Item.Properties().tab(ItemGroup.TAB_MISC).rarity(Rarity.UNCOMMON)); } } diff --git a/src/main/java/com/micle/totemofreviving/items/StrawTotemItem.java b/src/main/java/com/micle/totemofreviving/items/StrawTotemItem.java index bae3cf8..938d883 100755 --- a/src/main/java/com/micle/totemofreviving/items/StrawTotemItem.java +++ b/src/main/java/com/micle/totemofreviving/items/StrawTotemItem.java @@ -1,10 +1,86 @@ package com.micle.totemofreviving.items; +import com.micle.totemofreviving.TotemOfReviving; +import com.micle.totemofreviving.network.C2SRequestPlayerRevive; +import com.micle.totemofreviving.network.C2SRequestTotemCharge; +import com.micle.totemofreviving.network.C2SRequestTotemTarget; +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.ItemGroup; +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 java.util.List; public class StrawTotemItem extends Item { + public static final String TAG_CHARGE_AMOUNT = "charge"; + public static final String TAG_TARGET_INDEX = "target_index"; + public static final String TAG_TARGET_NAME = "target_name"; + public static final String TAG_FAIL_CHANCE = "fail_chance"; + public static final int STARTING_FAIL_CHANCE = 50; + public StrawTotemItem() { - super(new Properties().tab(ItemGroup.TAB_MISC)); + super(new Item.Properties().tab(ItemGroup.TAB_MISC).stacksTo(1).rarity(Rarity.UNCOMMON)); + } + + @Override + public void appendHoverText(ItemStack stack, World world, List tooltip, ITooltipFlag flag) { + super.appendHoverText(stack, world, tooltip, flag); + tooltip.add(new StringTextComponent( TextFormatting.GOLD + "Charges: " + TextFormatting.GRAY + stack.getOrCreateTag().getInt(TAG_CHARGE_AMOUNT))); + tooltip.add(new StringTextComponent(TextFormatting.GOLD + "Target: " + TextFormatting.GRAY + stack.getOrCreateTag().getString(TAG_TARGET_NAME))); + tooltip.add(new StringTextComponent(TextFormatting.GOLD + "Fail Chance: " + TextFormatting.GRAY + stack.getOrCreateTag().getInt(TAG_FAIL_CHANCE))); + tooltip.add(new StringTextComponent(TextFormatting.GRAY + "\"Feels kinda funky.\"")); + tooltip.add(new StringTextComponent("")); + if (InputMappings.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_KEY_LEFT_SHIFT)) { + tooltip.add(new StringTextComponent(TextFormatting.YELLOW + "R-CLICK")); + tooltip.add(new StringTextComponent(TextFormatting.GOLD + "When other hand is empty: attempt to revive target.")); + tooltip.add(new StringTextComponent(TextFormatting.GOLD + "When other hand has " + TextFormatting.GRAY + "Straw Reviving Charge" + TextFormatting.GOLD + ": charge totem.")); + tooltip.add(new StringTextComponent("")); + tooltip.add(new StringTextComponent(TextFormatting.YELLOW + "SHIFT R-CLICK")); + tooltip.add(new StringTextComponent(TextFormatting.GOLD + "Cycle through available targets.")); + } else { + tooltip.add(new StringTextComponent(TextFormatting.GRAY + "[" + TextFormatting.WHITE + "LSHIFT" + TextFormatting.GRAY + "] for advanced tooltip.")); + } + } + + @Override + public void onCraftedBy(ItemStack stack, World world, PlayerEntity player) { + super.onCraftedBy(stack, world, player); + stack.getOrCreateTag().putInt(TAG_CHARGE_AMOUNT, 0); + stack.getOrCreateTag().putInt(TAG_FAIL_CHANCE, STARTING_FAIL_CHANCE); + } + + @Override + @OnlyIn(Dist.CLIENT) + public ActionResult use(World world, PlayerEntity player, Hand hand) { + if (!world.isClientSide) { return super.use(world, player, hand); } + if (player.isCrouching()) { + TotemOfReviving.INSTANCE.sendToServer(new C2SRequestTotemTarget(player.getUUID(), hand)); + } else { + Hand item_charge_hand = Hand.MAIN_HAND; + if (hand.equals(Hand.MAIN_HAND)) { + item_charge_hand = Hand.OFF_HAND; + } + Item item_charge = player.getItemInHand(item_charge_hand).getItem(); + + if (item_charge instanceof StrawChargeItem) { + TotemOfReviving.INSTANCE.sendToServer(new C2SRequestTotemCharge(player.getUUID(), hand, item_charge_hand)); + } else { + TotemOfReviving.INSTANCE.sendToServer(new C2SRequestPlayerRevive(player.getUUID(), hand)); + } + } + return super.use(world, player, hand); } } diff --git a/src/main/java/com/micle/totemofreviving/items/TotemOfRevivingItem.java b/src/main/java/com/micle/totemofreviving/items/TotemOfRevivingItem.java index 9929a28..37bec95 100755 --- a/src/main/java/com/micle/totemofreviving/items/TotemOfRevivingItem.java +++ b/src/main/java/com/micle/totemofreviving/items/TotemOfRevivingItem.java @@ -52,6 +52,12 @@ public class TotemOfRevivingItem extends Item { } } + @Override + public void onCraftedBy(ItemStack stack, World world, PlayerEntity player) { + super.onCraftedBy(stack, world, player); + stack.getOrCreateTag().putInt(TAG_CHARGE_AMOUNT, 0); + } + @Override @OnlyIn(Dist.CLIENT) public ActionResult use(World world, PlayerEntity player, Hand hand) { diff --git a/src/main/java/com/micle/totemofreviving/network/C2SRequestPlayerRevive.java b/src/main/java/com/micle/totemofreviving/network/C2SRequestPlayerRevive.java index 7a31fa8..429b215 100755 --- a/src/main/java/com/micle/totemofreviving/network/C2SRequestPlayerRevive.java +++ b/src/main/java/com/micle/totemofreviving/network/C2SRequestPlayerRevive.java @@ -1,12 +1,18 @@ package com.micle.totemofreviving.network; import com.micle.totemofreviving.TotemOfReviving; +import com.micle.totemofreviving.items.StrawTotemItem; import com.micle.totemofreviving.items.TotemOfRevivingItem; +import com.micle.totemofreviving.utils.Utils; +import net.minecraft.client.audio.Sound; import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; import net.minecraft.network.PacketBuffer; import net.minecraft.stats.Stats; +import net.minecraft.util.DamageSource; import net.minecraft.util.Hand; +import net.minecraft.util.SoundEvent; import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.TextFormatting; import net.minecraft.world.GameType; @@ -41,7 +47,7 @@ public class C2SRequestPlayerRevive { if (sender == null) { return; } ItemStack item = sender.getItemInHand(msg.hand); - if (item.getOrCreateTag().getInt(TotemOfRevivingItem.TAG_TARGET_INDEX) > TotemOfReviving.players.getPlayerCount()-1) { + if (item.getOrCreateTag().getInt(TotemOfRevivingItem.TAG_TARGET_INDEX) > TotemOfReviving.players.getPlayerCount()-1 || item.getOrCreateTag().getString(TotemOfRevivingItem.TAG_TARGET_NAME).equals("")) { sender.sendMessage(new StringTextComponent(TextFormatting.RED + "Error getting target! (Try selecting the target again)"), sender.getUUID()); } else { ServerPlayerEntity player_to_revive = TotemOfReviving.players.getPlayerByName(item.getOrCreateTag().getString(TotemOfRevivingItem.TAG_TARGET_NAME)); @@ -51,18 +57,27 @@ public class C2SRequestPlayerRevive { if (player_to_revive.isSpectator()) { if (player_to_revive_world.equals(sender_world)) { if (item.getOrCreateTag().getInt(TotemOfRevivingItem.TAG_CHARGE_AMOUNT) >= required_charge) { + if (item.getOrCreateTag().contains(StrawTotemItem.TAG_FAIL_CHANCE)) { + int fail_chance = item.getOrCreateTag().getInt(StrawTotemItem.TAG_FAIL_CHANCE); + if (Utils.randomIntRange(0, 100) <= fail_chance) { + sender.setItemInHand(msg.hand, new ItemStack(Items.AIR)); + sender.hurt(DamageSource.GENERIC, (sender.getHealth() * (fail_chance / 100.0f))); + return; + } + sender.setItemInHand(msg.hand, new ItemStack(Items.AIR)); + } player_to_revive.teleportTo(sender.getX(), sender.getY(), sender.getZ()); player_to_revive.setGameMode(GameType.SURVIVAL); item.getOrCreateTag().putInt(TotemOfRevivingItem.TAG_CHARGE_AMOUNT, item.getOrCreateTag().getInt(TotemOfRevivingItem.TAG_CHARGE_AMOUNT) - required_charge); - sender.sendMessage(new StringTextComponent(TextFormatting.AQUA + "Successfully revived " + TextFormatting.BLUE + player_to_revive.getDisplayName().getString()), sender.getUUID()); + sender.sendMessage(new StringTextComponent(TextFormatting.GRAY + "Successfully revived " + TextFormatting.DARK_GRAY + player_to_revive.getDisplayName().getString()), sender.getUUID()); } else { - sender.sendMessage(new StringTextComponent(TextFormatting.AQUA + "Not enough charge! Required charge is: " + TextFormatting.BLUE + required_charge), sender.getUUID()); + sender.sendMessage(new StringTextComponent(TextFormatting.GRAY + "Not enough charge! Required charge is: " + TextFormatting.DARK_GRAY + required_charge), sender.getUUID()); } } else { - sender.sendMessage(new StringTextComponent(TextFormatting.BLUE + player_to_revive.getDisplayName().getString() + TextFormatting.AQUA + " is not in this dimension!"), sender.getUUID()); + sender.sendMessage(new StringTextComponent(TextFormatting.DARK_GRAY + player_to_revive.getDisplayName().getString() + TextFormatting.GRAY + " is not in this dimension!"), sender.getUUID()); } } else { - sender.sendMessage(new StringTextComponent(TextFormatting.BLUE + player_to_revive.getDisplayName().getString() + TextFormatting.AQUA + " is not dead!"), sender.getUUID()); + sender.sendMessage(new StringTextComponent(TextFormatting.DARK_GRAY + player_to_revive.getDisplayName().getString() + TextFormatting.GRAY + " is not dead!"), sender.getUUID()); } } }); diff --git a/src/main/java/com/micle/totemofreviving/network/C2SRequestTotemCharge.java b/src/main/java/com/micle/totemofreviving/network/C2SRequestTotemCharge.java index f068958..5e921aa 100755 --- a/src/main/java/com/micle/totemofreviving/network/C2SRequestTotemCharge.java +++ b/src/main/java/com/micle/totemofreviving/network/C2SRequestTotemCharge.java @@ -1,10 +1,15 @@ package com.micle.totemofreviving.network; import com.micle.totemofreviving.TotemOfReviving; +import com.micle.totemofreviving.items.StrawTotemItem; import com.micle.totemofreviving.items.TotemOfRevivingItem; +import com.micle.totemofreviving.utils.Utils; +import net.minecraft.block.material.Material; import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; import net.minecraft.network.PacketBuffer; +import net.minecraft.util.DamageSource; import net.minecraft.util.Hand; import net.minecraftforge.fml.network.NetworkEvent; @@ -43,6 +48,15 @@ public class C2SRequestTotemCharge { charge_item.setCount(charge_item.getCount()-1); totem_item.getOrCreateTag().putInt(TotemOfRevivingItem.TAG_CHARGE_AMOUNT, totem_item.getOrCreateTag().getInt(TotemOfRevivingItem.TAG_CHARGE_AMOUNT)+1); + if (totem_item.getOrCreateTag().contains(StrawTotemItem.TAG_FAIL_CHANCE)) { + int fail_chance = totem_item.getOrCreateTag().getInt(StrawTotemItem.TAG_FAIL_CHANCE); + if (Utils.randomIntRange(0, 100) <= fail_chance) { + sender.setItemInHand(msg.hand, new ItemStack(Items.AIR)); + sender.hurt(DamageSource.GENERIC, (sender.getHealth() * (fail_chance / 100.0f))); + } else { + totem_item.getOrCreateTag().putInt(StrawTotemItem.TAG_FAIL_CHANCE, (int)Utils.clamp(fail_chance+(fail_chance/10), 0, 100)); + } + } }); context.setPacketHandled(true); } diff --git a/src/main/java/com/micle/totemofreviving/network/C2SRequestTotemTarget.java b/src/main/java/com/micle/totemofreviving/network/C2SRequestTotemTarget.java index b806647..36d9ad2 100755 --- a/src/main/java/com/micle/totemofreviving/network/C2SRequestTotemTarget.java +++ b/src/main/java/com/micle/totemofreviving/network/C2SRequestTotemTarget.java @@ -45,7 +45,7 @@ public class C2SRequestTotemTarget { item.getOrCreateTag().putInt(TotemOfRevivingItem.TAG_TARGET_INDEX, current_player_index); item.getOrCreateTag().putString(TotemOfRevivingItem.TAG_TARGET_NAME, TotemOfReviving.players.getPlayers().get(current_player_index).getDisplayName().getString()); - sender.sendMessage(new StringTextComponent(TextFormatting.AQUA + "Target: " + TextFormatting.BLUE + item.getOrCreateTag().getString(TotemOfRevivingItem.TAG_TARGET_NAME)), sender.getUUID()); + sender.sendMessage(new StringTextComponent(TextFormatting.GRAY + "Target: " + TextFormatting.DARK_GRAY + item.getOrCreateTag().getString(TotemOfRevivingItem.TAG_TARGET_NAME)), sender.getUUID()); }); context.setPacketHandled(true); } diff --git a/src/main/java/com/micle/totemofreviving/utils/Utils.java b/src/main/java/com/micle/totemofreviving/utils/Utils.java index 9b51873..36bdac6 100755 --- a/src/main/java/com/micle/totemofreviving/utils/Utils.java +++ b/src/main/java/com/micle/totemofreviving/utils/Utils.java @@ -4,4 +4,16 @@ public class Utils { public static float clamp(float val, float min, float max) { return Math.max(min, Math.min(max, val)); } + + public static int randomIntRange(int min, int max) { + return (int) randomDoubleRange(min, max); + } + + public static float randomFloatRange(float min, float max) { + return (float) randomDoubleRange(min, max); + } + + public static double randomDoubleRange(double min, double max) { + return ((Math.random() * (max - min)) + min); + } }