|
1 |
| -/* |
2 |
| - * Copyright 2021 Siphalor |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, |
11 |
| - * software distributed under the License is distributed on an |
12 |
| - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
13 |
| - * either express or implied. |
14 |
| - * See the License for the specific language governing |
15 |
| - * permissions and limitations under the License. |
16 |
| - */ |
17 |
| - |
18 |
| -package de.siphalor.mousewheelie.client.inventory; |
19 |
| - |
20 |
| -import de.siphalor.mousewheelie.MWConfig; |
21 |
| -import de.siphalor.mousewheelie.client.MWClient; |
22 |
| -import net.fabricmc.api.EnvType; |
23 |
| -import net.fabricmc.api.Environment; |
24 |
| -import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; |
25 |
| -import net.minecraft.block.BlockState; |
26 |
| -import net.minecraft.entity.player.PlayerInventory; |
27 |
| -import net.minecraft.item.ItemStack; |
28 |
| -import net.minecraft.network.packet.c2s.play.PickFromInventoryC2SPacket; |
29 |
| - |
30 |
| -@Environment(EnvType.CLIENT) |
31 |
| -public class ToolPicker { |
32 |
| - PlayerInventory inventory; |
33 |
| - |
34 |
| - static int lastToolPickSlot = -1; |
35 |
| - |
36 |
| - public ToolPicker(PlayerInventory inventory) { |
37 |
| - this.inventory = inventory; |
38 |
| - } |
39 |
| - |
40 |
| - public int findToolFor(BlockState blockState) { |
41 |
| - float bestBreakSpeed = 1.0F; |
42 |
| - int bestSpeedSlot = -1; |
43 |
| - int invSize = (MWConfig.toolPicking.pickFromInventory ? inventory.main.size() : 9); |
44 |
| - for (int i = 1; i <= invSize; i++) { |
45 |
| - int index = (i + lastToolPickSlot) % invSize; |
46 |
| - if (index == inventory.selectedSlot) continue; |
47 |
| - ItemStack stack = inventory.main.get(index); |
48 |
| - if (stack.isEffectiveOn(blockState)) { |
49 |
| - return index; |
50 |
| - } else { |
51 |
| - float breakSpeed = stack.getMiningSpeed(blockState); |
52 |
| - if (breakSpeed > bestBreakSpeed) { |
53 |
| - bestSpeedSlot = index; |
54 |
| - bestBreakSpeed = breakSpeed; |
55 |
| - } |
56 |
| - } |
57 |
| - } |
58 |
| - if (bestBreakSpeed == -1) { |
59 |
| - ItemStack stack = inventory.main.get(inventory.selectedSlot); |
60 |
| - if (stack.isEffectiveOn(blockState) || stack.getMiningSpeed(blockState) > 1.0F) |
61 |
| - return inventory.selectedSlot; |
62 |
| - } |
63 |
| - return bestSpeedSlot; |
64 |
| - } |
65 |
| - |
66 |
| - public boolean pickToolFor(BlockState blockState) { |
67 |
| - return pick(findToolFor(blockState)); |
68 |
| - } |
69 |
| - |
70 |
| - public int findWeapon() { |
71 |
| - int invSize = (MWConfig.toolPicking.pickFromInventory ? inventory.main.size() : 9); |
72 |
| - for (int i = 1; i <= invSize; i++) { |
73 |
| - int index = (i + lastToolPickSlot) % invSize; |
74 |
| - if (index == inventory.selectedSlot) continue; |
75 |
| - if (MWClient.isWeapon(inventory.main.get(index).getItem())) |
76 |
| - return index; |
77 |
| - } |
78 |
| - return -1; |
79 |
| - } |
80 |
| - |
81 |
| - public boolean pickWeapon() { |
82 |
| - return pick(findWeapon()); |
83 |
| - } |
84 |
| - |
85 |
| - private boolean pick(int index) { |
86 |
| - lastToolPickSlot = index; |
87 |
| - if (index != -1 && index != inventory.selectedSlot) { |
88 |
| - PickFromInventoryC2SPacket packet = new PickFromInventoryC2SPacket(index); |
89 |
| - ClientSidePacketRegistry.INSTANCE.sendToServer(packet); |
90 |
| - return true; |
91 |
| - } |
92 |
| - return false; |
93 |
| - } |
94 |
| -} |
| 1 | +/* |
| 2 | + * Copyright 2021 Siphalor |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an |
| 12 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 13 | + * either express or implied. |
| 14 | + * See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package de.siphalor.mousewheelie.client.inventory; |
| 19 | + |
| 20 | +import de.siphalor.mousewheelie.MWConfig; |
| 21 | +import de.siphalor.mousewheelie.client.MWClient; |
| 22 | +import net.fabricmc.api.EnvType; |
| 23 | +import net.fabricmc.api.Environment; |
| 24 | +import net.minecraft.block.BlockState; |
| 25 | +import net.minecraft.client.MinecraftClient; |
| 26 | +import net.minecraft.entity.player.PlayerInventory; |
| 27 | +import net.minecraft.item.ItemStack; |
| 28 | +import net.minecraft.network.packet.c2s.play.PickFromInventoryC2SPacket; |
| 29 | + |
| 30 | +@Environment(EnvType.CLIENT) |
| 31 | +public class ToolPicker { |
| 32 | + PlayerInventory inventory; |
| 33 | + |
| 34 | + static int lastToolPickSlot = -1; |
| 35 | + |
| 36 | + public ToolPicker(PlayerInventory inventory) { |
| 37 | + this.inventory = inventory; |
| 38 | + } |
| 39 | + |
| 40 | + public int findToolFor(BlockState blockState) { |
| 41 | + float bestBreakSpeed = 1.0F; |
| 42 | + int bestSpeedSlot = -1; |
| 43 | + int invSize = (MWConfig.toolPicking.pickFromInventory ? inventory.main.size() : 9); |
| 44 | + for (int i = 1; i <= invSize; i++) { |
| 45 | + int index = (i + lastToolPickSlot) % invSize; |
| 46 | + if (index == inventory.selectedSlot) continue; |
| 47 | + ItemStack stack = inventory.main.get(index); |
| 48 | + if (stack.isEffectiveOn(blockState)) { |
| 49 | + return index; |
| 50 | + } else { |
| 51 | + float breakSpeed = stack.getMiningSpeed(blockState); |
| 52 | + if (breakSpeed > bestBreakSpeed) { |
| 53 | + bestSpeedSlot = index; |
| 54 | + bestBreakSpeed = breakSpeed; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + if (bestBreakSpeed == -1) { |
| 59 | + ItemStack stack = inventory.main.get(inventory.selectedSlot); |
| 60 | + if (stack.isEffectiveOn(blockState) || stack.getMiningSpeed(blockState) > 1.0F) |
| 61 | + return inventory.selectedSlot; |
| 62 | + } |
| 63 | + return bestSpeedSlot; |
| 64 | + } |
| 65 | + |
| 66 | + public boolean pickToolFor(BlockState blockState) { |
| 67 | + return pick(findToolFor(blockState)); |
| 68 | + } |
| 69 | + |
| 70 | + public int findWeapon() { |
| 71 | + int invSize = (MWConfig.toolPicking.pickFromInventory ? inventory.main.size() : 9); |
| 72 | + for (int i = 1; i <= invSize; i++) { |
| 73 | + int index = (i + lastToolPickSlot) % invSize; |
| 74 | + if (index == inventory.selectedSlot) continue; |
| 75 | + if (MWClient.isWeapon(inventory.main.get(index).getItem())) |
| 76 | + return index; |
| 77 | + } |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + public boolean pickWeapon() { |
| 82 | + return pick(findWeapon()); |
| 83 | + } |
| 84 | + |
| 85 | + private boolean pick(int index) { |
| 86 | + lastToolPickSlot = index; |
| 87 | + if (index != -1 && index != inventory.selectedSlot) { |
| 88 | + PickFromInventoryC2SPacket packet = new PickFromInventoryC2SPacket(index); |
| 89 | + MinecraftClient.getInstance().getNetworkHandler().sendPacket(packet); |
| 90 | + return true; |
| 91 | + } |
| 92 | + return false; |
| 93 | + } |
| 94 | +} |
0 commit comments