diff --git a/net/minecraft/world/entity/player/Inventory.java b/net/minecraft/world/entity/player/Inventory.java deleted file mode 100644 index ab7c7e4..0000000 --- a/net/minecraft/world/entity/player/Inventory.java +++ /dev/null @@ -1,618 +0,0 @@ -package net.minecraft.world.entity.player; - -import com.google.common.collect.ImmutableList; -import java.util.List; -import java.util.function.Predicate; -import net.minecraft.CrashReport; -import net.minecraft.CrashReportCategory; -import net.minecraft.ReportedException; -import net.minecraft.core.NonNullList; -import net.minecraft.core.component.DataComponents; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.network.chat.Component; -import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.tags.TagKey; -import net.minecraft.world.Container; -import net.minecraft.world.ContainerHelper; -import net.minecraft.world.Nameable; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.block.state.BlockState; - -public class Inventory implements Container, Nameable { - public static final int POP_TIME_DURATION = 5; - public static final int INVENTORY_SIZE = 36; - private static final int SELECTION_SIZE = 9; - public static final int SLOT_OFFHAND = 40; - public static final int NOT_FOUND_INDEX = -1; - public static final int[] ALL_ARMOR_SLOTS = new int[]{0, 1, 2, 3}; - public static final int[] HELMET_SLOT_ONLY = new int[]{3}; - public final NonNullList items = NonNullList.withSize(36, ItemStack.EMPTY); - public final NonNullList armor = NonNullList.withSize(4, ItemStack.EMPTY); - public final NonNullList offhand = NonNullList.withSize(1, ItemStack.EMPTY); - private final List> compartments = ImmutableList.of(this.items, this.armor, this.offhand); - public int selected; - public final Player player; - private int timesChanged; - - public Inventory(Player player) { - this.player = player; - } - - public ItemStack getSelected() { - return isHotbarSlot(this.selected) ? this.items.get(this.selected) : ItemStack.EMPTY; - } - - public static int getSelectionSize() { - return 9; - } - - private boolean hasRemainingSpaceForItem(ItemStack destination, ItemStack origin) { - return !destination.isEmpty() - && ItemStack.isSameItemSameComponents(destination, origin) - && destination.isStackable() - && destination.getCount() < this.getMaxStackSize(destination); - } - - public int getFreeSlot() { - for (int i = 0; i < this.items.size(); i++) { - if (this.items.get(i).isEmpty()) { - return i; - } - } - - return -1; - } - - public void setPickedItem(ItemStack stack) { - int i = this.findSlotMatchingItem(stack); - if (isHotbarSlot(i)) { - this.selected = i; - } else { - if (i == -1) { - this.selected = this.getSuitableHotbarSlot(); - if (!this.items.get(this.selected).isEmpty()) { - int j = this.getFreeSlot(); - if (j != -1) { - this.items.set(j, this.items.get(this.selected)); - } - } - - this.items.set(this.selected, stack); - } else { - this.pickSlot(i); - } - } - } - - public void pickSlot(int index) { - this.selected = this.getSuitableHotbarSlot(); - ItemStack itemstack = this.items.get(this.selected); - this.items.set(this.selected, this.items.get(index)); - this.items.set(index, itemstack); - } - - public static boolean isHotbarSlot(int index) { - return index >= 0 && index < 9; - } - - /** - * Finds the stack or an equivalent one in the main inventory - */ - public int findSlotMatchingItem(ItemStack stack) { - for (int i = 0; i < this.items.size(); i++) { - if (!this.items.get(i).isEmpty() && ItemStack.isSameItemSameComponents(stack, this.items.get(i))) { - return i; - } - } - - return -1; - } - - public int findSlotMatchingUnusedItem(ItemStack stack) { - for (int i = 0; i < this.items.size(); i++) { - ItemStack itemstack = this.items.get(i); - if (!itemstack.isEmpty() - && ItemStack.isSameItemSameComponents(stack, itemstack) - && !itemstack.isDamaged() - && !itemstack.isEnchanted() - && !itemstack.has(DataComponents.CUSTOM_NAME)) { - return i; - } - } - - return -1; - } - - public int getSuitableHotbarSlot() { - for (int i = 0; i < 9; i++) { - int j = (this.selected + i) % 9; - if (this.items.get(j).isEmpty()) { - return j; - } - } - - for (int k = 0; k < 9; k++) { - int l = (this.selected + k) % 9; - if (!this.items.get(l).isNotReplaceableByPickAction(this.player, l)) { - return l; - } - } - - return this.selected; - } - - /** - * Change the selected item in the hotbar after a mouse scroll. Select the slot to the left if {@code direction} is positive, or to the right if negative. - */ - public void swapPaint(double direction) { - int i = (int)Math.signum(direction); - this.selected -= i; - - while (this.selected < 0) { - this.selected += 9; - } - - while (this.selected >= 9) { - this.selected -= 9; - } - } - - public int clearOrCountMatchingItems(Predicate stackPredicate, int maxCount, Container inventory) { - int i = 0; - boolean flag = maxCount == 0; - i += ContainerHelper.clearOrCountMatchingItems(this, stackPredicate, maxCount - i, flag); - i += ContainerHelper.clearOrCountMatchingItems(inventory, stackPredicate, maxCount - i, flag); - ItemStack itemstack = this.player.containerMenu.getCarried(); - i += ContainerHelper.clearOrCountMatchingItems(itemstack, stackPredicate, maxCount - i, flag); - if (itemstack.isEmpty()) { - this.player.containerMenu.setCarried(ItemStack.EMPTY); - } - - return i; - } - - /** - * This function stores as many items of an ItemStack as possible in a matching slot and returns the quantity of left over items. - */ - private int addResource(ItemStack stack) { - int i = this.getSlotWithRemainingSpace(stack); - if (i == -1) { - i = this.getFreeSlot(); - } - - return i == -1 ? stack.getCount() : this.addResource(i, stack); - } - - private int addResource(int slot, ItemStack stack) { - int i = stack.getCount(); - ItemStack itemstack = this.getItem(slot); - if (itemstack.isEmpty()) { - itemstack = stack.copyWithCount(0); - this.setItem(slot, itemstack); - } - - int j = this.getMaxStackSize(itemstack) - itemstack.getCount(); - int k = Math.min(i, j); - if (k == 0) { - return i; - } else { - i -= k; - itemstack.grow(k); - itemstack.setPopTime(5); - return i; - } - } - - /** - * Stores a stack in the player's inventory. It first tries to place it in the selected slot in the player's hotbar, then the offhand slot, then any available/empty slot in the player's inventory. - */ - public int getSlotWithRemainingSpace(ItemStack stack) { - if (this.hasRemainingSpaceForItem(this.getItem(this.selected), stack)) { - return this.selected; - } else if (this.hasRemainingSpaceForItem(this.getItem(40), stack)) { - return 40; - } else { - for (int i = 0; i < this.items.size(); i++) { - if (this.hasRemainingSpaceForItem(this.items.get(i), stack)) { - return i; - } - } - - return -1; - } - } - - public void tick() { - int slot = 0; - for (NonNullList nonnulllist : this.compartments) { - for (int i = 0; i < nonnulllist.size(); i++) { - if (!nonnulllist.get(i).isEmpty()) { - // Neo: Fix the slot param to be the global index instead of the per-compartment index. - // Neo: Fix the selected param to only be true for hotbar slots. - nonnulllist.get(i).inventoryTick(this.player.level(), this.player, slot, this.selected == slot); - } - slot++; - } - } - } - - /** - * Adds the stack to the first empty slot in the player's inventory. Returns {@code false} if it's not possible to place the entire stack in the inventory. - */ - public boolean add(ItemStack stack) { - return this.add(-1, stack); - } - - /** - * Adds the stack to the specified slot in the player's inventory. Returns {@code false} if it's not possible to place the entire stack in the inventory. - */ - public boolean add(int slot, ItemStack stack) { - if (stack.isEmpty()) { - return false; - } else { - try { - if (stack.isDamaged()) { - if (slot == -1) { - slot = this.getFreeSlot(); - } - - if (slot >= 0) { - this.items.set(slot, stack.copyAndClear()); - this.items.get(slot).setPopTime(5); - return true; - } else if (this.player.hasInfiniteMaterials()) { - stack.setCount(0); - return true; - } else { - return false; - } - } else { - int i; - do { - i = stack.getCount(); - if (slot == -1) { - stack.setCount(this.addResource(stack)); - } else { - stack.setCount(this.addResource(slot, stack)); - } - } while (!stack.isEmpty() && stack.getCount() < i); - - if (stack.getCount() == i && this.player.hasInfiniteMaterials()) { - stack.setCount(0); - return true; - } else { - return stack.getCount() < i; - } - } - } catch (Throwable throwable) { - CrashReport crashreport = CrashReport.forThrowable(throwable, "Adding item to inventory"); - CrashReportCategory crashreportcategory = crashreport.addCategory("Item being added"); - crashreportcategory.setDetail("Registry Name", () -> String.valueOf(net.minecraft.core.registries.BuiltInRegistries.ITEM.getKey(stack.getItem()))); - crashreportcategory.setDetail("Item Class", () -> stack.getItem().getClass().getName()); - crashreportcategory.setDetail("Item ID", Item.getId(stack.getItem())); - crashreportcategory.setDetail("Item data", stack.getDamageValue()); - crashreportcategory.setDetail("Item name", () -> stack.getHoverName().getString()); - throw new ReportedException(crashreport); - } - } - } - - public void placeItemBackInInventory(ItemStack stack) { - this.placeItemBackInInventory(stack, true); - } - - public void placeItemBackInInventory(ItemStack stack, boolean sendPacket) { - while (!stack.isEmpty()) { - int i = this.getSlotWithRemainingSpace(stack); - if (i == -1) { - i = this.getFreeSlot(); - } - - if (i == -1) { - this.player.drop(stack, false); - break; - } - - int j = stack.getMaxStackSize() - this.getItem(i).getCount(); - if (this.add(i, stack.split(j)) && sendPacket && this.player instanceof ServerPlayer) { - ((ServerPlayer)this.player).connection.send(new ClientboundContainerSetSlotPacket(-2, 0, i, this.getItem(i))); - } - } - } - - /** - * Removes up to a specified number of items from an inventory slot and returns them in a new stack. - */ - @Override - public ItemStack removeItem(int index, int count) { - List list = null; - - for (NonNullList nonnulllist : this.compartments) { - if (index < nonnulllist.size()) { - list = nonnulllist; - break; - } - - index -= nonnulllist.size(); - } - - return list != null && !list.get(index).isEmpty() ? ContainerHelper.removeItem(list, index, count) : ItemStack.EMPTY; - } - - public void removeItem(ItemStack stack) { - for (NonNullList nonnulllist : this.compartments) { - for (int i = 0; i < nonnulllist.size(); i++) { - if (nonnulllist.get(i) == stack) { - nonnulllist.set(i, ItemStack.EMPTY); - break; - } - } - } - } - - /** - * Removes a stack from the given slot and returns it. - */ - @Override - public ItemStack removeItemNoUpdate(int index) { - NonNullList nonnulllist = null; - - for (NonNullList nonnulllist1 : this.compartments) { - if (index < nonnulllist1.size()) { - nonnulllist = nonnulllist1; - break; - } - - index -= nonnulllist1.size(); - } - - if (nonnulllist != null && !nonnulllist.get(index).isEmpty()) { - ItemStack itemstack = nonnulllist.get(index); - nonnulllist.set(index, ItemStack.EMPTY); - return itemstack; - } else { - return ItemStack.EMPTY; - } - } - - /** - * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections). - */ - @Override - public void setItem(int index, ItemStack stack) { - NonNullList nonnulllist = null; - - for (NonNullList nonnulllist1 : this.compartments) { - if (index < nonnulllist1.size()) { - nonnulllist = nonnulllist1; - break; - } - - index -= nonnulllist1.size(); - } - - if (nonnulllist != null) { - nonnulllist.set(index, stack); - } - } - - public float getDestroySpeed(BlockState state) { - return this.items.get(this.selected).getDestroySpeed(state); - } - - /** - * Writes the inventory out as a list of compound tags. This is where the slot indices are used (+100 for armor, +80 for crafting). - */ - public ListTag save(ListTag listTag) { - for (int i = 0; i < this.items.size(); i++) { - if (!this.items.get(i).isEmpty()) { - CompoundTag compoundtag = new CompoundTag(); - compoundtag.putByte("Slot", (byte)i); - listTag.add(this.items.get(i).save(this.player.registryAccess(), compoundtag)); - } - } - - for (int j = 0; j < this.armor.size(); j++) { - if (!this.armor.get(j).isEmpty()) { - CompoundTag compoundtag1 = new CompoundTag(); - compoundtag1.putByte("Slot", (byte)(j + 100)); - listTag.add(this.armor.get(j).save(this.player.registryAccess(), compoundtag1)); - } - } - - for (int k = 0; k < this.offhand.size(); k++) { - if (!this.offhand.get(k).isEmpty()) { - CompoundTag compoundtag2 = new CompoundTag(); - compoundtag2.putByte("Slot", (byte)(k + 150)); - listTag.add(this.offhand.get(k).save(this.player.registryAccess(), compoundtag2)); - } - } - - return listTag; - } - - /** - * Reads from the given tag list and fills the slots in the inventory with the correct items. - */ - public void load(ListTag listTag) { - this.items.clear(); - this.armor.clear(); - this.offhand.clear(); - - for (int i = 0; i < listTag.size(); i++) { - CompoundTag compoundtag = listTag.getCompound(i); - int j = compoundtag.getByte("Slot") & 255; - ItemStack itemstack = ItemStack.parse(this.player.registryAccess(), compoundtag).orElse(ItemStack.EMPTY); - if (j >= 0 && j < this.items.size()) { - this.items.set(j, itemstack); - } else if (j >= 100 && j < this.armor.size() + 100) { - this.armor.set(j - 100, itemstack); - } else if (j >= 150 && j < this.offhand.size() + 150) { - this.offhand.set(j - 150, itemstack); - } - } - } - - @Override - public int getContainerSize() { - return this.items.size() + this.armor.size() + this.offhand.size(); - } - - @Override - public boolean isEmpty() { - for (ItemStack itemstack : this.items) { - if (!itemstack.isEmpty()) { - return false; - } - } - - for (ItemStack itemstack1 : this.armor) { - if (!itemstack1.isEmpty()) { - return false; - } - } - - for (ItemStack itemstack2 : this.offhand) { - if (!itemstack2.isEmpty()) { - return false; - } - } - - return true; - } - - /** - * Returns the stack in the given slot. - */ - @Override - public ItemStack getItem(int index) { - List list = null; - - for (NonNullList nonnulllist : this.compartments) { - if (index < nonnulllist.size()) { - list = nonnulllist; - break; - } - - index -= nonnulllist.size(); - } - - return list == null ? ItemStack.EMPTY : list.get(index); - } - - @Override - public Component getName() { - return Component.translatable("container.inventory"); - } - - /** - * @return a player armor item (as an {@code ItemStack}) contained in specified armor slot - */ - public ItemStack getArmor(int slot) { - return this.armor.get(slot); - } - - public void dropAll() { - for (List list : this.compartments) { - for (int i = 0; i < list.size(); i++) { - ItemStack itemstack = list.get(i); - if (!itemstack.isEmpty()) { - this.player.drop(itemstack, true, false); - list.set(i, ItemStack.EMPTY); - } - } - } - } - - @Override - public void setChanged() { - this.timesChanged++; - } - - public int getTimesChanged() { - return this.timesChanged; - } - - /** - * Don't rename this method to canInteractWith due to conflicts with Container - */ - @Override - public boolean stillValid(Player player) { - return player.canInteractWithEntity(this.player, 4.0); - } - - /** - * Returns {@code true} if the specified {@link net.minecraft.world.item.ItemStack} exists in the inventory. - */ - public boolean contains(ItemStack stack) { - for (List list : this.compartments) { - for (ItemStack itemstack : list) { - if (!itemstack.isEmpty() && ItemStack.isSameItemSameComponents(itemstack, stack)) { - return true; - } - } - } - - return false; - } - - public boolean contains(TagKey tag) { - for (List list : this.compartments) { - for (ItemStack itemstack : list) { - if (!itemstack.isEmpty() && itemstack.is(tag)) { - return true; - } - } - } - - return false; - } - - public boolean contains(Predicate predicate) { - for (List list : this.compartments) { - for (ItemStack itemstack : list) { - if (predicate.test(itemstack)) { - return true; - } - } - } - - return false; - } - - /** - * Copy the ItemStack contents from another InventoryPlayer instance - */ - public void replaceWith(Inventory playerInventory) { - for (int i = 0; i < this.getContainerSize(); i++) { - this.setItem(i, playerInventory.getItem(i)); - } - - this.selected = playerInventory.selected; - } - - @Override - public void clearContent() { - for (List list : this.compartments) { - list.clear(); - } - } - - public void fillStackedContents(StackedContents stackedContent) { - for (ItemStack itemstack : this.items) { - stackedContent.accountSimpleStack(itemstack); - } - } - - /** - * @param removeStack Whether to remove the entire stack of items. If {@code false - * }, removes a single item. - */ - public ItemStack removeFromSelected(boolean removeStack) { - ItemStack itemstack = this.getSelected(); - return itemstack.isEmpty() ? ItemStack.EMPTY : this.removeItem(this.selected, removeStack ? itemstack.getCount() : 1); - } -} diff --git a/src/main/java/com/lzxnone/terraria/client/event/InputHandler.java b/src/main/java/com/lzxnone/terraria/client/event/InputHandler.java index 1b3239b..a9f3ec1 100644 --- a/src/main/java/com/lzxnone/terraria/client/event/InputHandler.java +++ b/src/main/java/com/lzxnone/terraria/client/event/InputHandler.java @@ -9,17 +9,20 @@ import com.lzxnone.terraria.item.weapon.melee.DevilsDevastation; import com.lzxnone.terraria.item.weapon.melee.FirstFractal; import com.lzxnone.terraria.item.weapon.melee.TrueCopperShortsword; import com.lzxnone.terraria.item.weapon.melee.Zenith; +import com.lzxnone.terraria.item.weapon.ranged.SniperRifle; import com.lzxnone.terraria.network.payload.ClearSummonPayload; import com.lzxnone.terraria.network.payload.DevilsDevastationKillModePayload; import com.lzxnone.terraria.network.payload.FirstFractalScrollPayload; import com.lzxnone.terraria.network.payload.ToggleSummonFreeTargetingPayload; import com.lzxnone.terraria.network.payload.ZenithScrollPayload; +import com.lzxnone.terraria.utils.SoundUtil; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.component.DataComponents; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; +import net.minecraft.sounds.SoundEvents; import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -67,6 +70,26 @@ public class InputHandler { PacketDistributor.sendToServer(new ZenithScrollPayload(scrollDelta)); } } + }else if(player.getUseItem().getItem() instanceof SniperRifle) { + if(player.isUsingItem()) { + double scrollDelta = event.getScrollDeltaY(); + if(scrollDelta != 0) { + event.setCanceled(true); + ItemStack useStack = player.getUseItem(); + double zoom = useStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getDouble("zoom") + scrollDelta * SniperRifle.getZoomScrollSpeed(); + if(zoom > SniperRifle.MAX_ZOOM) zoom = SniperRifle.MAX_ZOOM; + else if(zoom < 1) zoom = 1; + double finalZoom = zoom; + CustomData.update(DataComponents.CUSTOM_DATA, useStack, tag -> tag.putDouble("zoom", finalZoom)); + + MutableComponent displayText = Component.empty(); + displayText.append(Component.translatable("tooltip.lzxnoneterraria.zoom").withStyle(ChatFormatting.GREEN, ChatFormatting.BOLD)) + .append(Component.literal(": ").withStyle(ChatFormatting.GREEN, ChatFormatting.BOLD)) + .append(Component.literal(String.format("x%.1f", finalZoom)).withStyle(ChatFormatting.GREEN, ChatFormatting.BOLD)); + player.displayClientMessage(displayText, true); + SoundUtil.playClientSound(player, SoundEvents.SPYGLASS_USE, 1.0f, 1.0f); + } + } }else if(mainHand.getItem() instanceof FirstFractal) { if(player.isUsingItem() && (player.getUseItem().is(ModItems.FIRST_FRACTAL.get()))) { double scrollDelta = event.getScrollDeltaY(); diff --git a/src/main/java/com/lzxnone/terraria/client/event/RangedWeaponGuiHandler.java b/src/main/java/com/lzxnone/terraria/client/event/RangedWeaponGuiHandler.java index 94c4cb7..f0270b1 100644 --- a/src/main/java/com/lzxnone/terraria/client/event/RangedWeaponGuiHandler.java +++ b/src/main/java/com/lzxnone/terraria/client/event/RangedWeaponGuiHandler.java @@ -88,6 +88,7 @@ public class RangedWeaponGuiHandler { guiGraphics.pose().scale(0.25F, 0.25F, 1.0F); guiGraphics.renderItem(ammoStack, 0, 0); guiGraphics.pose().popPose(); + guiGraphics.flush(); if(player != null) { renderAmmoCount(guiGraphics, rangedWeapon, weaponStack, player, x, y); @@ -100,15 +101,16 @@ public class RangedWeaponGuiHandler { String text = Integer.toString(count); Minecraft minecraft = Minecraft.getInstance(); Font font = minecraft.font; - float scale = 0.75F; + float scale = 0.5F; int textWidth = font.width(text); - float textX = (iconX - 2) / scale - textWidth; - float textY = (iconY - 3) / scale; + float textX = iconX - textWidth * scale; + float textY = iconY; guiGraphics.pose().pushPose(); - guiGraphics.pose().translate(0.0F, 0.0F, 300.0F); + guiGraphics.pose().translate(textX, textY, 500.0F); guiGraphics.pose().scale(scale, scale, 1.0F); - guiGraphics.drawString(font, text, (int) textX, (int) textY, 0xFFFFFF, true); + guiGraphics.drawString(font, text, 0, 0, 0xFFFFFF, true); guiGraphics.pose().popPose(); + guiGraphics.flush(); } } diff --git a/src/main/java/com/lzxnone/terraria/client/event/SniperScopeHandler.java b/src/main/java/com/lzxnone/terraria/client/event/SniperScopeHandler.java new file mode 100644 index 0000000..711d8c6 --- /dev/null +++ b/src/main/java/com/lzxnone/terraria/client/event/SniperScopeHandler.java @@ -0,0 +1,74 @@ +package com.lzxnone.terraria.client.event; + +import com.lzxnone.terraria.LzxnoneTerraria; +import com.lzxnone.terraria.item.ModItems; +import com.lzxnone.terraria.item.weapon.ranged.SniperRifle; +import com.mojang.blaze3d.systems.RenderSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.core.component.DataComponents; +import net.minecraft.util.Mth; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.CustomData; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.client.event.ComputeFovModifierEvent; +import net.neoforged.neoforge.client.event.RenderGuiEvent; + +@EventBusSubscriber(modid = LzxnoneTerraria.MODID, value = Dist.CLIENT) +public class SniperScopeHandler { + @SubscribeEvent + public static void onComputeFov(ComputeFovModifierEvent event) { + ItemStack stack = getActiveSniperStack(); + if(stack.isEmpty()) return; + + double zoom = stack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getDouble("zoom"); + if(zoom <= 1) return; + event.setNewFovModifier((float)(event.getNewFovModifier() / zoom)); + } + + @SubscribeEvent + public static void onRenderGui(RenderGuiEvent.Post event) { + Minecraft minecraft = Minecraft.getInstance(); + if(minecraft.options.hideGui) return; + + ItemStack stack = getActiveSniperStack(); + if(stack.isEmpty()) return; + double zoom = stack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getDouble("zoom"); + if(zoom <= 1) return; + renderSpyglassOverlay(event.getGuiGraphics(), 1.0F); + } + + private static ItemStack getActiveSniperStack() { + Minecraft minecraft = Minecraft.getInstance(); + LocalPlayer player = minecraft.player; + if(player == null || !player.isUsingItem()) return ItemStack.EMPTY; + + ItemStack stack = player.getUseItem(); + if(stack.is(ModItems.SNIPER_RIFLE)) return stack; + return ItemStack.EMPTY; + } + + private static void renderSpyglassOverlay(GuiGraphics guiGraphics, float scopeScale) { + float minSize = (float)Math.min(guiGraphics.guiWidth(), guiGraphics.guiHeight()); + float scale = Math.min((float)guiGraphics.guiWidth() / minSize, (float)guiGraphics.guiHeight() / minSize) * scopeScale; + int width = Mth.floor(minSize * scale); + int height = Mth.floor(minSize * scale); + int x = (guiGraphics.guiWidth() - width) / 2; + int y = (guiGraphics.guiHeight() - height) / 2; + int right = x + width; + int bottom = y + height; + + RenderSystem.enableBlend(); + guiGraphics.blit(Gui.SPYGLASS_SCOPE_LOCATION, x, y, -90, 0.0F, 0.0F, width, height, width, height); + RenderSystem.disableBlend(); + guiGraphics.fill(RenderType.guiOverlay(), 0, bottom, guiGraphics.guiWidth(), guiGraphics.guiHeight(), -90, 0xFF000000); + guiGraphics.fill(RenderType.guiOverlay(), 0, 0, guiGraphics.guiWidth(), y, -90, 0xFF000000); + guiGraphics.fill(RenderType.guiOverlay(), 0, y, x, bottom, -90, 0xFF000000); + guiGraphics.fill(RenderType.guiOverlay(), right, y, guiGraphics.guiWidth(), bottom, -90, 0xFF000000); + } +} diff --git a/src/main/java/com/lzxnone/terraria/item/weapon/ranged/SniperRifle.java b/src/main/java/com/lzxnone/terraria/item/weapon/ranged/SniperRifle.java index a7c7eea..03bc99f 100644 --- a/src/main/java/com/lzxnone/terraria/item/weapon/ranged/SniperRifle.java +++ b/src/main/java/com/lzxnone/terraria/item/weapon/ranged/SniperRifle.java @@ -4,17 +4,23 @@ import com.lzxnone.terraria.ModSounds; import com.lzxnone.terraria.entity.summon.StaticSummon; import com.lzxnone.terraria.item.ModItems; import com.lzxnone.terraria.item.weapon.RangedWeapon; +import com.lzxnone.terraria.ui.config.ConfigFactory; import com.lzxnone.terraria.ui.config.ConfigListItem; +import com.lzxnone.terraria.ui.config.ConfigUtil; import com.lzxnone.terraria.ui.config.IConfigData; import com.lzxnone.terraria.utils.AmmoUtil; import com.lzxnone.terraria.utils.SoundUtil; +import net.minecraft.core.component.DataComponents; import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; import net.minecraft.world.InteractionHand; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Rarity; import net.minecraft.world.item.Tiers; +import net.minecraft.world.item.component.CustomData; import net.minecraft.world.level.Level; import org.joml.Vector3f; @@ -22,13 +28,38 @@ public class SniperRifle extends BasicGun { public static final String ID = "sniper_rifle"; public static final Vector3f OFFSET = new Vector3f(-0.3f, -0.1f, 1.5f); public static final float DAMAGE_DEFAULT = 35.0f; - public static final IConfigData CONFIG_DATA = createConfigData(ID, DAMAGE_DEFAULT); + public static final double MAX_ZOOM = 10.0D; + + private static final String CONFIG_TRANSLATION_PREFIX = "lzxnoneterraria.configuration."; + public static final String ZOOM_SCROLL_SPEED_PATH = "weapon.sniper_rifle.zoom_scroll_speed"; + public static final double ZOOM_SCROLL_SPEED_DEFAULT = 0.5D; + public static final double ZOOM_SCROLL_SPEED_MIN = 0.0D; + public static final double ZOOM_SCROLL_SPEED_MAX = 10.0D; + + public static final IConfigData CONFIG_DATA = new IConfigData() { + @Override + public void onConfigLoad() { + createConfigData(ID, DAMAGE_DEFAULT).onConfigLoad(); + ConfigFactory.loadDoubleConfig( + ZOOM_SCROLL_SPEED_PATH, + Component.translatable(CONFIG_TRANSLATION_PREFIX + "sniper_rifle_zoom_scroll_speed"), + Component.translatable(CONFIG_TRANSLATION_PREFIX + "sniper_rifle_zoom_scroll_speed.tooltip"), + ZOOM_SCROLL_SPEED_DEFAULT, + ZOOM_SCROLL_SPEED_MIN, + ZOOM_SCROLL_SPEED_MAX + ); + } + }; public static final ConfigListItem CONFIG_LIST_ITEM = createConfigListItem(ID, CONFIG_DATA); public SniperRifle() { super(Tiers.DIAMOND, new Item.Properties().stacksTo(1).rarity(Rarity.EPIC), ID, OFFSET, DAMAGE_DEFAULT, 36, ModSounds.SHOT, 2.0f, 10); } + public static double getZoomScrollSpeed() { + return Math.clamp(ConfigUtil.readDouble(ZOOM_SCROLL_SPEED_PATH, ZOOM_SCROLL_SPEED_DEFAULT), ZOOM_SCROLL_SPEED_MIN, ZOOM_SCROLL_SPEED_MAX); + } + @Override protected void shoot(Level level, Player player, InteractionHand hand, ItemStack stack) { SoundUtil.playClientSound(player, ModSounds.SHOT.get()); diff --git a/src/main/java/com/lzxnone/terraria/network/handler/ToggleSummonFreeTargetingHandler.java b/src/main/java/com/lzxnone/terraria/network/handler/ToggleSummonFreeTargetingHandler.java index b45ccda..4963219 100644 --- a/src/main/java/com/lzxnone/terraria/network/handler/ToggleSummonFreeTargetingHandler.java +++ b/src/main/java/com/lzxnone/terraria/network/handler/ToggleSummonFreeTargetingHandler.java @@ -2,6 +2,7 @@ package com.lzxnone.terraria.network.handler; import com.lzxnone.terraria.attachment.ModAttachments; import com.lzxnone.terraria.network.payload.ToggleSummonFreeTargetingPayload; +import net.minecraft.ChatFormatting; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Player; import net.neoforged.neoforge.network.handling.IPayloadContext; @@ -17,7 +18,8 @@ public class ToggleSummonFreeTargetingHandler { player.displayClientMessage( Component.translatable(enabled ? "message.lzxnoneterraria.summon_free_targeting_enabled" - : "message.lzxnoneterraria.summon_free_targeting_disabled"), + : "message.lzxnoneterraria.summon_free_targeting_disabled") + .withStyle(enabled ? ChatFormatting.GREEN : ChatFormatting.RED), true ); }); diff --git a/src/main/resources/assets/lzxnoneterraria/lang/en_us.json b/src/main/resources/assets/lzxnoneterraria/lang/en_us.json index e84a00b..18a4bee 100644 --- a/src/main/resources/assets/lzxnoneterraria/lang/en_us.json +++ b/src/main/resources/assets/lzxnoneterraria/lang/en_us.json @@ -89,6 +89,7 @@ "tooltip.lzxnoneterraria.relative_distance": "Relative Distance", "tooltip.lzxnoneterraria.absolute_distance": "Absolute Distance", + "tooltip.lzxnoneterraria.zoom": "Zoom", "tooltip.lzxnoneterraria.ammo": "Ammo: %s", "tooltip.lzxnoneterraria.devils_devastation_kill_mode": "Kill Mode", "tooltip.lzxnoneterraria.devils_devastation.0": "The perfect blade of the Underworld, wielding the power of Brimstone directly.", @@ -625,6 +626,8 @@ "lzxnoneterraria.configuration.sniper_rifle_settings": "Sniper Rifle Settings", "lzxnoneterraria.configuration.sniper_rifle_damage": "Damage", "lzxnoneterraria.configuration.sniper_rifle_damage.tooltip": "Damage per hit", + "lzxnoneterraria.configuration.sniper_rifle_zoom_scroll_speed": "Zoom Scroll Speed", + "lzxnoneterraria.configuration.sniper_rifle_zoom_scroll_speed.tooltip": "Mouse wheel speed for adjusting sniper scope zoom", "lzxnoneterraria.configuration.chain_gun_settings": "Chain Gun Settings", "lzxnoneterraria.configuration.chain_gun_damage": "Damage", "lzxnoneterraria.configuration.chain_gun_damage.tooltip": "Damage per hit", diff --git a/src/main/resources/assets/lzxnoneterraria/lang/zh_cn.json b/src/main/resources/assets/lzxnoneterraria/lang/zh_cn.json index 4d29c92..8a37498 100644 --- a/src/main/resources/assets/lzxnoneterraria/lang/zh_cn.json +++ b/src/main/resources/assets/lzxnoneterraria/lang/zh_cn.json @@ -89,6 +89,7 @@ "tooltip.lzxnoneterraria.relative_distance": "相对距离", "tooltip.lzxnoneterraria.absolute_distance": "绝对距离", + "tooltip.lzxnoneterraria.zoom": "倍率", "tooltip.lzxnoneterraria.ammo": "弹药: %s", "tooltip.lzxnoneterraria.devils_devastation_kill_mode": "杀戮模式", "tooltip.lzxnoneterraria.devils_devastation.0": "完美的地狱之剑,直接掌握着硫磺火之力。", @@ -624,6 +625,8 @@ "lzxnoneterraria.configuration.sniper_rifle_settings": "狙击步枪 设置", "lzxnoneterraria.configuration.sniper_rifle_damage": "伤害", "lzxnoneterraria.configuration.sniper_rifle_damage.tooltip": "每次命中伤害", + "lzxnoneterraria.configuration.sniper_rifle_zoom_scroll_speed": "滚轮调节系数", + "lzxnoneterraria.configuration.sniper_rifle_zoom_scroll_speed.tooltip": "鼠标滚轮调节狙击镜倍率的速度", "lzxnoneterraria.configuration.chain_gun_settings": "链式机枪 设置", "lzxnoneterraria.configuration.chain_gun_damage": "伤害", "lzxnoneterraria.configuration.chain_gun_damage.tooltip": "每次命中伤害",