From f9cfbd835ce251554b4c687f9f9ab52e7b57f34b Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 17:15:20 +0100 Subject: [PATCH 01/38] update bs+deps --- dependencies.gradle | 6 +++--- gradle/wrapper/gradle-wrapper.jar | Bin 43504 -> 43583 bytes settings.gradle | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index 7f72087..875e8c3 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -34,9 +34,9 @@ * For more details, see https://docs.gradle.org/8.0.1/userguide/java_library_plugin.html#sec:java_library_configurations_graph */ dependencies { - api("com.github.GTNewHorizons:GTNHLib:0.6.0:dev") - runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.7.13-GTNH:dev") + api("com.github.GTNewHorizons:GTNHLib:0.6.2:dev") + runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.7.18-GTNH:dev") compileOnly("com.github.GTNewHorizons:inventory-tweaks:1.7.0:dev") - compileOnly(deobf("https://github.com/Roadhog360/Et-Futurum-Requiem/releases/download/2.6.0/Et_Futurum_Requiem-2.6.0-nomixin.jar", "Et Futurum Requiem")) + compileOnly(rfg.deobf('maven.modrinth:etfuturum:2.6.2')) compileOnly("com.github.GTNewHorizons:CarpentersBlocks:3.7.0-GTNH:dev") } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 2c3521197d7c4586c843d1d3e9090525f1898cde..a4b76b9530d66f5e68d973ea569d8e19de379189 100644 GIT binary patch delta 3990 zcmV;H4{7l5(*nQL0Kr1kzC=_KMxQY0|W5(lc#i zH*M1^P4B}|{x<+fkObwl)u#`$GxKKV&3pg*-y6R6txw)0qU|Clf9Uds3x{_-**c=7 z&*)~RHPM>Rw#Hi1R({;bX|7?J@w}DMF>dQQU2}9yj%iLjJ*KD6IEB2^n#gK7M~}6R zkH+)bc--JU^pV~7W=3{E*4|ZFpDpBa7;wh4_%;?XM-5ZgZNnVJ=vm!%a2CdQb?oTa z70>8rTb~M$5Tp!Se+4_OKWOB1LF+7gv~$$fGC95ToUM(I>vrd$>9|@h=O?eARj0MH zT4zo(M>`LWoYvE>pXvqG=d96D-4?VySz~=tPVNyD$XMshoTX(1ZLB5OU!I2OI{kb) zS8$B8Qm>wLT6diNnyJZC?yp{Kn67S{TCOt-!OonOK7$K)e-13U9GlnQXPAb&SJ0#3 z+vs~+4Qovv(%i8g$I#FCpCG^C4DdyQw3phJ(f#y*pvNDQCRZ~MvW<}fUs~PL=4??j zmhPyg<*I4RbTz|NHFE-DC7lf2=}-sGkE5e!RM%3ohM7_I^IF=?O{m*uUPH(V?gqyc(Rp?-Qu(3bBIL4Fz(v?=_Sh?LbK{nqZMD>#9D_hNhaV$0ef3@9V90|0u#|PUNTO>$F=qRhg1duaE z0`v~X3G{8RVT@kOa-pU+z8{JWyP6GF*u2e8eKr7a2t1fuqQy)@d|Qn(%YLZ62TWtoX@$nL}9?atE#Yw`rd(>cr0gY;dT9~^oL;u)zgHUvxc2I*b&ZkGM-iq=&(?kyO(3}=P! zRp=rErEyMT5UE9GjPHZ#T<`cnD)jyIL!8P{H@IU#`e8cAG5jMK zVyKw7--dAC;?-qEu*rMr$5@y535qZ6p(R#+fLA_)G~!wnT~~)|s`}&fA(s6xXN`9j zP#Fd3GBa#HeS{5&8p?%DKUyN^X9cYUc6vq}D_3xJ&d@=6j(6BZKPl?!k1?!`f3z&a zR4ZF60Mx7oBxLSxGuzA*Dy5n-d2K=+)6VMZh_0KetK|{e;E{8NJJ!)=_E~1uu=A=r zrn&gh)h*SFhsQJo!f+wKMIE;-EOaMSMB@aXRU(UcnJhZW^B^mgs|M9@5WF@s6B0p& zm#CTz)yiQCgURE{%hjxHcJ6G&>G9i`7MyftL!QQd5 z@RflRs?7)99?X`kHNt>W3l7YqscBpi*R2+fsgABor>KVOu(i(`03aytf2UA!&SC9v z!E}whj#^9~=XHMinFZ;6UOJjo=mmNaWkv~nC=qH9$s-8roGeyaW-E~SzZ3Gg>j zZ8}<320rg4=$`M0nxN!w(PtHUjeeU?MvYgWKZ6kkzABK;vMN0|U;X9abJleJA(xy<}5h5P(5 z{RzAFPvMnX2m0yH0Jn2Uo-p`daE|(O`YQiC#jB8;6bVIUf?SY(k$#C0`d6qT`>Xe0+0}Oj0=F&*D;PVe=Z<=0AGI<6$gYLwa#r` zm449x*fU;_+J>Mz!wa;T-wldoBB%&OEMJgtm#oaI60TSYCy7;+$5?q!zi5K`u66Wq zvg)Fx$s`V3Em{=OEY{3lmh_7|08ykS&U9w!kp@Ctuzqe1JFOGz6%i5}Kmm9>^=gih z?kRxqLA<3@e=}G4R_?phW{4DVr?`tPfyZSN@R=^;P;?!2bh~F1I|fB7P=V=9a6XU5 z<#0f>RS0O&rhc&nTRFOW7&QhevP0#>j0eq<1@D5yAlgMl5n&O9X|Vq}%RX}iNyRFF z7sX&u#6?E~bm~N|z&YikXC=I0E*8Z$v7PtWfjy)$e_Ez25fnR1Q=q1`;U!~U>|&YS zaOS8y!^ORmr2L4ik!IYR8@Dcx8MTC=(b4P6iE5CnrbI~7j7DmM8em$!da&D!6Xu)!vKPdLG z9f#)se|6=5yOCe)N6xDhPI!m81*dNe7u985zi%IVfOfJh69+#ag4ELzGne?o`eA`42K4T)h3S+s)5IT97%O>du- z0U54L8m4}rkRQ?QBfJ%DLssy^+a7Ajw;0&`NOTY4o;0-ivm9 zBz1C%nr_hQ)X)^QM6T1?=yeLkuG9Lf50(eH}`tFye;01&(p?8i+6h};VV-2B~qdxeC#=X z(JLlzy&fHkyi9Ksbcs~&r^%lh^2COldLz^H@X!s~mr9Dr6z!j+4?zkD@Ls7F8(t(f z9`U?P$Lmn*Y{K}aR4N&1N=?xtQ1%jqf1~pJyQ4SgBrEtR`j4lQuh7cqP49Em5cO=I zB(He2`iPN5M=Y0}h(IU$37ANTGx&|b-u1BYA*#dE(L-lptoOpo&th~E)_)y-`6kSH z3vvyVrcBwW^_XYReJ=JYd9OBQrzv;f2AQdZH#$Y{Y+Oa33M70XFI((fs;mB4e`<<{ ze4dv2B0V_?Ytsi>>g%qs*}oDGd5d(RNZ*6?7qNbdp7wP4T72=F&r?Ud#kZr8Ze5tB z_oNb7{G+(o2ajL$!69FW@jjPQ2a5C)m!MKKRirC$_VYIuVQCpf9rIms0GRDf)8AH${I`q^~5rjot@#3$2#zT2f`(N^P7Z;6(@EK$q*Jgif00I6*^ZGV+XB5uw*1R-@23yTw&WKD{s1;HTL;dO)%5i#`dc6b7;5@^{KU%N|A-$zsYw4)7LA{3`Zp>1 z-?K9_IE&z)dayUM)wd8K^29m-l$lFhi$zj0l!u~4;VGR6Y!?MAfBC^?QD53hy6VdD z@eUZIui}~L%#SmajaRq1J|#> z4m=o$vZ*34=ZWK2!QMNEcp2Lbc5N1q!lEDq(bz0b;WI9;e>l=CG9^n#ro`w>_0F$Q zfZ={2QyTkfByC&gy;x!r*NyXXbk=a%~~(#K?< zTke0HuF5{Q+~?@!KDXR|g+43$+;ab`^flS%miup_0OUTm=nIc%d5nLP)i308PIjl_YMF6cpQ__6&$n6it8K- z8PIjl_YMF6cpQ_!r)L8IivW`WdK8mBs6PXdjR2DYdK8nCs73=4j{uVadK8oNjwX|E wpAeHLsTu^*Y>Trk?aBtSQ(D-o$(D8Px^?ZI-PUB? z*1fv!{YdHme3Fc8%cR@*@zc5A_nq&2=R47Hp@$-JF4Fz*;SLw5}K^y>s-s;V!}b2i=5=M- zComP?ju>8Fe@=H@rlwe1l`J*6BTTo`9b$zjQ@HxrAhp0D#u?M~TxGC_!?ccCHCjt| zF*PgJf@kJB`|Ml}cmsyrAjO#Kjr^E5p29w+#>$C`Q|54BoDv$fQ9D?3n32P9LPMIzu?LjNqggOH=1@T{9bMn*u8(GI z!;MLTtFPHal^S>VcJdiYqX0VU|Rn@A}C1xOlxCribxes0~+n2 z6qDaIA2$?e`opx3_KW!rAgbpzU)gFdjAKXh|5w``#F0R|c)Y)Du0_Ihhz^S?k^pk% zP>9|pIDx)xHH^_~+aA=^$M!<8K~Hy(71nJGf6`HnjtS=4X4=Hk^O71oNia2V{HUCC zoN3RSBS?mZCLw;l4W4a+D8qc)XJS`pUJ5X-f^1ytxwr`@si$lAE?{4G|o; zO0l>`rr?;~c;{ZEFJ!!3=7=FdGJ?Q^xfNQh4A?i;IJ4}B+A?4olTK(fN++3CRBP97 ze~lG9h%oegkn)lpW-4F8o2`*WW0mZHwHez`ko@>U1_;EC_6ig|Drn@=DMV9YEUSCa zIf$kHei3(u#zm9I!Jf(4t`Vm1lltJ&lVHy(eIXE8sy9sUpmz%I_gA#8x^Zv8%w?r2 z{GdkX1SkzRIr>prRK@rqn9j2wG|rUvf6PJbbin=yy-TAXrguvzN8jL$hUrIXzr^s5 zVM?H4;eM-QeRFr06@ifV(ocvk?_)~N@1c2ien56UjWXid6W%6ievIh)>dk|rIs##^kY67ib8Kw%#-oVFaXG7$ERyA9(NSJUvWiOA5H(!{uOpcW zg&-?iqPhds%3%tFspHDqqr;A!e@B#iPQjHd=c>N1LoOEGRehVoPOdxJ>b6>yc#o#+ zl8s8!(|NMeqjsy@0x{8^j0d00SqRZjp{Kj)&4UHYGxG+z9b-)72I*&J70?+8e?p_@ z=>-(>l6z5vYlP~<2%DU02b!mA{7mS)NS_eLe=t)sm&+Pmk?asOEKlkPQ)EUvvfC=;4M&*|I!w}(@V_)eUKLA_t^%`o z0PM9LV|UKTLnk|?M3u!|f2S0?UqZsEIH9*NJS-8lzu;A6-rr-ot=dg9SASoluZUkFH$7X; zP=?kYX!K?JL-b~<#7wU;b;eS)O;@?h%sPPk{4xEBxb{!sm0AY|f9cNvx6>$3F!*0c z75H=dy8JvTyO8}g1w{$9T$p~5en}AeSLoCF>_RT9YPMpChUjl310o*$QocjbH& zbnwg#gssR#jDVN{uEi3n(PZ%PFZ|6J2 z5_rBf0-u>e4sFe0*Km49ATi7>Kn0f9!uc|rRMR1Dtt6m1LW8^>qFlo}h$@br=Rmpi z;mI&>OF64Be{dVeHI8utrh)v^wsZ0jii%x8UgZ8TC%K~@I(4E};GFW&(;WVov}3%H zH;IhRkfD^(vt^DjZz(MyHLZxv8}qzPc(%itBkBwf_fC~sDBgh<3XAv5cxxfF3<2U! z03Xe&z`is!JDHbe;mNmfkH+_LFE*I2^mdL@7(@9DfAcP6O04V-ko;Rpgp<%Cj5r8Z zd0`sXoIjV$j)--;jA6Zy^D5&5v$o^>e%>Q?9GLm{i~p^lAn!%ZtF$I~>39XVZxk0b zROh^Bk9cE0AJBLozZIEmy7xG(yHWGztvfnr0(2ro1%>zsGMS^EMu+S$r=_;9 zWwZkgf7Q7`H9sLf2Go^Xy6&h~a&%s2_T@_Csf19MntF$aVFiFkvE3_hUg(B@&Xw@YJ zpL$wNYf78=0c@!QU6_a$>CPiXT7QAGDM}7Z(0z#_ZA=fmLUj{2z7@Ypo71UDy8GHr z-&TLKf6a5WCf@Adle3VglBt4>Z>;xF}}-S~B7<(%B;Y z0QR55{z-buw>8ilNM3u6I+D$S%?)(p>=eBx-HpvZj{7c*_?K=d()*7q?93us}1dq%FAFYLsW8ZTQ_XZLh`P2*6(NgS}qGcfGXVWpwsp#Rs}IuKbk*`2}&) zI^Vsk6S&Q4@oYS?dJ`NwMVBs6f57+RxdqVub#PvMu?$=^OJy5xEl0<5SLsSRy%%a0 zi}Y#1-F3m;Ieh#Y12UgW?-R)|eX>ZuF-2cc!1>~NS|XSF-6In>zBoZg+ml!6%fk7U zw0LHcz8VQk(jOJ+Yu)|^|15ufl$KQd_1eUZZzj`aC%umU6F1&D5XVWce_wAe(qCSZ zpX-QF4e{EmEVN9~6%bR5U*UT{eMHfcUo`jw*u?4r2s_$`}U{?NjvEm(u&<>B|%mq$Q3weshxk z76<``8vh{+nX`@9CB6IE&z)I%IFjR^LH{s1p|eppv=x za(g_jLU|xjWMAn-V7th$f({|LG8zzIE0g?cyW;%Dmtv%C+0@xVxPE^ zyZzi9P%JAD6ynwHptuzP`Kox7*9h7XSMonCalv;Md0i9Vb-c*!f0ubfk?&T&T}AHh z4m8Bz{JllKcdNg?D^%a5MFQ;#1z|*}H^qHLzW)L}wp?2tY7RejtSh8<;Zw)QGJYUm z|MbTxyj*McKlStlT9I5XlSWtQGN&-LTr2XyNU+`490rg?LYLMRnz-@oKqT1hpCGqP zyRXt4=_Woj$%n5ee<3zhLF>5>`?m9a#xQH+Jk_+|RM8Vi;2*XbK- zEL6sCpaGPzP>k8f4Kh|##_imt#zJMB;ir|JrMPGW`rityK1vHXMLy18%qmMQAm4WZ zP)i30KR&5vs15)C+8dM66&$k~i|ZT;KR&5vs15)C+8dJ(sAmGPijyIz6_bsqKLSFH zlOd=TljEpH0>h4zA*dCTK&emy#FCRCs1=i^sZ9bFmXjf<6_X39E(XY)00000#N437 diff --git a/settings.gradle b/settings.gradle index 16e84e7..dd825d7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,5 +17,5 @@ pluginManagement { } plugins { - id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.30' + id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.31' } From b7005250e7310445bda14fdb4c55b7b0a44c64e1 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 17:16:00 +0100 Subject: [PATCH 02/38] try for a more native feeling & less hacky offhand --- src/main/java/xonin/backhand/Backhand.java | 11 - src/main/java/xonin/backhand/CommonProxy.java | 85 ++- .../xonin/backhand/HookContainerClass.java | 617 ++++++++---------- .../xonin/backhand/ServerEventsHandler.java | 158 ----- .../xonin/backhand/ServerTickHandler.java | 155 ----- .../xonin/backhand/api/PlayerEventChild.java | 167 ----- .../backhand/api/core/BackhandUtils.java | 350 +--------- .../backhand/api/core/IBackhandPlayer.java | 12 +- .../backhand/api/core/IOffhandInventory.java | 17 + .../api/core/InventoryPlayerBackhand.java | 161 ----- .../api/core/OffhandExtendedProperty.java | 51 -- .../client/BackhandClientTickHandler.java | 399 ----------- .../backhand/client/ClientEventHandler.java | 95 ++- .../xonin/backhand/client/ClientProxy.java | 64 -- .../backhand/client/ClientTickHandler.java | 152 ----- .../client/utils/BackhandClientUtils.java | 47 -- .../client/utils/BackhandRenderHelper.java | 81 +-- .../client/world/ClientFakePlayer.java | 107 --- .../client/world/DummyChunkProvider.java | 97 --- .../client/world/DummySaveHandler.java | 99 --- .../backhand/client/world/DummyWorld.java | 149 ----- .../backhand/constants/EnumPacketClient.java | 3 - .../backhand/constants/EnumPacketServer.java | 3 - .../java/xonin/backhand/mixins/Mixins.java | 26 +- .../xonin/backhand/mixins/TargetedMod.java | 2 +- .../early/minecraft/MixinContainerPlayer.java | 24 + .../early/minecraft/MixinEntityFishHook.java | 32 + .../early/minecraft/MixinEntityItem.java | 24 - .../early/minecraft/MixinEntityPlayer.java | 56 +- .../minecraft/MixinEntityPlayerClient.java | 33 +- .../early/minecraft/MixinEntityPlayerMP.java | 31 - .../minecraft/MixinEntityTrackerEntry.java | 39 ++ .../early/minecraft/MixinGuiInventory.java | 43 ++ .../early/minecraft/MixinInventoryPlayer.java | 61 ++ .../mixins/early/minecraft/MixinItemBow.java | 31 +- .../early/minecraft/MixinItemRenderer.java | 9 +- .../early/minecraft/MixinItemStack.java | 33 +- .../early/minecraft/MixinItemStackClient.java | 26 +- .../early/minecraft/MixinMinecraft.java | 258 ++++++++ .../minecraft/MixinNetHandlerPlayClient.java | 4 +- .../minecraft/MixinNetHandlerPlayServer.java | 92 +-- .../minecraft/MixinPlayerControllerMP.java | 33 - .../mixins/early/minecraft/MixinWorld.java | 23 - .../MixinPlayerPermissions.java | 25 - .../etfuturum/MixinServerEventHandler.java | 51 ++ .../packet/BackhandPacketHandler.java | 7 +- .../backhand/packet/OffhandAttackPacket.java | 64 -- .../packet/OffhandConfigSyncPacket.java | 22 +- .../packet/OffhandPlaceBlockPacket.java | 207 ------ .../packet/OffhandSwapClientPacket.java | 8 +- .../packet/OffhandToServerPacket.java | 46 -- .../xonin/backhand/utils/BackhandConfig.java | 47 +- 52 files changed, 1100 insertions(+), 3337 deletions(-) delete mode 100644 src/main/java/xonin/backhand/ServerEventsHandler.java delete mode 100644 src/main/java/xonin/backhand/ServerTickHandler.java delete mode 100644 src/main/java/xonin/backhand/api/PlayerEventChild.java create mode 100644 src/main/java/xonin/backhand/api/core/IOffhandInventory.java delete mode 100644 src/main/java/xonin/backhand/api/core/InventoryPlayerBackhand.java delete mode 100644 src/main/java/xonin/backhand/api/core/OffhandExtendedProperty.java delete mode 100644 src/main/java/xonin/backhand/client/BackhandClientTickHandler.java delete mode 100644 src/main/java/xonin/backhand/client/ClientTickHandler.java delete mode 100644 src/main/java/xonin/backhand/client/utils/BackhandClientUtils.java delete mode 100644 src/main/java/xonin/backhand/client/world/ClientFakePlayer.java delete mode 100644 src/main/java/xonin/backhand/client/world/DummyChunkProvider.java delete mode 100644 src/main/java/xonin/backhand/client/world/DummySaveHandler.java delete mode 100644 src/main/java/xonin/backhand/client/world/DummyWorld.java delete mode 100644 src/main/java/xonin/backhand/constants/EnumPacketClient.java delete mode 100644 src/main/java/xonin/backhand/constants/EnumPacketServer.java create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityFishHook.java delete mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityItem.java delete mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerMP.java create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityTrackerEntry.java create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiInventory.java create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java delete mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinPlayerControllerMP.java delete mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinWorld.java delete mode 100644 src/main/java/xonin/backhand/mixins/late/carpentersblocks/MixinPlayerPermissions.java create mode 100644 src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java delete mode 100644 src/main/java/xonin/backhand/packet/OffhandAttackPacket.java delete mode 100644 src/main/java/xonin/backhand/packet/OffhandPlaceBlockPacket.java delete mode 100644 src/main/java/xonin/backhand/packet/OffhandToServerPacket.java diff --git a/src/main/java/xonin/backhand/Backhand.java b/src/main/java/xonin/backhand/Backhand.java index 921770b..61a5697 100644 --- a/src/main/java/xonin/backhand/Backhand.java +++ b/src/main/java/xonin/backhand/Backhand.java @@ -2,7 +2,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.server.MinecraftServer; -import net.minecraftforge.common.MinecraftForge; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -50,16 +49,6 @@ public void load(FMLPreInitializationEvent event) { } proxy.load(); - - MinecraftForge.EVENT_BUS.register(new ServerEventsHandler()); - FMLCommonHandler.instance() - .bus() - .register(new ServerTickHandler()); - - MinecraftForge.EVENT_BUS.register(HookContainerClass.INSTANCE); - FMLCommonHandler.instance() - .bus() - .register(HookContainerClass.INSTANCE); } @Mod.EventHandler diff --git a/src/main/java/xonin/backhand/CommonProxy.java b/src/main/java/xonin/backhand/CommonProxy.java index 4cc40ce..fe0e98d 100644 --- a/src/main/java/xonin/backhand/CommonProxy.java +++ b/src/main/java/xonin/backhand/CommonProxy.java @@ -1,11 +1,8 @@ package xonin.backhand; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import cpw.mods.fml.common.event.FMLServerStoppingEvent; -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.utils.EnumAnimations; public class CommonProxy { @@ -16,45 +13,45 @@ public void load() { } public void onServerStopping(FMLServerStoppingEvent event) { - for (EntityPlayer player : Backhand.getServer() - .getConfigurationManager().playerEntityList) { - if (BackhandUtils.getOffhandItem(player) != null) { - BackhandUtils.resetAndDelayHotswap(player, 0); - } - } - } - - public EntityPlayer getClientPlayer() { - return null; - } - - public void sendAnimationPacket(EnumAnimations animation, EntityPlayer entityPlayer) {} - - // Should not be called on the server anyway - public boolean isRightClickHeld() { - return false; - } - - public int getRightClickCounter() { - return 0; - } - - public void setRightClickCounter(int i) {} - - public int getRightClickDelay() { - return 0; - } - - // Should not be called on the server anyway - public boolean isLeftClickHeld() { - return false; - } - - // Should not be called on the server anyway - public int getLeftClickCounter() { - return 0; - } - - // Should not be called on the server anyway - public void setLeftClickCounter(int i) {} + // for (EntityPlayer player : Backhand.getServer() + // .getConfigurationManager().playerEntityList) { + // if (BackhandUtils.getOffhandItem(player) != null) { + // BackhandUtils.resetAndDelayHotswap(player, 0); + // } + // } + } + + // public EntityPlayer getClientPlayer() { + // return null; + // } + + // public void sendAnimationPacket(EnumAnimations animation, EntityPlayer entityPlayer) {} + // + // // Should not be called on the server anyway + // public boolean isRightClickHeld() { + // return false; + // } + // + // public int getRightClickCounter() { + // return 0; + // } + // + // public void setRightClickCounter(int i) {} + // + // public int getRightClickDelay() { + // return 0; + // } + // + // // Should not be called on the server anyway + // public boolean isLeftClickHeld() { + // return false; + // } + // + // // Should not be called on the server anyway + // public int getLeftClickCounter() { + // return 0; + // } + // + // // Should not be called on the server anyway + // public void setLeftClickCounter(int i) {} } diff --git a/src/main/java/xonin/backhand/HookContainerClass.java b/src/main/java/xonin/backhand/HookContainerClass.java index 989927b..bc2aeb4 100644 --- a/src/main/java/xonin/backhand/HookContainerClass.java +++ b/src/main/java/xonin/backhand/HookContainerClass.java @@ -1,51 +1,7 @@ package xonin.backhand; -import net.minecraft.block.Block; import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityList; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.Item; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemBucket; -import net.minecraft.item.ItemDoor; -import net.minecraft.item.ItemMonsterPlacer; -import net.minecraft.item.ItemRedstone; -import net.minecraft.item.ItemReed; -import net.minecraft.item.ItemSeedFood; -import net.minecraft.item.ItemSign; -import net.minecraft.item.ItemSkull; -import net.minecraft.item.ItemStack; -import net.minecraft.util.MathHelper; -import net.minecraft.util.MovingObjectPosition; -import net.minecraft.util.Vec3; -import net.minecraft.world.World; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.FakePlayer; -import net.minecraftforge.event.ForgeEventFactory; -import net.minecraftforge.event.entity.EntityEvent; -import net.minecraftforge.event.entity.EntityJoinWorldEvent; -import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent; -import net.minecraftforge.event.entity.player.PlayerEvent; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; - -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.eventhandler.EventPriority; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import xonin.backhand.api.PlayerEventChild; -import xonin.backhand.api.core.BackhandTranslator; -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.api.core.InventoryPlayerBackhand; -import xonin.backhand.api.core.OffhandExtendedProperty; -import xonin.backhand.packet.OffhandConfigSyncPacket; -import xonin.backhand.packet.OffhandPlaceBlockPacket; -import xonin.backhand.packet.OffhandSyncItemPacket; -import xonin.backhand.utils.BackhandConfig; -import xonin.backhand.utils.EnumAnimations; public final class HookContainerClass { @@ -57,321 +13,260 @@ private boolean isFake(Entity entity) { return entity instanceof FakePlayer; } - @SubscribeEvent(priority = EventPriority.LOWEST) - public void onEntityJoin(EntityJoinWorldEvent event) { - if (event.entity instanceof EntityPlayer player && !(isFake(player))) { - if (FMLCommonHandler.instance() - .getEffectiveSide() == Side.SERVER) { - if (!(player.inventory instanceof InventoryPlayerBackhand)) { - Backhand.LOGGER.info("Player inventory has been replaced with {}", player.inventory.getClass()); - } - Backhand.packetHandler.sendPacketToPlayer( - new OffhandConfigSyncPacket(player).generatePacket(), - (EntityPlayerMP) event.entity); - Backhand.packetHandler.sendPacketToPlayer( - new OffhandSyncItemPacket(player).generatePacket(), - (EntityPlayerMP) event.entity); - } - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - if (Backhand.isOffhandBlacklisted(offhandItem)) { - BackhandUtils.setPlayerOffhandItem(player, null); - if (!player.inventory.addItemStackToInventory(offhandItem)) { - event.entity.entityDropItem(offhandItem, 0); - } - } - } - } - - @SubscribeEvent(priority = EventPriority.HIGHEST) - public void onEntityConstructing(EntityEvent.EntityConstructing event) { - if (!(event.entity instanceof EntityPlayer player && !(isFake(event.entity)))) return; - event.entity.registerExtendedProperties("OffhandStorage", new OffhandExtendedProperty(player)); - } - - public static MovingObjectPosition getRaytraceBlock(EntityPlayer p) { - float scaleFactor = 1.0F; - float rotPitch = p.prevRotationPitch + (p.rotationPitch - p.prevRotationPitch) * scaleFactor; - float rotYaw = p.prevRotationYaw + (p.rotationYaw - p.prevRotationYaw) * scaleFactor; - double testX = p.prevPosX + (p.posX - p.prevPosX) * scaleFactor; - double testY = p.prevPosY + (p.posY - p.prevPosY) * scaleFactor + 1.62D - p.yOffset;// 1.62 is player eye height - double testZ = p.prevPosZ + (p.posZ - p.prevPosZ) * scaleFactor; - Vec3 testVector = Vec3.createVectorHelper(testX, testY, testZ); - float var14 = MathHelper.cos(-rotYaw * 0.017453292F - (float) Math.PI); - float var15 = MathHelper.sin(-rotYaw * 0.017453292F - (float) Math.PI); - float var16 = -MathHelper.cos(-rotPitch * 0.017453292F); - float vectorY = MathHelper.sin(-rotPitch * 0.017453292F); - float vectorX = var15 * var16; - float vectorZ = var14 * var16; - double reachLength = 5.0D; - Vec3 testVectorFar = testVector.addVector(vectorX * reachLength, vectorY * reachLength, vectorZ * reachLength); - return p.worldObj.rayTraceBlocks(testVector, testVectorFar, false); - } - - @SubscribeEvent - public void playerInteract(PlayerInteractEvent event) { - if (isFake(event.entityPlayer)) return; - - if (!BackhandConfig.EmptyOffhand && BackhandUtils.getOffhandItem(event.entityPlayer) == null) { - return; - } - - if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR - || event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) {// Right click - ItemStack mainHandItem = event.entityPlayer.getCurrentEquippedItem(); - ItemStack offhandItem = BackhandUtils.getOffhandItem(event.entityPlayer); - - if (mainHandItem != null - && (BackhandUtils.checkForRightClickFunction(mainHandItem) || offhandItem == null)) { - return; - } - - if (event.action != PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK && Backhand.proxy.isRightClickHeld()) { - Backhand.proxy.setRightClickCounter(Backhand.proxy.getRightClickCounter() + 1); - if (Backhand.proxy.getRightClickCounter() > 1) { - return; - } - } - - if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK && mainHandItem != null - && mainHandItem.getItem() instanceof ItemMonsterPlacer) { - if (event.world.isRemote && !event.entityPlayer.capabilities.isCreativeMode) { - mainHandItem.stackSize--; - } - } - - boolean swingHand = true; - PlayerInteractEvent.Result blk = event.useBlock; - PlayerInteractEvent.Result itm = event.useItem; - event.useBlock = PlayerInteractEvent.Result.DENY; - MovingObjectPosition mop = getRaytraceBlock(event.entityPlayer); - if (mop != null) { - event.setCanceled(true); - int i = mop.blockX, j = mop.blockY, k = mop.blockZ; - - if (!event.entityPlayer.isSneaking() - && canBlockBeInteractedWith(event.entityPlayer.worldObj, i, j, k)) { - event.setCanceled(false); - event.useBlock = blk; - event.useItem = itm; - swingHand = false; - } - } - if (event.entityPlayer.worldObj.isRemote && !BackhandUtils.usagePriorAttack(offhandItem) - && BackhandConfig.OffhandAttack - && swingHand) { - HookContainerClass.sendOffSwingEventNoCheck(event.entityPlayer, mainHandItem, offhandItem); - } - } - } - - private static String[] activatedBlockMethodNames = { - BackhandTranslator.getMapedMethodName("Block", "func_149727_a", "onBlockActivated"), - BackhandTranslator.getMapedMethodName("Block", "func_149699_a", "onBlockClicked") }; - private static Class[][] activatedBlockMethodParams = { - new Class[] { World.class, int.class, int.class, int.class, EntityPlayer.class, int.class, float.class, - float.class, float.class }, - new Class[] { World.class, int.class, int.class, int.class, EntityPlayer.class } }; - - @SuppressWarnings("unchecked") - public static boolean canBlockBeInteractedWith(World worldObj, int x, int y, int z) { - if (worldObj == null) return false; - Block block = worldObj.getBlock(x, y, z); - if (block == null) return false; - if (block.getClass() - .equals(Block.class)) return false; - try { - Class c = block.getClass(); - while (!(c.equals(Block.class))) { - try { - try { - c.getDeclaredMethod(activatedBlockMethodNames[0], activatedBlockMethodParams[0]); - return true; - } catch (NoSuchMethodException ignored) {} - - try { - c.getDeclaredMethod(activatedBlockMethodNames[1], activatedBlockMethodParams[1]); - return true; - } catch (NoSuchMethodException ignored) {} - } catch (NoClassDefFoundError ignored) { - - } - - c = c.getSuperclass(); - } - - return false; - } catch (NullPointerException e) { - return true; - } - } - - public static boolean changedHeldItemTooltips = false; - // used in hostwapping the item to dig with, to remember where to return the main slot to - public static int prevOffhandOffset; - - public static boolean isItemBlock(Item item) { - return item instanceof ItemBlock || item instanceof ItemDoor - || item instanceof ItemSign - || item instanceof ItemReed - || item instanceof ItemSeedFood - || item instanceof ItemRedstone - || item instanceof ItemBucket - || item instanceof ItemSkull; - } - - /** - * Attempts to right-click-use an item by the given EntityPlayer - */ - public static boolean tryUseItem(EntityPlayer player, ItemStack itemStack, Side side) { - if (side.isClient()) { - Backhand.packetHandler.sendPacketToServer( - new OffhandPlaceBlockPacket(-1, -1, -1, 255, itemStack, 0.0F, 0.0F, 0.0F).generatePacket()); - } - - if (itemStack == null || player.getCurrentEquippedItem() == null) return false; - - final int i = itemStack.stackSize; - final int j = itemStack.getItemDamage(); - ItemStack prevHeldItem = player.getCurrentEquippedItem(); - - player.setCurrentItemOrArmor(0, itemStack); - ItemStack itemUsed = player.getCurrentEquippedItem() - .copy(); - ItemStack itemStackResult = itemStack.useItemRightClick(player.getEntityWorld(), player); - if (!ItemStack.areItemStacksEqual(itemUsed, itemStackResult)) { - BackhandUtils.setPlayerOffhandItem(player, itemStackResult); - BackhandUtils.getOffhandEP(player).syncOffhand = true; - if (player.getCurrentEquippedItem() == null || player.getCurrentEquippedItem().stackSize == 0) { - ForgeEventFactory.onPlayerDestroyItem(player, player.getCurrentEquippedItem()); - } - } - - player.setCurrentItemOrArmor(0, prevHeldItem); - CommonProxy.offhandItemUsed = itemStackResult; - - if (itemStackResult == itemStack && itemStackResult.stackSize == i - && (!side.isServer() - || itemStackResult.getMaxItemUseDuration() <= 0 && itemStackResult.getItemDamage() == j)) { - return false; - } else { - BackhandUtils.setPlayerOffhandItem(player, itemStackResult); - if (side.isServer() && (player).capabilities.isCreativeMode) { - itemStackResult.stackSize = i; - if (itemStackResult.isItemStackDamageable()) { - itemStackResult.setItemDamage(j); - } - } - if (itemStackResult.stackSize <= 0) { - BackhandUtils.setPlayerOffhandItem(player, null); - ForgeEventFactory.onPlayerDestroyItem(player, itemStackResult); - } - if (side.isServer() && !player.isUsingItem()) { - ((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer); - } - return true; - } - } - - @SideOnly(Side.CLIENT) - public static void sendOffSwingEvent(PlayerEvent event, ItemStack mainHandItem, ItemStack offhandItem) { - if (!MinecraftForge.EVENT_BUS.post(new PlayerEventChild.OffhandSwingEvent(event, mainHandItem, offhandItem))) { - ((IBackhandPlayer) event.entityPlayer).swingOffItem(); - Backhand.proxy.sendAnimationPacket(EnumAnimations.OffHandSwing, event.entityPlayer); - } - } - - @SideOnly(Side.CLIENT) - public static void sendOffSwingEventNoCheck(EntityPlayer player, ItemStack mainHandItem, ItemStack offhandItem) { - ((IBackhandPlayer) player).swingOffItem(); - Backhand.proxy.sendAnimationPacket(EnumAnimations.OffHandSwing, player); - } - - @SideOnly(Side.CLIENT) - @SubscribeEvent(priority = EventPriority.HIGHEST) - public void onOffhandSwing(PlayerEventChild.OffhandSwingEvent event) {} - - public boolean interactWithNoEvent(EntityPlayer pl, Entity p_70998_1_) { - ItemStack itemstack = pl.getCurrentEquippedItem(); - ItemStack itemstack1 = ItemStack.copyItemStack(itemstack); - - if (!p_70998_1_.interactFirst(pl)) { - if (itemstack != null && p_70998_1_ instanceof EntityLivingBase) { - if (pl.capabilities.isCreativeMode) { - itemstack = itemstack1; - } - - if (itemstack.interactWithEntity(pl, (EntityLivingBase) p_70998_1_)) { - if (itemstack.stackSize <= 0 && !pl.capabilities.isCreativeMode) { - pl.destroyCurrentEquippedItem(); - } - - return true; - } - } - - return false; - } else { - if (itemstack != null && itemstack == pl.getCurrentEquippedItem()) { - if (itemstack.stackSize <= 0 && !pl.capabilities.isCreativeMode) { - pl.destroyCurrentEquippedItem(); - } else if (itemstack.stackSize < itemstack1.stackSize && pl.capabilities.isCreativeMode) { - itemstack.stackSize = itemstack1.stackSize; - } - } - - return true; - } - } - - @SubscribeEvent(priority = EventPriority.HIGHEST) - public void onOffhandAttack(PlayerEventChild.OffhandAttackEvent event) { - if (event.offHand != null) { - if (hasEntityInteraction( - event.getPlayer().capabilities.isCreativeMode ? event.offHand.copy() : event.offHand, - event.getTarget(), - event.getPlayer(), - false)) { - event.setCanceled(true); - if (event.offHand.stackSize <= 0 && !event.getPlayer().capabilities.isCreativeMode) { - ItemStack orig = event.offHand; - BackhandUtils.setPlayerOffhandItem(event.getPlayer(), null); - MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(event.getPlayer(), orig)); - } - } - } - } - - /** - * Check if a stack has a specific interaction with an entity. - * Use a call to {@link net.minecraft.item.ItemStack#interactWithEntity(EntityPlayer, EntityLivingBase)} - * - * @param itemStack to interact last with - * @param entity to interact first with - * @param entityPlayer holding the stack - * @param asTest if data should be cloned before testing - * @return true if a specific interaction exist (and has been done if asTest is false) - */ - private boolean hasEntityInteraction(ItemStack itemStack, Entity entity, EntityPlayer entityPlayer, - boolean asTest) { - if (asTest) { - Entity clone = EntityList.createEntityByName(EntityList.getEntityString(entity), entity.worldObj); - if (clone != null) { - clone.copyDataFrom(entity, true); - return !clone.interactFirst(entityPlayer) && clone instanceof EntityLivingBase - && itemStack.copy() - .interactWithEntity(entityPlayer, (EntityLivingBase) clone); - } - } else if (!entity.interactFirst(entityPlayer) && entity instanceof EntityLivingBase) { - return itemStack.interactWithEntity(entityPlayer, (EntityLivingBase) entity); - } - return false; - } - - @SubscribeEvent - public void addTracking(PlayerEvent.StartTracking event) { - if (event.target instanceof EntityPlayer && !isFake(event.target)) { - ((EntityPlayerMP) event.entityPlayer).playerNetServerHandler - .sendPacket(new OffhandSyncItemPacket((EntityPlayer) event.target).generatePacket()); - } - } + // @SubscribeEvent(priority = EventPriority.LOWEST) + // public void onEntityJoin(EntityJoinWorldEvent event) { + // if (event.entity instanceof EntityPlayer player && !(isFake(player))) { + // if (FMLCommonHandler.instance() + // .getEffectiveSide() == Side.SERVER) { + // if (!(player.inventory instanceof InventoryPlayerBackhand)) { + // Backhand.LOGGER.info("Player inventory has been replaced with {}", player.inventory.getClass()); + // } + // Backhand.packetHandler.sendPacketToPlayer( + // new OffhandConfigSyncPacket(player).generatePacket(), + // (EntityPlayerMP) event.entity); + // Backhand.packetHandler.sendPacketToPlayer( + // new OffhandSyncItemPacket(player).generatePacket(), + // (EntityPlayerMP) event.entity); + // } + // ItemStack offhandItem = BackhandUtils.getOffhandItem(player); + // if (Backhand.isOffhandBlacklisted(offhandItem)) { + // BackhandUtils.setPlayerOffhandItem(player, null); + // if (!player.inventory.addItemStackToInventory(offhandItem)) { + // event.entity.entityDropItem(offhandItem, 0); + // } + // } + // } + // } + + // @SubscribeEvent(priority = EventPriority.HIGHEST) + // public void onEntityConstructing(EntityEvent.EntityConstructing event) { + // if (!(event.entity instanceof EntityPlayer player && !(isFake(event.entity)))) return; + // event.entity.registerExtendedProperties("OffhandStorage", new OffhandExtendedProperty(player)); + // } + + // public static MovingObjectPosition getRaytraceBlock(EntityPlayer p) { + // float scaleFactor = 1.0F; + // float rotPitch = p.prevRotationPitch + (p.rotationPitch - p.prevRotationPitch) * scaleFactor; + // float rotYaw = p.prevRotationYaw + (p.rotationYaw - p.prevRotationYaw) * scaleFactor; + // double testX = p.prevPosX + (p.posX - p.prevPosX) * scaleFactor; + // double testY = p.prevPosY + (p.posY - p.prevPosY) * scaleFactor + 1.62D - p.yOffset;// 1.62 is player eye height + // double testZ = p.prevPosZ + (p.posZ - p.prevPosZ) * scaleFactor; + // Vec3 testVector = Vec3.createVectorHelper(testX, testY, testZ); + // float var14 = MathHelper.cos(-rotYaw * 0.017453292F - (float) Math.PI); + // float var15 = MathHelper.sin(-rotYaw * 0.017453292F - (float) Math.PI); + // float var16 = -MathHelper.cos(-rotPitch * 0.017453292F); + // float vectorY = MathHelper.sin(-rotPitch * 0.017453292F); + // float vectorX = var15 * var16; + // float vectorZ = var14 * var16; + // double reachLength = 5.0D; + // Vec3 testVectorFar = testVector.addVector(vectorX * reachLength, vectorY * reachLength, vectorZ * reachLength); + // return p.worldObj.rayTraceBlocks(testVector, testVectorFar, false); + // } + + // // @SubscribeEvent + // public void playerInteract(PlayerInteractEvent event) { + // if (isFake(event.entityPlayer)) return; + // + // if (!BackhandConfig.EmptyOffhand && BackhandUtils.getOffhandItem(event.entityPlayer) == null) { + // return; + // } + // + // if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR + // || event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) {// Right click + // ItemStack mainHandItem = event.entityPlayer.getCurrentEquippedItem(); + // ItemStack offhandItem = BackhandUtils.getOffhandItem(event.entityPlayer); + // + // if (mainHandItem != null + // && (BackhandUtils.checkForRightClickFunction(mainHandItem) || offhandItem == null)) { + // return; + // } + // + // if (event.action != PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK && Backhand.proxy.isRightClickHeld()) { + // Backhand.proxy.setRightClickCounter(Backhand.proxy.getRightClickCounter() + 1); + // if (Backhand.proxy.getRightClickCounter() > 1) { + // return; + // } + // } + // + // if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK && mainHandItem != null + // && mainHandItem.getItem() instanceof ItemMonsterPlacer) { + // if (event.world.isRemote && !event.entityPlayer.capabilities.isCreativeMode) { + // mainHandItem.stackSize--; + // } + // } + // + // boolean swingHand = true; + // PlayerInteractEvent.Result blk = event.useBlock; + // PlayerInteractEvent.Result itm = event.useItem; + // event.useBlock = PlayerInteractEvent.Result.DENY; + // MovingObjectPosition mop = getRaytraceBlock(event.entityPlayer); + // if (mop != null) { + // event.setCanceled(true); + // int i = mop.blockX, j = mop.blockY, k = mop.blockZ; + // + // if (!event.entityPlayer.isSneaking() + // && canBlockBeInteractedWith(event.entityPlayer.worldObj, i, j, k)) { + // event.setCanceled(false); + // event.useBlock = blk; + // event.useItem = itm; + // swingHand = false; + // } + // } + // if (event.entityPlayer.worldObj.isRemote && !BackhandUtils.usagePriorAttack(offhandItem) + // && BackhandConfig.OffhandAttack + // && swingHand) { + // HookContainerClass.sendOffSwingEventNoCheck(event.entityPlayer, mainHandItem, offhandItem); + // } + // } + // } + + // private static String[] activatedBlockMethodNames = { + // BackhandTranslator.getMapedMethodName("Block", "func_149727_a", "onBlockActivated"), + // BackhandTranslator.getMapedMethodName("Block", "func_149699_a", "onBlockClicked") }; + // private static Class[][] activatedBlockMethodParams = { + // new Class[] { World.class, int.class, int.class, int.class, EntityPlayer.class, int.class, float.class, + // float.class, float.class }, + // new Class[] { World.class, int.class, int.class, int.class, EntityPlayer.class } }; + // + // @SuppressWarnings("unchecked") + // public static boolean canBlockBeInteractedWith(World worldObj, int x, int y, int z) { + // if (worldObj == null) return false; + // Block block = worldObj.getBlock(x, y, z); + // if (block == null) return false; + // if (block.getClass() + // .equals(Block.class)) return false; + // try { + // Class c = block.getClass(); + // while (!(c.equals(Block.class))) { + // try { + // try { + // c.getDeclaredMethod(activatedBlockMethodNames[0], activatedBlockMethodParams[0]); + // return true; + // } catch (NoSuchMethodException ignored) {} + // + // try { + // c.getDeclaredMethod(activatedBlockMethodNames[1], activatedBlockMethodParams[1]); + // return true; + // } catch (NoSuchMethodException ignored) {} + // } catch (NoClassDefFoundError ignored) { + // + // } + // + // c = c.getSuperclass(); + // } + // + // return false; + // } catch (NullPointerException e) { + // return true; + // } + // } + // + // public static boolean changedHeldItemTooltips = false; + // // used in hostwapping the item to dig with, to remember where to return the main slot to + // public static int prevOffhandOffset; + // + // public static boolean isItemBlock(Item item) { + // return item instanceof ItemBlock || item instanceof ItemDoor + // || item instanceof ItemSign + // || item instanceof ItemReed + // || item instanceof ItemSeedFood + // || item instanceof ItemRedstone + // || item instanceof ItemBucket + // || item instanceof ItemSkull; + // } + // + // @SideOnly(Side.CLIENT) + // public static void sendOffSwingEventNoCheck(EntityPlayer player, ItemStack mainHandItem, ItemStack offhandItem) { + // ((IBackhandPlayer) player).swingOffItem(); + // Backhand.proxy.sendAnimationPacket(EnumAnimations.OffHandSwing, player); + // } + // + // // @SideOnly(Side.CLIENT) + // // @SubscribeEvent(priority = EventPriority.HIGHEST) + // // public void onOffhandSwing(PlayerEventChild.OffhandSwingEvent event) {} + // + // public boolean interactWithNoEvent(EntityPlayer pl, Entity p_70998_1_) { + // ItemStack itemstack = pl.getCurrentEquippedItem(); + // ItemStack itemstack1 = ItemStack.copyItemStack(itemstack); + // + // if (!p_70998_1_.interactFirst(pl)) { + // if (itemstack != null && p_70998_1_ instanceof EntityLivingBase) { + // if (pl.capabilities.isCreativeMode) { + // itemstack = itemstack1; + // } + // + // if (itemstack.interactWithEntity(pl, (EntityLivingBase) p_70998_1_)) { + // if (itemstack.stackSize <= 0 && !pl.capabilities.isCreativeMode) { + // pl.destroyCurrentEquippedItem(); + // } + // + // return true; + // } + // } + // + // return false; + // } else { + // if (itemstack != null && itemstack == pl.getCurrentEquippedItem()) { + // if (itemstack.stackSize <= 0 && !pl.capabilities.isCreativeMode) { + // pl.destroyCurrentEquippedItem(); + // } else if (itemstack.stackSize < itemstack1.stackSize && pl.capabilities.isCreativeMode) { + // itemstack.stackSize = itemstack1.stackSize; + // } + // } + // + // return true; + // } + // } + // + // // @SubscribeEvent(priority = EventPriority.HIGHEST) + // // public void onOffhandAttack(PlayerEventChild.OffhandAttackEvent event) { + // // if (event.offHand != null) { + // // if (hasEntityInteraction( + // // event.getPlayer().capabilities.isCreativeMode ? event.offHand.copy() : event.offHand, + // // event.getTarget(), + // // event.getPlayer(), + // // false)) { + // // event.setCanceled(true); + // // if (event.offHand.stackSize <= 0 && !event.getPlayer().capabilities.isCreativeMode) { + // // ItemStack orig = event.offHand; + // // BackhandUtils.setPlayerOffhandItem(event.getPlayer(), null); + // // MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(event.getPlayer(), orig)); + // // } + // // } + // // } + // // } + // + // /** + // * Check if a stack has a specific interaction with an entity. + // * Use a call to {@link net.minecraft.item.ItemStack#interactWithEntity(EntityPlayer, EntityLivingBase)} + // * + // * @param itemStack to interact last with + // * @param entity to interact first with + // * @param entityPlayer holding the stack + // * @param asTest if data should be cloned before testing + // * @return true if a specific interaction exist (and has been done if asTest is false) + // */ + // private boolean hasEntityInteraction(ItemStack itemStack, Entity entity, EntityPlayer entityPlayer, + // boolean asTest) { + // if (asTest) { + // Entity clone = EntityList.createEntityByName(EntityList.getEntityString(entity), entity.worldObj); + // if (clone != null) { + // clone.copyDataFrom(entity, true); + // return !clone.interactFirst(entityPlayer) && clone instanceof EntityLivingBase + // && itemStack.copy() + // .interactWithEntity(entityPlayer, (EntityLivingBase) clone); + // } + // } else if (!entity.interactFirst(entityPlayer) && entity instanceof EntityLivingBase) { + // return itemStack.interactWithEntity(entityPlayer, (EntityLivingBase) entity); + // } + // return false; + // } + + // @SubscribeEvent + // public void addTracking(PlayerEvent.StartTracking event) { + // if (event.target instanceof EntityPlayer && !isFake(event.target)) { + // ((EntityPlayerMP) event.entityPlayer).playerNetServerHandler + // .sendPacket(new OffhandSyncItemPacket((EntityPlayer) event.target).generatePacket()); + // } + // } } diff --git a/src/main/java/xonin/backhand/ServerEventsHandler.java b/src/main/java/xonin/backhand/ServerEventsHandler.java deleted file mode 100644 index 7928dae..0000000 --- a/src/main/java/xonin/backhand/ServerEventsHandler.java +++ /dev/null @@ -1,158 +0,0 @@ -package xonin.backhand; - -import net.minecraft.block.BlockDispenser; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; -import net.minecraft.item.Item; -import net.minecraft.item.ItemBow; -import net.minecraft.item.ItemStack; -import net.minecraft.util.RegistrySimple; -import net.minecraftforge.event.entity.living.LivingDeathEvent; -import net.minecraftforge.event.entity.living.LivingHurtEvent; -import net.minecraftforge.event.entity.player.ArrowNockEvent; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; -import net.minecraftforge.event.entity.player.PlayerUseItemEvent; - -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.eventhandler.EventPriority; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.relauncher.Side; -import ganymedes01.etfuturum.ModItems; -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.api.core.OffhandExtendedProperty; -import xonin.backhand.utils.BackhandConfig; - -public class ServerEventsHandler { - - public static int fireworkHotSwapped = -1; - - @SubscribeEvent - public void onPlayerInteractNonVanilla(PlayerInteractEvent event) { - if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR) { - EntityPlayer player = event.entityPlayer; - ItemStack mainhandItem = player.getHeldItem(); - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - if ((mainhandItem == null - || mainhandItem.getItem() != Items.fireworks && !BackhandUtils.usagePriorAttack(mainhandItem)) - && offhandItem != null - && offhandItem.getItem() == Items.fireworks) { - fireworkHotSwapped = 1; - } - } - } - - @SubscribeEvent - public void onLivingDeath(LivingDeathEvent event) { - if (!(event.entityLiving instanceof EntityPlayer player)) return; - - if (!BackhandUtils.hasOffhandInventory(player)) { - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - player.func_146097_a(offhandItem, true, false); - BackhandUtils.setPlayerOffhandItem(player, null); - } - } - - @SubscribeEvent - public void onLivingHurt(LivingHurtEvent event) { - if (!(event.entityLiving instanceof EntityPlayer player) || event.entityLiving.getHealth() - event.ammount > 0) - return; - - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - ItemStack mainhandItem = player.getCurrentEquippedItem(); - OffhandExtendedProperty offhandProp = BackhandUtils.getOffhandEP(player); - - if (offhandItem == null) { - return; - } - - if (Backhand.isEFRLoaded) { - Item totem = ModItems.TOTEM_OF_UNDYING.get(); - - if (offhandItem.getItem() == totem && (mainhandItem == null || mainhandItem.getItem() != totem)) { - BackhandUtils.swapOffhandItem(player, 5); - offhandProp.regularHotSwap = true; - } - } - } - - @SubscribeEvent - public void onItemUseStart(PlayerUseItemEvent.Start event) { - EntityPlayer player = event.entityPlayer; - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - ItemStack mainhandItem = player.getCurrentEquippedItem(); - boolean mainhandUse = BackhandUtils.checkForRightClickFunction(mainhandItem); - - if (offhandItem != null && !mainhandUse) { - event.setCanceled(true); - } - } - - @SubscribeEvent(priority = EventPriority.HIGHEST) - public void onArrowNock(ArrowNockEvent event) { - if (!BackhandConfig.UseOffhandArrows) { - return; - } - EntityPlayer player = event.entityPlayer; - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - if (offhandItem != null) { - if (!((RegistrySimple) BlockDispenser.dispenseBehaviorRegistry).containsKey(offhandItem.getItem())) { - return; - } - - event.setCanceled(true); - player.setItemInUse( - event.result, - event.result.getItem() - .getMaxItemUseDuration(event.result)); - } - } - - @SubscribeEvent - public void onItemFinish(PlayerUseItemEvent.Finish event) { - EntityPlayer player = event.entityPlayer; - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - ItemStack mainhandItem = player.getCurrentEquippedItem(); - OffhandExtendedProperty offhandProp = BackhandUtils.getOffhandEP(player); - boolean mainhandUse = BackhandUtils.checkForRightClickFunction(mainhandItem); - if (offhandItem == null || mainhandUse || offhandProp == null) { - return; - } - if (FMLCommonHandler.instance() - .getEffectiveSide() == Side.SERVER && !ServerTickHandler.tickStartItems.containsKey(player.getUniqueID())) { - BackhandUtils.swapOffhandItem(player); - offhandProp.regularHotSwap = true; - } - } - - @SubscribeEvent(priority = EventPriority.HIGHEST) - public void onItemStop(PlayerUseItemEvent.Stop event) { - EntityPlayer player = event.entityPlayer; - ItemStack mainhandItem = player.getCurrentEquippedItem(); - OffhandExtendedProperty offhandProp = BackhandUtils.getOffhandEP(player); - boolean skip = BackhandUtils.checkForRightClickFunction(mainhandItem) - || BackhandUtils.getOffhandItem(player) == null; - - if (!skip && !ServerTickHandler.tickStartItems.containsKey(player.getUniqueID()) - && !offhandProp.regularHotSwap) { - BackhandUtils.swapOffhandItem(player); - offhandProp.regularHotSwap = true; - return; - } - - if (!BackhandConfig.UseOffhandArrows || !(event.item.getItem() instanceof ItemBow)) { - return; - } - - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - if (offhandItem != null) { - if (!((RegistrySimple) BlockDispenser.dispenseBehaviorRegistry).containsKey(offhandItem.getItem())) { - return; - } - - if (offhandItem.getItem() != Items.arrow) { - offhandProp.regularHotSwap = true; - BackhandUtils.swapOffhandItem(player, 5); - } - } - } -} diff --git a/src/main/java/xonin/backhand/ServerTickHandler.java b/src/main/java/xonin/backhand/ServerTickHandler.java deleted file mode 100644 index 0e80357..0000000 --- a/src/main/java/xonin/backhand/ServerTickHandler.java +++ /dev/null @@ -1,155 +0,0 @@ -package xonin.backhand; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.ItemStack; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; - -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.eventhandler.EventPriority; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.common.gameevent.TickEvent; -import cpw.mods.fml.relauncher.Side; -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.api.core.OffhandExtendedProperty; -import xonin.backhand.packet.OffhandSyncItemPacket; -import xonin.backhand.utils.BackhandConfig; - -public class ServerTickHandler { - - public ItemStack prevStackInSlot; - public int blacklistDelay = -1; - - public static final HashMap> tickStartItems = new HashMap<>(); - - public static void resetTickingHotswap(EntityPlayerMP player) { - List tickedItems = tickStartItems.get(player.getUniqueID()); - if (tickedItems != null) { - BackhandUtils.getOffhandEP(player).ignoreSetSlot = false; - player.setCurrentItemOrArmor(0, tickedItems.get(0)); - BackhandUtils.setPlayerOffhandItem(player, tickedItems.get(1)); - tickStartItems.remove(player.getUniqueID()); - } - } - - public static void tickHotswap(EntityPlayerMP player) { - ItemStack mainhand = ItemStack.copyItemStack(player.getCurrentEquippedItem()); - ItemStack offhand = BackhandUtils.getOffhandItem(player); - if (offhand == null || player.currentWindowId != 0) return; - UUID key = player.getUniqueID(); - if (!BackhandUtils.checkForRightClickFunction(mainhand)) { - if (!tickStartItems.containsKey(key)) { - BackhandUtils.getOffhandEP(player).ignoreSetSlot = true; - } - tickStartItems.put(key, Arrays.asList(mainhand, offhand)); - player.setCurrentItemOrArmor( - 0, - tickStartItems.get(key) - .get(1)); - BackhandUtils.getOffhandEP(player).activeSlot = player.inventory.currentItem - + player.inventory.mainInventory.length; - } - } - - @SubscribeEvent - public void onUpdateWorld(TickEvent.WorldTickEvent event) { - if (FMLCommonHandler.instance() - .getEffectiveSide() != Side.SERVER) { - return; - } - if (BackhandConfig.OffhandTickHotswap) { - for (EntityPlayer player : event.world.playerEntities) { - if (!(player instanceof EntityPlayerMP playerMP)) continue; - OffhandExtendedProperty offhandProp = BackhandUtils.getOffhandEP(player); - - if (event.phase == TickEvent.Phase.START && !player.isUsingItem() - && offhandProp.tickingHotswapDelay <= 0) { - tickHotswap(playerMP); - } else { - if (offhandProp.tickingHotswapDelay > 0) offhandProp.tickingHotswapDelay--; - resetTickingHotswap(playerMP); - } - } - } - } - - @SubscribeEvent(priority = EventPriority.HIGHEST) - public void onUpdatePlayer(TickEvent.PlayerTickEvent event) { - EntityPlayer player = event.player; - OffhandExtendedProperty offhandProp = BackhandUtils.getOffhandEP(player); - - if (FMLCommonHandler.instance() - .getEffectiveSide() != Side.SERVER) { - if (offhandProp.regularHotSwap) { - BackhandUtils.swapOffhandItem(player); - offhandProp.regularHotSwap = false; - } - return; - } - - ItemStack offhand = BackhandUtils.getOffhandItem(player); - - if (event.phase == TickEvent.Phase.END) { - if (blacklistDelay > 0) { - blacklistDelay--; - } - if (Backhand.isOffhandBlacklisted(offhand)) { - if (!ItemStack.areItemStacksEqual(offhand, prevStackInSlot)) { - blacklistDelay = 10; - } else if (blacklistDelay == 0) { - BackhandUtils.setPlayerOffhandItem(player, null); - - boolean foundSlot = false; - for (int i = 0; i < player.inventory.getSizeInventory() - 4; i++) { - if (i == BackhandConfig.AlternateOffhandSlot) continue; - if (player.inventory.getStackInSlot(i) == null) { - player.inventory.setInventorySlotContents(i, offhand); - foundSlot = true; - break; - } - } - if (!foundSlot) { - player.entityDropItem(offhand, 0); - } - player.inventoryContainer.detectAndSendChanges(); - } - } - prevStackInSlot = offhand; - } - - if (offhandProp.syncOffhand) { - if (!tickStartItems.containsKey(player.getUniqueID())) { - Backhand.packetHandler.sendPacketToAll(new OffhandSyncItemPacket(player).generatePacket()); - } - offhandProp.syncOffhand = false; - } - - if (offhandProp.regularHotSwap) { - BackhandUtils.swapOffhandItem(player, 5); - offhandProp.regularHotSwap = false; - } - - if (ServerEventsHandler.fireworkHotSwapped > 0) { - ServerEventsHandler.fireworkHotSwapped--; - } else if (ServerEventsHandler.fireworkHotSwapped == 0) { - BackhandUtils.swapOffhandItem(player); - ServerEventsHandler.fireworkHotSwapped--; - MinecraftForge.EVENT_BUS.post( - new PlayerInteractEvent( - player, - PlayerInteractEvent.Action.RIGHT_CLICK_AIR, - (int) player.posX, - (int) player.posY, - (int) player.posZ, - -1, - player.worldObj)); - BackhandUtils.swapOffhandItem(player); - } - } -} diff --git a/src/main/java/xonin/backhand/api/PlayerEventChild.java b/src/main/java/xonin/backhand/api/PlayerEventChild.java deleted file mode 100644 index ebb0239..0000000 --- a/src/main/java/xonin/backhand/api/PlayerEventChild.java +++ /dev/null @@ -1,167 +0,0 @@ -package xonin.backhand.api; - -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraftforge.event.entity.player.EntityInteractEvent; -import net.minecraftforge.event.entity.player.PlayerEvent; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; - -import cpw.mods.fml.common.eventhandler.Cancelable; - -public abstract class PlayerEventChild extends PlayerEvent { - - /** - * The event that this event is a child of - */ - public final PlayerEvent parent; - - public PlayerEventChild(PlayerEvent parent) { - super(parent.entityPlayer); - this.parent = parent; - } - - public void setCancelParentEvent(boolean cancel) { - parent.setCanceled(cancel); - } - - @Override - public void setCanceled(boolean cancel) { - super.setCanceled(cancel); - parent.setCanceled(cancel); - } - - @Override - public void setResult(Result value) { - super.setResult(value); - parent.setResult(value); - } - - public EntityPlayer getPlayer() { - return parent.entityPlayer; - } - - /** - * Called when a player right clicks in battlemode - * The parent event can be either {@link PlayerInteractEvent} or {@link EntityInteractEvent} if the - * OffhandAttackEvent allowed swinging - * Both {@link ItemStack} can be null - * If cancelled, no offhand swinging will be performed - */ - @Cancelable - public static class OffhandSwingEvent extends PlayerEventChild { - - public final ItemStack mainHand; - public final ItemStack offHand; - - @Deprecated - public OffhandSwingEvent(PlayerEvent parent, ItemStack mainHand, ItemStack offHand) { - this(parent, offHand); - } - - public OffhandSwingEvent(PlayerEvent parent, ItemStack offHand) { - super(parent); - this.mainHand = parent.entityPlayer.getCurrentEquippedItem(); - this.offHand = offHand; - } - - public boolean onEntity() { - return parent instanceof EntityInteractEvent; - } - - public boolean onBlock() { - return parent instanceof PlayerInteractEvent; - } - } - - /** - * Called when a player right clicks an entity in battlemode - * Both {@link ItemStack} can be null - * Cancelling will prevent any further processing and prevails over the boolean fields - */ - @Cancelable - public static class OffhandAttackEvent extends PlayerEventChild { - - /** - * If we should call the OffhandSwingEvent and perform swinging animation - */ - public boolean swingOffhand = true; - /** - * If we should perform an attack on the entity with the offhand item - * Note: Will post AttackEntityEvent and Item#onLeftClickEntity(ItemStack, EntityPlayer, Entity) - * with InventoryPlayer#currentItem offset to the offhand. - */ - public boolean shouldAttack = true; - /** - * If we should Prevent PlayerInteractEvent.Action.RIGHT_CLICK_AIR and - * ItemStack#useItemRightClick(World, EntityPlayer) - * from being called for the item in main hand. - */ - public boolean cancelParent = true; - /** - * The base entity interaction event - * This event has already been posted in EventBus, handled by all potential listeners and was not cancelled. - * Changing its state will have no effect. - */ - public final EntityInteractEvent event; - /** - * Content of the main hand slot - */ - public final ItemStack mainHand; - /** - * Content of the off hand slot - */ - public final ItemStack offHand; - - @Deprecated - public OffhandAttackEvent(EntityInteractEvent parent, ItemStack mainHand, ItemStack offHand) { - this(parent, offHand); - } - - public OffhandAttackEvent(EntityInteractEvent parent, ItemStack offHand) { - super(parent); - this.event = parent; - this.mainHand = parent.entityPlayer.getCurrentEquippedItem(); - this.offHand = offHand; - } - - public Entity getTarget() { - return ((EntityInteractEvent) parent).target; - } - } - - /** - * This event replicates the event usage of {@link PlayerInteractEvent} for the item in left hand on right click, - * allowing support for other mods that use such event to customize item usage - * Item#onItemUseFirst, Item#onItemRightClick and Item#onItemUse will then get called the same way as with the item - * in the player right hand for PlayerInteractEvent - */ - @Cancelable - public static class UseOffhandItemEvent extends PlayerEventChild { - - /** - * If we should call the OffhandSwingEvent and perform swinging animation - */ - public boolean swingOffhand; - /** - * The {@link ItemStack} held in left hand - */ - public final ItemStack offhand; - /** - * The equivalent {@link PlayerInteractEvent} that would have been triggered if the offhand item was held in - * right hand and right click was pressed - */ - public final PlayerInteractEvent event; - - public UseOffhandItemEvent(PlayerInteractEvent event, ItemStack offhand) { - super(event); - this.event = event; - this.offhand = offhand; - this.swingOffhand = onBlock(); - } - - public boolean onBlock() { - return event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK; - } - } -} diff --git a/src/main/java/xonin/backhand/api/core/BackhandUtils.java b/src/main/java/xonin/backhand/api/core/BackhandUtils.java index 1836f4f..0faf443 100644 --- a/src/main/java/xonin/backhand/api/core/BackhandUtils.java +++ b/src/main/java/xonin/backhand/api/core/BackhandUtils.java @@ -1,16 +1,12 @@ package xonin.backhand.api.core; -import java.io.Closeable; -import java.io.IOException; +import java.util.function.BooleanSupplier; import javax.annotation.Nullable; -import net.minecraft.entity.Entity; +import net.minecraft.client.Minecraft; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.SharedMonsterAttributes; -import net.minecraft.entity.ai.attributes.BaseAttributeMap; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.EnumAction; import net.minecraft.item.Item; import net.minecraft.item.ItemBed; @@ -33,21 +29,8 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.ItemSword; import net.minecraft.item.ItemTool; -import net.minecraft.nbt.CompressedStreamTools; -import net.minecraft.nbt.NBTSizeTracker; -import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; -import net.minecraftforge.common.IExtendedEntityProperties; import net.minecraftforge.common.util.FakePlayer; -import net.minecraftforge.event.ForgeEventFactory; -import net.minecraftforge.event.entity.player.AttackEntityEvent; - -import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteArrayDataOutput; - -import xonin.backhand.Backhand; -import xonin.backhand.ServerTickHandler; -import xonin.backhand.utils.BackhandConfig; /** * Store commonly used method, mostly for the {@link EntityPlayer} {@link ItemStack}s management @@ -67,29 +50,10 @@ public class BackhandUtils { float.class, float.class, float.class }, new Class[] { ItemStack.class, World.class, EntityPlayer.class }, new Class[] { ItemStack.class, EntityPlayer.class, EntityLivingBase.class } }; - private static ItemStack prevNotWieldable; - /** - * The generic attack damage key for {@link ItemStack#getAttributeModifiers()} - */ - private static String genericAttack = SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(); - - public static boolean hasOffhandInventory(EntityPlayer player) { - return player.inventory instanceof InventoryPlayerBackhand; - } public static void swapOffhandItem(EntityPlayer player) { - swapOffhandItem(player, 0); - } - - public static void swapOffhandItem(EntityPlayer player, int delayHotswap) { - if (delayHotswap > 0) resetAndDelayHotswap(player, delayHotswap); - final ItemStack mainhandItem = getLegalStack(player.getCurrentEquippedItem()); - final ItemStack offhandItem = getLegalStack(BackhandUtils.getOffhandItem(player)); - BackhandUtils.setPlayerCurrentItem(player, offhandItem); - BackhandUtils.setPlayerOffhandItem(player, mainhandItem); - if (BackhandConfig.UseInventorySlot) { - player.inventoryContainer.detectAndSendChanges(); - } + player.setCurrentItemOrArmor(0, BackhandUtils.getOffhandItem(player)); + BackhandUtils.setPlayerOffhandItem(player, BackhandUtils.getOffhandItem(player)); } public static void setPlayerCurrentItem(EntityPlayer player, ItemStack stack) { @@ -97,95 +61,12 @@ public static void setPlayerCurrentItem(EntityPlayer player, ItemStack stack) { } public static void setPlayerOffhandItem(EntityPlayer player, ItemStack stack) { - if (!Backhand.isOffhandBlacklisted(stack)) { - if (BackhandConfig.UseInventorySlot) { - player.inventory.setInventorySlotContents(BackhandConfig.AlternateOffhandSlot, stack); - } else { - getOffhandEP(player).setOffhandItem(stack); - } - } + ((IOffhandInventory) player.inventory).backhand$setOffhandItem(stack); } public static @Nullable ItemStack getOffhandItem(EntityPlayer player) { if (player instanceof FakePlayer) return null; - - if (BackhandConfig.UseInventorySlot) { - return player.inventory.getStackInSlot(BackhandConfig.AlternateOffhandSlot); - } else { - return getOffhandEP(player).getOffhandItem(); - } - } - - public static OffhandExtendedProperty getOffhandEP(EntityPlayer player) { - IExtendedEntityProperties prop = player.getExtendedProperties("OffhandStorage"); - - if (prop instanceof OffhandExtendedProperty offhandProp) { - return offhandProp; - } - // Probably a fake player, return a dummy property - return new OffhandExtendedProperty(player); - } - - /** - * Defines a generic weapon - * - * @param main the item to check - * @return true if the item is a generic weapon - */ - public static boolean isWeapon(ItemStack main) { - if (main.getMaxStackSize() == 1 && main.getMaxDamage() > 0 && !main.getHasSubtypes())// Usual values for tools, - // sword, and bow - return true; - else if (main == prevNotWieldable)// Prevent lag from check spam - return false; - prevNotWieldable = main; - return false; - } - - /** - * Defines a combination of left hand/right hand items that is valid to wield - * - * @param main item to be in the right hand - * @param off item to be in the left hand - * @return true if the right hand item allows left hand item - */ - public static boolean isMainHand(ItemStack main, ItemStack off) { - if (main == null) return true; - else if (usagePriorAttack(main))// "Usable" item - return off == null || !usagePriorAttack(off);// With empty hand or non "usable item" - else if (isWeapon(main))// A generic weapon - return main.getItemUseAction() == EnumAction.bow || main.getAttributeModifiers() - .containsKey(genericAttack);// With either bow or generic attack, or registered - return false; - } - - /** - * Defines a item which can be wield in the left hand - * - * @param off the item to be wield in left hand - * @return true if the item is allowed in left hand - */ - public static boolean isOffHand(ItemStack off) { - if (off == null) return true; - else if (isWeapon(off))// A generic weapon - return off.getAttributeModifiers() - .containsKey(genericAttack);// with a generic attack or registered - return false; - } - - /** - * Patch in EntityPlayer#onUpdate() to support hotswap of itemInUse - * - * @param entityPlayer - * @param itemInUse - * @return - */ - public static ItemStack getCurrentItemOnUpdate(EntityPlayer entityPlayer, ItemStack itemInUse) { - ItemStack itemStack = getOffhandItem(entityPlayer); - if (itemInUse == itemStack) { - return itemStack; - } - return entityPlayer.getCurrentEquippedItem(); + return ((IOffhandInventory) player.inventory).backhand$getOffhandItem(); } /** @@ -253,31 +134,6 @@ public static boolean isItemBlock(Item item) { || item instanceof ItemSkull; } - @SuppressWarnings("unchecked") - public static boolean hasEntityInteraction(ItemStack stack) { - if (stack == null) { - return false; - } - try { - Class c = stack.getItem() - .getClass(); - while (!(c.equals(Item.class) || c.equals(ItemTool.class) || c.equals(ItemSword.class))) { - try { - try { - c.getDeclaredMethod(itemBlackListMethodNames[2], itemBlackListMethodParams[2]); - return true; - } catch (NoSuchMethodException ignored) {} - } catch (NoClassDefFoundError ignored) {} - - c = c.getSuperclass(); - } - - return false; - } catch (NullPointerException e) { - return true; - } - } - @SuppressWarnings("unchecked") public static boolean checkForRightClickFunction(ItemStack stack) { if (stack == null) { @@ -337,189 +193,21 @@ public static boolean checkForRightClickFunctionNoAction(ItemStack stack) { } } - /** - * Reads a {@link ItemStack} from the InputStream - */ - public static ItemStack readItemStack(ByteArrayDataInput par0DataInputStream) throws IOException { - ItemStack itemstack = null; - int short1 = par0DataInputStream.readInt(); - - if (short1 >= 0) { - byte b0 = par0DataInputStream.readByte(); - short short2 = par0DataInputStream.readShort(); - itemstack = new ItemStack(Item.getItemById(short1), b0, short2); - itemstack.stackTagCompound = readNBTTagCompound(par0DataInputStream); - } - - return itemstack; - } - - /** - * Reads a compressed {@link NBTTagCompound} from the InputStream - */ - public static NBTTagCompound readNBTTagCompound(ByteArrayDataInput par0DataInputStream) throws IOException { - short short1 = par0DataInputStream.readShort(); - - if (short1 < 0) { - return null; - } else { - byte[] abyte = new byte[short1]; - par0DataInputStream.readFully(abyte); - - return CompressedStreamTools.func_152457_a(abyte, NBTSizeTracker.field_152451_a); - } - } - - /** - * Writes a {@link ItemStack} to the OutputStream - * - * @param par1DataOutputStream the output stream - * @param par0ItemStack to write - * @throws IOException - */ - public static void writeItemStack(ByteArrayDataOutput par1DataOutputStream, ItemStack par0ItemStack) - throws IOException { - - if (par0ItemStack == null) { - par1DataOutputStream.writeShort(-1); - } else { - par1DataOutputStream.writeInt(Item.getIdFromItem(par0ItemStack.getItem())); - par1DataOutputStream.writeByte(par0ItemStack.stackSize); - par1DataOutputStream.writeShort(par0ItemStack.getItemDamage()); - NBTTagCompound nbttagcompound = null; - - if (par0ItemStack.getItem() - .isDamageable() - || par0ItemStack.getItem() - .getShareTag()) { - nbttagcompound = par0ItemStack.stackTagCompound; - } - - writeNBTTagCompound(nbttagcompound, par1DataOutputStream); - } - } - - /** - * Writes a compressed {@link NBTTagCompound} to the output - * - * @param par0NBTTagCompound - * @param par1DataOutputStream - * @throws IOException - */ - protected static void writeNBTTagCompound(NBTTagCompound par0NBTTagCompound, - ByteArrayDataOutput par1DataOutputStream) throws IOException { - if (par0NBTTagCompound == null) { - par1DataOutputStream.writeShort(-1); - } else { - byte[] abyte = CompressedStreamTools.compress(par0NBTTagCompound); - par1DataOutputStream.writeShort((short) abyte.length); - par1DataOutputStream.write(abyte); - } - } - - /** - * Basically, a copy of {@link EntityPlayer#attackTargetEntityWithCurrentItem(Entity)}, adapted for the offhand - * Hotswap the "current item" value to the offhand, then refresh the player attributes according to the newly - * selected item - * Reset everything back if the attack is cancelled by {@link AttackEntityEvent} or - * {@link Item#onLeftClickEntity(ItemStack, EntityPlayer, Entity)} - * - * @param player the attacker - * @param par1Entity the attacked - */ - public static void attackTargetEntityWithCurrentOffItem(EntityPlayer player, Entity par1Entity) { - final ItemStack oldItem = player.getCurrentEquippedItem(); - final ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - if (!BackhandConfig.OffhandAttack || (offhandItem == null && !BackhandConfig.EmptyOffhand)) return; - - BackhandUtils.setPlayerCurrentItem(player, offhandItem); - refreshAttributes(player.getAttributeMap(), oldItem, player.getCurrentEquippedItem()); - - int prevDamage = offhandItem != null ? offhandItem.getItemDamage() : 0; - int damage = offhandItem == null ? 1 : offhandItem.getMaxDamage() - offhandItem.getItemDamage(); - player.attackTargetEntityWithCurrentItem(par1Entity); - - refreshAttributes(player.getAttributeMap(), player.getCurrentEquippedItem(), oldItem); - BackhandUtils.setPlayerCurrentItem(player, oldItem); - - if (offhandItem != null && damage <= 0 && offhandItem.getItemDamage() < prevDamage) { - BackhandUtils.setPlayerOffhandItem(player, null); - ForgeEventFactory.onPlayerDestroyItem(player, offhandItem); - } - } - - /** - * Refresh the attribute map by removing from the old item and applying the current item - * - * @param attributeMap the map to refresh - * @param oldItem the old item whose attributes will be removed - * @param currentItem the current item whose attributes will be applied - */ - public static void refreshAttributes(BaseAttributeMap attributeMap, ItemStack oldItem, ItemStack currentItem) { - if (oldItem != null) attributeMap.removeAttributeModifiers(oldItem.getAttributeModifiers()); - if (currentItem != null) attributeMap.applyAttributeModifiers(currentItem.getAttributeModifiers()); - } - - /** - * Helper to close a stream fail-safely by printing the error stack trace - * - * @param c the stream to close - */ - public static void closeStream(Closeable c) { - try { - if (c != null) { - c.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * Patch over the PlayerUseItemEvent.Finish in EntityPlayer#onItemUseFinish() to pass the previous stacksize - * - * @param entityPlayer the {@link EntityPlayer} who finished using the itemInUse - * @param itemInUse the {@link ItemStack} which finished being used - * @param itemInUseCount the {@link EntityPlayer} item use count - * @param previousStackSize the itemInUse {@link ItemStack#stackSize} before - * {@link ItemStack#onFoodEaten(World, EntityPlayer)} - * @param result from itemInUse#onFoodEaten(entityPlayer.worldObj, entityPlayer) - * @return the final resulting {@link ItemStack} - */ - public static ItemStack beforeFinishUseEvent(EntityPlayer entityPlayer, ItemStack itemInUse, int itemInUseCount, - ItemStack result, int previousStackSize) { - if (result != itemInUse || (result != null && result.stackSize != previousStackSize)) { - // Compare with either hands content - if (itemInUse == entityPlayer.getCurrentEquippedItem()) { - if (result != null && result.stackSize == 0) { - setPlayerCurrentItem(entityPlayer, null); - } else { - setPlayerCurrentItem(entityPlayer, result); - } - } else if (itemInUse == getOffhandItem(entityPlayer)) { - if (result != null && result.stackSize == 0) { - setPlayerOffhandItem(entityPlayer, null); - } else { - setPlayerOffhandItem(entityPlayer, result); - } - } - } - // Reset stuff so that vanilla doesn't do anything - entityPlayer.clearItemInUse(); - return null; + public static void useOffhandItem(EntityPlayer player, Runnable action) { + useOffhandItem(player, () -> { + action.run(); + return true; + }); } - public static ItemStack getLegalStack(ItemStack stack) { - if (stack != null && stack.stackSize == 0) { - return null; + public static boolean useOffhandItem(EntityPlayer player, BooleanSupplier action) { + int oldSlot = player.inventory.currentItem; + player.inventory.currentItem = IOffhandInventory.OFFHAND_HOTBAR_SLOT; + boolean result = action.getAsBoolean(); + player.inventory.currentItem = oldSlot; + if (player.worldObj.isRemote) { + Minecraft.getMinecraft().playerController.syncCurrentPlayItem(); } - - return stack; - } - - public static void resetAndDelayHotswap(EntityPlayer player, int delayTicks) { - if (!(player instanceof EntityPlayerMP playerMP)) return; - ServerTickHandler.resetTickingHotswap(playerMP); - getOffhandEP(player).tickingHotswapDelay = delayTicks; + return result; } } diff --git a/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java b/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java index b35c004..c2809fd 100644 --- a/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java +++ b/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java @@ -1,10 +1,8 @@ package xonin.backhand.api.core; -import net.minecraft.entity.Entity; - /** * Interface added to EntityPlayer to support offhand management - * + * * @author GotoLink */ public interface IBackhandPlayer { @@ -18,12 +16,4 @@ public interface IBackhandPlayer { * The partial render progress for the offhand swing animation */ float getOffSwingProgress(float frame); - - /** - * Hotswap the EntityPlayer current item to offhand, behaves like - * EntityPlayer#attackTargetEntityWithCurrentItem(Entity) - * - * @param target to attack - */ - void attackTargetEntityWithCurrentOffItem(Entity target); } diff --git a/src/main/java/xonin/backhand/api/core/IOffhandInventory.java b/src/main/java/xonin/backhand/api/core/IOffhandInventory.java new file mode 100644 index 0000000..dd19da6 --- /dev/null +++ b/src/main/java/xonin/backhand/api/core/IOffhandInventory.java @@ -0,0 +1,17 @@ +package xonin.backhand.api.core; + +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.item.ItemStack; + +public interface IOffhandInventory { + + int OFFHAND_HOTBAR_SLOT = 36; + + ItemStack backhand$getOffhandItem(); + + void backhand$setOffhandItem(ItemStack stack); + + static boolean isValidSwitch(int id) { + return (id >= 0 && id < InventoryPlayer.getHotbarSize()) || id == OFFHAND_HOTBAR_SLOT; + } +} diff --git a/src/main/java/xonin/backhand/api/core/InventoryPlayerBackhand.java b/src/main/java/xonin/backhand/api/core/InventoryPlayerBackhand.java deleted file mode 100644 index 32caee7..0000000 --- a/src/main/java/xonin/backhand/api/core/InventoryPlayerBackhand.java +++ /dev/null @@ -1,161 +0,0 @@ -package xonin.backhand.api.core; - -import net.minecraft.block.Block; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.InventoryPlayer; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; - -import xonin.backhand.Backhand; -import xonin.backhand.utils.BackhandConfig; - -/** - * User: nerd-boy - * Date: 15/07/13 - * Time: 3:08 PM - * Replacement for the player inventory - */ -public class InventoryPlayerBackhand extends InventoryPlayer { - - public static final int OFFHAND_HOTBAR_SLOT = 9; - - public InventoryPlayerBackhand(EntityPlayer entityPlayer) { - super(entityPlayer); - } - - /** - * Patch used for "set current slot" vanilla packets - * - * @param id the value to test for currentItem setting - * @return true if it is possible for currentItem to be set with this value - */ - public static boolean isValidSwitch(int id) { - return (id >= 0 && id < getHotbarSize()) || id == OFFHAND_HOTBAR_SLOT; - } - - public ItemStack getOffhandItem() { - return BackhandUtils.getOffhandEP(player) - .getOffhandItem(); - } - - public void setOffhandItem(ItemStack stack) { - BackhandUtils.getOffhandEP(player) - .setOffhandItem(stack); - } - - public int clearInventory(Item item, int metadata) { - int amount = 0; - ItemStack itemstack = this.getOffhandItem(); - if (itemstack != null && (item == null || itemstack.getItem() == item) - && (metadata <= -1 || itemstack.getItemDamage() == metadata)) { - amount += itemstack.stackSize; - this.setOffhandItem(null); - } - - return amount + super.clearInventory(item, metadata); - } - - public boolean addItemStackToInventory(ItemStack itemStack) { - if (itemStack == null || itemStack.stackSize == 0 || itemStack.getItem() == null) return false; - - if (!Backhand.isOffhandBlacklisted(itemStack)) { - if (BackhandConfig.OffhandPickup && this.getOffhandItem() == null && getFirstEmptyStack() == -1) { - this.setOffhandItem(ItemStack.copyItemStack(itemStack)); - itemStack.stackSize = 0; - BackhandUtils.getOffhandEP(player).syncOffhand = true; - return true; - } - - if (this.getOffhandItem() != null && this.getOffhandItem() - .getItem() == itemStack.getItem() - && this.getOffhandItem() - .isStackable() - && this.getOffhandItem().stackSize < this.getOffhandItem() - .getMaxStackSize() - && this.getOffhandItem().stackSize < this.getInventoryStackLimit() - && (!this.getOffhandItem() - .getHasSubtypes() - || this.getOffhandItem() - .getItemDamage() == itemStack.getItemDamage()) - && ItemStack.areItemStackTagsEqual(this.getOffhandItem(), itemStack)) { - if (this.getOffhandItem().stackSize + itemStack.stackSize > this.getOffhandItem() - .getMaxStackSize()) { - itemStack.stackSize -= this.getOffhandItem().stackSize; - this.getOffhandItem().stackSize = this.getOffhandItem() - .getMaxStackSize(); - BackhandUtils.getOffhandEP(player).syncOffhand = true; - return super.addItemStackToInventory(itemStack); - } else { - this.getOffhandItem().stackSize += itemStack.stackSize; - itemStack.stackSize = 0; - BackhandUtils.getOffhandEP(player).syncOffhand = true; - return true; - } - } - } - return super.addItemStackToInventory(itemStack); - } - - public boolean consumeInventoryItem(Item item) { - if (this.getOffhandItem() != null && this.getOffhandItem() - .getItem() == item) { - if (--this.getOffhandItem().stackSize <= 0) { - this.setOffhandItem(null); - } - return true; - } else { - return super.consumeInventoryItem(item); - } - } - - public boolean hasItem(Item item) { - if (this.getOffhandItem() != null && this.getOffhandItem() - .getItem() == item) { - return true; - } - - return super.hasItem(item); - } - - /** - * Copy the slots content from another instance, usually for changing dimensions - * - * @param par1InventoryPlayer the instance to copy from - */ - @Override - public void copyInventory(InventoryPlayer par1InventoryPlayer) { - this.mainInventory = new ItemStack[par1InventoryPlayer.mainInventory.length]; - this.armorInventory = new ItemStack[par1InventoryPlayer.armorInventory.length]; - super.copyInventory(par1InventoryPlayer); - if (par1InventoryPlayer instanceof InventoryPlayerBackhand) { - this.setOffhandItem( - ItemStack.copyItemStack(((InventoryPlayerBackhand) par1InventoryPlayer).getOffhandItem())); - } - } - - public void dropAllItems() { - super.dropAllItems(); - this.player.func_146097_a(this.getOffhandItem(), true, false); - this.setOffhandItem(null); - } - - @Override - public boolean func_146025_b(Block block) { - if (!super.func_146025_b(block)) { - ItemStack offhand = getOffhandItem(); - if (offhand != null && getCurrentItem() != null && offhand.getItem() == getCurrentItem().getItem()) { - return offhand.func_150998_b(block); - } - return false; - } - return true; - } - - @Override - public ItemStack getCurrentItem() { - if (currentItem == InventoryPlayerBackhand.OFFHAND_HOTBAR_SLOT) { - return BackhandUtils.getOffhandItem(player); - } - return super.getCurrentItem(); - } -} diff --git a/src/main/java/xonin/backhand/api/core/OffhandExtendedProperty.java b/src/main/java/xonin/backhand/api/core/OffhandExtendedProperty.java deleted file mode 100644 index 7e568e3..0000000 --- a/src/main/java/xonin/backhand/api/core/OffhandExtendedProperty.java +++ /dev/null @@ -1,51 +0,0 @@ -package xonin.backhand.api.core; - -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.world.World; -import net.minecraftforge.common.IExtendedEntityProperties; - -public class OffhandExtendedProperty implements IExtendedEntityProperties { - - public EntityPlayer player; - public boolean syncOffhand = true; - private ItemStack offhandItem; - public boolean ignoreSetSlot = false; - public int activeSlot = -1; - public boolean regularHotSwap = false; - public int tickingHotswapDelay = 0; - - public OffhandExtendedProperty(EntityPlayer player) { - this.player = player; - } - - @Override - public void saveNBTData(NBTTagCompound compound) { - if (offhandItem != null) { - compound.setTag("OffhandItemStack", offhandItem.writeToNBT(new NBTTagCompound())); - } - } - - @Override - public void loadNBTData(NBTTagCompound compound) { - if (compound.hasKey("OffhandItemStack")) { - this.setOffhandItem(ItemStack.loadItemStackFromNBT(compound.getCompoundTag("OffhandItemStack"))); - } - } - - @Override - public void init(Entity entity, World world) {} - - public ItemStack getOffhandItem() { - return offhandItem; - } - - public void setOffhandItem(ItemStack stack) { - if (!ItemStack.areItemStacksEqual(stack, this.offhandItem)) { - this.syncOffhand = true; - } - this.offhandItem = stack; - } -} diff --git a/src/main/java/xonin/backhand/client/BackhandClientTickHandler.java b/src/main/java/xonin/backhand/client/BackhandClientTickHandler.java deleted file mode 100644 index bdacd08..0000000 --- a/src/main/java/xonin/backhand/client/BackhandClientTickHandler.java +++ /dev/null @@ -1,399 +0,0 @@ -package xonin.backhand.client; - -import net.minecraft.block.Block; -import net.minecraft.block.BlockGrass; -import net.minecraft.block.BlockLog; -import net.minecraft.block.material.Material; -import net.minecraft.client.Minecraft; -import net.minecraft.client.audio.PositionedSoundRecord; -import net.minecraft.client.multiplayer.PlayerControllerMP; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemAxe; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemBow; -import net.minecraft.item.ItemSpade; -import net.minecraft.item.ItemStack; -import net.minecraft.item.ItemSword; -import net.minecraft.network.play.client.C07PacketPlayerDigging; -import net.minecraft.util.MovingObjectPosition; -import net.minecraft.util.ResourceLocation; -import net.minecraft.util.Vec3; -import net.minecraft.world.World; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.ForgeEventFactory; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; - -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.common.gameevent.TickEvent; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import xonin.backhand.Backhand; -import xonin.backhand.CommonProxy; -import xonin.backhand.HookContainerClass; -import xonin.backhand.api.PlayerEventChild; -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.api.core.InventoryPlayerBackhand; -import xonin.backhand.client.utils.BackhandClientUtils; -import xonin.backhand.packet.OffhandAttackPacket; -import xonin.backhand.packet.OffhandPlaceBlockPacket; -import xonin.backhand.packet.OffhandToServerPacket; -import xonin.backhand.utils.BackhandConfig; - -public final class BackhandClientTickHandler { - - public final Minecraft mc = Minecraft.getMinecraft(); - public static float ticksBeforeUse = 0; - public static boolean prevRightClickHeld = false; - public static int attackDelay = 0; - - public BackhandClientTickHandler() {} - - @SubscribeEvent - @SideOnly(Side.CLIENT) - public void onPlayerTick(TickEvent.PlayerTickEvent event) { - if (event.player == mc.thePlayer) { - if (event.phase == TickEvent.Phase.START) { - if (ticksBeforeUse > 0) ticksBeforeUse--; - tickStart(mc.thePlayer); - } - } - } - - @SideOnly(Side.CLIENT) - public void tickStart(EntityPlayer player) { - ItemStack mainhand = player.getCurrentEquippedItem(); - ItemStack offhand = BackhandUtils.getOffhandItem(player); - boolean mainhandUse = BackhandUtils.checkForRightClickFunction(mainhand); - boolean offhandUse = BackhandUtils.checkForRightClickFunction(offhand); - if (attackDelay > 0) { - attackDelay--; - } - - boolean usedItem = false; - if (offhand != null) { - if (mc.gameSettings.keyBindUseItem.getIsKeyPressed()) { - if (ticksBeforeUse == 0) { - usedItem = tryCheckUseItem(offhand, player); - } - } else { - ticksBeforeUse = 0; - } - } - if (mc.gameSettings.keyBindUseItem.getIsKeyPressed() && attackDelay == 0 && !usedItem) { - if (!prevRightClickHeld && player.getItemInUse() == null && !mainhandUse && !offhandUse) { - tryAttackEntity(player); - } - prevRightClickHeld = true; - } else { - prevRightClickHeld = false; - } - if (player.getItemInUse() == null) { - CommonProxy.offhandItemUsed = null; - } - } - - public void tryAttackEntity(EntityPlayer player) { - Minecraft mc = Minecraft.getMinecraft(); - if (mc.objectMouseOver != null - && mc.objectMouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY) { - Entity target = mc.objectMouseOver.entityHit; - mc.getNetHandler() - .addToSendQueue(new OffhandAttackPacket(player, target).generatePacket()); - } - } - - @SideOnly(Side.CLIENT) - public boolean tryCheckUseItem(ItemStack offhandItem, EntityPlayer player) { - MovingObjectPosition mouseOver = mc.objectMouseOver; - - if (offhandItem.getItem() instanceof ItemBow && !BackhandConfig.UseOffhandBow) { - return false; - } - - ItemStack mainHandItem = player.getCurrentEquippedItem(); - if (mainHandItem != null && (BackhandUtils.checkForRightClickFunction(mainHandItem) - || HookContainerClass.isItemBlock(mainHandItem.getItem()) - || player.getItemInUse() == mainHandItem)) { - ticksBeforeUse = 10; - return false; - } - - if (mouseOver != null && mouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) { - if (BackhandClientUtils - .canBlockBeInteractedWith(offhandItem, mouseOver.blockX, mouseOver.blockY, mouseOver.blockZ) - && !BackhandUtils.getOffhandItem(player) - .getItem() - .doesSneakBypassUse(player.worldObj, mouseOver.blockX, mouseOver.blockY, mouseOver.blockZ, player) - && !(offhandItem.getItem() instanceof ItemBlock)) { - ticksBeforeUse = 4; - return false; - } - } - - boolean interacted = false; - if (BackhandUtils.usagePriorAttack(offhandItem)) { - boolean flag = true; - if (mouseOver != null) { - if (mouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY) { - if (mc.playerController.interactWithEntitySendPacket(player, mouseOver.entityHit)) { - flag = false; - interacted = true; - } - } - - if (flag) { - offhandItem = BackhandUtils.getOffhandItem(player); - PlayerEventChild.UseOffhandItemEvent useItemEvent = new PlayerEventChild.UseOffhandItemEvent( - new PlayerInteractEvent( - player, - PlayerInteractEvent.Action.RIGHT_CLICK_AIR, - 0, - 0, - 0, - -1, - player.worldObj), - offhandItem); - if (offhandItem != null && !MinecraftForge.EVENT_BUS.post(useItemEvent)) { - interacted = HookContainerClass.tryUseItem(player, offhandItem, Side.CLIENT); - } - } - - offhandItem = BackhandUtils.getOffhandItem(player); - if (offhandItem != null && mouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) { - int j = mouseOver.blockX; - int k = mouseOver.blockY; - int l = mouseOver.blockZ; - if (!player.worldObj.getBlock(j, k, l) - .isAir(player.worldObj, j, k, l)) { - int i1 = mouseOver.sideHit; - PlayerEventChild.UseOffhandItemEvent useItemEvent = new PlayerEventChild.UseOffhandItemEvent( - new PlayerInteractEvent( - player, - PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK, - j, - k, - l, - i1, - player.worldObj), - offhandItem); - if (player.capabilities.allowEdit || !HookContainerClass.isItemBlock(offhandItem.getItem())) { - if (!MinecraftForge.EVENT_BUS.post(useItemEvent) && onPlayerPlaceBlock( - mc.playerController, - player, - offhandItem, - j, - k, - l, - i1, - mouseOver.hitVec)) { - ((IBackhandPlayer) player).swingOffItem(); - interacted = true; - } - } - if (offhandItem.stackSize == 0) { - BackhandUtils.setPlayerOffhandItem(player, null); - } - } - } - } - ticksBeforeUse = 4; - } - - return interacted; - } - - private boolean onPlayerPlaceBlock(PlayerControllerMP controller, EntityPlayer player, ItemStack offhand, int i, - int j, int k, int l, Vec3 hitVec) { - float f = (float) hitVec.xCoord - i; - float f1 = (float) hitVec.yCoord - j; - float f2 = (float) hitVec.zCoord - k; - boolean flag = false; - int i1; - final World worldObj = player.worldObj; - - Minecraft mc = Minecraft.getMinecraft(); - MovingObjectPosition objectMouseOver = mc.objectMouseOver; - Block block = mc.theWorld.getBlock(objectMouseOver.blockX, objectMouseOver.blockY, objectMouseOver.blockZ); - - Block b = worldObj.getBlock(i, j, k); - boolean blockActivated = b.onBlockActivated(worldObj, i, j, k, player, l, f, f1, f2); - boolean prevSneaking = player.isSneaking(); - player.setSneaking(true); - boolean blockSneakActivated = b.onBlockActivated(worldObj, i, j, k, player, l, f, f1, f2); - player.setSneaking(prevSneaking); - - if (blockActivated && !(player.isSneaking() && blockSneakActivated)) { - return false; - } - - if (player.getCurrentEquippedItem() != null) { - if ((block instanceof BlockLog && player.getCurrentEquippedItem() - .getItem() instanceof ItemAxe) || (block instanceof BlockGrass - && player.getCurrentEquippedItem() - .getItem() instanceof ItemSpade)) { - return false; - } - } - - if (offhand.getItem() - .onItemUseFirst(offhand, player, worldObj, i, j, k, l, f, f1, f2)) { - return true; - } - if (!flag && offhand.getItem() instanceof ItemBlock itemblock) { - if (!itemblock.func_150936_a(worldObj, i, j, k, l, player, offhand)) { - return false; - } - } - Backhand.packetHandler - .sendPacketToServer(new OffhandPlaceBlockPacket(i, j, k, l, offhand, f, f1, f2).generatePacket()); - if (flag) { - return true; - } else { - if (controller.isInCreativeMode()) { - i1 = offhand.getItemDamage(); - int j1 = offhand.stackSize; - boolean flag1 = offhand.tryPlaceItemIntoWorld(player, worldObj, i, j, k, l, f, f1, f2); - offhand.setItemDamage(i1); - offhand.stackSize = j1; - if (flag1) { - HookContainerClass.sendOffSwingEventNoCheck(player, offhand, player.getCurrentEquippedItem()); - } - return flag1; - } else { - if (!offhand.tryPlaceItemIntoWorld(player, worldObj, i, j, k, l, f, f1, f2)) { - return false; - } - if (offhand.stackSize <= 0) { - ForgeEventFactory.onPlayerDestroyItem(player, offhand); - } - HookContainerClass.sendOffSwingEventNoCheck(player, offhand, player.getCurrentEquippedItem()); - return true; - } - } - } - - @SideOnly(Side.CLIENT) - public static void tryBreakBlockOffhand(MovingObjectPosition objectMouseOver, ItemStack offhandItem, - ItemStack mainHandItem, TickEvent.PlayerTickEvent event) { - Minecraft mcInstance = Minecraft.getMinecraft(); - int i = objectMouseOver.blockX; - int j = objectMouseOver.blockY; - int k = objectMouseOver.blockZ; - int prevHeldItem = event.player.inventory.currentItem; - boolean broken = false; - - if (mcInstance.thePlayer.capabilities.isCreativeMode) { - if (ClientTickHandler.delay <= 0) { - mcInstance.effectRenderer.addBlockHitEffects(i, j, k, objectMouseOver); - mcInstance.effectRenderer.addBlockHitEffects(i, j, k, objectMouseOver); - if (!(BackhandUtils.usagePriorAttack(offhandItem)) - && (offhandItem == null || !(offhandItem.getItem() instanceof ItemSword))) { - PlayerControllerMP - .clickBlockCreative(mcInstance, mcInstance.playerController, i, j, k, objectMouseOver.sideHit); - HookContainerClass.sendOffSwingEventNoCheck(event.player, mainHandItem, offhandItem); - mcInstance.getNetHandler() - .addToSendQueue(new C07PacketPlayerDigging(2, i, j, k, objectMouseOver.sideHit)); - } - ClientTickHandler.delay = 20; - } - return; - } - if (mcInstance.theWorld.getBlock(i, j, k) - .getMaterial() != Material.air) { - if (mcInstance.playerController.blockHitDelay > 0) { - --mcInstance.playerController.blockHitDelay; - } else { - mcInstance.playerController.isHittingBlock = true; - - if (mcInstance.playerController.currentBlockX != i || mcInstance.playerController.currentBlockY != j - || mcInstance.playerController.currentblockZ != k) { - mcInstance.playerController.curBlockDamageMP = 0f; - } - - mcInstance.playerController.currentBlockX = i; - mcInstance.playerController.currentBlockY = j; - mcInstance.playerController.currentblockZ = k; - - if (offhandItem != null) { - if (mcInstance.gameSettings.heldItemTooltips) { - mcInstance.gameSettings.heldItemTooltips = false; - HookContainerClass.changedHeldItemTooltips = true; - } - - mcInstance.thePlayer.inventory.currentItem = InventoryPlayerBackhand.OFFHAND_HOTBAR_SLOT; - mcInstance.playerController.currentItemHittingBlock = BackhandUtils - .getOffhandItem(mcInstance.thePlayer); - mcInstance.playerController.syncCurrentPlayItem(); - } - - Block block = mcInstance.theWorld.getBlock(i, j, k); - if (block.getMaterial() == Material.air) { - mcInstance.playerController.isHittingBlock = false; - return; - } - BackhandClientUtils.countToCancel = 5; - mcInstance.playerController.curBlockDamageMP += block - .getPlayerRelativeBlockHardness(mcInstance.thePlayer, mcInstance.thePlayer.worldObj, i, j, k); - - if (mcInstance.playerController.stepSoundTickCounter % 4.0F == 0.0F) { - mcInstance.getSoundHandler() - .playSound( - new PositionedSoundRecord( - new ResourceLocation(block.stepSound.getStepResourcePath()), - (block.stepSound.getVolume() + 1.0F) / 8.0F, - block.stepSound.getPitch() * 0.5F, - (float) i + 0.5F, - (float) j + 0.5F, - (float) k + 0.5F)); - } - - ++mcInstance.playerController.stepSoundTickCounter; - - if (mcInstance.playerController.curBlockDamageMP >= 1.0F) { - - ItemStack itemstack = mcInstance.thePlayer.getCurrentEquippedItem(); - - if (itemstack != null) { - int prevDamage = itemstack.getItemDamage(); - int damage = itemstack.getMaxDamage() - itemstack.getItemDamage(); - itemstack.func_150999_a(mcInstance.theWorld, block, i, j, k, mcInstance.thePlayer); - if (itemstack.stackSize == 0 || (damage <= 0 && prevDamage < itemstack.getItemDamage())) { - broken = true; - mcInstance.thePlayer.destroyCurrentEquippedItem(); - } - } - mcInstance.playerController.isHittingBlock = false; - mcInstance.getNetHandler() - .addToSendQueue(new C07PacketPlayerDigging(2, i, j, k, objectMouseOver.sideHit)); - mcInstance.playerController.onPlayerDestroyBlock(i, j, k, objectMouseOver.sideHit); - mcInstance.playerController.curBlockDamageMP = 0.0F; - mcInstance.playerController.stepSoundTickCounter = 0.0F; - mcInstance.playerController.blockHitDelay = 5; - } - mcInstance.theWorld.destroyBlockInWorldPartially( - mcInstance.thePlayer.getEntityId(), - mcInstance.playerController.currentBlockX, - mcInstance.playerController.currentBlockY, - mcInstance.playerController.currentblockZ, - (int) (mcInstance.playerController.curBlockDamageMP * 10.0F) - 1); - } - - if (mcInstance.thePlayer.isCurrentToolAdventureModeExempt(i, j, k)) { - mcInstance.effectRenderer.addBlockHitEffects(i, j, k, objectMouseOver); - } - HookContainerClass.sendOffSwingEventNoCheck(event.player, mainHandItem, offhandItem); // force offhand swing - // anyway because we - // broke a block - } - event.player.inventory.currentItem = prevHeldItem; - mcInstance.playerController.syncCurrentPlayItem(); - - if (broken) { - BackhandUtils.setPlayerOffhandItem(event.player, null); - mcInstance.getNetHandler() - .addToSendQueue(new OffhandToServerPacket(null, event.player).generatePacket()); - } - } -} diff --git a/src/main/java/xonin/backhand/client/ClientEventHandler.java b/src/main/java/xonin/backhand/client/ClientEventHandler.java index bdfece4..3e63e4b 100644 --- a/src/main/java/xonin/backhand/client/ClientEventHandler.java +++ b/src/main/java/xonin/backhand/client/ClientEventHandler.java @@ -1,6 +1,7 @@ package xonin.backhand.client; import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.gui.GuiIngame; import net.minecraft.client.model.ModelBiped; import net.minecraft.client.renderer.RenderHelper; @@ -15,21 +16,38 @@ import net.minecraftforge.client.event.RenderLivingEvent; import net.minecraftforge.client.event.RenderPlayerEvent; +import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; +import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber; + +import cpw.mods.fml.common.Optional; import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.InputEvent; +import cpw.mods.fml.common.gameevent.TickEvent; +import cpw.mods.fml.relauncher.Side; +import invtweaks.InvTweaks; import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.client.utils.BackhandRenderHelper; +import xonin.backhand.packet.OffhandSwapPacket; import xonin.backhand.utils.BackhandConfig; +@EventBusSubscriber(side = Side.CLIENT) public class ClientEventHandler { public static EntityPlayer renderingPlayer; + public static boolean prevInvTweaksAutoRefill; + public static boolean prevInvTweaksBreakRefill; + + public static int invTweaksDelay; + public static boolean allowSwap = true; + + public static int renderPass; @SubscribeEvent - public void renderHotbarOverlay(RenderGameOverlayEvent event) { + public static void renderHotbarOverlay(RenderGameOverlayEvent event) { if (event.type == RenderGameOverlayEvent.ElementType.HOTBAR) { Minecraft mc = Minecraft.getMinecraft(); renderHotbar( @@ -40,7 +58,37 @@ public void renderHotbarOverlay(RenderGameOverlayEvent event) { } } - protected void renderHotbar(GuiIngame gui, int width, int height, float partialTicks) { + @SubscribeEvent + public static void onKeyInputEvent(InputEvent.KeyInputEvent event) { + Minecraft mc = Minecraft.getMinecraft(); + EntityClientPlayerMP player = mc.thePlayer; + + if (ClientProxy.swapOffhand.getIsKeyPressed() && Keyboard.isKeyDown(Keyboard.getEventKey()) && allowSwap) { + allowSwap = false; + // try { + // getClass() + // .getMethod("invTweaksSwapPatch"); + // invTweaksSwapPatch(); + // } catch (Exception ignored) {} + player.sendQueue.addToSendQueue(new OffhandSwapPacket(player).generatePacket()); + } + } + + @SubscribeEvent + public static void onClientTick(TickEvent.ClientTickEvent event) { + if (invTweaksDelay > 0) { + invTweaksDelay--; + if (invTweaksDelay == 0) { + // try { + // this.getClass() + // .getMethod("restoreInvTweaksConfigs"); + // restoreInvTweaksConfigs(); + // } catch (Exception ignored) {} + } + } + } + + private static void renderHotbar(GuiIngame gui, int width, int height, float partialTicks) { Minecraft mc = Minecraft.getMinecraft(); ItemStack itemstack = BackhandUtils.getOffhandItem(mc.thePlayer); if (itemstack == null) { @@ -71,7 +119,7 @@ protected void renderHotbar(GuiIngame gui, int width, int height, float partialT mc.mcProfiler.endSection(); } - protected void renderOffhandInventorySlot(int p_73832_2_, int p_73832_3_, float p_73832_4_) { + private static void renderOffhandInventorySlot(int p_73832_2_, int p_73832_3_, float p_73832_4_) { Minecraft mc = Minecraft.getMinecraft(); ItemStack itemstack = BackhandUtils.getOffhandItem(mc.thePlayer); @@ -98,10 +146,8 @@ protected void renderOffhandInventorySlot(int p_73832_2_, int p_73832_3_, float } } - public static int renderPass; - @SubscribeEvent - public void onRenderHand(RenderHandEvent event) { + public static void onRenderHand(RenderHandEvent event) { renderPass = event.renderPass; } @@ -110,7 +156,7 @@ public void onRenderHand(RenderHandEvent event) { * And stop the right hand inappropriate bending */ @SubscribeEvent(priority = EventPriority.LOW) - public void renderPlayerLeftItemUsage(RenderLivingEvent.Pre event) { + public static void renderPlayerLeftItemUsage(RenderLivingEvent.Pre event) { if (event.entity instanceof EntityPlayer entityPlayer) { renderingPlayer = entityPlayer; ItemStack offhand = BackhandUtils.getOffhandItem(entityPlayer); @@ -135,12 +181,12 @@ public void renderPlayerLeftItemUsage(RenderLivingEvent.Pre event) { * Reset models to default values */ @SubscribeEvent(priority = EventPriority.LOW) - public void resetPlayerLeftHand(RenderPlayerEvent.Post event) { + public static void resetPlayerLeftHand(RenderPlayerEvent.Post event) { event.renderer.modelArmorChestplate.heldItemLeft = event.renderer.modelArmor.heldItemLeft = event.renderer.modelBipedMain.heldItemLeft = 0; } @SubscribeEvent - public void render3rdPersonOffhand(RenderPlayerEvent.Specials.Post event) { + public static void render3rdPersonOffhand(RenderPlayerEvent.Specials.Post event) { if (!BackhandConfig.EmptyOffhand && BackhandUtils.getOffhandItem(event.entityPlayer) == null) { return; } @@ -152,4 +198,35 @@ public void render3rdPersonOffhand(RenderPlayerEvent.Specials.Post event) { .renderOffhandItemIn3rdPerson(event.entityPlayer, biped, event.partialRenderTick); GL11.glPopMatrix(); } + + @Optional.Method(modid = "inventorytweaks") + public void restoreInvTweaksConfigs() { + InvTweaks.getConfigManager() + .getConfig() + .setProperty("enableAutoRefill", String.valueOf(prevInvTweaksAutoRefill)); + InvTweaks.getConfigManager() + .getConfig() + .setProperty("autoRefillBeforeBreak", String.valueOf(prevInvTweaksBreakRefill)); + } + + @Optional.Method(modid = "inventorytweaks") + public void invTweaksSwapPatch() { + if (invTweaksDelay <= 0) { + prevInvTweaksAutoRefill = Boolean.parseBoolean( + InvTweaks.getConfigManager() + .getConfig() + .getProperty("enableAutoRefill")); + prevInvTweaksBreakRefill = Boolean.parseBoolean( + InvTweaks.getConfigManager() + .getConfig() + .getProperty("autoRefillBeforeBreak")); + InvTweaks.getConfigManager() + .getConfig() + .setProperty("enableAutoRefill", "false"); + InvTweaks.getConfigManager() + .getConfig() + .setProperty("autoRefillBeforeBreak", "false"); + } + invTweaksDelay = 15; + } } diff --git a/src/main/java/xonin/backhand/client/ClientProxy.java b/src/main/java/xonin/backhand/client/ClientProxy.java index 5fb7545..89cb071 100644 --- a/src/main/java/xonin/backhand/client/ClientProxy.java +++ b/src/main/java/xonin/backhand/client/ClientProxy.java @@ -1,18 +1,11 @@ package xonin.backhand.client; -import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.settings.KeyBinding; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraftforge.common.MinecraftForge; import org.lwjgl.input.Keyboard; import cpw.mods.fml.client.registry.ClientRegistry; -import cpw.mods.fml.common.FMLCommonHandler; import xonin.backhand.CommonProxy; -import xonin.backhand.packet.OffhandAnimationPacket; -import xonin.backhand.utils.EnumAnimations; public class ClientProxy extends CommonProxy { @@ -20,65 +13,8 @@ public class ClientProxy extends CommonProxy { "Swap Offhand", Keyboard.KEY_F, "key.categories.gameplay"); - public static int rightClickCounter = 0; public void load() { - MinecraftForge.EVENT_BUS.register(new ClientEventHandler()); - FMLCommonHandler.instance() - .bus() - .register(new ClientTickHandler()); - FMLCommonHandler.instance() - .bus() - .register(new BackhandClientTickHandler()); - ClientRegistry.registerKeyBinding(swapOffhand); } - - @Override - public EntityPlayer getClientPlayer() { - return Minecraft.getMinecraft().thePlayer; - } - - @Override - public void sendAnimationPacket(EnumAnimations animation, EntityPlayer entityPlayer) { - if (entityPlayer instanceof EntityClientPlayerMP) { - ((EntityClientPlayerMP) entityPlayer).sendQueue - .addToSendQueue(new OffhandAnimationPacket(animation, entityPlayer).generatePacket()); - } - } - - @Override - public boolean isRightClickHeld() { - return Minecraft.getMinecraft().gameSettings.keyBindUseItem.getIsKeyPressed(); - } - - @Override - public int getRightClickCounter() { - return rightClickCounter; - } - - @Override - public int getRightClickDelay() { - return ClientTickHandler.delay; - } - - @Override - public void setRightClickCounter(int i) { - rightClickCounter = i; - } - - @Override - public boolean isLeftClickHeld() { - return Minecraft.getMinecraft().gameSettings.keyBindAttack.getIsKeyPressed(); - } - - @Override - public int getLeftClickCounter() { - return Minecraft.getMinecraft().leftClickCounter; - } - - @Override - public void setLeftClickCounter(int i) { - Minecraft.getMinecraft().leftClickCounter = i; - } } diff --git a/src/main/java/xonin/backhand/client/ClientTickHandler.java b/src/main/java/xonin/backhand/client/ClientTickHandler.java deleted file mode 100644 index 80a0fc0..0000000 --- a/src/main/java/xonin/backhand/client/ClientTickHandler.java +++ /dev/null @@ -1,152 +0,0 @@ -package xonin.backhand.client; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.EntityClientPlayerMP; -import net.minecraft.item.ItemStack; -import net.minecraft.util.MovingObjectPosition; - -import org.lwjgl.input.Keyboard; - -import cpw.mods.fml.common.Optional; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.common.gameevent.InputEvent; -import cpw.mods.fml.common.gameevent.TickEvent; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import invtweaks.InvTweaks; -import xonin.backhand.Backhand; -import xonin.backhand.HookContainerClass; -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.client.utils.BackhandClientUtils; -import xonin.backhand.packet.OffhandSwapPacket; -import xonin.backhand.utils.BackhandConfig; - -public class ClientTickHandler { - - public static int delay; - public static boolean prevInvTweaksAutoRefill; - public static boolean prevInvTweaksBreakRefill; - - public static int invTweaksDelay; - public static boolean allowSwap = true; - - @SubscribeEvent - public void onKeyInputEvent(InputEvent.KeyInputEvent event) { - Minecraft mc = Minecraft.getMinecraft(); - EntityClientPlayerMP player = mc.thePlayer; - - if (ClientProxy.swapOffhand.getIsKeyPressed() && Keyboard.isKeyDown(Keyboard.getEventKey()) && allowSwap) { - allowSwap = false; - try { - this.getClass() - .getMethod("invTweaksSwapPatch"); - invTweaksSwapPatch(); - } catch (Exception ignored) {} - player.sendQueue.addToSendQueue(new OffhandSwapPacket(player).generatePacket()); - } - } - - @Optional.Method(modid = "inventorytweaks") - public void invTweaksSwapPatch() { - if (invTweaksDelay <= 0) { - prevInvTweaksAutoRefill = Boolean.parseBoolean( - InvTweaks.getConfigManager() - .getConfig() - .getProperty("enableAutoRefill")); - prevInvTweaksBreakRefill = Boolean.parseBoolean( - InvTweaks.getConfigManager() - .getConfig() - .getProperty("autoRefillBeforeBreak")); - InvTweaks.getConfigManager() - .getConfig() - .setProperty("enableAutoRefill", "false"); - InvTweaks.getConfigManager() - .getConfig() - .setProperty("autoRefillBeforeBreak", "false"); - } - invTweaksDelay = 15; - } - - @SubscribeEvent - public void onClientTick(TickEvent.ClientTickEvent event) { - if (invTweaksDelay > 0) { - invTweaksDelay--; - if (invTweaksDelay == 0) { - try { - this.getClass() - .getMethod("restoreInvTweaksConfigs"); - restoreInvTweaksConfigs(); - } catch (Exception ignored) {} - } - } - } - - @Optional.Method(modid = "inventorytweaks") - public void restoreInvTweaksConfigs() { - InvTweaks.getConfigManager() - .getConfig() - .setProperty("enableAutoRefill", String.valueOf(prevInvTweaksAutoRefill)); - InvTweaks.getConfigManager() - .getConfig() - .setProperty("autoRefillBeforeBreak", String.valueOf(prevInvTweaksBreakRefill)); - } - - @SideOnly(Side.CLIENT) - @SubscribeEvent - public void clientHelper(TickEvent.PlayerTickEvent event) { - if (ClientTickHandler.delay > 0) { - ClientTickHandler.delay--; - } - - if (!BackhandConfig.OffhandBreakBlocks) { - return; - } - - if (!BackhandConfig.EmptyOffhand && BackhandUtils.getOffhandItem(event.player) == null) { - return; - } - - if (!Backhand.proxy.isRightClickHeld()) { - Backhand.proxy.setRightClickCounter(0); - } - - ItemStack mainHandItem = event.player.getCurrentEquippedItem(); - ItemStack offhandItem = BackhandUtils.getOffhandItem(event.player); - - if (mainHandItem != null && (BackhandUtils.checkForRightClickFunction(mainHandItem) || offhandItem == null)) { - return; - } - - Minecraft mc = Minecraft.getMinecraft(); - - if (event.player.worldObj.isRemote && Backhand.proxy.getLeftClickCounter() <= 0 - && mc.objectMouseOver != null - && mc.objectMouseOver.typeOfHit != MovingObjectPosition.MovingObjectType.ENTITY) { - if (event.player.capabilities.allowEdit) { - if (Backhand.proxy.isRightClickHeld() - && !(mainHandItem != null && BackhandUtils.isItemBlock(mainHandItem.getItem()))) { - MovingObjectPosition mop = HookContainerClass.getRaytraceBlock(event.player); - if (offhandItem != null && HookContainerClass.isItemBlock(offhandItem.getItem())) { - if (!BackhandUtils.usagePriorAttack(offhandItem) && mop != null) { - BackhandClientTickHandler.tryBreakBlockOffhand(mop, offhandItem, mainHandItem, event); - Backhand.proxy.setLeftClickCounter(10); - } else { - mc.playerController.resetBlockRemoving(); - } - } else { - if (mop != null && !BackhandUtils.usagePriorAttack(offhandItem) - && !BackhandClientUtils - .canBlockBeInteractedWith(offhandItem, mop.blockX, mop.blockY, mop.blockZ)) { - BackhandClientTickHandler.tryBreakBlockOffhand(mop, offhandItem, mainHandItem, event); - Backhand.proxy.setLeftClickCounter(10); - } else { - mc.playerController.resetBlockRemoving(); - } - } - } else if (!Backhand.proxy.isLeftClickHeld()) { - mc.playerController.resetBlockRemoving(); - } - } - } - } -} diff --git a/src/main/java/xonin/backhand/client/utils/BackhandClientUtils.java b/src/main/java/xonin/backhand/client/utils/BackhandClientUtils.java deleted file mode 100644 index 97f170a..0000000 --- a/src/main/java/xonin/backhand/client/utils/BackhandClientUtils.java +++ /dev/null @@ -1,47 +0,0 @@ -package xonin.backhand.client.utils; - -import net.minecraft.client.entity.EntityOtherPlayerMP; -import net.minecraft.item.ItemStack; - -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.client.world.ClientFakePlayer; - -public final class BackhandClientUtils { - - public static boolean disableMainhandAnimation = false; - public static int countToCancel = 0; - public static float firstPersonFrame; - public static boolean offhandFPRender; - public static boolean receivedConfigs = false; - - /** - * Patch over EntityOtherPlayerMP#onUpdate() to update isItemInUse field - * - * @param player the player whose #onUpdate method is triggered - * @param isItemInUse the old value for isItemInUse field - * @return the new value for isItemInUse field - */ - public static boolean entityOtherPlayerIsItemInUseHook(EntityOtherPlayerMP player, boolean isItemInUse) { - ItemStack itemStack = player.getCurrentEquippedItem(); - ItemStack offhand = BackhandUtils.getOffhandItem(player); - if (BackhandUtils.usagePriorAttack(offhand)) itemStack = offhand; - if (!isItemInUse && player.isEating() && itemStack != null) { - player.setItemInUse(itemStack, itemStack.getMaxItemUseDuration()); - return true; - } else if (isItemInUse && !player.isEating()) { - player.clearItemInUse(); - return false; - } else { - return isItemInUse; - } - } - - public static boolean canBlockBeInteractedWith(ItemStack offhand, int x, int y, int z) { - try { - return ClientFakePlayer.INSTANCE.simulateBlockInteraction(offhand, x, y, z); - } catch (Exception e) { - // Something went wrong, block the offhand interaction - return true; - } - } -} diff --git a/src/main/java/xonin/backhand/client/utils/BackhandRenderHelper.java b/src/main/java/xonin/backhand/client/utils/BackhandRenderHelper.java index 46dd236..48c005a 100644 --- a/src/main/java/xonin/backhand/client/utils/BackhandRenderHelper.java +++ b/src/main/java/xonin/backhand/client/utils/BackhandRenderHelper.java @@ -4,50 +4,25 @@ import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.model.ModelBiped; import net.minecraft.client.renderer.ItemRenderer; -import net.minecraft.client.renderer.Tessellator; import net.minecraft.entity.Entity; import net.minecraft.item.ItemStack; import net.minecraft.util.MathHelper; -import net.minecraft.util.ResourceLocation; import org.lwjgl.opengl.GL11; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; import xonin.backhand.api.core.IBackhandPlayer; import xonin.backhand.client.renderer.ItemRendererOffhand; public final class BackhandRenderHelper { - public static final float RENDER_UNIT = 1F / 16F;// 0.0625 public static final ItemRendererOffhand itemRenderer = new ItemRendererOffhand(Minecraft.getMinecraft()); - - private static final ResourceLocation ITEM_GLINT = new ResourceLocation("textures/misc/enchanted_item_glint.png"); - - public static final float[] arrowX = new float[64]; - public static final float[] arrowY = new float[arrowX.length]; - public static final float[] arrowDepth = new float[arrowX.length]; - public static final float[] arrowPitch = new float[arrowX.length]; - public static final float[] arrowYaw = new float[arrowX.length]; - - static { - for (int i = 0; i < arrowX.length; i++) { - double r = Math.random() * 5; - double theta = Math.random() * Math.PI * 2; - - arrowX[i] = (float) (r * Math.cos(theta)); - arrowY[i] = (float) (r * Math.sin(theta)); - arrowDepth[i] = (float) (Math.random() * 0.5 + 0.5F); - - arrowPitch[i] = (float) (Math.random() * 50 - 25); - arrowYaw[i] = (float) (Math.random() * 50 - 25); - } - } + public static float firstPersonFrame; + public static boolean offhandFPRender; @SuppressWarnings("SuspiciousNameCombination") public static void moveOffHandArm(Entity entity, ModelBiped biped, float frame) { if (entity instanceof IBackhandPlayer player && (player != Minecraft.getMinecraft().thePlayer - || player.getOffSwingProgress(BackhandClientUtils.firstPersonFrame) != 0)) { + || player.getOffSwingProgress(BackhandRenderHelper.firstPersonFrame) != 0)) { float offhandSwing = player.getOffSwingProgress(frame); if (offhandSwing > 0.0F) { @@ -72,56 +47,6 @@ public static void moveOffHandArm(Entity entity, ModelBiped biped, float frame) } } - public static void applyColorFromItemStack(ItemStack itemStack, int pass) { - int col = itemStack.getItem() - .getColorFromItemStack(itemStack, pass); - float r = (float) (col >> 16 & 255) / 255.0F; - float g = (float) (col >> 8 & 255) / 255.0F; - float b = (float) (col & 255) / 255.0F; - GL11.glColor4f(r, g, b, 1.0F); - } - - @SideOnly(Side.CLIENT) - public static void renderEnchantmentEffects(Tessellator tessellator) { - GL11.glDepthFunc(GL11.GL_EQUAL); - GL11.glDisable(GL11.GL_LIGHTING); - Minecraft.getMinecraft().renderEngine.bindTexture(ITEM_GLINT); - GL11.glEnable(GL11.GL_BLEND); - GL11.glBlendFunc(GL11.GL_SRC_COLOR, GL11.GL_ONE); - float f7 = 0.76F; - GL11.glColor4f(0.5F * f7, 0.25F * f7, 0.8F * f7, 1.0F); - GL11.glMatrixMode(GL11.GL_TEXTURE); - GL11.glPushMatrix(); - float f8 = 0.125F; - GL11.glScalef(f8, f8, f8); - float f9 = (float) (Minecraft.getSystemTime() % 3000L) / 3000.0F * 8.0F; - GL11.glTranslatef(f9, 0.0F, 0.0F); - GL11.glRotatef(-50.0F, 0.0F, 0.0F, 1.0F); - ItemRenderer.renderItemIn2D(tessellator, 0.0F, 0.0F, 1.0F, 1.0F, 256, 256, RENDER_UNIT); - GL11.glPopMatrix(); - GL11.glPushMatrix(); - GL11.glScalef(f8, f8, f8); - f9 = (float) (Minecraft.getSystemTime() % 4873L) / 4873.0F * 8.0F; - GL11.glTranslatef(-f9, 0.0F, 0.0F); - GL11.glRotatef(10.0F, 0.0F, 0.0F, 1.0F); - ItemRenderer.renderItemIn2D(tessellator, 0.0F, 0.0F, 1.0F, 1.0F, 256, 256, RENDER_UNIT); - GL11.glPopMatrix(); - GL11.glMatrixMode(GL11.GL_MODELVIEW); - GL11.glDisable(GL11.GL_BLEND); - GL11.glEnable(GL11.GL_LIGHTING); - GL11.glDepthFunc(GL11.GL_LEQUAL); - } - - public static void renderTexturedQuad(int x, int y, float z, int width, int height) { - Tessellator tessellator = Tessellator.instance; - tessellator.startDrawingQuads(); - tessellator.addVertexWithUV((double) (x + 0), (double) (y + height), (double) z, 0D, 1D); - tessellator.addVertexWithUV((double) (x + width), (double) (y + height), (double) z, 1D, 1D); - tessellator.addVertexWithUV((double) (x + width), (double) (y + 0), (double) z, 1D, 0D); - tessellator.addVertexWithUV((double) (x + 0), (double) (y + 0), (double) z, 0D, 0D); - tessellator.draw(); - } - public static void renderOffhandItem(ItemRenderer otherItemRenderer, float frame) { Minecraft mc = Minecraft.getMinecraft(); EntityClientPlayerMP player = mc.thePlayer; diff --git a/src/main/java/xonin/backhand/client/world/ClientFakePlayer.java b/src/main/java/xonin/backhand/client/world/ClientFakePlayer.java deleted file mode 100644 index e543971..0000000 --- a/src/main/java/xonin/backhand/client/world/ClientFakePlayer.java +++ /dev/null @@ -1,107 +0,0 @@ -package xonin.backhand.client.world; - -import java.util.UUID; - -import net.minecraft.block.Block; -import net.minecraft.client.Minecraft; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; -import net.minecraft.item.ItemStack; -import net.minecraft.stats.StatBase; -import net.minecraft.util.ChunkCoordinates; -import net.minecraft.util.DamageSource; -import net.minecraft.util.IChatComponent; -import net.minecraft.util.MovingObjectPosition; -import net.minecraft.world.World; - -import com.mojang.authlib.GameProfile; - -public class ClientFakePlayer extends EntityPlayer { - - public static final ClientFakePlayer INSTANCE = new ClientFakePlayer(); - - public ClientFakePlayer() { - super(DummyWorld.INSTANCE, new GameProfile(UUID.randomUUID(), "[BackhandClient]")); - } - - @Override - public void addChatMessage(IChatComponent message) {} - - @Override - public boolean canCommandSenderUseCommand(int i, String s) { - return false; - } - - @Override - public ChunkCoordinates getPlayerCoordinates() { - return new ChunkCoordinates(0, 0, 0); - } - - @Override - public void addChatComponentMessage(IChatComponent message) {} - - @Override - public void addStat(StatBase par1StatBase, int par2) {} - - @Override - public void openGui(Object mod, int modGuiId, World world, int x, int y, int z) {} - - @Override - public boolean isEntityInvulnerable() { - return true; - } - - @Override - public boolean canAttackPlayer(EntityPlayer player) { - return false; - } - - @Override - public void onDeath(DamageSource source) {} - - @Override - public void onUpdate() {} - - @Override - public void travelToDimension(int dim) {} - - public boolean simulateBlockInteraction(ItemStack offhand, int x, int y, int z) { - Minecraft mc = Minecraft.getMinecraft(); - MovingObjectPosition mop = mc.objectMouseOver; - ItemStack stack = ItemStack.copyItemStack(offhand); - - if (mop == null) return false; - - float subX = (float) mop.hitVec.xCoord - x; - float subY = (float) mop.hitVec.yCoord - y; - float subZ = (float) mop.hitVec.zCoord - z; - - copyPlayerPosition(mc.thePlayer); - Block block = DummyWorld.INSTANCE.copyAndSetBlock(mc.theWorld, x, y, z, mop); - - if (block == null || block == Blocks.air) return false; - - prepareForInteraction(mc.thePlayer, stack); - - return block.onBlockActivated(DummyWorld.INSTANCE, x, y, z, this, mop.sideHit, subX, subY, subZ); - } - - private void prepareForInteraction(EntityPlayer player, ItemStack stack) { - copyPlayerPosition(player); - inventory.copyInventory(player.inventory); - setCurrentItemOrArmor(0, stack); - experienceLevel = player.experienceLevel; - experienceTotal = player.experienceTotal; - experience = player.experience; - capabilities = player.capabilities; - isAirBorne = player.isAirBorne; - inWater = player.isInWater(); - fallDistance = player.fallDistance; - this.setSprinting(player.isSprinting()); - } - - public void copyPlayerPosition(EntityPlayer player) { - this.setPositionAndRotation(player.posX, player.posY, player.posZ, player.rotationYaw, player.rotationPitch); - this.setSneaking(player.isSneaking()); - } -} diff --git a/src/main/java/xonin/backhand/client/world/DummyChunkProvider.java b/src/main/java/xonin/backhand/client/world/DummyChunkProvider.java deleted file mode 100644 index 50222c8..0000000 --- a/src/main/java/xonin/backhand/client/world/DummyChunkProvider.java +++ /dev/null @@ -1,97 +0,0 @@ -package xonin.backhand.client.world; - -import java.util.List; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import net.minecraft.entity.EnumCreatureType; -import net.minecraft.util.IProgressUpdate; -import net.minecraft.util.LongHashMap; -import net.minecraft.world.ChunkCoordIntPair; -import net.minecraft.world.ChunkPosition; -import net.minecraft.world.World; -import net.minecraft.world.biome.BiomeGenBase; -import net.minecraft.world.chunk.Chunk; -import net.minecraft.world.chunk.IChunkProvider; - -public class DummyChunkProvider implements IChunkProvider { - - private final World world; - private LongHashMap loadedChunks = new LongHashMap(); - - public DummyChunkProvider(World world) { - this.world = world; - } - - @Nullable - @Override - public Chunk loadChunk(int x, int z) { - return (Chunk) loadedChunks.getValueByKey(ChunkCoordIntPair.chunkXZ2Int(x, z)); - } - - @Nonnull - @Override - public Chunk provideChunk(int x, int z) { - long chunkKey = ChunkCoordIntPair.chunkXZ2Int(x, z); - if (loadedChunks.containsItem(chunkKey)) { - return (Chunk) loadedChunks.getValueByKey(chunkKey); - } - - Chunk chunk = new Chunk(world, x, z); - loadedChunks.add(chunkKey, chunk); - return chunk; - } - - @Override - public void populate(IChunkProvider p_73153_1_, int p_73153_2_, int p_73153_3_) {} - - @Override - public boolean saveChunks(boolean p_73151_1_, IProgressUpdate p_73151_2_) { - return false; - } - - @Override - public boolean unloadQueuedChunks() { - return false; - } - - @Override - public boolean canSave() { - return false; - } - - @Nonnull - @Override - public String makeString() { - return "Dummy"; - } - - @Override - public List getPossibleCreatures(EnumCreatureType p_73155_1_, int p_73155_2_, - int p_73155_3_, int p_73155_4_) { - return null; - } - - @Override - public ChunkPosition func_147416_a(World p_147416_1_, String p_147416_2_, int p_147416_3_, int p_147416_4_, - int p_147416_5_) { - return null; - } - - @Override - public int getLoadedChunkCount() { - return 0; - } - - @Override - public void recreateStructures(int p_82695_1_, int p_82695_2_) {} - - @Override - public void saveExtraData() {} - - @Override - public boolean chunkExists(int x, int z) { - return true; - } -} diff --git a/src/main/java/xonin/backhand/client/world/DummySaveHandler.java b/src/main/java/xonin/backhand/client/world/DummySaveHandler.java deleted file mode 100644 index a7789a3..0000000 --- a/src/main/java/xonin/backhand/client/world/DummySaveHandler.java +++ /dev/null @@ -1,99 +0,0 @@ -package xonin.backhand.client.world; - -import java.io.File; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.world.World; -import net.minecraft.world.WorldProvider; -import net.minecraft.world.chunk.Chunk; -import net.minecraft.world.chunk.storage.IChunkLoader; -import net.minecraft.world.storage.IPlayerFileData; -import net.minecraft.world.storage.ISaveHandler; -import net.minecraft.world.storage.WorldInfo; - -public class DummySaveHandler implements ISaveHandler, IPlayerFileData, IChunkLoader { - - @Nullable - @Override - public WorldInfo loadWorldInfo() { - return null; - } - - @Override - public void checkSessionLock() {} - - @Nonnull - @Override - public IChunkLoader getChunkLoader(@Nonnull WorldProvider provider) { - return this; - } - - @Nonnull - @Override - public IPlayerFileData getSaveHandler() { - return this; - } - - @Override - public void saveWorldInfoWithPlayer(@Nonnull WorldInfo worldInformation, @Nonnull NBTTagCompound tagCompound) {} - - @Override - public void saveWorldInfo(@Nonnull WorldInfo worldInformation) {} - - @Nonnull - @Override - public File getWorldDirectory() { - return null; - } - - @Nonnull - @Override - public File getMapFileFromName(@Nonnull String mapName) { - return null; - } - - @Override - public String getWorldDirectoryName() { - return null; - } - - @Nullable - @Override - public Chunk loadChunk(@Nonnull World worldIn, int x, int z) { - return null; - } - - @Override - public void saveChunk(@Nonnull World worldIn, @Nonnull Chunk chunkIn) {} - - @Override - public void saveExtraChunkData(@Nonnull World worldIn, @Nonnull Chunk chunkIn) {} - - @Override - public void chunkTick() {} - - @Override - public void saveExtraData() {} - - @Override - public void flush() {} - - @Override - public void writePlayerData(@Nonnull EntityPlayer player) {} - - @Nullable - @Override - public NBTTagCompound readPlayerData(@Nonnull EntityPlayer player) { - return null; - } - - @Nonnull - @Override - public String[] getAvailablePlayerDat() { - return new String[0]; - } -} diff --git a/src/main/java/xonin/backhand/client/world/DummyWorld.java b/src/main/java/xonin/backhand/client/world/DummyWorld.java deleted file mode 100644 index 13ad6ca..0000000 --- a/src/main/java/xonin/backhand/client/world/DummyWorld.java +++ /dev/null @@ -1,149 +0,0 @@ -package xonin.backhand.client.world; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import net.minecraft.block.Block; -import net.minecraft.entity.Entity; -import net.minecraft.init.Blocks; -import net.minecraft.item.ItemStack; -import net.minecraft.profiler.Profiler; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.MovingObjectPosition; -import net.minecraft.world.EnumSkyBlock; -import net.minecraft.world.World; -import net.minecraft.world.WorldProviderSurface; -import net.minecraft.world.WorldSettings; -import net.minecraft.world.WorldType; -import net.minecraft.world.chunk.IChunkProvider; - -import com.gtnewhorizon.gtnhlib.util.CoordinatePacker; - -import it.unimi.dsi.fastutil.longs.Long2ObjectMap; -import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongOpenHashSet; -import it.unimi.dsi.fastutil.longs.LongSet; - -public class DummyWorld extends World { - - private static final WorldSettings DEFAULT_SETTINGS = new WorldSettings( - 1L, - WorldSettings.GameType.SURVIVAL, - true, - false, - WorldType.DEFAULT); - - public static final DummyWorld INSTANCE = new DummyWorld(); - private static final LongSet placedBlocks = new LongOpenHashSet(); - private static final Long2ObjectMap tileEntities = new Long2ObjectOpenHashMap<>(); - - public DummyWorld() { - super(new DummySaveHandler(), "DummyServer", DEFAULT_SETTINGS, new WorldProviderSurface(), new Profiler()); - // Guarantee the dimension ID was not reset by the provider - this.provider.setDimension(Integer.MAX_VALUE); - int providerDim = this.provider.dimensionId; - this.provider.worldObj = this; - this.provider.setDimension(providerDim); - this.chunkProvider = this.createChunkProvider(); - this.calculateInitialSkylight(); - this.calculateInitialWeatherBody(); - } - - @Override - public void updateEntities() {} - - @Override - public void markBlockRangeForRenderUpdate(int x1, int y1, int z1, int x2, int y2, int z2) {} - - @Override - protected int func_152379_p() { - return 0; - } - - @Override - public Entity getEntityByID(int p_73045_1_) { - return null; - } - - @Nonnull - @Override - protected IChunkProvider createChunkProvider() { - return new DummyChunkProvider(this); - } - - @Override - public boolean setBlock(int x, int y, int z, Block blockIn, int metadataIn, int flags) { - if (super.setBlock(x, y, z, blockIn, metadataIn, flags)) { - long key = CoordinatePacker.pack(x, y, z); - if (blockIn != Blocks.air) { - placedBlocks.add(key); - } - return true; - } - return false; - } - - @Override - public boolean updateLightByType(EnumSkyBlock p_147463_1_, int p_147463_2_, int p_147463_3_, int p_147463_4_) { - return true; - } - - @Override - public void setTileEntity(int x, int y, int z, TileEntity tile) { - if (tile == null || tile.isInvalid()) return; - long key = CoordinatePacker.pack(x, y, z); - tileEntities.put(key, tile); - } - - @Override - public TileEntity getTileEntity(int x, int y, int z) { - return tileEntities.get(CoordinatePacker.pack(x, y, z)); - } - - @Override - public void removeTileEntity(int x, int y, int z) { - tileEntities.remove(CoordinatePacker.pack(x, y, z)); - } - - @Nullable - public Block copyAndSetBlock(World world, int x, int y, int z, MovingObjectPosition mop) { - resetWorld(); - Block block = world.getBlock(x, y, z); - - if (block == null || block == Blocks.air) return null; - - int meta = block.getDamageValue(world, x, y, z); - ItemStack stack = block.getPickBlock(mop, world, x, y, z, ClientFakePlayer.INSTANCE); - boolean placed = false; - - if (stack != null) { - for (int i = 0; i < 6; i++) { - // Adjust when placing on a block "below" - int aY = i == 1 ? y - 1 : y; - if (stack.getItem() - .onItemUse(stack, ClientFakePlayer.INSTANCE, this, x, aY, z, i, x, aY, z)) { - placed = true; - break; - } - } - } - - if (!placed) { - setBlock(x, y, z, block, meta, 3); - } - - return getBlock(x, y, z); - } - - private void resetWorld() { - if (placedBlocks.isEmpty()) return; - for (long key : placedBlocks) { - int x = CoordinatePacker.unpackX(key); - int y = CoordinatePacker.unpackY(key); - int z = CoordinatePacker.unpackZ(key); - setBlockToAir(x, y, z); - tileEntities.clear(); - } - placedBlocks.clear(); - } -} diff --git a/src/main/java/xonin/backhand/constants/EnumPacketClient.java b/src/main/java/xonin/backhand/constants/EnumPacketClient.java deleted file mode 100644 index f69fa7b..0000000 --- a/src/main/java/xonin/backhand/constants/EnumPacketClient.java +++ /dev/null @@ -1,3 +0,0 @@ -package xonin.backhand.constants; - -public enum EnumPacketClient {} diff --git a/src/main/java/xonin/backhand/constants/EnumPacketServer.java b/src/main/java/xonin/backhand/constants/EnumPacketServer.java deleted file mode 100644 index d1daaa7..0000000 --- a/src/main/java/xonin/backhand/constants/EnumPacketServer.java +++ /dev/null @@ -1,3 +0,0 @@ -package xonin.backhand.constants; - -public enum EnumPacketServer {} diff --git a/src/main/java/xonin/backhand/mixins/Mixins.java b/src/main/java/xonin/backhand/mixins/Mixins.java index 1ddfca6..4b44af4 100644 --- a/src/main/java/xonin/backhand/mixins/Mixins.java +++ b/src/main/java/xonin/backhand/mixins/Mixins.java @@ -16,10 +16,13 @@ public enum Mixins { MINECRAFT(new Builder("Shared MC Mixins") .addMixinClasses( "minecraft.MixinEntityPlayer", - "minecraft.MixinEntityPlayerMP", - "minecraft.MixinItemStack", "minecraft.MixinNetHandlerPlayServer", - "minecraft.MixinEntityItem") + "minecraft.MixinItemBow", + "minecraft.MixinEntityFishHook", + "minecraft.MixinInventoryPlayer", + "minecraft.MixinEntityTrackerEntry", + "minecraft.MixinContainerPlayer", + "minecraft.MixinItemStack") .setPhase(Phase.EARLY) .setSide(Side.BOTH) .addTargetedMod(TargetedMod.VANILLA)), @@ -30,18 +33,19 @@ public enum Mixins { "minecraft.MixinItemRenderer", "minecraft.MixinModelBiped", "minecraft.MixinNetHandlerPlayClient", - "minecraft.MixinPlayerControllerMP", - "minecraft.MixinItemBow", + // "minecraft.MixinPlayerControllerMP", + // "minecraft.MixinItemBow", "minecraft.MixinItemStackClient", - "minecraft.MixinWorld") + "minecraft.MixinMinecraft", + "minecraft.MixinGuiInventory") .setPhase(Phase.EARLY) .setSide(Side.CLIENT) .addTargetedMod(TargetedMod.VANILLA)), - FIX_SIMULATED_INTERACTION_NPE(new Builder("Fix NPE when simulating interaction with CB blocks") - .addMixinClasses("carpentersblocks.MixinPlayerPermissions") - .setPhase(Phase.LATE) - .setSide(Side.CLIENT) - .addTargetedMod(TargetedMod.CARPENTERS_BLOCKS)); + ET_FUTURUM_TOTEM_SUPPORT( + new Builder("Et Futurum Totem Support").addMixinClasses("etfuturum.MixinServerEventHandler") + .setPhase(Phase.LATE) + .setSide(Side.BOTH) + .addTargetedMod(TargetedMod.ET_FUTURUM)),; private final List mixinClasses; private final Supplier applyIf; diff --git a/src/main/java/xonin/backhand/mixins/TargetedMod.java b/src/main/java/xonin/backhand/mixins/TargetedMod.java index 5c3abda..237c521 100644 --- a/src/main/java/xonin/backhand/mixins/TargetedMod.java +++ b/src/main/java/xonin/backhand/mixins/TargetedMod.java @@ -3,7 +3,7 @@ public enum TargetedMod { VANILLA("Minecraft", null), - CARPENTERS_BLOCKS("Carpenter's Blocks", null, "CarpentersBlocks"); + ET_FUTURUM("Et Futurum Requiem", "ganymedes01.etfuturum.mixinplugin.EtFuturumEarlyMixins", "etfuturum"); /** The "name" in the @Mod annotation */ public final String modName; diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java new file mode 100644 index 0000000..bd7525d --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java @@ -0,0 +1,24 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.ContainerPlayer; +import net.minecraft.inventory.Slot; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import xonin.backhand.api.core.IOffhandInventory; + +@Mixin(ContainerPlayer.class) +public abstract class MixinContainerPlayer extends Container { + + @Inject(method = "", at = @At(value = "TAIL")) + private void backhand2$addOffhandSlot(InventoryPlayer p_i1819_1_, boolean p_i1819_2_, EntityPlayer p_i1819_3_, + CallbackInfo ci) { + addSlotToContainer(new Slot(p_i1819_1_, IOffhandInventory.OFFHAND_HOTBAR_SLOT, 152, 65)); + } +} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityFishHook.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityFishHook.java new file mode 100644 index 0000000..b348088 --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityFishHook.java @@ -0,0 +1,32 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.projectile.EntityFishHook; +import net.minecraft.init.Items; +import net.minecraft.item.ItemStack; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import xonin.backhand.api.core.BackhandUtils; + +@Mixin(EntityFishHook.class) +public abstract class MixinEntityFishHook { + + @Shadow + public EntityPlayer field_146042_b; + + @Inject( + method = "onUpdate", + cancellable = true, + at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/projectile/EntityFishHook;setDead()V", ordinal = 0)) + private void backhand$onUpdate(CallbackInfo ci) { + ItemStack itemstack = BackhandUtils.getOffhandItem(field_146042_b); + if (itemstack != null && Items.fishing_rod.equals(itemstack.getItem())) { + ci.cancel(); + } + } +} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityItem.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityItem.java deleted file mode 100644 index fccf7cd..0000000 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityItem.java +++ /dev/null @@ -1,24 +0,0 @@ -package xonin.backhand.mixins.early.minecraft; - -import net.minecraft.entity.item.EntityItem; -import net.minecraft.entity.player.EntityPlayer; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import xonin.backhand.api.core.BackhandUtils; - -@Mixin(EntityItem.class) -public abstract class MixinEntityItem { - - @Inject( - method = "onCollideWithPlayer", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/entity/item/EntityItem;getEntityItem()Lnet/minecraft/item/ItemStack;")) - private void backhand$resetHotswapOnPickup(EntityPlayer entityIn, CallbackInfo ci) { - BackhandUtils.resetAndDelayHotswap(entityIn, 0); - } -} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java index 7086937..bf83359 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java @@ -1,6 +1,7 @@ package xonin.backhand.mixins.early.minecraft; -import net.minecraft.entity.Entity; +import java.util.Objects; + import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; @@ -13,11 +14,12 @@ import org.spongepowered.asm.mixin.injection.At; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; -import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.api.core.InventoryPlayerBackhand; +import xonin.backhand.api.core.IOffhandInventory; @Mixin(EntityPlayer.class) public abstract class MixinEntityPlayer extends EntityLivingBase implements IBackhandPlayer { @@ -27,7 +29,7 @@ public abstract class MixinEntityPlayer extends EntityLivingBase implements IBac @Shadow private int itemInUseCount; @Shadow - public InventoryPlayer inventory = new InventoryPlayerBackhand((EntityPlayer) (Object) this); + public InventoryPlayer inventory; @Unique private float backhand$offHandSwingProgress = 0F; @Unique @@ -36,32 +38,19 @@ public abstract class MixinEntityPlayer extends EntityLivingBase implements IBac private int backhand$offHandSwingProgressInt = 0; @Unique private boolean backhand$isOffHandSwingInProgress = false; - @Unique - private int backhand$specialActionTimer = 0; private MixinEntityPlayer(World p_i1594_1_) { super(p_i1594_1_); } - // TODO: Why are we doing this? - @ModifyReturnValue(method = "isPlayer", at = @At(value = "RETURN")) - private boolean backhand$isPlayer(boolean original) { - return false; - } - - @ModifyExpressionValue( - method = "onItemUseFinish", - at = @At( - value = "INVOKE", - target = "Lnet/minecraftforge/event/ForgeEventFactory;onItemUseFinish(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;ILnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;", - remap = false)) - private ItemStack backhand$onItemUseFinish$beforeFinishUse(ItemStack itemStack) { - return BackhandUtils.beforeFinishUseEvent( - (EntityPlayer) (Object) this, - this.itemInUse, - this.itemInUseCount, - itemStack, - this.itemInUse.stackSize); + @WrapMethod(method = "onItemUseFinish") + private void backhand$onItemUseFinishEnd(Operation original) { + EntityPlayer player = (EntityPlayer) (Object) this; + if (Objects.equals(itemInUse, BackhandUtils.getOffhandItem(player))) { + BackhandUtils.useOffhandItem(player, () -> original.call()); + } else { + original.call(); + } } @ModifyExpressionValue( @@ -79,8 +68,12 @@ private MixinEntityPlayer(World p_i1594_1_) { } @Override - public void attackTargetEntityWithCurrentOffItem(Entity target) { - BackhandUtils.attackTargetEntityWithCurrentOffItem((EntityPlayer) (Object) this, target); + public void swingItem() { + if (inventory.currentItem == IOffhandInventory.OFFHAND_HOTBAR_SLOT) { + this.swingOffItem(); + } else { + super.swingItem(); + } } @Override @@ -120,14 +113,5 @@ protected void updateArmSwingProgress() { } this.backhand$offHandSwingProgress = (float) this.backhand$offHandSwingProgressInt / (float) var1; - if (this.backhand$specialActionTimer > 0) { - this.backhand$isOffHandSwingInProgress = false; - this.isSwingInProgress = false; - this.backhand$offHandSwingProgress = 0.0F; - this.backhand$offHandSwingProgressInt = 0; - this.swingProgress = 0.0F; - this.swingProgressInt = 0; - } - } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java index 0bd95bd..fba048a 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java @@ -1,20 +1,13 @@ package xonin.backhand.mixins.early.minecraft; -import net.minecraft.client.Minecraft; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemBow; -import net.minecraft.item.ItemStack; -import net.minecraft.util.IIcon; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.client.utils.BackhandClientUtils; +import xonin.backhand.client.utils.BackhandRenderHelper; @Mixin(EntityPlayer.class) public abstract class MixinEntityPlayerClient extends EntityLivingBase implements IBackhandPlayer { @@ -23,21 +16,21 @@ private MixinEntityPlayerClient(World p_i1594_1_) { super(p_i1594_1_); } - @Inject(method = "getItemIcon", at = @At(value = "HEAD")) - private void backhand2$getItemIcon(ItemStack itemStackIn, int p_70620_2_, CallbackInfoReturnable cir) { - EntityPlayer player = Minecraft.getMinecraft().thePlayer; - if (itemStackIn == player.getCurrentEquippedItem() && player.getCurrentEquippedItem() != null - && player.getItemInUse() != null - && player.getCurrentEquippedItem() - .getItem() instanceof ItemBow - && player.getCurrentEquippedItem() != player.getItemInUse()) { - BackhandClientUtils.disableMainhandAnimation = true; - } - } + // @Inject(method = "getItemIcon", at = @At(value = "HEAD")) + // private void backhand2$getItemIcon(ItemStack itemStackIn, int p_70620_2_, CallbackInfoReturnable cir) { + // EntityPlayer player = Minecraft.getMinecraft().thePlayer; + // if (itemStackIn == player.getCurrentEquippedItem() && player.getCurrentEquippedItem() != null + // && player.getItemInUse() != null + // && player.getCurrentEquippedItem() + // .getItem() instanceof ItemBow + // && player.getCurrentEquippedItem() != player.getItemInUse()) { + // BackhandClientUtils.disableMainhandAnimation = true; + // } + // } @Override public float getSwingProgress(float partialTicks) { - if (BackhandClientUtils.offhandFPRender) { + if (BackhandRenderHelper.offhandFPRender) { return getOffSwingProgress(partialTicks); } return super.getSwingProgress(partialTicks); diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerMP.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerMP.java deleted file mode 100644 index 3dcba09..0000000 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerMP.java +++ /dev/null @@ -1,31 +0,0 @@ -package xonin.backhand.mixins.early.minecraft; - -import java.util.List; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.inventory.Container; -import net.minecraft.item.ItemStack; -import net.minecraft.world.World; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import com.mojang.authlib.GameProfile; - -import xonin.backhand.api.core.BackhandUtils; - -@Mixin(EntityPlayerMP.class) -public abstract class MixinEntityPlayerMP extends EntityPlayer { - - public MixinEntityPlayerMP(World p_i45324_1_, GameProfile p_i45324_2_) { - super(p_i45324_1_, p_i45324_2_); - } - - @Inject(method = "sendContainerAndContentsToPlayer", at = @At(value = "TAIL")) - private void backhand$syncOffhand(Container p_71110_1_, List p_71110_2_, CallbackInfo ci) { - BackhandUtils.getOffhandEP(this).syncOffhand = true; - } -} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityTrackerEntry.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityTrackerEntry.java new file mode 100644 index 0000000..6f1d03a --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityTrackerEntry.java @@ -0,0 +1,39 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityTrackerEntry; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.ItemStack; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.llamalad7.mixinextras.sugar.Local; + +import xonin.backhand.Backhand; +import xonin.backhand.api.core.BackhandUtils; +import xonin.backhand.packet.OffhandSyncItemPacket; + +@Mixin(EntityTrackerEntry.class) +public abstract class MixinEntityTrackerEntry { + + @Shadow + public Entity myEntity; + + @Inject( + method = "tryStartWachingThis", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/entity/EntityLivingBase;getEquipmentInSlot(I)Lnet/minecraft/item/ItemStack;")) + private void backhand$syncOffhand(EntityPlayerMP receivingPlayer, CallbackInfo ci, @Local(name = "i") int i) { + if (!(myEntity instanceof EntityPlayerMP playerMP) || i > 0) return; + ItemStack offhand = BackhandUtils.getOffhandItem(playerMP); + if (offhand != null) { + Backhand.packetHandler + .sendPacketToPlayer(new OffhandSyncItemPacket(playerMP).generatePacket(), receivingPlayer); + } + } +} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiInventory.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiInventory.java new file mode 100644 index 0000000..6865e1e --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiInventory.java @@ -0,0 +1,43 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.inventory.GuiInventory; +import net.minecraft.client.renderer.InventoryEffectRenderer; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.inventory.Container; + +import org.lwjgl.opengl.GL11; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(GuiInventory.class) +public abstract class MixinGuiInventory extends InventoryEffectRenderer { + + public MixinGuiInventory(Container p_i1089_1_) { + super(p_i1089_1_); + } + + @Inject(method = "drawGuiContainerBackgroundLayer", at = @At("TAIL")) + protected void backhand$drawOffhandSlot(float partialTicks, int mouseX, int mouseY, CallbackInfo ci) { + backhand$drawItemStackSlot(guiLeft + 150, guiTop + 63); + } + + @Unique + private static void backhand$drawItemStackSlot(int x, int y) { + GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + Minecraft.getMinecraft() + .getTextureManager() + .bindTexture(Gui.statIcons); + Tessellator tessellator = Tessellator.instance; + tessellator.startDrawingQuads(); + tessellator.addVertexWithUV(x + 1, y + 1 + 18, 0, 0 * 0.0078125f, 18 * 0.0078125f); + tessellator.addVertexWithUV(x + 1 + 18, y + 1 + 18, 0, 18 * 0.0078125f, 18 * 0.0078125f); + tessellator.addVertexWithUV(x + 1 + 18, y + 1, 0, 18 * 0.0078125f, 0 * 0.0078125f); + tessellator.addVertexWithUV(x + 1, y + 1, 0, 0 * 0.0078125f, 0 * 0.0078125f); + tessellator.draw(); + } +} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java new file mode 100644 index 0000000..ec3c769 --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java @@ -0,0 +1,61 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.item.ItemStack; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; + +import com.llamalad7.mixinextras.injector.ModifyReturnValue; + +import xonin.backhand.api.core.BackhandUtils; +import xonin.backhand.api.core.IOffhandInventory; +import xonin.backhand.utils.BackhandConfig; + +@Mixin(InventoryPlayer.class) +public abstract class MixinInventoryPlayer implements IOffhandInventory { + + @Shadow + public int currentItem; + + @Shadow + public EntityPlayer player; + + @Shadow + public ItemStack[] mainInventory = new ItemStack[37]; + + @ModifyConstant(method = "readFromNBT", constant = @Constant(intValue = 36)) + private int backhand$correctInventorySize(int constant) { + return 37; + } + + @ModifyReturnValue(method = "getCurrentItem", at = @At("RETURN")) + private ItemStack backhand$getOffhandItem(ItemStack original) { + if (currentItem == OFFHAND_HOTBAR_SLOT) { + return BackhandUtils.getOffhandItem(player); + } + return original; + } + + @ModifyReturnValue(method = "getFirstEmptyStack", at = @At("RETURN")) + private int backhand$checkOffhandPickup(int original) { + if (!BackhandConfig.OffhandPickup && original == OFFHAND_HOTBAR_SLOT) { + return -1; + } + return original; + } + + @Override + public ItemStack backhand$getOffhandItem() { + return mainInventory[OFFHAND_HOTBAR_SLOT]; + } + + @Override + public void backhand$setOffhandItem(ItemStack stack) { + mainInventory[OFFHAND_HOTBAR_SLOT] = stack; + } +} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemBow.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemBow.java index 9f51e62..91c5ab7 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemBow.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemBow.java @@ -1,31 +1,34 @@ package xonin.backhand.mixins.early.minecraft; -import net.minecraft.client.Minecraft; +import net.minecraft.block.BlockDispenser; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemBow; -import net.minecraft.util.IIcon; +import net.minecraft.item.ItemStack; +import net.minecraft.util.RegistrySimple; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.sugar.Local; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import xonin.backhand.client.utils.BackhandClientUtils; +import xonin.backhand.api.core.BackhandUtils; @Mixin(ItemBow.class) public abstract class MixinItemBow extends Item { - @SideOnly(Side.CLIENT) - @ModifyReturnValue(method = "getItemIconForUseDuration", at = @At("RETURN")) - private IIcon backhand$cancelAnimation(IIcon original) { - if (BackhandClientUtils.disableMainhandAnimation) { - BackhandClientUtils.disableMainhandAnimation = false; - EntityPlayer player = Minecraft.getMinecraft().thePlayer; - return getIcon(player.getCurrentEquippedItem(), 0, player, player.getItemInUse(), 0); + @ModifyExpressionValue( + method = "onItemRightClick", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/entity/player/InventoryPlayer;hasItem(Lnet/minecraft/item/Item;)Z")) + private boolean backhand$cancelAnimation(boolean original, @Local(argsOnly = true) EntityPlayer player) { + if (!original) { + ItemStack offhand = BackhandUtils.getOffhandItem(player); + return offhand != null + && ((RegistrySimple) BlockDispenser.dispenseBehaviorRegistry).containsKey(offhand.getItem()); } - return original; + return true; } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java index a6ac6d8..ac75bca 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java @@ -15,7 +15,6 @@ import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IBackhandPlayer; import xonin.backhand.client.ClientEventHandler; -import xonin.backhand.client.utils.BackhandClientUtils; import xonin.backhand.client.utils.BackhandRenderHelper; import xonin.backhand.utils.BackhandConfig; import xonin.backhand.utils.BackhandConfigClient; @@ -25,7 +24,7 @@ public abstract class MixinItemRenderer { @Inject(method = "renderItemInFirstPerson", at = @At("RETURN")) private void backhand$renderItemInFirstPerson(float frame, CallbackInfo ci) { - if (BackhandClientUtils.offhandFPRender) return; + if (BackhandRenderHelper.offhandFPRender) return; EntityPlayer player = Minecraft.getMinecraft().thePlayer; ClientEventHandler.renderingPlayer = player; @@ -43,13 +42,13 @@ public abstract class MixinItemRenderer { return; } - BackhandClientUtils.firstPersonFrame = frame; + BackhandRenderHelper.firstPersonFrame = frame; BackhandRenderHelper.itemRenderer.updateEquippedItem(); - BackhandClientUtils.offhandFPRender = true; + BackhandRenderHelper.offhandFPRender = true; GL11.glEnable(GL11.GL_CULL_FACE); GL11.glCullFace(GL11.GL_FRONT); BackhandRenderHelper.renderOffhandItem((ItemRenderer) (Object) this, frame); GL11.glCullFace(GL11.GL_BACK); - BackhandClientUtils.offhandFPRender = false; + BackhandRenderHelper.offhandFPRender = false; } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java index 8c21b0c..b02732c 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java @@ -1,30 +1,33 @@ package xonin.backhand.mixins.early.minecraft; -import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraftforge.common.util.FakePlayer; -import net.minecraftforge.event.ForgeEventFactory; +import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; import xonin.backhand.api.core.BackhandUtils; +import xonin.backhand.api.core.IOffhandInventory; @Mixin(ItemStack.class) -public abstract class MixinItemStack { - - @Inject(method = "damageItem", at = @At(value = "TAIL")) - private void backhand$damageOffhand(int p_77972_1_, EntityLivingBase entity, CallbackInfo ci) { - if (!(entity instanceof EntityPlayer player) || entity instanceof FakePlayer) return; +public class MixinItemStack { - ItemStack itemStack = (ItemStack) (Object) this; - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - if (offhandItem != null && itemStack == offhandItem && itemStack.stackSize == 0) { - BackhandUtils.setPlayerOffhandItem(player, null); - ForgeEventFactory.onPlayerDestroyItem(player, offhandItem); + @WrapWithCondition( + method = "updateAnimation", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/item/Item;onUpdate(Lnet/minecraft/item/ItemStack;Lnet/minecraft/world/World;Lnet/minecraft/entity/Entity;IZ)V")) + private boolean backhand$updateOffhand(Item item, ItemStack stack, World worldIn, Entity entityIn, int index, + boolean p_77663_5_) { + if (index == IOffhandInventory.OFFHAND_HOTBAR_SLOT && entityIn instanceof EntityPlayer player) { + BackhandUtils.useOffhandItem(player, () -> item.onUpdate(stack, worldIn, entityIn, index, p_77663_5_)); + return false; } + return true; } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStackClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStackClient.java index 5ee3bd4..fc474e2 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStackClient.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStackClient.java @@ -2,14 +2,23 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.EnumAction; +import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemBucket; +import net.minecraft.item.ItemDoor; +import net.minecraft.item.ItemRedstone; +import net.minecraft.item.ItemReed; +import net.minecraft.item.ItemSeedFood; +import net.minecraft.item.ItemSign; +import net.minecraft.item.ItemSkull; import net.minecraft.item.ItemStack; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import com.llamalad7.mixinextras.injector.ModifyReturnValue; -import xonin.backhand.HookContainerClass; import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.client.ClientEventHandler; @@ -27,16 +36,27 @@ public abstract class MixinItemStackClient { if (offhandItem != null) { ItemStack mainHandItem = player.getCurrentEquippedItem(); if (mainHandItem != null && (BackhandUtils.checkForRightClickFunctionNoAction(mainHandItem) - || HookContainerClass.isItemBlock(mainHandItem.getItem()))) { + || backhand$isItemBlock(mainHandItem.getItem()))) { if (itemStack == offhandItem) { return EnumAction.none; } } else if (itemStack == mainHandItem && (!(BackhandUtils.checkForRightClickFunctionNoAction(offhandItem) - || HookContainerClass.isItemBlock(offhandItem.getItem())) || player.getItemInUse() != mainHandItem)) { + || backhand$isItemBlock(offhandItem.getItem())) || player.getItemInUse() != mainHandItem)) { return EnumAction.none; } } return original; } + @Unique + private boolean backhand$isItemBlock(Item item) { + return item instanceof ItemBlock || item instanceof ItemDoor + || item instanceof ItemSign + || item instanceof ItemReed + || item instanceof ItemSeedFood + || item instanceof ItemRedstone + || item instanceof ItemBucket + || item instanceof ItemSkull; + } + } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java new file mode 100644 index 0000000..c69401a --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -0,0 +1,258 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.block.material.Material; +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.client.multiplayer.PlayerControllerMP; +import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.client.particle.EffectRenderer; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.item.EnumAction; +import net.minecraft.item.ItemStack; +import net.minecraft.util.MovingObjectPosition; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; + +import org.apache.logging.log4j.Logger; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; + +import xonin.backhand.api.core.BackhandUtils; +import xonin.backhand.api.core.IBackhandPlayer; +import xonin.backhand.api.core.IOffhandInventory; +import xonin.backhand.utils.BackhandConfig; + +@Mixin(Minecraft.class) +public abstract class MixinMinecraft { + + @Shadow + public EntityClientPlayerMP thePlayer; + + @Shadow + public WorldClient theWorld; + + @Shadow + public MovingObjectPosition objectMouseOver; + + @Shadow + public PlayerControllerMP playerController; + + @Shadow + public int rightClickDelayTimer; + + @Shadow + @Final + private static Logger logger; + + @Shadow + public EntityRenderer entityRenderer; + + @Shadow + public EffectRenderer effectRenderer; + @Unique + private int backhand$breakBlockTimer = 0; + + /** + * @author Lyft + * @reason Offhand support + */ + @Overwrite + public void func_147121_ag() { + rightClickDelayTimer = 4; + ItemStack mainHandItem = thePlayer.inventory.getCurrentItem(); + ItemStack offhandItem = BackhandUtils.getOffhandItem(thePlayer); + boolean useMainhand = true; + boolean useOffhand = true; + + if (objectMouseOver == null) { + logger.warn("Null returned as \'hitResult\', this shouldn\'t happen!"); + } else { + switch (objectMouseOver.typeOfHit) { + case ENTITY: + if (playerController.interactWithEntitySendPacket(thePlayer, objectMouseOver.entityHit)) { + useMainhand = false; + } else if (BackhandUtils.useOffhandItem( + thePlayer, + () -> playerController.interactWithEntitySendPacket(thePlayer, objectMouseOver.entityHit))) { + useOffhand = false; + } + + break; + case BLOCK: + int x = objectMouseOver.blockX; + int y = objectMouseOver.blockY; + int z = objectMouseOver.blockZ; + + if (!theWorld.getBlock(x, y, z) + .isAir(theWorld, x, y, z)) { + int l = mainHandItem != null ? mainHandItem.stackSize : 0; + + boolean result = !net.minecraftforge.event.ForgeEventFactory + .onPlayerInteract( + thePlayer, + net.minecraftforge.event.entity.player.PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK, + x, + y, + z, + objectMouseOver.sideHit, + theWorld) + .isCanceled(); + if (result && playerController.onPlayerRightClick( + thePlayer, + theWorld, + mainHandItem, + x, + y, + z, + objectMouseOver.sideHit, + objectMouseOver.hitVec)) { + useMainhand = false; + thePlayer.swingItem(); + } + + if (!useMainhand) { + ItemStack offhand = BackhandUtils.getOffhandItem(thePlayer); + if (offhand == null) return; + PlayerInteractEvent event = new PlayerInteractEvent( + thePlayer, + PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK, + x, + y, + z, + objectMouseOver.sideHit, + theWorld); + + if (!MinecraftForge.EVENT_BUS.post(event) && BackhandUtils.useOffhandItem( + thePlayer, + () -> playerController.onPlayerRightClick( + thePlayer, + theWorld, + offhand, + x, + y, + z, + objectMouseOver.sideHit, + objectMouseOver.hitVec))) { + useOffhand = false; + ((IBackhandPlayer) thePlayer).swingOffItem(); + } + } + + if (mainHandItem != null) { + if (mainHandItem.stackSize == 0) { + thePlayer.setCurrentItemOrArmor(0, null); + } else if (mainHandItem.stackSize != l || playerController.isInCreativeMode()) { + entityRenderer.itemRenderer.resetEquippedProgress(); + } + } + + if (offhandItem != null) { + if (offhandItem.stackSize == 0) { + thePlayer.inventory + .setInventorySlotContents(IOffhandInventory.OFFHAND_HOTBAR_SLOT, null); + } + } + } + } + + if (useMainhand) { + ItemStack itemstack1 = thePlayer.inventory.getCurrentItem(); + + boolean result = !net.minecraftforge.event.ForgeEventFactory + .onPlayerInteract( + thePlayer, + net.minecraftforge.event.entity.player.PlayerInteractEvent.Action.RIGHT_CLICK_AIR, + 0, + 0, + 0, + -1, + theWorld) + .isCanceled(); + if (result && itemstack1 != null && playerController.sendUseItem(thePlayer, theWorld, itemstack1)) { + entityRenderer.itemRenderer.resetEquippedProgress2(); + } + } + + if (useOffhand && offhandItem != null && thePlayer.getItemInUse() == null) { + boolean trySecondaryAction = !BackhandUtils.useOffhandItem(thePlayer, () -> { + PlayerInteractEvent useItemEvent = new PlayerInteractEvent( + thePlayer, + PlayerInteractEvent.Action.RIGHT_CLICK_AIR, + 0, + 0, + -1, + 0, + theWorld); + if (!MinecraftForge.EVENT_BUS.post(useItemEvent)) { + playerController.sendUseItem(thePlayer, theWorld, offhandItem); + return thePlayer.getItemInUse() != null; + } + return false; + }); + + if (trySecondaryAction && offhandItem.getItemUseAction() == EnumAction.none) { + switch (objectMouseOver.typeOfHit) { + case ENTITY -> BackhandUtils.useOffhandItem(thePlayer, () -> { + thePlayer.swingItem(); + playerController.attackEntity(thePlayer, objectMouseOver.entityHit); + }); + case BLOCK -> { + if (BackhandConfig.OffhandBreakBlocks) { + BackhandUtils.useOffhandItem(thePlayer, () -> { + backhand$breakBlockTimer = 5; + playerController.clickBlock( + objectMouseOver.blockX, + objectMouseOver.blockY, + objectMouseOver.blockZ, + objectMouseOver.sideHit); + }); + } + } + } + } + } + } + } + + @WrapWithCondition( + method = "func_147115_a", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/multiplayer/PlayerControllerMP;resetBlockRemoving()V")) + protected boolean backhand$pauseReset(PlayerControllerMP instance) { + if (backhand$breakBlockTimer > 0) { + backhand$breakBlockTimer--; + return false; + } + return true; + } + + @Inject(method = "func_147115_a", at = @At(value = "HEAD")) + protected void backhand$breakBlockOffhand(boolean leftClick, CallbackInfo ci) { + if (backhand$breakBlockTimer > 0) { + BackhandUtils.useOffhandItem(thePlayer, () -> { + int i = objectMouseOver.blockX; + int j = objectMouseOver.blockY; + int k = objectMouseOver.blockZ; + + if (theWorld.getBlock(i, j, k) + .getMaterial() != Material.air) { + playerController.onPlayerDamageBlock(i, j, k, objectMouseOver.sideHit); + + if (thePlayer.isCurrentToolAdventureModeExempt(i, j, k)) { + effectRenderer.addBlockHitEffects(i, j, k, objectMouseOver); + thePlayer.swingItem(); + } + } + }); + } + } +} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java index b9c5984..872b730 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java @@ -7,7 +7,7 @@ import com.llamalad7.mixinextras.injector.ModifyExpressionValue; -import xonin.backhand.api.core.InventoryPlayerBackhand; +import xonin.backhand.api.core.IOffhandInventory; @Mixin(NetHandlerPlayClient.class) public abstract class MixinNetHandlerPlayClient { @@ -20,6 +20,6 @@ public abstract class MixinNetHandlerPlayClient { ordinal = 1)) private int backhand$isValidInventorySlot(int original) { // return a valid int e.g. between 0 and < 9 - return InventoryPlayerBackhand.isValidSwitch(original) ? 0 : -1; + return IOffhandInventory.isValidSwitch(original) ? 0 : -1; } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java index dc6261e..17eef70 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java @@ -1,26 +1,20 @@ package xonin.backhand.mixins.early.minecraft; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.EnumAction; -import net.minecraft.item.ItemStack; import net.minecraft.network.NetHandlerPlayServer; -import net.minecraft.network.Packet; -import net.minecraft.network.play.client.C02PacketUseEntity; -import net.minecraft.network.play.client.C07PacketPlayerDigging; -import net.minecraft.network.play.server.S2FPacketSetSlot; import net.minecraft.server.management.ItemInWorldManager; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.api.core.InventoryPlayerBackhand; +import xonin.backhand.api.core.IOffhandInventory; +import xonin.backhand.utils.BackhandConfig; @Mixin(NetHandlerPlayServer.class) public abstract class MixinNetHandlerPlayServer { @@ -39,82 +33,26 @@ public abstract class MixinNetHandlerPlayServer { ordinal = 1)) private int backhand$isValidInventorySlot(int original) { // return a valid int e.g. between 0 and < 9 - return InventoryPlayerBackhand.isValidSwitch(original) ? 0 : -1; + return IOffhandInventory.isValidSwitch(original) ? 0 : -1; } - @Inject( + @WrapWithCondition( method = "processPlayerDigging", at = @At( value = "INVOKE", target = "Lnet/minecraft/server/management/ItemInWorldManager;uncheckedTryHarvestBlock(III)V")) - private void backhand$playerDigging(C07PacketPlayerDigging packetIn, CallbackInfo ci) { - ItemInWorldManager instance = playerEntity.theItemInWorldManager; - int x = packetIn.func_149505_c(); - int y = packetIn.func_149503_d(); - int z = packetIn.func_149502_e(); - instance.theWorld.destroyBlockInWorldPartially(instance.thisPlayerMP.getEntityId(), x, y, z, -1); - instance.tryHarvestBlock(x, y, z); + private boolean backhand$playerDigging(ItemInWorldManager instance, int l, int block, int i) { + return BackhandConfig.OffhandBreakBlocks + || playerEntity.inventory.currentItem != IOffhandInventory.OFFHAND_HOTBAR_SLOT; } - @Inject( - method = "processPlayerDigging", + @WrapWithCondition( + method = "processUseEntity", at = @At( value = "INVOKE", - target = "Lnet/minecraft/entity/player/EntityPlayerMP;dropOneItem(Z)Lnet/minecraft/entity/item/EntityItem;")) - private void backhand$playerDropItem(C07PacketPlayerDigging packetIn, CallbackInfo ci) { - BackhandUtils.resetAndDelayHotswap(playerEntity, 5); - } - - @Inject(method = "processUseEntity", at = @At("HEAD")) - private void backhand$hotswapOnEntityInteract(C02PacketUseEntity packetIn, CallbackInfo ci) { - if (backhand$shouldSwapOffhand(packetIn.func_149565_c())) { - BackhandUtils.swapOffhandItem(playerEntity); - backhand$swappedOffhand = true; - } - } - - @Inject( - method = "processUseEntity", - at = { - @At( - value = "INVOKE", - target = "Lnet/minecraft/network/NetHandlerPlayServer;kickPlayerFromServer(Ljava/lang/String;)V"), - @At(value = "TAIL") }) - private void backhand$swapBackPostEntityInteract(C02PacketUseEntity packetIn, CallbackInfo ci) { - if (backhand$swappedOffhand) { - BackhandUtils.swapOffhandItem(playerEntity); - } - backhand$swappedOffhand = false; - } - - @Inject(method = "sendPacket", at = @At(value = "HEAD"), cancellable = true) - private void backhand$skipSetSlotOnHotswap(Packet packetIn, CallbackInfo ci) { - if (packetIn instanceof S2FPacketSetSlot setSlotPacket) { - if (backhand$shouldSkipSetSlot(setSlotPacket)) { - ci.cancel(); - } - } - } - - @Unique - private boolean backhand$shouldSwapOffhand(C02PacketUseEntity.Action action) { - ItemStack offhandStack = BackhandUtils.getOffhandItem(playerEntity); - return !playerEntity.isUsingItem() && BackhandUtils.checkForRightClickFunction(offhandStack) - && offhandStack.getItemUseAction() != EnumAction.bow - && !BackhandUtils.checkForRightClickFunction(playerEntity.getCurrentEquippedItem()) - && action == C02PacketUseEntity.Action.INTERACT; - } - - @Unique - private boolean backhand$shouldSkipSetSlot(S2FPacketSetSlot setSlotPacket) { - int packetWindowId = setSlotPacket.field_149179_a; - int packetSlot = setSlotPacket.field_149177_b; - ItemStack packetStack = setSlotPacket.field_149178_c; - ItemStack offhandStack = BackhandUtils.getOffhandItem(playerEntity); - return offhandStack != null && BackhandUtils.getOffhandEP(playerEntity).ignoreSetSlot - && packetStack != null - && packetSlot == BackhandUtils.getOffhandEP(playerEntity).activeSlot - && packetStack.getItem() == offhandStack.getItem() - && packetWindowId != -1; + target = "Lnet/minecraft/entity/player/EntityPlayerMP;attackTargetEntityWithCurrentItem(Lnet/minecraft/entity/Entity;)V")) + private boolean backhand$checkOffhandAttack(EntityPlayerMP instance, Entity entity) { + return BackhandConfig.OffhandAttack + || playerEntity.inventory.currentItem != IOffhandInventory.OFFHAND_HOTBAR_SLOT; } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinPlayerControllerMP.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinPlayerControllerMP.java deleted file mode 100644 index 4c3179c..0000000 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinPlayerControllerMP.java +++ /dev/null @@ -1,33 +0,0 @@ -package xonin.backhand.mixins.early.minecraft; - -import net.minecraft.client.multiplayer.PlayerControllerMP; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import com.llamalad7.mixinextras.injector.ModifyReturnValue; - -import xonin.backhand.client.BackhandClientTickHandler; -import xonin.backhand.client.utils.BackhandClientUtils; - -@Mixin(PlayerControllerMP.class) -public abstract class MixinPlayerControllerMP { - - @Inject(method = "resetBlockRemoving", at = @At("HEAD"), cancellable = true) - private void backhand$cancelRemoval(CallbackInfo ci) { - if (BackhandClientUtils.countToCancel > 0) { - BackhandClientUtils.countToCancel--; - ci.cancel(); - } - } - - @ModifyReturnValue(method = "interactWithEntitySendPacket", at = @At("RETURN")) - private boolean backhand$setAttackDelay(boolean original) { - if (original) { - BackhandClientTickHandler.attackDelay = 5; - } - return original; - } -} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinWorld.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinWorld.java deleted file mode 100644 index 96256dc..0000000 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinWorld.java +++ /dev/null @@ -1,23 +0,0 @@ -package xonin.backhand.mixins.early.minecraft; - -import net.minecraft.world.IBlockAccess; -import net.minecraft.world.World; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import xonin.backhand.client.world.DummyWorld; - -@Mixin(World.class) -public abstract class MixinWorld { - - @Inject(method = "doesBlockHaveSolidTopSurface", at = @At(value = "HEAD"), cancellable = true) - private static void backhand$allowPlacementInDummyWorld(IBlockAccess worldIn, int x, int y, int z, - CallbackInfoReturnable cir) { - if (worldIn instanceof DummyWorld) { - cir.setReturnValue(true); - } - } -} diff --git a/src/main/java/xonin/backhand/mixins/late/carpentersblocks/MixinPlayerPermissions.java b/src/main/java/xonin/backhand/mixins/late/carpentersblocks/MixinPlayerPermissions.java deleted file mode 100644 index 39de8b9..0000000 --- a/src/main/java/xonin/backhand/mixins/late/carpentersblocks/MixinPlayerPermissions.java +++ /dev/null @@ -1,25 +0,0 @@ -package xonin.backhand.mixins.late.carpentersblocks; - -import net.minecraft.entity.player.EntityPlayer; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import com.carpentersblocks.util.protection.IProtected; -import com.carpentersblocks.util.protection.PlayerPermissions; - -import xonin.backhand.client.world.ClientFakePlayer; - -@Mixin(value = PlayerPermissions.class, remap = false) -public abstract class MixinPlayerPermissions { - - @Inject(method = "hasElevatedPermission", at = @At(value = "HEAD"), cancellable = true) - private static void backhand$fixFakePlayerNPE(IProtected object, EntityPlayer entityPlayer, - boolean enforceOwnership, CallbackInfoReturnable cir) { - if (entityPlayer instanceof ClientFakePlayer) { - cir.setReturnValue(true); - } - } -} diff --git a/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java b/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java new file mode 100644 index 0000000..faba504 --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java @@ -0,0 +1,51 @@ +package xonin.backhand.mixins.late.etfuturum; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraftforge.event.entity.living.LivingHurtEvent; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import ganymedes01.etfuturum.ModItems; +import ganymedes01.etfuturum.core.handlers.ServerEventHandler; +import xonin.backhand.api.core.BackhandUtils; + +@Mixin(ServerEventHandler.class) +public abstract class MixinServerEventHandler { + + @Shadow(remap = false) + public abstract void handleTotemCheck(EntityLivingBase entity, LivingHurtEvent event); + + @Unique + private boolean backhand$skipCheck; + + @Inject( + method = "handleTotemCheck", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/entity/EntityLivingBase;getHeldItem()Lnet/minecraft/item/ItemStack;", + ordinal = 0), + remap = false, + cancellable = true) + private void backhand$useOffhandTotem(EntityLivingBase entity, LivingHurtEvent event, CallbackInfo ci) { + if (backhand$skipCheck || !(entity instanceof EntityPlayer player)) return; + ItemStack offhand = BackhandUtils.getOffhandItem(player); + if (offhand == null) return; + ItemStack mainhand = player.getHeldItem(); + Item totem = ModItems.TOTEM_OF_UNDYING.get(); + + if (totem.equals(offhand.getItem()) && (mainhand == null || totem.equals(mainhand.getItem()))) { + ci.cancel(); + backhand$skipCheck = true; + BackhandUtils.useOffhandItem(player, () -> handleTotemCheck(entity, event)); + backhand$skipCheck = false; + } + } +} diff --git a/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java b/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java index 28b422a..3610d3c 100644 --- a/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java +++ b/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java @@ -3,6 +3,7 @@ import java.util.Hashtable; import java.util.Map; +import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.network.NetHandlerPlayServer; @@ -14,7 +15,6 @@ import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.internal.FMLProxyPacket; import cpw.mods.fml.relauncher.Side; -import xonin.backhand.Backhand; public final class BackhandPacketHandler { @@ -24,11 +24,8 @@ public final class BackhandPacketHandler { public BackhandPacketHandler() { map.put(OffhandSyncItemPacket.packetName, new OffhandSyncItemPacket()); map.put(OffhandAnimationPacket.packetName, new OffhandAnimationPacket()); - map.put(OffhandPlaceBlockPacket.packetName, new OffhandPlaceBlockPacket()); - map.put(OffhandToServerPacket.packetName, new OffhandToServerPacket()); map.put(OffhandSwapPacket.packetName, new OffhandSwapPacket()); map.put(OffhandSwapClientPacket.packetName, new OffhandSwapClientPacket()); - map.put(OffhandAttackPacket.packetName, new OffhandAttackPacket()); map.put(OffhandConfigSyncPacket.packetName, new OffhandConfigSyncPacket()); } @@ -50,7 +47,7 @@ public void onServerPacket(FMLNetworkEvent.ServerCustomPacketEvent event) { @SubscribeEvent public void onClientPacket(FMLNetworkEvent.ClientCustomPacketEvent event) { map.get(event.packet.channel()) - .process(event.packet.payload(), Backhand.proxy.getClientPlayer()); + .process(event.packet.payload(), Minecraft.getMinecraft().thePlayer); } public void sendPacketToPlayer(FMLProxyPacket packet, EntityPlayerMP player) { diff --git a/src/main/java/xonin/backhand/packet/OffhandAttackPacket.java b/src/main/java/xonin/backhand/packet/OffhandAttackPacket.java deleted file mode 100644 index be30c6c..0000000 --- a/src/main/java/xonin/backhand/packet/OffhandAttackPacket.java +++ /dev/null @@ -1,64 +0,0 @@ -package xonin.backhand.packet; - -import net.minecraft.entity.Entity; -import net.minecraft.entity.item.EntityItem; -import net.minecraft.entity.item.EntityXPOrb; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.projectile.EntityArrow; - -import cpw.mods.fml.common.network.ByteBufUtils; -import io.netty.buffer.ByteBuf; -import xonin.backhand.Backhand; -import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.utils.BackhandConfig; -import xonin.backhand.utils.EnumAnimations; - -public class OffhandAttackPacket extends AbstractPacket { - - public static final String packetName = "MB2|Attack"; - private String user; - private int targetId; - - public OffhandAttackPacket(EntityPlayer player, Entity target) { - this.user = player.getCommandSenderName(); - this.targetId = target.getEntityId(); - } - - public OffhandAttackPacket() {} - - @Override - public String getChannel() { - return packetName; - } - - @Override - public void write(ByteBuf out) { - ByteBufUtils.writeUTF8String(out, user); - out.writeInt(targetId); - } - - @Override - public void process(ByteBuf inputStream, EntityPlayer sender) { - if (!BackhandConfig.OffhandAttack) { - return; - } - - this.user = ByteBufUtils.readUTF8String(inputStream); - this.targetId = inputStream.readInt(); - - EntityPlayer player = sender.worldObj.getPlayerEntityByName(user); - Entity target = sender.worldObj.getEntityByID(this.targetId); - if (player != null && target != null) { - if (target instanceof EntityItem || target instanceof EntityXPOrb - || target instanceof EntityArrow - || target == player) { - return; - } - ((IBackhandPlayer) player).attackTargetEntityWithCurrentOffItem(target); - Backhand.packetHandler.sendPacketAround( - player, - 120, - new OffhandAnimationPacket(EnumAnimations.OffHandSwing, player).generatePacket()); - } - } -} diff --git a/src/main/java/xonin/backhand/packet/OffhandConfigSyncPacket.java b/src/main/java/xonin/backhand/packet/OffhandConfigSyncPacket.java index ab4c84b..63ccbdb 100644 --- a/src/main/java/xonin/backhand/packet/OffhandConfigSyncPacket.java +++ b/src/main/java/xonin/backhand/packet/OffhandConfigSyncPacket.java @@ -4,7 +4,6 @@ import net.minecraft.entity.player.EntityPlayer; import io.netty.buffer.ByteBuf; -import xonin.backhand.client.utils.BackhandClientUtils; import xonin.backhand.utils.BackhandConfig; public final class OffhandConfigSyncPacket extends AbstractPacket { @@ -23,12 +22,11 @@ public void process(ByteBuf inputStream, EntityPlayer player) { BackhandConfig.OffhandAttack = inputStream.readBoolean(); BackhandConfig.EmptyOffhand = inputStream.readBoolean(); BackhandConfig.OffhandBreakBlocks = inputStream.readBoolean(); - BackhandConfig.UseOffhandArrows = inputStream.readBoolean(); - BackhandConfig.UseOffhandBow = inputStream.readBoolean(); - BackhandConfig.OffhandTickHotswap = inputStream.readBoolean(); - BackhandConfig.AlternateOffhandSlot = inputStream.readInt(); - BackhandConfig.UseInventorySlot = inputStream.readBoolean(); - BackhandClientUtils.receivedConfigs = true; + // BackhandConfig.UseOffhandArrows = inputStream.readBoolean(); + // BackhandConfig.UseOffhandBow = inputStream.readBoolean(); + // BackhandConfig.OffhandTickHotswap = inputStream.readBoolean(); + // BackhandConfig.AlternateOffhandSlot = inputStream.readInt(); + // BackhandConfig.UseInventorySlot = inputStream.readBoolean(); } @Override @@ -41,10 +39,10 @@ public void write(ByteBuf out) { out.writeBoolean(BackhandConfig.OffhandAttack); out.writeBoolean(BackhandConfig.EmptyOffhand); out.writeBoolean(BackhandConfig.OffhandBreakBlocks); - out.writeBoolean(BackhandConfig.UseOffhandArrows); - out.writeBoolean(BackhandConfig.UseOffhandBow); - out.writeBoolean(BackhandConfig.OffhandTickHotswap); - out.writeInt(BackhandConfig.AlternateOffhandSlot); - out.writeBoolean(BackhandConfig.UseInventorySlot); + // out.writeBoolean(BackhandConfig.UseOffhandArrows); + // out.writeBoolean(BackhandConfig.UseOffhandBow); + // out.writeBoolean(BackhandConfig.OffhandTickHotswap); + // out.writeInt(BackhandConfig.AlternateOffhandSlot); + // out.writeBoolean(BackhandConfig.UseInventorySlot); } } diff --git a/src/main/java/xonin/backhand/packet/OffhandPlaceBlockPacket.java b/src/main/java/xonin/backhand/packet/OffhandPlaceBlockPacket.java deleted file mode 100644 index ccfe1a4..0000000 --- a/src/main/java/xonin/backhand/packet/OffhandPlaceBlockPacket.java +++ /dev/null @@ -1,207 +0,0 @@ -package xonin.backhand.packet; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.ItemStack; -import net.minecraft.network.play.server.S02PacketChat; -import net.minecraft.network.play.server.S23PacketBlockChange; -import net.minecraft.server.MinecraftServer; -import net.minecraft.util.ChatComponentTranslation; -import net.minecraft.util.EnumChatFormatting; -import net.minecraft.world.World; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.ForgeEventFactory; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; - -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.eventhandler.Event; -import cpw.mods.fml.common.network.ByteBufUtils; -import cpw.mods.fml.relauncher.Side; -import io.netty.buffer.ByteBuf; -import xonin.backhand.Backhand; -import xonin.backhand.HookContainerClass; -import xonin.backhand.api.PlayerEventChild; -import xonin.backhand.api.core.BackhandUtils; - -public final class OffhandPlaceBlockPacket extends AbstractPacket { - - public static final String packetName = "MB2|Place"; - private int xPosition; - private int yPosition; - private int zPosition; - /** The offset to use for block/item placement. */ - private int direction; - private ItemStack itemStack; - /** The offset from xPosition where the actual click took place */ - private float xOffset; - /** The offset from yPosition where the actual click took place */ - private float yOffset; - /** The offset from zPosition where the actual click took place */ - private float zOffset; - - public OffhandPlaceBlockPacket() {} - - public OffhandPlaceBlockPacket(int par1, int par2, int par3, int par4, ItemStack par5ItemStack, float par6, - float par7, float par8) { - this.xPosition = par1; - this.yPosition = par2; - this.zPosition = par3; - this.direction = par4; - this.itemStack = ItemStack.copyItemStack(par5ItemStack); - this.xOffset = par6; - this.yOffset = par7; - this.zOffset = par8; - } - - @Override - public String getChannel() { - return packetName; - } - - @Override - public void write(ByteBuf out) { - out.writeInt(this.xPosition); - out.writeByte(this.yPosition); - out.writeInt(this.zPosition); - out.writeByte(this.direction); - ByteBufUtils.writeItemStack(out, this.itemStack); - out.writeByte((int) (this.xOffset * 16.0F)); - out.writeByte((int) (this.yOffset * 16.0F)); - out.writeByte((int) (this.zOffset * 16.0F)); - } - - @Override - public void process(ByteBuf in, EntityPlayer player) { - try { - this.xPosition = in.readInt(); - this.yPosition = in.readUnsignedByte(); - this.zPosition = in.readInt(); - this.direction = in.readUnsignedByte(); - this.itemStack = ByteBufUtils.readItemStack(in); - this.xOffset = in.readUnsignedByte() / 16.0F; - this.yOffset = in.readUnsignedByte() / 16.0F; - this.zOffset = in.readUnsignedByte() / 16.0F; - } catch (Exception io) { - return; - } - if (!(player instanceof EntityPlayerMP playerMP)) return; - ItemStack offhandWeapon = BackhandUtils.getOffhandItem(playerMP); - if (offhandWeapon != null && !BackhandUtils.usagePriorAttack(offhandWeapon)) return; - boolean flag = true; - int i = xPosition; - int j = yPosition; - int k = zPosition; - int l = direction; - playerMP.func_143004_u(); - if (direction == 255) { - if (offhandWeapon == null) return; - PlayerInteractEvent event = new PlayerInteractEvent( - playerMP, - PlayerInteractEvent.Action.RIGHT_CLICK_AIR, - 0, - 0, - 0, - -1, - playerMP.getEntityWorld()); - MinecraftForge.EVENT_BUS.post(new PlayerEventChild.UseOffhandItemEvent(event, offhandWeapon)); - if (event.useItem != Event.Result.DENY) { - HookContainerClass.tryUseItem(playerMP, offhandWeapon, Side.SERVER); - } - flag = false; - } else { - MinecraftServer mcServer = FMLCommonHandler.instance() - .getMinecraftServerInstance(); - if (yPosition >= mcServer.getBuildLimit() - 1 - && (direction == 1 || yPosition >= mcServer.getBuildLimit())) { - ChatComponentTranslation chat = new ChatComponentTranslation("build.tooHigh", mcServer.getBuildLimit()); - chat.getChatStyle() - .setColor(EnumChatFormatting.RED); - playerMP.playerNetServerHandler.sendPacket(new S02PacketChat(chat)); - } else { - double dist = playerMP.theItemInWorldManager.getBlockReachDistance() + 1; - dist *= dist; - if (playerMP.getDistanceSq(i + 0.5D, j + 0.5D, k + 0.5D) < dist - && !mcServer.isBlockProtected(playerMP.getEntityWorld(), i, j, k, playerMP)) { - this.useItem(playerMP, offhandWeapon, i, j, k, l, xOffset, yOffset, zOffset); - } - } - } - if (flag) { - playerMP.playerNetServerHandler.sendPacket(new S23PacketBlockChange(i, j, k, playerMP.getEntityWorld())); - if (l == 0) { - --j; - } - if (l == 1) { - ++j; - } - if (l == 2) { - --k; - } - if (l == 3) { - ++k; - } - if (l == 4) { - --i; - } - if (l == 5) { - ++i; - } - playerMP.playerNetServerHandler.sendPacket(new S23PacketBlockChange(i, j, k, playerMP.getEntityWorld())); - } - offhandWeapon = BackhandUtils.getOffhandItem(playerMP); - if (offhandWeapon != null && HookContainerClass.isItemBlock(offhandWeapon.getItem())) { - if (offhandWeapon.stackSize <= 0) { - BackhandUtils.setPlayerOffhandItem(playerMP, null); - offhandWeapon = null; - } - if (offhandWeapon == null || offhandWeapon.getMaxItemUseDuration() == 0) { - playerMP.isChangingQuantityOnly = true; - BackhandUtils - .setPlayerOffhandItem(playerMP, ItemStack.copyItemStack(BackhandUtils.getOffhandItem(playerMP))); - playerMP.openContainer.detectAndSendChanges(); - playerMP.isChangingQuantityOnly = false; - } - } - - if (!ItemStack.areItemStacksEqual(itemStack, offhandWeapon)) { - Backhand.packetHandler.sendPacketToPlayer(new OffhandSyncItemPacket(playerMP).generatePacket(), playerMP); - } - } - - public boolean useItem(EntityPlayerMP playerMP, ItemStack itemStack, int x, int y, int z, int side, float xOffset, - float yOffset, float zOffset) { - World theWorld = playerMP.getEntityWorld(); - if (itemStack != null) { - final int meta = itemStack.getItemDamage(); - if (itemStack.getItem() - .onItemUseFirst(itemStack, playerMP, theWorld, x, y, z, side, xOffset, yOffset, zOffset)) { - if (itemStack.stackSize <= 0) { - ForgeEventFactory.onPlayerDestroyItem(playerMP, itemStack); - BackhandUtils.setPlayerOffhandItem(playerMP, null); - } else if (itemStack.getItemDamage() != meta) { - BackhandUtils.setPlayerOffhandItem(playerMP, BackhandUtils.getOffhandItem(playerMP)); - } - return true; - } - } - - boolean result = false; - if (itemStack != null) { - final int meta = itemStack.getItemDamage(); - final int size = itemStack.stackSize; - result = itemStack.tryPlaceItemIntoWorld(playerMP, theWorld, x, y, z, side, xOffset, yOffset, zOffset); - if (playerMP.theItemInWorldManager.isCreative()) { - itemStack.setItemDamage(meta); - itemStack.stackSize = size; - } - if (itemStack.stackSize <= 0) { - ForgeEventFactory.onPlayerDestroyItem(playerMP, itemStack); - BackhandUtils.setPlayerOffhandItem(playerMP, null); - } else if (itemStack.getItemDamage() != meta) { - BackhandUtils.setPlayerOffhandItem(playerMP, itemStack); - } - } - return result; - } - -} diff --git a/src/main/java/xonin/backhand/packet/OffhandSwapClientPacket.java b/src/main/java/xonin/backhand/packet/OffhandSwapClientPacket.java index 700652a..887f28a 100644 --- a/src/main/java/xonin/backhand/packet/OffhandSwapClientPacket.java +++ b/src/main/java/xonin/backhand/packet/OffhandSwapClientPacket.java @@ -6,8 +6,8 @@ import cpw.mods.fml.common.network.ByteBufUtils; import io.netty.buffer.ByteBuf; import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.api.core.InventoryPlayerBackhand; -import xonin.backhand.client.ClientTickHandler; +import xonin.backhand.api.core.IOffhandInventory; +import xonin.backhand.client.ClientEventHandler; /** * User: nerd-boy @@ -33,10 +33,10 @@ public void process(ByteBuf inputStream, EntityPlayer player) { this.player = player.worldObj.getPlayerEntityByName(user); if (this.player != null) { int slot = inputStream.readInt(); - if (InventoryPlayerBackhand.isValidSwitch(slot)) this.player.inventory.currentItem = slot; + if (IOffhandInventory.isValidSwitch(slot)) this.player.inventory.currentItem = slot; BackhandUtils.swapOffhandItem(player); } - ClientTickHandler.allowSwap = true; + ClientEventHandler.allowSwap = true; } @Override diff --git a/src/main/java/xonin/backhand/packet/OffhandToServerPacket.java b/src/main/java/xonin/backhand/packet/OffhandToServerPacket.java deleted file mode 100644 index e8314ee..0000000 --- a/src/main/java/xonin/backhand/packet/OffhandToServerPacket.java +++ /dev/null @@ -1,46 +0,0 @@ -package xonin.backhand.packet; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; - -import cpw.mods.fml.common.network.ByteBufUtils; -import io.netty.buffer.ByteBuf; -import xonin.backhand.api.core.BackhandUtils; - -public class OffhandToServerPacket extends AbstractPacket { - - public static final String packetName = "MB2|OffhandToServer"; - - private ItemStack offhandItem; - private String user; - EntityPlayer player; - - public OffhandToServerPacket(ItemStack offhandItem, EntityPlayer player) { - this.offhandItem = offhandItem; - this.player = player; - this.user = player.getCommandSenderName(); - } - - public OffhandToServerPacket() {} - - @Override - public String getChannel() { - return packetName; - } - - @Override - public void write(ByteBuf out) { - ByteBufUtils.writeUTF8String(out, player.getCommandSenderName()); - ByteBufUtils.writeItemStack(out, offhandItem); - } - - @Override - public void process(ByteBuf inputStream, EntityPlayer player) { - this.user = ByteBufUtils.readUTF8String(inputStream); - this.player = player.worldObj.getPlayerEntityByName(user); - if (this.player != null) { - ItemStack offhandItem = ByteBufUtils.readItemStack(inputStream); - BackhandUtils.setPlayerOffhandItem(this.player, offhandItem); - } - } -} diff --git a/src/main/java/xonin/backhand/utils/BackhandConfig.java b/src/main/java/xonin/backhand/utils/BackhandConfig.java index 0c3cee1..bab2239 100644 --- a/src/main/java/xonin/backhand/utils/BackhandConfig.java +++ b/src/main/java/xonin/backhand/utils/BackhandConfig.java @@ -20,28 +20,31 @@ public class BackhandConfig { @Config.DefaultBoolean(false) public static boolean OffhandBreakBlocks; - @Config.Comment("If enabled, arrows in the offhand will be used first when shooting a bow. Compatible with Et-Futurum's tipped arrows! True in vanilla.") - @Config.DefaultBoolean(true) - public static boolean UseOffhandArrows; - - @Config.Comment("If enabled, bows can be used in the offhand. True in vanilla.") - @Config.DefaultBoolean(true) - public static boolean UseOffhandBow; - - @Config.Comment("If the main offhand inventory can't be used, this slot in the main inventory will be used as the offhand instead. Slot 9 by default.") - @Config.DefaultInt(9) - public static int AlternateOffhandSlot; - - @Config.Comment("If enabled, the alternate offhand slot configured above will always be used for the offhand. False by default.") - @Config.DefaultBoolean(false) - public static boolean UseInventorySlot; - - @Config.Comment(""" - If enabled, a hotswap will be performed every tick if the main hand has no use or is empty. - This hotswap allows for many more items like fishing rods to be used in the offhand, but may be unstable. - """) - @Config.DefaultBoolean(false) - public static boolean OffhandTickHotswap; + // @Config.Comment("If enabled, arrows in the offhand will be used first when shooting a bow. Compatible with + // Et-Futurum's tipped arrows! True in vanilla.") + // @Config.DefaultBoolean(true) + // public static boolean UseOffhandArrows; + + // @Config.Comment("If enabled, bows can be used in the offhand. True in vanilla.") + // @Config.DefaultBoolean(true) + // public static boolean UseOffhandBow; + + // @Config.Comment("If the main offhand inventory can't be used, this slot in the main inventory will be used as the + // offhand instead. Slot 9 by default.") + // @Config.DefaultInt(9) + // public static int AlternateOffhandSlot; + // + // @Config.Comment("If enabled, the alternate offhand slot configured above will always be used for the offhand. + // False by default.") + // @Config.DefaultBoolean(false) + // public static boolean UseInventorySlot; + // + // @Config.Comment(""" + // If enabled, a hotswap will be performed every tick if the main hand has no use or is empty. + // This hotswap allows for many more items like fishing rods to be used in the offhand, but may be unstable. + // """) + // @Config.DefaultBoolean(false) + // public static boolean OffhandTickHotswap; @Config.Comment(""" These items will be unable to be swapped into the offhand. From 90deb2e3ee0d54a2bc0405db9b2d227c4f8154ea Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 17:24:44 +0100 Subject: [PATCH 03/38] fix tinker's crosshair not rendering for offhand crossbow --- .../java/xonin/backhand/mixins/Mixins.java | 7 +++- .../xonin/backhand/mixins/TargetedMod.java | 3 +- .../tconstruct/MixinCrosshairHandler.java | 34 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/main/java/xonin/backhand/mixins/late/tconstruct/MixinCrosshairHandler.java diff --git a/src/main/java/xonin/backhand/mixins/Mixins.java b/src/main/java/xonin/backhand/mixins/Mixins.java index 4b44af4..4076fcd 100644 --- a/src/main/java/xonin/backhand/mixins/Mixins.java +++ b/src/main/java/xonin/backhand/mixins/Mixins.java @@ -45,7 +45,12 @@ public enum Mixins { new Builder("Et Futurum Totem Support").addMixinClasses("etfuturum.MixinServerEventHandler") .setPhase(Phase.LATE) .setSide(Side.BOTH) - .addTargetedMod(TargetedMod.ET_FUTURUM)),; + .addTargetedMod(TargetedMod.ET_FUTURUM)), + TINKERS_CROSSHAIR_FIX(new Builder("Fix crosshair not rendering for offhand weapon") + .addMixinClasses("tconstruct.MixinCrosshairHandler") + .setPhase(Phase.LATE) + .setSide(Side.CLIENT) + .addTargetedMod(TargetedMod.TINKERS_CONSTRUCT)),; private final List mixinClasses; private final Supplier applyIf; diff --git a/src/main/java/xonin/backhand/mixins/TargetedMod.java b/src/main/java/xonin/backhand/mixins/TargetedMod.java index 237c521..c075c24 100644 --- a/src/main/java/xonin/backhand/mixins/TargetedMod.java +++ b/src/main/java/xonin/backhand/mixins/TargetedMod.java @@ -3,7 +3,8 @@ public enum TargetedMod { VANILLA("Minecraft", null), - ET_FUTURUM("Et Futurum Requiem", "ganymedes01.etfuturum.mixinplugin.EtFuturumEarlyMixins", "etfuturum"); + ET_FUTURUM("Et Futurum Requiem", "ganymedes01.etfuturum.mixinplugin.EtFuturumEarlyMixins", "etfuturum"), + TINKERS_CONSTRUCT("Tinkers' Construct", null, "TConstruct"); /** The "name" in the @Mod annotation */ public final String modName; diff --git a/src/main/java/xonin/backhand/mixins/late/tconstruct/MixinCrosshairHandler.java b/src/main/java/xonin/backhand/mixins/late/tconstruct/MixinCrosshairHandler.java new file mode 100644 index 0000000..a7ceb30 --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/late/tconstruct/MixinCrosshairHandler.java @@ -0,0 +1,34 @@ +package xonin.backhand.mixins.late.tconstruct; + +import net.minecraft.client.Minecraft; +import net.minecraft.item.ItemStack; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; + +import tconstruct.library.weaponry.IWindup; +import tconstruct.weaponry.client.CrosshairHandler; +import xonin.backhand.api.core.BackhandUtils; + +@Mixin(CrosshairHandler.class) +public class MixinCrosshairHandler { + + @ModifyExpressionValue( + method = "onRenderOverlay", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/entity/EntityClientPlayerMP;getCurrentEquippedItem()Lnet/minecraft/item/ItemStack;", + ordinal = 0), + remap = false) + private ItemStack backhand$onRenderOverlay(ItemStack original) { + if (original == null || !(original.getItem() instanceof IWindup)) { + ItemStack offhand = BackhandUtils.getOffhandItem(Minecraft.getMinecraft().thePlayer); + if (offhand != null && offhand.getItem() instanceof IWindup) { + return offhand; + } + } + return original; + } +} From 17b846e3e4e89b495df5b2b2eb1df9ff2d3ff960 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 17:57:26 +0100 Subject: [PATCH 04/38] fix an incorrect check --- .../backhand/mixins/early/minecraft/MixinMinecraft.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index c69401a..f8dbdb2 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -116,11 +116,7 @@ public void func_147121_ag() { objectMouseOver.hitVec)) { useMainhand = false; thePlayer.swingItem(); - } - - if (!useMainhand) { - ItemStack offhand = BackhandUtils.getOffhandItem(thePlayer); - if (offhand == null) return; + } else if (offhandItem != null) { PlayerInteractEvent event = new PlayerInteractEvent( thePlayer, PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK, @@ -135,7 +131,7 @@ public void func_147121_ag() { () -> playerController.onPlayerRightClick( thePlayer, theWorld, - offhand, + offhandItem, x, y, z, From ae080fa9491c01211cebecb610a8b1dcad7ad89d Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 17:58:11 +0100 Subject: [PATCH 05/38] add client-side check for offhand attack --- .../mixins/early/minecraft/MixinMinecraft.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index f8dbdb2..422d19a 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -196,10 +196,14 @@ public void func_147121_ag() { if (trySecondaryAction && offhandItem.getItemUseAction() == EnumAction.none) { switch (objectMouseOver.typeOfHit) { - case ENTITY -> BackhandUtils.useOffhandItem(thePlayer, () -> { - thePlayer.swingItem(); - playerController.attackEntity(thePlayer, objectMouseOver.entityHit); - }); + case ENTITY -> { + if (BackhandConfig.OffhandAttack) { + BackhandUtils.useOffhandItem(thePlayer, () -> { + thePlayer.swingItem(); + playerController.attackEntity(thePlayer, objectMouseOver.entityHit); + }); + } + } case BLOCK -> { if (BackhandConfig.OffhandBreakBlocks) { BackhandUtils.useOffhandItem(thePlayer, () -> { From f27b0344f3057ab53d03014c8214a12e342e731d Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 18:21:12 +0100 Subject: [PATCH 06/38] re-add invtweaks hack --- dependencies.gradle | 1 + src/main/java/xonin/backhand/Backhand.java | 3 --- .../backhand/client/ClientEventHandler.java | 22 ++++++------------- src/main/java/xonin/backhand/utils/Mods.java | 22 +++++++++++++++++++ 4 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 src/main/java/xonin/backhand/utils/Mods.java diff --git a/dependencies.gradle b/dependencies.gradle index 875e8c3..bb7cff3 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -35,6 +35,7 @@ */ dependencies { api("com.github.GTNewHorizons:GTNHLib:0.6.2:dev") + compileOnly("com.github.GTNewHorizons:TinkersConstruct:1.13.4-GTNH:dev") runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.7.18-GTNH:dev") compileOnly("com.github.GTNewHorizons:inventory-tweaks:1.7.0:dev") compileOnly(rfg.deobf('maven.modrinth:etfuturum:2.6.2')) diff --git a/src/main/java/xonin/backhand/Backhand.java b/src/main/java/xonin/backhand/Backhand.java index 61a5697..6f57c33 100644 --- a/src/main/java/xonin/backhand/Backhand.java +++ b/src/main/java/xonin/backhand/Backhand.java @@ -10,7 +10,6 @@ import com.gtnewhorizon.gtnhlib.config.ConfigurationManager; import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInitializationEvent; @@ -36,11 +35,9 @@ public class Backhand { @SidedProxy(clientSide = "xonin.backhand.client.ClientProxy", serverSide = "xonin.backhand.CommonProxy") public static CommonProxy proxy; public static BackhandPacketHandler packetHandler; - public static boolean isEFRLoaded; @Mod.EventHandler public void load(FMLPreInitializationEvent event) { - isEFRLoaded = Loader.isModLoaded("etfuturum"); try { ConfigurationManager.registerConfig(BackhandConfig.class); ConfigurationManager.registerConfig(BackhandConfigClient.class); diff --git a/src/main/java/xonin/backhand/client/ClientEventHandler.java b/src/main/java/xonin/backhand/client/ClientEventHandler.java index 3e63e4b..4d46d71 100644 --- a/src/main/java/xonin/backhand/client/ClientEventHandler.java +++ b/src/main/java/xonin/backhand/client/ClientEventHandler.java @@ -22,7 +22,6 @@ import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber; -import cpw.mods.fml.common.Optional; import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.InputEvent; @@ -33,6 +32,7 @@ import xonin.backhand.client.utils.BackhandRenderHelper; import xonin.backhand.packet.OffhandSwapPacket; import xonin.backhand.utils.BackhandConfig; +import xonin.backhand.utils.Mods; @EventBusSubscriber(side = Side.CLIENT) public class ClientEventHandler { @@ -65,11 +65,7 @@ public static void onKeyInputEvent(InputEvent.KeyInputEvent event) { if (ClientProxy.swapOffhand.getIsKeyPressed() && Keyboard.isKeyDown(Keyboard.getEventKey()) && allowSwap) { allowSwap = false; - // try { - // getClass() - // .getMethod("invTweaksSwapPatch"); - // invTweaksSwapPatch(); - // } catch (Exception ignored) {} + invTweaksSwapPatch(); player.sendQueue.addToSendQueue(new OffhandSwapPacket(player).generatePacket()); } } @@ -79,11 +75,7 @@ public static void onClientTick(TickEvent.ClientTickEvent event) { if (invTweaksDelay > 0) { invTweaksDelay--; if (invTweaksDelay == 0) { - // try { - // this.getClass() - // .getMethod("restoreInvTweaksConfigs"); - // restoreInvTweaksConfigs(); - // } catch (Exception ignored) {} + restoreInvTweaksConfigs(); } } } @@ -199,8 +191,8 @@ public static void render3rdPersonOffhand(RenderPlayerEvent.Specials.Post event) GL11.glPopMatrix(); } - @Optional.Method(modid = "inventorytweaks") - public void restoreInvTweaksConfigs() { + public static void restoreInvTweaksConfigs() { + if (!Mods.INV_TWEAKS.isLoaded()) return; InvTweaks.getConfigManager() .getConfig() .setProperty("enableAutoRefill", String.valueOf(prevInvTweaksAutoRefill)); @@ -209,8 +201,8 @@ public void restoreInvTweaksConfigs() { .setProperty("autoRefillBeforeBreak", String.valueOf(prevInvTweaksBreakRefill)); } - @Optional.Method(modid = "inventorytweaks") - public void invTweaksSwapPatch() { + public static void invTweaksSwapPatch() { + if (!Mods.INV_TWEAKS.isLoaded()) return; if (invTweaksDelay <= 0) { prevInvTweaksAutoRefill = Boolean.parseBoolean( InvTweaks.getConfigManager() diff --git a/src/main/java/xonin/backhand/utils/Mods.java b/src/main/java/xonin/backhand/utils/Mods.java new file mode 100644 index 0000000..8c40512 --- /dev/null +++ b/src/main/java/xonin/backhand/utils/Mods.java @@ -0,0 +1,22 @@ +package xonin.backhand.utils; + +import cpw.mods.fml.common.Loader; + +public enum Mods { + + INV_TWEAKS("inventorytweaks"),; + + private final String modId; + private Boolean loaded; + + Mods(String modId) { + this.modId = modId; + } + + public boolean isLoaded() { + if (loaded == null) { + loaded = Loader.isModLoaded(modId); + } + return loaded; + } +} From 31898f1b576a93b3631e808f5af767dd75a97195 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 18:25:34 +0100 Subject: [PATCH 07/38] move inv slot next to player model --- .../backhand/mixins/early/minecraft/MixinContainerPlayer.java | 2 +- .../backhand/mixins/early/minecraft/MixinGuiInventory.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java index bd7525d..2e36ab1 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java @@ -19,6 +19,6 @@ public abstract class MixinContainerPlayer extends Container { @Inject(method = "", at = @At(value = "TAIL")) private void backhand2$addOffhandSlot(InventoryPlayer p_i1819_1_, boolean p_i1819_2_, EntityPlayer p_i1819_3_, CallbackInfo ci) { - addSlotToContainer(new Slot(p_i1819_1_, IOffhandInventory.OFFHAND_HOTBAR_SLOT, 152, 65)); + addSlotToContainer(new Slot(p_i1819_1_, IOffhandInventory.OFFHAND_HOTBAR_SLOT, 80, 65)); } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiInventory.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiInventory.java index 6865e1e..b5e2422 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiInventory.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiInventory.java @@ -23,7 +23,7 @@ public MixinGuiInventory(Container p_i1089_1_) { @Inject(method = "drawGuiContainerBackgroundLayer", at = @At("TAIL")) protected void backhand$drawOffhandSlot(float partialTicks, int mouseX, int mouseY, CallbackInfo ci) { - backhand$drawItemStackSlot(guiLeft + 150, guiTop + 63); + backhand$drawItemStackSlot(guiLeft + 78, guiTop + 63); } @Unique From 3542fbe2d3dcc803e252c0f0c00b2c52024d3620 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 18:42:06 +0100 Subject: [PATCH 08/38] get the config sync going again --- src/main/java/xonin/backhand/Backhand.java | 6 -- src/main/java/xonin/backhand/CommonProxy.java | 65 ++++++------------- .../backhand/api/core/BackhandUtils.java | 6 -- .../java/xonin/backhand/mixins/Mixins.java | 2 - .../minecraft/MixinEntityPlayerClient.java | 12 ---- .../packet/OffhandConfigSyncPacket.java | 15 ----- .../xonin/backhand/utils/BackhandConfig.java | 26 -------- 7 files changed, 21 insertions(+), 111 deletions(-) diff --git a/src/main/java/xonin/backhand/Backhand.java b/src/main/java/xonin/backhand/Backhand.java index 6f57c33..2493f40 100644 --- a/src/main/java/xonin/backhand/Backhand.java +++ b/src/main/java/xonin/backhand/Backhand.java @@ -14,7 +14,6 @@ import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; -import cpw.mods.fml.common.event.FMLServerStoppingEvent; import xonin.backhand.packet.BackhandPacketHandler; import xonin.backhand.utils.BackhandConfig; import xonin.backhand.utils.BackhandConfigClient; @@ -54,11 +53,6 @@ public void init(FMLInitializationEvent event) { packetHandler.register(); } - @Mod.EventHandler - public void onServerStopping(FMLServerStoppingEvent event) { - proxy.onServerStopping(event); - } - public static MinecraftServer getServer() { return FMLCommonHandler.instance() .getMinecraftServerInstance(); diff --git a/src/main/java/xonin/backhand/CommonProxy.java b/src/main/java/xonin/backhand/CommonProxy.java index fe0e98d..fa9167d 100644 --- a/src/main/java/xonin/backhand/CommonProxy.java +++ b/src/main/java/xonin/backhand/CommonProxy.java @@ -1,57 +1,34 @@ package xonin.backhand; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; -import cpw.mods.fml.common.event.FMLServerStoppingEvent; +import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber; -public class CommonProxy { +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent; +import xonin.backhand.api.core.BackhandUtils; +import xonin.backhand.packet.OffhandConfigSyncPacket; - public static ItemStack offhandItemUsed; +@EventBusSubscriber +public class CommonProxy { public void load() { } - public void onServerStopping(FMLServerStoppingEvent event) { - // for (EntityPlayer player : Backhand.getServer() - // .getConfigurationManager().playerEntityList) { - // if (BackhandUtils.getOffhandItem(player) != null) { - // BackhandUtils.resetAndDelayHotswap(player, 0); - // } - // } + @SubscribeEvent + public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) { + if (!(event.player instanceof EntityPlayerMP playerMP)) { + return; + } + Backhand.packetHandler.sendPacketToPlayer(new OffhandConfigSyncPacket().generatePacket(), playerMP); + ItemStack offhandItem = BackhandUtils.getOffhandItem(playerMP); + if (Backhand.isOffhandBlacklisted(offhandItem)) { + BackhandUtils.setPlayerOffhandItem(playerMP, null); + if (!playerMP.inventory.addItemStackToInventory(offhandItem)) { + event.player.entityDropItem(offhandItem, 0); + } + } } - - // public EntityPlayer getClientPlayer() { - // return null; - // } - - // public void sendAnimationPacket(EnumAnimations animation, EntityPlayer entityPlayer) {} - // - // // Should not be called on the server anyway - // public boolean isRightClickHeld() { - // return false; - // } - // - // public int getRightClickCounter() { - // return 0; - // } - // - // public void setRightClickCounter(int i) {} - // - // public int getRightClickDelay() { - // return 0; - // } - // - // // Should not be called on the server anyway - // public boolean isLeftClickHeld() { - // return false; - // } - // - // // Should not be called on the server anyway - // public int getLeftClickCounter() { - // return 0; - // } - // - // // Should not be called on the server anyway - // public void setLeftClickCounter(int i) {} } diff --git a/src/main/java/xonin/backhand/api/core/BackhandUtils.java b/src/main/java/xonin/backhand/api/core/BackhandUtils.java index 0faf443..e811789 100644 --- a/src/main/java/xonin/backhand/api/core/BackhandUtils.java +++ b/src/main/java/xonin/backhand/api/core/BackhandUtils.java @@ -107,12 +107,6 @@ public static boolean isCommonlyUsable(Item item) { || item instanceof ItemEgg || item instanceof ItemMonsterPlacer; } - // item instanceof ItemCrossbow || item instanceof ItemMounter || item instanceof ItemTeleporter || item instanceof - // ItemElementalStaff || item instanceof ItemGun || item instanceof ItemNpcMovingPath || item instanceof - // ItemMachineGun || item instanceof ItemMusket || item instanceof ItemNpcCloner || item instanceof ItemNpcScripter - // || item instanceof SpellBase || item instanceof ItemNpcWand || item instanceof ItemShield || item instanceof - // ItemSlingshot || item instanceof ItemSoulstoneFilled || item instanceof ItemSoulstoneEmpty || item instanceof - // ItemStaff || item instanceof ItemThrowingWeapon || item instanceof ItemThrowingShuriken /** * Defines a bow diff --git a/src/main/java/xonin/backhand/mixins/Mixins.java b/src/main/java/xonin/backhand/mixins/Mixins.java index 4076fcd..e3e92a4 100644 --- a/src/main/java/xonin/backhand/mixins/Mixins.java +++ b/src/main/java/xonin/backhand/mixins/Mixins.java @@ -33,8 +33,6 @@ public enum Mixins { "minecraft.MixinItemRenderer", "minecraft.MixinModelBiped", "minecraft.MixinNetHandlerPlayClient", - // "minecraft.MixinPlayerControllerMP", - // "minecraft.MixinItemBow", "minecraft.MixinItemStackClient", "minecraft.MixinMinecraft", "minecraft.MixinGuiInventory") diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java index fba048a..27627bd 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java @@ -16,18 +16,6 @@ private MixinEntityPlayerClient(World p_i1594_1_) { super(p_i1594_1_); } - // @Inject(method = "getItemIcon", at = @At(value = "HEAD")) - // private void backhand2$getItemIcon(ItemStack itemStackIn, int p_70620_2_, CallbackInfoReturnable cir) { - // EntityPlayer player = Minecraft.getMinecraft().thePlayer; - // if (itemStackIn == player.getCurrentEquippedItem() && player.getCurrentEquippedItem() != null - // && player.getItemInUse() != null - // && player.getCurrentEquippedItem() - // .getItem() instanceof ItemBow - // && player.getCurrentEquippedItem() != player.getItemInUse()) { - // BackhandClientUtils.disableMainhandAnimation = true; - // } - // } - @Override public float getSwingProgress(float partialTicks) { if (BackhandRenderHelper.offhandFPRender) { diff --git a/src/main/java/xonin/backhand/packet/OffhandConfigSyncPacket.java b/src/main/java/xonin/backhand/packet/OffhandConfigSyncPacket.java index 63ccbdb..1250896 100644 --- a/src/main/java/xonin/backhand/packet/OffhandConfigSyncPacket.java +++ b/src/main/java/xonin/backhand/packet/OffhandConfigSyncPacket.java @@ -9,11 +9,6 @@ public final class OffhandConfigSyncPacket extends AbstractPacket { public static final String packetName = "MB2|ConfigSync"; - private EntityPlayer player; - - public OffhandConfigSyncPacket(EntityPlayer player) { - this.player = player; - } public OffhandConfigSyncPacket() {} @@ -22,11 +17,6 @@ public void process(ByteBuf inputStream, EntityPlayer player) { BackhandConfig.OffhandAttack = inputStream.readBoolean(); BackhandConfig.EmptyOffhand = inputStream.readBoolean(); BackhandConfig.OffhandBreakBlocks = inputStream.readBoolean(); - // BackhandConfig.UseOffhandArrows = inputStream.readBoolean(); - // BackhandConfig.UseOffhandBow = inputStream.readBoolean(); - // BackhandConfig.OffhandTickHotswap = inputStream.readBoolean(); - // BackhandConfig.AlternateOffhandSlot = inputStream.readInt(); - // BackhandConfig.UseInventorySlot = inputStream.readBoolean(); } @Override @@ -39,10 +29,5 @@ public void write(ByteBuf out) { out.writeBoolean(BackhandConfig.OffhandAttack); out.writeBoolean(BackhandConfig.EmptyOffhand); out.writeBoolean(BackhandConfig.OffhandBreakBlocks); - // out.writeBoolean(BackhandConfig.UseOffhandArrows); - // out.writeBoolean(BackhandConfig.UseOffhandBow); - // out.writeBoolean(BackhandConfig.OffhandTickHotswap); - // out.writeInt(BackhandConfig.AlternateOffhandSlot); - // out.writeBoolean(BackhandConfig.UseInventorySlot); } } diff --git a/src/main/java/xonin/backhand/utils/BackhandConfig.java b/src/main/java/xonin/backhand/utils/BackhandConfig.java index bab2239..d503d98 100644 --- a/src/main/java/xonin/backhand/utils/BackhandConfig.java +++ b/src/main/java/xonin/backhand/utils/BackhandConfig.java @@ -20,32 +20,6 @@ public class BackhandConfig { @Config.DefaultBoolean(false) public static boolean OffhandBreakBlocks; - // @Config.Comment("If enabled, arrows in the offhand will be used first when shooting a bow. Compatible with - // Et-Futurum's tipped arrows! True in vanilla.") - // @Config.DefaultBoolean(true) - // public static boolean UseOffhandArrows; - - // @Config.Comment("If enabled, bows can be used in the offhand. True in vanilla.") - // @Config.DefaultBoolean(true) - // public static boolean UseOffhandBow; - - // @Config.Comment("If the main offhand inventory can't be used, this slot in the main inventory will be used as the - // offhand instead. Slot 9 by default.") - // @Config.DefaultInt(9) - // public static int AlternateOffhandSlot; - // - // @Config.Comment("If enabled, the alternate offhand slot configured above will always be used for the offhand. - // False by default.") - // @Config.DefaultBoolean(false) - // public static boolean UseInventorySlot; - // - // @Config.Comment(""" - // If enabled, a hotswap will be performed every tick if the main hand has no use or is empty. - // This hotswap allows for many more items like fishing rods to be used in the offhand, but may be unstable. - // """) - // @Config.DefaultBoolean(false) - // public static boolean OffhandTickHotswap; - @Config.Comment(""" These items will be unable to be swapped into the offhand. Formatting of an item should be: modid:itemname From 0a50db07e168f659bd91b3796132507d8e078d27 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 18:55:03 +0100 Subject: [PATCH 09/38] remove a ton of unused AT's --- .../client/renderer/ItemRendererOffhand.java | 2 +- src/main/resources/META-INF/backhand_at.cfg | 51 ++----------------- 2 files changed, 4 insertions(+), 49 deletions(-) diff --git a/src/main/java/xonin/backhand/client/renderer/ItemRendererOffhand.java b/src/main/java/xonin/backhand/client/renderer/ItemRendererOffhand.java index 18a00d9..15eac83 100644 --- a/src/main/java/xonin/backhand/client/renderer/ItemRendererOffhand.java +++ b/src/main/java/xonin/backhand/client/renderer/ItemRendererOffhand.java @@ -139,7 +139,7 @@ public void renderOffhandItemIn3rdPerson(EntityPlayer player, ModelBiped modelBi public void updateEquippedItem() { this.prevEquippedProgress = this.equippedProgress; - EntityClientPlayerMP player = this.mc.thePlayer; + EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer; ItemStack itemstack = BackhandUtils.getOffhandItem(player); boolean flag = itemstack == this.itemToRender; diff --git a/src/main/resources/META-INF/backhand_at.cfg b/src/main/resources/META-INF/backhand_at.cfg index 1ac515b..3bfd01e 100644 --- a/src/main/resources/META-INF/backhand_at.cfg +++ b/src/main/resources/META-INF/backhand_at.cfg @@ -1,52 +1,7 @@ -public net.minecraft.client.Minecraft.func_152340_a(Ljava/io/InputStream;)Ljava/nio/ByteBuffer -public net.minecraft.client.renderer.entity.RenderPlayer func_77029_c(Lnet/minecraft/client/entity/AbstractClientPlayer;F)V -#EntityArrow -public net.minecraft.entity.projectile.EntityArrow field_145791_d #xTile -public net.minecraft.entity.projectile.EntityArrow field_145792_e #yTile -public net.minecraft.entity.projectile.EntityArrow field_145789_f #zTile -public net.minecraft.client.renderer.ItemRenderer * #therearewaytoomanyfieldstomanuallyATthemall -public net.minecraft.client.gui.GuiIngame field_73841_b #itemRenderer -public net.minecraft.entity.projectile.EntityArrow field_70252_j #ticksInGround -#EntityLivingBase public net.minecraft.entity.EntityLivingBase func_82166_i()I #getArmSwingAnimationEnd -public net.minecraft.client.Minecraft func_147121_ag()V #rightClickMouse -#NetHandlerPlayServer -public net.minecraft.network.NetHandlerPlayServer field_147380_r #hasMoved -public net.minecraft.network.NetHandlerPlayServer field_147367_d #serverController -public net.minecraft.network.NetHandlerPlayServer field_147375_m #itemDropThreshold -#Client side -#GuiContainerCreative -public net.minecraft.client.gui.inventory.GuiContainerCreative func_147053_i()V # -public net.minecraft.client.gui.inventory.GuiContainerCreative func_147050_b(Lnet/minecraft/creativetab/CreativeTabs;)V # -public net.minecraft.client.gui.inventory.GuiContainerCreative field_147058_w # selectedTabIndex -public net.minecraft.client.gui.inventory.GuiContainerCreative * # -public net.minecraft.client.gui.inventory.GuiContainer field_147008_s # -#RendererLivingEntity -public net.minecraft.client.renderer.entity.RendererLivingEntity field_77045_g #mainModel -#PlayerControllerMP public net.minecraft.client.multiplayer.PlayerControllerMP func_78750_j()V #syncCurrentPlayItem -public net.minecraft.client.multiplayer.PlayerControllerMP * #therearewaytoomanyfieldstomanuallyATthemall -#Minecraft -public net.minecraft.client.Minecraft field_71467_ac #rightClickDelayTimer -public net.minecraft.client.Minecraft field_71429_W #leftClickCounter -public net.minecraft.client.Minecraft func_147112_ai()V #middle click -#NetHandlerPlayClient -public net.minecraft.client.network.NetHandlerPlayClient field_147299_f # gameController -#Item -public net.minecraft.item.Item field_77791_bV # itemIcon -#Render -public net.minecraft.client.renderer.entity.Render field_76990_c #renderManager -# SpecialSource needs to update to handle these. -public net.minecraft.client.gui.inventory.GuiContainerCreative$ContainerCreative -public net.minecraft.client.gui.inventory.GuiContainerCreative$CreativeSlot -public net.minecraft.client.gui.inventory.GuiContainerCreative$CreativeSlot field_148332_b # theSlot -public net.minecraft.client.settings.KeyBinding field_74512_d # keyCode public net.minecraft.client.gui.inventory.GuiContainer field_147003_i # guiLeft (1.6 field_74198_m) public net.minecraft.client.gui.inventory.GuiContainer field_147009_r # guiTop (1.6 field_74197_n) -public net.minecraft.client.gui.inventory.GuiContainer field_146999_f # xSize (1.6 field_74194_b) -public net.minecraft.client.gui.inventory.GuiContainer field_147000_g # ySize (1.6 field_74195_c) -public net.minecraft.client.gui.GuiScreen field_146292_n # buttonList (1.6 field_73887_h) -# S2FPacketSetSlot -public net.minecraft.network.play.server.S2FPacketSetSlot field_149179_a # field_149179_a / windowId -public net.minecraft.network.play.server.S2FPacketSetSlot field_149177_b # field_149177_b / slot -public net.minecraft.network.play.server.S2FPacketSetSlot field_149178_c # field_149178_c / itemStack +public net.minecraft.client.renderer.ItemRenderer field_78453_b # itemToRender +public net.minecraft.client.renderer.ItemRenderer field_78454_c # equippedProgress +public net.minecraft.client.renderer.ItemRenderer field_78451_d # prevEquippedProgress From b5ebef30166e4bfabfdb26ea77d359ee47534e55 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Wed, 15 Jan 2025 19:20:10 +0100 Subject: [PATCH 10/38] fix server crash --- .../xonin/backhand/mixins/early/minecraft/MixinMinecraft.java | 4 ++-- .../java/xonin/backhand/packet/BackhandPacketHandler.java | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index 422d19a..b920207 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -46,7 +46,7 @@ public abstract class MixinMinecraft { public PlayerControllerMP playerController; @Shadow - public int rightClickDelayTimer; + private int rightClickDelayTimer; @Shadow @Final @@ -65,7 +65,7 @@ public abstract class MixinMinecraft { * @reason Offhand support */ @Overwrite - public void func_147121_ag() { + private void func_147121_ag() { rightClickDelayTimer = 4; ItemStack mainHandItem = thePlayer.inventory.getCurrentItem(); ItemStack offhandItem = BackhandUtils.getOffhandItem(thePlayer); diff --git a/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java b/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java index 3610d3c..cb5a2e0 100644 --- a/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java +++ b/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java @@ -15,6 +15,7 @@ import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.internal.FMLProxyPacket; import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; public final class BackhandPacketHandler { @@ -45,6 +46,7 @@ public void onServerPacket(FMLNetworkEvent.ServerCustomPacketEvent event) { } @SubscribeEvent + @SideOnly(Side.CLIENT) public void onClientPacket(FMLNetworkEvent.ClientCustomPacketEvent event) { map.get(event.packet.channel()) .process(event.packet.payload(), Minecraft.getMinecraft().thePlayer); From 369f44617a2b6c89b9be3104120f9600c7d299dd Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Thu, 16 Jan 2025 20:23:32 +0100 Subject: [PATCH 11/38] cleanup rendering a bit --- .../backhand/api/core/BackhandUtils.java | 37 +--- .../backhand/client/ClientEventHandler.java | 4 +- .../client/renderer/ItemRendererOffhand.java | 172 ------------------ .../client/utils/BackhandRenderHelper.java | 142 ++++++++++++--- .../java/xonin/backhand/mixins/Mixins.java | 2 - .../minecraft/MixinEntityPlayerClient.java | 26 --- .../early/minecraft/MixinItemRenderer.java | 40 +++- .../early/minecraft/MixinItemStackClient.java | 62 ------- 8 files changed, 159 insertions(+), 326 deletions(-) delete mode 100644 src/main/java/xonin/backhand/client/renderer/ItemRendererOffhand.java delete mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java delete mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStackClient.java diff --git a/src/main/java/xonin/backhand/api/core/BackhandUtils.java b/src/main/java/xonin/backhand/api/core/BackhandUtils.java index e811789..6478066 100644 --- a/src/main/java/xonin/backhand/api/core/BackhandUtils.java +++ b/src/main/java/xonin/backhand/api/core/BackhandUtils.java @@ -160,46 +160,27 @@ public static boolean checkForRightClickFunction(ItemStack stack) { } } - @SuppressWarnings("unchecked") - public static boolean checkForRightClickFunctionNoAction(ItemStack stack) { - if (stack == null) { - return false; - } - try { - Class c = stack.getItem() - .getClass(); - while (!(c.equals(Item.class) || c.equals(ItemTool.class) || c.equals(ItemSword.class))) { - try { - for (int i = 0; i < itemBlackListMethodNames.length; i++) { - try { - c.getDeclaredMethod(itemBlackListMethodNames[i], itemBlackListMethodParams[i]); - return true; - } catch (NoSuchMethodException ignored) {} - } - } catch (NoClassDefFoundError ignored) {} - - c = c.getSuperclass(); - } - - return false; - } catch (NullPointerException e) { - return true; - } + public static void useOffhandItem(EntityPlayer player, Runnable action) { + useOffhandItem(player, true, action); } - public static void useOffhandItem(EntityPlayer player, Runnable action) { - useOffhandItem(player, () -> { + public static void useOffhandItem(EntityPlayer player, boolean syncSlot, Runnable action) { + useOffhandItem(player, syncSlot, () -> { action.run(); return true; }); } public static boolean useOffhandItem(EntityPlayer player, BooleanSupplier action) { + return useOffhandItem(player, true, action); + } + + public static boolean useOffhandItem(EntityPlayer player, boolean syncSlot, BooleanSupplier action) { int oldSlot = player.inventory.currentItem; player.inventory.currentItem = IOffhandInventory.OFFHAND_HOTBAR_SLOT; boolean result = action.getAsBoolean(); player.inventory.currentItem = oldSlot; - if (player.worldObj.isRemote) { + if (syncSlot && player.worldObj.isRemote) { Minecraft.getMinecraft().playerController.syncCurrentPlayItem(); } return result; diff --git a/src/main/java/xonin/backhand/client/ClientEventHandler.java b/src/main/java/xonin/backhand/client/ClientEventHandler.java index 4d46d71..ae2e61e 100644 --- a/src/main/java/xonin/backhand/client/ClientEventHandler.java +++ b/src/main/java/xonin/backhand/client/ClientEventHandler.java @@ -185,9 +185,7 @@ public static void render3rdPersonOffhand(RenderPlayerEvent.Specials.Post event) GL11.glPushMatrix(); ModelBiped biped = event.renderer.modelBipedMain; - BackhandRenderHelper.itemRenderer.updateEquippedItem(); - BackhandRenderHelper.itemRenderer - .renderOffhandItemIn3rdPerson(event.entityPlayer, biped, event.partialRenderTick); + BackhandRenderHelper.renderOffhandItemIn3rdPerson(event.entityPlayer, biped, event.partialRenderTick); GL11.glPopMatrix(); } diff --git a/src/main/java/xonin/backhand/client/renderer/ItemRendererOffhand.java b/src/main/java/xonin/backhand/client/renderer/ItemRendererOffhand.java deleted file mode 100644 index 15eac83..0000000 --- a/src/main/java/xonin/backhand/client/renderer/ItemRendererOffhand.java +++ /dev/null @@ -1,172 +0,0 @@ -package xonin.backhand.client.renderer; - -import net.minecraft.block.Block; -import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.EntityClientPlayerMP; -import net.minecraft.client.model.ModelBiped; -import net.minecraft.client.renderer.ItemRenderer; -import net.minecraft.client.renderer.RenderBlocks; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; -import net.minecraft.item.EnumAction; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemStack; -import net.minecraftforge.client.IItemRenderer; -import net.minecraftforge.client.MinecraftForgeClient; - -import org.lwjgl.opengl.GL11; - -import xonin.backhand.api.core.BackhandUtils; - -public class ItemRendererOffhand extends ItemRenderer { - - public ItemRendererOffhand(Minecraft mc) { - super(mc); - } - - public void renderOffhandItemIn3rdPerson(EntityPlayer player, ModelBiped modelBipedMain, float frame) { - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - float f2; - float f4; - - if (offhandItem != null) { - GL11.glPushMatrix(); - modelBipedMain.bipedLeftArm.postRender(0.0625F); - GL11.glTranslatef( - -modelBipedMain.bipedLeftArm.rotationPointX * 0.0625F, - -modelBipedMain.bipedLeftArm.rotationPointY * 0.0625F, - -modelBipedMain.bipedLeftArm.rotationPointZ * 0.0625F); - GL11.glScalef(-1, 1, 1); - GL11.glTranslatef( - -modelBipedMain.bipedLeftArm.rotationPointX * 0.0625F, - modelBipedMain.bipedLeftArm.rotationPointY * 0.0625F, - -modelBipedMain.bipedLeftArm.rotationPointZ * 0.0625F); - - GL11.glTranslatef(-0.0625F, 0.4375F, 0.0625F); - - if (player.fishEntity != null) { - offhandItem = new ItemStack(Items.stick); - } - - EnumAction enumaction = null; - - if (player.getItemInUseCount() > 0) { - enumaction = offhandItem.getItemUseAction(); - } - - IItemRenderer customRenderer = MinecraftForgeClient - .getItemRenderer(offhandItem, IItemRenderer.ItemRenderType.EQUIPPED); - boolean is3D = (customRenderer != null && customRenderer.shouldUseRenderHelper( - IItemRenderer.ItemRenderType.EQUIPPED, - offhandItem, - IItemRenderer.ItemRendererHelper.BLOCK_3D)); - - if (is3D || offhandItem.getItem() instanceof ItemBlock && RenderBlocks.renderItemIn3d( - Block.getBlockFromItem(offhandItem.getItem()) - .getRenderType())) { - f2 = 0.5F; - GL11.glTranslatef(0.0F, 0.1875F, -0.3125F); - f2 *= 0.75F; - GL11.glRotatef(20.0F, 1.0F, 0.0F, 0.0F); - GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F); - GL11.glScalef(-f2, -f2, f2); - } else if (offhandItem.getItem() == Items.bow) { - f2 = 0.625F; - GL11.glTranslatef(0.0F, 0.125F, 0.3125F); - GL11.glRotatef(-20.0F, 0.0F, 1.0F, 0.0F); - GL11.glScalef(f2, -f2, f2); - GL11.glRotatef(-100.0F, 1.0F, 0.0F, 0.0F); - GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F); - } else if (offhandItem.getItem() - .isFull3D()) { - f2 = 0.625F; - - if (offhandItem.getItem() - .shouldRotateAroundWhenRendering()) { - GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F); - GL11.glTranslatef(0.0F, -0.125F, 0.0F); - } - - if (player.getItemInUseCount() > 0 && enumaction == EnumAction.block) { - GL11.glTranslatef(0.05F, 0.0F, -0.1F); - GL11.glRotatef(-50.0F, 0.0F, 1.0F, 0.0F); - GL11.glRotatef(-10.0F, 1.0F, 0.0F, 0.0F); - GL11.glRotatef(-60.0F, 0.0F, 0.0F, 1.0F); - } - - GL11.glTranslatef(0.0F, 0.1875F, 0.0F); - GL11.glScalef(f2, -f2, f2); - GL11.glRotatef(-100.0F, 1.0F, 0.0F, 0.0F); - GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F); - } else { - f2 = 0.375F; - GL11.glTranslatef(0.25F, 0.1875F, -0.1875F); - GL11.glScalef(f2, f2, f2); - GL11.glRotatef(60.0F, 0.0F, 0.0F, 1.0F); - GL11.glRotatef(-90.0F, 1.0F, 0.0F, 0.0F); - GL11.glRotatef(20.0F, 0.0F, 0.0F, 1.0F); - } - - float f3; - int k; - float f12; - - if (offhandItem.getItem() - .requiresMultipleRenderPasses()) { - for (k = 0; k < offhandItem.getItem() - .getRenderPasses(offhandItem.getItemDamage()); ++k) { - int i = offhandItem.getItem() - .getColorFromItemStack(offhandItem, k); - f12 = (float) (i >> 16 & 255) / 255.0F; - f3 = (float) (i >> 8 & 255) / 255.0F; - f4 = (float) (i & 255) / 255.0F; - GL11.glColor4f(f12, f3, f4, 1.0F); - this.renderItem(player, offhandItem, k); - } - } else { - k = offhandItem.getItem() - .getColorFromItemStack(offhandItem, 0); - float f11 = (float) (k >> 16 & 255) / 255.0F; - f12 = (float) (k >> 8 & 255) / 255.0F; - f3 = (float) (k & 255) / 255.0F; - GL11.glColor4f(f11, f12, f3, 1.0F); - this.renderItem(player, offhandItem, 0); - } - - GL11.glPopMatrix(); - } - } - - public void updateEquippedItem() { - this.prevEquippedProgress = this.equippedProgress; - EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer; - ItemStack itemstack = BackhandUtils.getOffhandItem(player); - boolean flag = itemstack == this.itemToRender; - - if (itemstack != null && this.itemToRender != null - && itemstack != this.itemToRender - && itemstack.getItem() == this.itemToRender.getItem() - && itemstack.getItemDamage() == this.itemToRender.getItemDamage()) { - this.itemToRender = itemstack; - flag = true; - } - - float f = 0.4F; - float f1 = flag ? 1.0F : 0.0F; - float f2 = f1 - this.equippedProgress; - - if (f2 < -f) { - f2 = -f; - } - - if (f2 > f) { - f2 = f; - } - - this.equippedProgress += f2 / 2.0F; - - if (this.equippedProgress < 0.1F) { - this.itemToRender = itemstack; - } - } -} diff --git a/src/main/java/xonin/backhand/client/utils/BackhandRenderHelper.java b/src/main/java/xonin/backhand/client/utils/BackhandRenderHelper.java index 48c005a..7d1bf15 100644 --- a/src/main/java/xonin/backhand/client/utils/BackhandRenderHelper.java +++ b/src/main/java/xonin/backhand/client/utils/BackhandRenderHelper.java @@ -1,23 +1,29 @@ package xonin.backhand.client.utils; +import net.minecraft.block.Block; import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.model.ModelBiped; import net.minecraft.client.renderer.ItemRenderer; +import net.minecraft.client.renderer.RenderBlocks; import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Items; +import net.minecraft.item.EnumAction; +import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.MathHelper; +import net.minecraftforge.client.IItemRenderer; +import net.minecraftforge.client.MinecraftForgeClient; import org.lwjgl.opengl.GL11; +import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.client.renderer.ItemRendererOffhand; public final class BackhandRenderHelper { - public static final ItemRendererOffhand itemRenderer = new ItemRendererOffhand(Minecraft.getMinecraft()); + public static final ItemRenderer itemRenderer = new ItemRenderer(Minecraft.getMinecraft()); public static float firstPersonFrame; - public static boolean offhandFPRender; @SuppressWarnings("SuspiciousNameCombination") public static void moveOffHandArm(Entity entity, ModelBiped biped, float frame) { @@ -47,32 +53,116 @@ public static void moveOffHandArm(Entity entity, ModelBiped biped, float frame) } } - public static void renderOffhandItem(ItemRenderer otherItemRenderer, float frame) { - Minecraft mc = Minecraft.getMinecraft(); - EntityClientPlayerMP player = mc.thePlayer; - - GL11.glPushMatrix(); - GL11.glScalef(-1, 1, 1); - - ItemStack itemToRender = otherItemRenderer.itemToRender; - float equippedProgress = otherItemRenderer.equippedProgress; - float prevEquippedProgress = otherItemRenderer.prevEquippedProgress; + public static void renderOffhandItemIn3rdPerson(EntityPlayer player, ModelBiped modelBipedMain, float frame) { + ItemStack offhandItem = BackhandUtils.getOffhandItem(player); + float f2; + float f4; + + if (offhandItem != null) { + GL11.glPushMatrix(); + modelBipedMain.bipedLeftArm.postRender(0.0625F); + GL11.glTranslatef( + -modelBipedMain.bipedLeftArm.rotationPointX * 0.0625F, + -modelBipedMain.bipedLeftArm.rotationPointY * 0.0625F, + -modelBipedMain.bipedLeftArm.rotationPointZ * 0.0625F); + GL11.glScalef(-1, 1, 1); + GL11.glTranslatef( + -modelBipedMain.bipedLeftArm.rotationPointX * 0.0625F, + modelBipedMain.bipedLeftArm.rotationPointY * 0.0625F, + -modelBipedMain.bipedLeftArm.rotationPointZ * 0.0625F); + + GL11.glTranslatef(-0.0625F, 0.4375F, 0.0625F); + + if (player.fishEntity != null) { + offhandItem = new ItemStack(Items.stick); + } - otherItemRenderer.itemToRender = itemRenderer.itemToRender; - otherItemRenderer.equippedProgress = itemRenderer.equippedProgress; - otherItemRenderer.prevEquippedProgress = itemRenderer.prevEquippedProgress; + EnumAction enumaction = null; - float f3 = player.prevRenderArmPitch + (player.renderArmPitch - player.prevRenderArmPitch) * frame; - float f4 = player.prevRenderArmYaw + (player.renderArmYaw - player.prevRenderArmYaw) * frame; - GL11.glRotatef((player.rotationPitch - f3) * -0.1F, 1.0F, 0.0F, 0.0F); - GL11.glRotatef((player.rotationYaw - f4) * -0.1F, 0.0F, 1.0F, 0.0F); + if (player.getItemInUseCount() > 0) { + enumaction = offhandItem.getItemUseAction(); + } - otherItemRenderer.renderItemInFirstPerson(frame); + IItemRenderer customRenderer = MinecraftForgeClient + .getItemRenderer(offhandItem, IItemRenderer.ItemRenderType.EQUIPPED); + boolean is3D = (customRenderer != null && customRenderer.shouldUseRenderHelper( + IItemRenderer.ItemRenderType.EQUIPPED, + offhandItem, + IItemRenderer.ItemRendererHelper.BLOCK_3D)); + + if (is3D || offhandItem.getItem() instanceof ItemBlock && RenderBlocks.renderItemIn3d( + Block.getBlockFromItem(offhandItem.getItem()) + .getRenderType())) { + f2 = 0.5F; + GL11.glTranslatef(0.0F, 0.1875F, -0.3125F); + f2 *= 0.75F; + GL11.glRotatef(20.0F, 1.0F, 0.0F, 0.0F); + GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F); + GL11.glScalef(-f2, -f2, f2); + } else if (offhandItem.getItem() == Items.bow) { + f2 = 0.625F; + GL11.glTranslatef(0.0F, 0.125F, 0.3125F); + GL11.glRotatef(-20.0F, 0.0F, 1.0F, 0.0F); + GL11.glScalef(f2, -f2, f2); + GL11.glRotatef(-100.0F, 1.0F, 0.0F, 0.0F); + GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F); + } else if (offhandItem.getItem() + .isFull3D()) { + f2 = 0.625F; + + if (offhandItem.getItem() + .shouldRotateAroundWhenRendering()) { + GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F); + GL11.glTranslatef(0.0F, -0.125F, 0.0F); + } + + if (player.getItemInUseCount() > 0 && enumaction == EnumAction.block) { + GL11.glTranslatef(0.05F, 0.0F, -0.1F); + GL11.glRotatef(-50.0F, 0.0F, 1.0F, 0.0F); + GL11.glRotatef(-10.0F, 1.0F, 0.0F, 0.0F); + GL11.glRotatef(-60.0F, 0.0F, 0.0F, 1.0F); + } + + GL11.glTranslatef(0.0F, 0.1875F, 0.0F); + GL11.glScalef(f2, -f2, f2); + GL11.glRotatef(-100.0F, 1.0F, 0.0F, 0.0F); + GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F); + } else { + f2 = 0.375F; + GL11.glTranslatef(0.25F, 0.1875F, -0.1875F); + GL11.glScalef(f2, f2, f2); + GL11.glRotatef(60.0F, 0.0F, 0.0F, 1.0F); + GL11.glRotatef(-90.0F, 1.0F, 0.0F, 0.0F); + GL11.glRotatef(20.0F, 0.0F, 0.0F, 1.0F); + } - otherItemRenderer.itemToRender = itemToRender; - otherItemRenderer.equippedProgress = equippedProgress; - otherItemRenderer.prevEquippedProgress = prevEquippedProgress; + float f3; + int k; + float f12; + + if (offhandItem.getItem() + .requiresMultipleRenderPasses()) { + for (k = 0; k < offhandItem.getItem() + .getRenderPasses(offhandItem.getItemDamage()); ++k) { + int i = offhandItem.getItem() + .getColorFromItemStack(offhandItem, k); + f12 = (float) (i >> 16 & 255) / 255.0F; + f3 = (float) (i >> 8 & 255) / 255.0F; + f4 = (float) (i & 255) / 255.0F; + GL11.glColor4f(f12, f3, f4, 1.0F); + itemRenderer.renderItem(player, offhandItem, k); + } + } else { + k = offhandItem.getItem() + .getColorFromItemStack(offhandItem, 0); + float f11 = (float) (k >> 16 & 255) / 255.0F; + f12 = (float) (k >> 8 & 255) / 255.0F; + f3 = (float) (k & 255) / 255.0F; + GL11.glColor4f(f11, f12, f3, 1.0F); + itemRenderer.renderItem(player, offhandItem, 0); + } - GL11.glPopMatrix(); + GL11.glPopMatrix(); + } } } diff --git a/src/main/java/xonin/backhand/mixins/Mixins.java b/src/main/java/xonin/backhand/mixins/Mixins.java index e3e92a4..f5df3ce 100644 --- a/src/main/java/xonin/backhand/mixins/Mixins.java +++ b/src/main/java/xonin/backhand/mixins/Mixins.java @@ -29,11 +29,9 @@ public enum Mixins { MINECRAFT_CLIENT(new Builder("Client MC Mixins") .addMixinClasses( "minecraft.MixinEntityOtherPlayerMP", - "minecraft.MixinEntityPlayerClient", "minecraft.MixinItemRenderer", "minecraft.MixinModelBiped", "minecraft.MixinNetHandlerPlayClient", - "minecraft.MixinItemStackClient", "minecraft.MixinMinecraft", "minecraft.MixinGuiInventory") .setPhase(Phase.EARLY) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java deleted file mode 100644 index 27627bd..0000000 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java +++ /dev/null @@ -1,26 +0,0 @@ -package xonin.backhand.mixins.early.minecraft; - -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.world.World; - -import org.spongepowered.asm.mixin.Mixin; - -import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.client.utils.BackhandRenderHelper; - -@Mixin(EntityPlayer.class) -public abstract class MixinEntityPlayerClient extends EntityLivingBase implements IBackhandPlayer { - - private MixinEntityPlayerClient(World p_i1594_1_) { - super(p_i1594_1_); - } - - @Override - public float getSwingProgress(float partialTicks) { - if (BackhandRenderHelper.offhandFPRender) { - return getOffSwingProgress(partialTicks); - } - return super.getSwingProgress(partialTicks); - } -} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java index ac75bca..084b2f7 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java @@ -1,13 +1,14 @@ package xonin.backhand.mixins.early.minecraft; import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.renderer.ItemRenderer; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemMap; import net.minecraft.item.ItemStack; import org.lwjgl.opengl.GL11; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -22,11 +23,17 @@ @Mixin(ItemRenderer.class) public abstract class MixinItemRenderer { + @Unique + private static boolean backhand$isUpdatingOffhand; + + @Unique + private static boolean backhand$isRenderingOffhand; + @Inject(method = "renderItemInFirstPerson", at = @At("RETURN")) private void backhand$renderItemInFirstPerson(float frame, CallbackInfo ci) { - if (BackhandRenderHelper.offhandFPRender) return; + if (backhand$isRenderingOffhand) return; - EntityPlayer player = Minecraft.getMinecraft().thePlayer; + EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer; ClientEventHandler.renderingPlayer = player; ItemStack mainhandItem = player.getCurrentEquippedItem(); @@ -43,12 +50,31 @@ public abstract class MixinItemRenderer { } BackhandRenderHelper.firstPersonFrame = frame; - BackhandRenderHelper.itemRenderer.updateEquippedItem(); - BackhandRenderHelper.offhandFPRender = true; + backhand$isRenderingOffhand = true; GL11.glEnable(GL11.GL_CULL_FACE); GL11.glCullFace(GL11.GL_FRONT); - BackhandRenderHelper.renderOffhandItem((ItemRenderer) (Object) this, frame); + GL11.glPushMatrix(); + GL11.glScalef(-1, 1, 1); + float f3 = player.prevRenderArmPitch + (player.renderArmPitch - player.prevRenderArmPitch) * frame; + float f4 = player.prevRenderArmYaw + (player.renderArmYaw - player.prevRenderArmYaw) * frame; + GL11.glRotatef((player.rotationPitch - f3) * -0.1F, 1.0F, 0.0F, 0.0F); + GL11.glRotatef((player.rotationYaw - f4) * -0.1F, 0.0F, 1.0F, 0.0F); + BackhandUtils + .useOffhandItem(player, false, () -> BackhandRenderHelper.itemRenderer.renderItemInFirstPerson(frame)); + GL11.glPopMatrix(); GL11.glCullFace(GL11.GL_BACK); - BackhandRenderHelper.offhandFPRender = false; + backhand$isRenderingOffhand = false; } + + @Inject(method = "updateEquippedItem", at = @At("RETURN")) + private void backhand$updateOffhandItem(CallbackInfo ci) { + if (backhand$isUpdatingOffhand) return; + backhand$isUpdatingOffhand = true; + BackhandUtils.useOffhandItem( + Minecraft.getMinecraft().thePlayer, + false, + BackhandRenderHelper.itemRenderer::updateEquippedItem); + backhand$isUpdatingOffhand = false; + } + } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStackClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStackClient.java deleted file mode 100644 index fc474e2..0000000 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStackClient.java +++ /dev/null @@ -1,62 +0,0 @@ -package xonin.backhand.mixins.early.minecraft; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.EnumAction; -import net.minecraft.item.Item; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemBucket; -import net.minecraft.item.ItemDoor; -import net.minecraft.item.ItemRedstone; -import net.minecraft.item.ItemReed; -import net.minecraft.item.ItemSeedFood; -import net.minecraft.item.ItemSign; -import net.minecraft.item.ItemSkull; -import net.minecraft.item.ItemStack; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; - -import com.llamalad7.mixinextras.injector.ModifyReturnValue; - -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.client.ClientEventHandler; - -@Mixin(ItemStack.class) -public abstract class MixinItemStackClient { - - @ModifyReturnValue(method = "getItemUseAction", at = @At(value = "TAIL")) - private EnumAction backhand$getOffhandUseAction(EnumAction original) { - if (original == EnumAction.none || ClientEventHandler.renderingPlayer == null) { - return original; - } - EntityPlayer player = ClientEventHandler.renderingPlayer; - ItemStack itemStack = (ItemStack) (Object) this; - ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - if (offhandItem != null) { - ItemStack mainHandItem = player.getCurrentEquippedItem(); - if (mainHandItem != null && (BackhandUtils.checkForRightClickFunctionNoAction(mainHandItem) - || backhand$isItemBlock(mainHandItem.getItem()))) { - if (itemStack == offhandItem) { - return EnumAction.none; - } - } else if (itemStack == mainHandItem && (!(BackhandUtils.checkForRightClickFunctionNoAction(offhandItem) - || backhand$isItemBlock(offhandItem.getItem())) || player.getItemInUse() != mainHandItem)) { - return EnumAction.none; - } - } - return original; - } - - @Unique - private boolean backhand$isItemBlock(Item item) { - return item instanceof ItemBlock || item instanceof ItemDoor - || item instanceof ItemSign - || item instanceof ItemReed - || item instanceof ItemSeedFood - || item instanceof ItemRedstone - || item instanceof ItemBucket - || item instanceof ItemSkull; - } - -} From 036d66c42081dc0f5ceea67661aca7e750946b21 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Thu, 16 Jan 2025 20:26:10 +0100 Subject: [PATCH 12/38] fix mp sync --- dependencies.gradle | 1 - .../backhand/api/core/IBackhandPlayer.java | 5 ++ .../java/xonin/backhand/mixins/Mixins.java | 3 +- .../minecraft/MixinEntityLivingBase.java | 44 ++++++++++++++++++ .../minecraft/MixinEntityOtherPlayerMP.java | 10 ++-- .../early/minecraft/MixinEntityPlayer.java | 46 ++++++++++++++++++- .../packet/BackhandPacketHandler.java | 8 ++++ .../packet/OffhandSyncOffhandUse.java | 39 ++++++++++++++++ 8 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java create mode 100644 src/main/java/xonin/backhand/packet/OffhandSyncOffhandUse.java diff --git a/dependencies.gradle b/dependencies.gradle index bb7cff3..79e353a 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -39,5 +39,4 @@ dependencies { runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.7.18-GTNH:dev") compileOnly("com.github.GTNewHorizons:inventory-tweaks:1.7.0:dev") compileOnly(rfg.deobf('maven.modrinth:etfuturum:2.6.2')) - compileOnly("com.github.GTNewHorizons:CarpentersBlocks:3.7.0-GTNH:dev") } diff --git a/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java b/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java index c2809fd..f9112c3 100644 --- a/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java +++ b/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java @@ -16,4 +16,9 @@ public interface IBackhandPlayer { * The partial render progress for the offhand swing animation */ float getOffSwingProgress(float frame); + + void setUsingOffhand(boolean usingOffhand); + + boolean isUsingOffhand(); + } diff --git a/src/main/java/xonin/backhand/mixins/Mixins.java b/src/main/java/xonin/backhand/mixins/Mixins.java index f5df3ce..bb06405 100644 --- a/src/main/java/xonin/backhand/mixins/Mixins.java +++ b/src/main/java/xonin/backhand/mixins/Mixins.java @@ -22,7 +22,8 @@ public enum Mixins { "minecraft.MixinInventoryPlayer", "minecraft.MixinEntityTrackerEntry", "minecraft.MixinContainerPlayer", - "minecraft.MixinItemStack") + "minecraft.MixinItemStack", + "minecraft.MixinEntityLivingBase") .setPhase(Phase.EARLY) .setSide(Side.BOTH) .addTargetedMod(TargetedMod.VANILLA)), diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java new file mode 100644 index 0000000..7072346 --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java @@ -0,0 +1,44 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.llamalad7.mixinextras.sugar.Local; + +import xonin.backhand.Backhand; +import xonin.backhand.api.core.BackhandUtils; +import xonin.backhand.packet.OffhandSyncItemPacket; + +@Mixin(EntityLivingBase.class) +public abstract class MixinEntityLivingBase extends Entity { + + @Unique + private ItemStack backhand$previousOffhandStack; + + public MixinEntityLivingBase(World worldIn) { + super(worldIn); + } + + @Inject( + method = "onUpdate", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/item/ItemStack;areItemStacksEqual(Lnet/minecraft/item/ItemStack;Lnet/minecraft/item/ItemStack;)Z", + ordinal = 0)) + private void backhand$isItemInUseHook(CallbackInfo ci, @Local(name = "j") int index) { + if (!((EntityLivingBase) (Object) this instanceof EntityPlayer player) || index > 0) return; + ItemStack offhand = BackhandUtils.getOffhandItem(player); + if (offhand == null || ItemStack.areItemStacksEqual(backhand$previousOffhandStack, offhand)) return; + backhand$previousOffhandStack = offhand; + Backhand.packetHandler.sendPacketToAllTracking(player, new OffhandSyncItemPacket(player).generatePacket()); + } +} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java index 84cd711..5498e6d 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java @@ -14,9 +14,10 @@ import com.mojang.authlib.GameProfile; import xonin.backhand.api.core.BackhandUtils; +import xonin.backhand.api.core.IBackhandPlayer; @Mixin(EntityOtherPlayerMP.class) -public abstract class MixinEntityOtherPlayerMP extends AbstractClientPlayer { +public abstract class MixinEntityOtherPlayerMP extends AbstractClientPlayer implements IBackhandPlayer { @Shadow private boolean isItemInUse; @@ -33,11 +34,10 @@ private MixinEntityOtherPlayerMP(World p_i45074_1_, GameProfile p_i45074_2_) { target = "Lnet/minecraft/client/entity/EntityOtherPlayerMP;isItemInUse:Z", ordinal = 0)) private void backhand$isItemInUseHook(CallbackInfo ci) { - ItemStack itemStack = getCurrentEquippedItem(); + if (!isUsingOffhand()) return; ItemStack offhand = BackhandUtils.getOffhandItem(this); - if (BackhandUtils.usagePriorAttack(offhand)) itemStack = offhand; - if (!isItemInUse && isEating() && itemStack != null) { - setItemInUse(itemStack, itemStack.getMaxItemUseDuration()); + if (!isItemInUse && isEating() && offhand != null) { + setItemInUse(offhand, offhand.getMaxItemUseDuration()); isItemInUse = true; } else if (isItemInUse && !isEating()) { clearItemInUse(); diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java index bf83359..1b22306 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java @@ -12,14 +12,18 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import xonin.backhand.Backhand; import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IBackhandPlayer; import xonin.backhand.api.core.IOffhandInventory; +import xonin.backhand.packet.OffhandSyncOffhandUse; @Mixin(EntityPlayer.class) public abstract class MixinEntityPlayer extends EntityLivingBase implements IBackhandPlayer { @@ -27,8 +31,6 @@ public abstract class MixinEntityPlayer extends EntityLivingBase implements IBac @Shadow private ItemStack itemInUse; @Shadow - private int itemInUseCount; - @Shadow public InventoryPlayer inventory; @Unique private float backhand$offHandSwingProgress = 0F; @@ -38,6 +40,8 @@ public abstract class MixinEntityPlayer extends EntityLivingBase implements IBac private int backhand$offHandSwingProgressInt = 0; @Unique private boolean backhand$isOffHandSwingInProgress = false; + @Unique + private boolean backhand$isUsingOffhand = false; private MixinEntityPlayer(World p_i1594_1_) { super(p_i1594_1_); @@ -67,6 +71,34 @@ private MixinEntityPlayer(World p_i1594_1_) { return original; } + @Inject( + method = "setItemInUse", + at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;setEating(Z)V")) + private void backhand$onUpdate$getCurrentItem(ItemStack p_71008_1_, int p_71008_2_, CallbackInfo ci) { + EntityPlayer player = (EntityPlayer) (Object) this; + if (Objects.equals(p_71008_1_, BackhandUtils.getOffhandItem(player))) { + Backhand.packetHandler + .sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, true).generatePacket()); + setUsingOffhand(true); + } else if (isUsingOffhand()) { + Backhand.packetHandler + .sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, false).generatePacket()); + setUsingOffhand(false); + } + } + + @Inject( + method = "clearItemInUse", + at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;setEating(Z)V")) + private void backhand$onUpdate$getCurrentItem(CallbackInfo ci) { + EntityPlayer player = (EntityPlayer) (Object) this; + if (isUsingOffhand()) { + Backhand.packetHandler + .sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, false).generatePacket()); + setUsingOffhand(false); + } + } + @Override public void swingItem() { if (inventory.currentItem == IOffhandInventory.OFFHAND_HOTBAR_SLOT) { @@ -114,4 +146,14 @@ protected void updateArmSwingProgress() { this.backhand$offHandSwingProgress = (float) this.backhand$offHandSwingProgressInt / (float) var1; } + + @Override + public void setUsingOffhand(boolean usingOffhand) { + this.backhand$isUsingOffhand = usingOffhand; + } + + @Override + public boolean isUsingOffhand() { + return this.backhand$isUsingOffhand; + } } diff --git a/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java b/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java index cb5a2e0..471083a 100644 --- a/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java +++ b/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java @@ -7,6 +7,7 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.network.NetHandlerPlayServer; +import net.minecraft.world.WorldServer; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; @@ -28,6 +29,7 @@ public BackhandPacketHandler() { map.put(OffhandSwapPacket.packetName, new OffhandSwapPacket()); map.put(OffhandSwapClientPacket.packetName, new OffhandSwapClientPacket()); map.put(OffhandConfigSyncPacket.packetName, new OffhandConfigSyncPacket()); + map.put(OffhandSyncOffhandUse.packetName, new OffhandSyncOffhandUse()); } public void register() { @@ -83,4 +85,10 @@ public void sendPacketToAll(FMLProxyPacket packet) { .sendToAll(packet); } } + + public void sendPacketToAllTracking(Entity entity, FMLProxyPacket packet) { + if (!(entity.worldObj instanceof WorldServer world)) return; + world.getEntityTracker() + .func_151247_a(entity, packet); + } } diff --git a/src/main/java/xonin/backhand/packet/OffhandSyncOffhandUse.java b/src/main/java/xonin/backhand/packet/OffhandSyncOffhandUse.java new file mode 100644 index 0000000..49e986f --- /dev/null +++ b/src/main/java/xonin/backhand/packet/OffhandSyncOffhandUse.java @@ -0,0 +1,39 @@ +package xonin.backhand.packet; + +import net.minecraft.entity.player.EntityPlayer; + +import cpw.mods.fml.common.network.ByteBufUtils; +import io.netty.buffer.ByteBuf; +import xonin.backhand.api.core.IBackhandPlayer; + +public final class OffhandSyncOffhandUse extends AbstractPacket { + + public static final String packetName = "SyncItemUsage"; + private boolean isUsingOffhand; + private EntityPlayer player; + + public OffhandSyncOffhandUse(EntityPlayer player, boolean isUsingOffhand) { + this.player = player; + this.isUsingOffhand = isUsingOffhand; + } + + public OffhandSyncOffhandUse() {} + + @Override + public void process(ByteBuf inputStream, EntityPlayer player) { + this.player = player.worldObj.getPlayerEntityByName(ByteBufUtils.readUTF8String(inputStream)); + if (this.player == null) return; + ((IBackhandPlayer) this.player).setUsingOffhand(inputStream.readBoolean()); + } + + @Override + public String getChannel() { + return packetName; + } + + @Override + public void write(ByteBuf out) { + ByteBufUtils.writeUTF8String(out, player.getCommandSenderName()); + out.writeBoolean(isUsingOffhand); + } +} From bea2d8c800183e118e1441f67a3fbe98e0346234 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Thu, 16 Jan 2025 22:33:08 +0100 Subject: [PATCH 13/38] also sync when offhand is emptied --- src/main/java/xonin/backhand/CommonProxy.java | 14 +- .../backhand/api/core/BackhandUtils.java | 135 +----------------- .../xonin/backhand/client/ClientProxy.java | 1 + .../minecraft/MixinEntityLivingBase.java | 2 +- 4 files changed, 20 insertions(+), 132 deletions(-) diff --git a/src/main/java/xonin/backhand/CommonProxy.java b/src/main/java/xonin/backhand/CommonProxy.java index fa9167d..e279d30 100644 --- a/src/main/java/xonin/backhand/CommonProxy.java +++ b/src/main/java/xonin/backhand/CommonProxy.java @@ -1,5 +1,6 @@ package xonin.backhand; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; @@ -9,13 +10,12 @@ import cpw.mods.fml.common.gameevent.PlayerEvent; import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.packet.OffhandConfigSyncPacket; +import xonin.backhand.packet.OffhandSyncItemPacket; @EventBusSubscriber public class CommonProxy { - public void load() { - - } + public void load() {} @SubscribeEvent public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) { @@ -31,4 +31,12 @@ public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) { } } } + + @SubscribeEvent + public static void addTracking(net.minecraftforge.event.entity.player.PlayerEvent.StartTracking event) { + if (event.entityPlayer instanceof EntityPlayerMP playerMP && BackhandUtils.isValidPlayer(event.target)) { + Backhand.packetHandler + .sendPacketToPlayer(new OffhandSyncItemPacket((EntityPlayer) event.target).generatePacket(), playerMP); + } + } } diff --git a/src/main/java/xonin/backhand/api/core/BackhandUtils.java b/src/main/java/xonin/backhand/api/core/BackhandUtils.java index 6478066..8f1d145 100644 --- a/src/main/java/xonin/backhand/api/core/BackhandUtils.java +++ b/src/main/java/xonin/backhand/api/core/BackhandUtils.java @@ -5,31 +5,10 @@ import javax.annotation.Nullable; import net.minecraft.client.Minecraft; -import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.EnumAction; -import net.minecraft.item.Item; -import net.minecraft.item.ItemBed; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemBook; -import net.minecraft.item.ItemBow; -import net.minecraft.item.ItemBucket; -import net.minecraft.item.ItemDoor; -import net.minecraft.item.ItemEgg; -import net.minecraft.item.ItemEnderPearl; -import net.minecraft.item.ItemHangingEntity; -import net.minecraft.item.ItemHoe; -import net.minecraft.item.ItemMonsterPlacer; -import net.minecraft.item.ItemRedstone; -import net.minecraft.item.ItemReed; -import net.minecraft.item.ItemSeedFood; -import net.minecraft.item.ItemSign; -import net.minecraft.item.ItemSkull; -import net.minecraft.item.ItemSnowball; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; -import net.minecraft.item.ItemSword; -import net.minecraft.item.ItemTool; -import net.minecraft.world.World; import net.minecraftforge.common.util.FakePlayer; /** @@ -37,20 +16,6 @@ */ public class BackhandUtils { - private static String[] itemBlackListMethodNames = { - BackhandTranslator.getMapedMethodName("Item", "func_77648_a", "onItemUse"), - BackhandTranslator.getMapedMethodName("Item", "func_77659_a", "onItemRightClick"), - BackhandTranslator.getMapedMethodName("Item", "func_111207_a", "itemInteractionForEntity") }; - - /** - * Method arguments classes that are not allowed in {@link Item} subclasses for common wielding - */ - private static Class[][] itemBlackListMethodParams = { - new Class[] { ItemStack.class, EntityPlayer.class, World.class, int.class, int.class, int.class, int.class, - float.class, float.class, float.class }, - new Class[] { ItemStack.class, World.class, EntityPlayer.class }, - new Class[] { ItemStack.class, EntityPlayer.class, EntityLivingBase.class } }; - public static void swapOffhandItem(EntityPlayer player) { player.setCurrentItemOrArmor(0, BackhandUtils.getOffhandItem(player)); BackhandUtils.setPlayerOffhandItem(player, BackhandUtils.getOffhandItem(player)); @@ -69,97 +34,6 @@ public static void setPlayerOffhandItem(EntityPlayer player, ItemStack stack) { return ((IOffhandInventory) player.inventory).backhand$getOffhandItem(); } - /** - * Defines a item which "use" (effect on right click) should have priority over its "attack" (effect on left click) - * - * @param itemStack the item which will be "used", instead of attacking - * @return true if such item prefer being "used" - */ - public static boolean usagePriorAttack(ItemStack itemStack) { - if (itemStack == null) { - return false; - } - if (itemStack.getItemUseAction() == EnumAction.drink || itemStack.getItemUseAction() == EnumAction.eat - || itemStack.getItemUseAction() == EnumAction.bow) { - return true; - } - return !(itemStack.getItem() instanceof ItemSword) - && (checkForRightClickFunction(itemStack) || isCommonlyUsable(itemStack.getItem())); - } - - /** - * Defines items that are usually usable (the vanilla instances do, at least) - * - * @param item the instance to consider for usability - * @return true if it is commonly usable - */ - public static boolean isCommonlyUsable(Item item) { - return isBow(item) || item.getClass() - .toString() - .equalsIgnoreCase("class D.f") - || item instanceof ItemBed - || item instanceof ItemHangingEntity - || item instanceof ItemBook - || isItemBlock(item) - || item instanceof ItemHoe - || item instanceof ItemSnowball - || item instanceof ItemEnderPearl - || item instanceof ItemEgg - || item instanceof ItemMonsterPlacer; - } - - /** - * Defines a bow - * - * @param item the instance - * @return true if it is considered a generic enough bow - */ - public static boolean isBow(Item item) { - return item instanceof ItemBow; - } - - public static boolean isItemBlock(Item item) { - return item instanceof ItemBlock || item instanceof ItemDoor - || item instanceof ItemSign - || item instanceof ItemReed - || item instanceof ItemSeedFood - || item instanceof ItemRedstone - || item instanceof ItemBucket - || item instanceof ItemSkull; - } - - @SuppressWarnings("unchecked") - public static boolean checkForRightClickFunction(ItemStack stack) { - if (stack == null) { - return false; - } - try { - if (stack.getItemUseAction() == EnumAction.block || stack.getItemUseAction() == EnumAction.none) { - - Class c = stack.getItem() - .getClass(); - while (!(c.equals(Item.class) || c.equals(ItemTool.class) || c.equals(ItemSword.class))) { - try { - for (int i = 0; i < itemBlackListMethodNames.length; i++) { - try { - c.getDeclaredMethod(itemBlackListMethodNames[i], itemBlackListMethodParams[i]); - return true; - } catch (NoSuchMethodException ignored) {} - } - } catch (NoClassDefFoundError ignored) {} - - c = c.getSuperclass(); - } - - return false; - } else { - return true; - } - } catch (NullPointerException e) { - return true; - } - } - public static void useOffhandItem(EntityPlayer player, Runnable action) { useOffhandItem(player, true, action); } @@ -185,4 +59,9 @@ public static boolean useOffhandItem(EntityPlayer player, boolean syncSlot, Bool } return result; } + + public static boolean isValidPlayer(Entity entity) { + return entity instanceof EntityPlayerMP playerMP + && !(entity instanceof FakePlayer || playerMP.playerNetServerHandler == null); + } } diff --git a/src/main/java/xonin/backhand/client/ClientProxy.java b/src/main/java/xonin/backhand/client/ClientProxy.java index 89cb071..b2212f4 100644 --- a/src/main/java/xonin/backhand/client/ClientProxy.java +++ b/src/main/java/xonin/backhand/client/ClientProxy.java @@ -14,6 +14,7 @@ public class ClientProxy extends CommonProxy { Keyboard.KEY_F, "key.categories.gameplay"); + @Override public void load() { ClientRegistry.registerKeyBinding(swapOffhand); } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java index 7072346..1e942c8 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java @@ -37,7 +37,7 @@ public MixinEntityLivingBase(World worldIn) { private void backhand$isItemInUseHook(CallbackInfo ci, @Local(name = "j") int index) { if (!((EntityLivingBase) (Object) this instanceof EntityPlayer player) || index > 0) return; ItemStack offhand = BackhandUtils.getOffhandItem(player); - if (offhand == null || ItemStack.areItemStacksEqual(backhand$previousOffhandStack, offhand)) return; + if (ItemStack.areItemStacksEqual(backhand$previousOffhandStack, offhand)) return; backhand$previousOffhandStack = offhand; Backhand.packetHandler.sendPacketToAllTracking(player, new OffhandSyncItemPacket(player).generatePacket()); } From 9411cd87c457470f744578ac9a98d4c0d7212bc0 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Thu, 16 Jan 2025 22:40:54 +0100 Subject: [PATCH 14/38] remove more now unused stuff & shuffle some mixins around --- .../xonin/backhand/HookContainerClass.java | 272 ------------------ .../java/xonin/backhand/mixins/Mixins.java | 1 - .../minecraft/MixinEntityLivingBase.java | 2 +- .../early/minecraft/MixinEntityPlayer.java | 40 ++- .../minecraft/MixinEntityTrackerEntry.java | 39 --- .../minecraft/MixinNetHandlerPlayServer.java | 4 - .../packet/BackhandPacketHandler.java | 1 - .../packet/OffhandAnimationPacket.java | 60 ---- .../xonin/backhand/utils/EnumAnimations.java | 17 -- 9 files changed, 20 insertions(+), 416 deletions(-) delete mode 100644 src/main/java/xonin/backhand/HookContainerClass.java delete mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityTrackerEntry.java delete mode 100644 src/main/java/xonin/backhand/packet/OffhandAnimationPacket.java delete mode 100644 src/main/java/xonin/backhand/utils/EnumAnimations.java diff --git a/src/main/java/xonin/backhand/HookContainerClass.java b/src/main/java/xonin/backhand/HookContainerClass.java deleted file mode 100644 index bc2aeb4..0000000 --- a/src/main/java/xonin/backhand/HookContainerClass.java +++ /dev/null @@ -1,272 +0,0 @@ -package xonin.backhand; - -import net.minecraft.entity.Entity; -import net.minecraftforge.common.util.FakePlayer; - -public final class HookContainerClass { - - public static final HookContainerClass INSTANCE = new HookContainerClass(); - - private HookContainerClass() {} - - private boolean isFake(Entity entity) { - return entity instanceof FakePlayer; - } - - // @SubscribeEvent(priority = EventPriority.LOWEST) - // public void onEntityJoin(EntityJoinWorldEvent event) { - // if (event.entity instanceof EntityPlayer player && !(isFake(player))) { - // if (FMLCommonHandler.instance() - // .getEffectiveSide() == Side.SERVER) { - // if (!(player.inventory instanceof InventoryPlayerBackhand)) { - // Backhand.LOGGER.info("Player inventory has been replaced with {}", player.inventory.getClass()); - // } - // Backhand.packetHandler.sendPacketToPlayer( - // new OffhandConfigSyncPacket(player).generatePacket(), - // (EntityPlayerMP) event.entity); - // Backhand.packetHandler.sendPacketToPlayer( - // new OffhandSyncItemPacket(player).generatePacket(), - // (EntityPlayerMP) event.entity); - // } - // ItemStack offhandItem = BackhandUtils.getOffhandItem(player); - // if (Backhand.isOffhandBlacklisted(offhandItem)) { - // BackhandUtils.setPlayerOffhandItem(player, null); - // if (!player.inventory.addItemStackToInventory(offhandItem)) { - // event.entity.entityDropItem(offhandItem, 0); - // } - // } - // } - // } - - // @SubscribeEvent(priority = EventPriority.HIGHEST) - // public void onEntityConstructing(EntityEvent.EntityConstructing event) { - // if (!(event.entity instanceof EntityPlayer player && !(isFake(event.entity)))) return; - // event.entity.registerExtendedProperties("OffhandStorage", new OffhandExtendedProperty(player)); - // } - - // public static MovingObjectPosition getRaytraceBlock(EntityPlayer p) { - // float scaleFactor = 1.0F; - // float rotPitch = p.prevRotationPitch + (p.rotationPitch - p.prevRotationPitch) * scaleFactor; - // float rotYaw = p.prevRotationYaw + (p.rotationYaw - p.prevRotationYaw) * scaleFactor; - // double testX = p.prevPosX + (p.posX - p.prevPosX) * scaleFactor; - // double testY = p.prevPosY + (p.posY - p.prevPosY) * scaleFactor + 1.62D - p.yOffset;// 1.62 is player eye height - // double testZ = p.prevPosZ + (p.posZ - p.prevPosZ) * scaleFactor; - // Vec3 testVector = Vec3.createVectorHelper(testX, testY, testZ); - // float var14 = MathHelper.cos(-rotYaw * 0.017453292F - (float) Math.PI); - // float var15 = MathHelper.sin(-rotYaw * 0.017453292F - (float) Math.PI); - // float var16 = -MathHelper.cos(-rotPitch * 0.017453292F); - // float vectorY = MathHelper.sin(-rotPitch * 0.017453292F); - // float vectorX = var15 * var16; - // float vectorZ = var14 * var16; - // double reachLength = 5.0D; - // Vec3 testVectorFar = testVector.addVector(vectorX * reachLength, vectorY * reachLength, vectorZ * reachLength); - // return p.worldObj.rayTraceBlocks(testVector, testVectorFar, false); - // } - - // // @SubscribeEvent - // public void playerInteract(PlayerInteractEvent event) { - // if (isFake(event.entityPlayer)) return; - // - // if (!BackhandConfig.EmptyOffhand && BackhandUtils.getOffhandItem(event.entityPlayer) == null) { - // return; - // } - // - // if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR - // || event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) {// Right click - // ItemStack mainHandItem = event.entityPlayer.getCurrentEquippedItem(); - // ItemStack offhandItem = BackhandUtils.getOffhandItem(event.entityPlayer); - // - // if (mainHandItem != null - // && (BackhandUtils.checkForRightClickFunction(mainHandItem) || offhandItem == null)) { - // return; - // } - // - // if (event.action != PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK && Backhand.proxy.isRightClickHeld()) { - // Backhand.proxy.setRightClickCounter(Backhand.proxy.getRightClickCounter() + 1); - // if (Backhand.proxy.getRightClickCounter() > 1) { - // return; - // } - // } - // - // if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK && mainHandItem != null - // && mainHandItem.getItem() instanceof ItemMonsterPlacer) { - // if (event.world.isRemote && !event.entityPlayer.capabilities.isCreativeMode) { - // mainHandItem.stackSize--; - // } - // } - // - // boolean swingHand = true; - // PlayerInteractEvent.Result blk = event.useBlock; - // PlayerInteractEvent.Result itm = event.useItem; - // event.useBlock = PlayerInteractEvent.Result.DENY; - // MovingObjectPosition mop = getRaytraceBlock(event.entityPlayer); - // if (mop != null) { - // event.setCanceled(true); - // int i = mop.blockX, j = mop.blockY, k = mop.blockZ; - // - // if (!event.entityPlayer.isSneaking() - // && canBlockBeInteractedWith(event.entityPlayer.worldObj, i, j, k)) { - // event.setCanceled(false); - // event.useBlock = blk; - // event.useItem = itm; - // swingHand = false; - // } - // } - // if (event.entityPlayer.worldObj.isRemote && !BackhandUtils.usagePriorAttack(offhandItem) - // && BackhandConfig.OffhandAttack - // && swingHand) { - // HookContainerClass.sendOffSwingEventNoCheck(event.entityPlayer, mainHandItem, offhandItem); - // } - // } - // } - - // private static String[] activatedBlockMethodNames = { - // BackhandTranslator.getMapedMethodName("Block", "func_149727_a", "onBlockActivated"), - // BackhandTranslator.getMapedMethodName("Block", "func_149699_a", "onBlockClicked") }; - // private static Class[][] activatedBlockMethodParams = { - // new Class[] { World.class, int.class, int.class, int.class, EntityPlayer.class, int.class, float.class, - // float.class, float.class }, - // new Class[] { World.class, int.class, int.class, int.class, EntityPlayer.class } }; - // - // @SuppressWarnings("unchecked") - // public static boolean canBlockBeInteractedWith(World worldObj, int x, int y, int z) { - // if (worldObj == null) return false; - // Block block = worldObj.getBlock(x, y, z); - // if (block == null) return false; - // if (block.getClass() - // .equals(Block.class)) return false; - // try { - // Class c = block.getClass(); - // while (!(c.equals(Block.class))) { - // try { - // try { - // c.getDeclaredMethod(activatedBlockMethodNames[0], activatedBlockMethodParams[0]); - // return true; - // } catch (NoSuchMethodException ignored) {} - // - // try { - // c.getDeclaredMethod(activatedBlockMethodNames[1], activatedBlockMethodParams[1]); - // return true; - // } catch (NoSuchMethodException ignored) {} - // } catch (NoClassDefFoundError ignored) { - // - // } - // - // c = c.getSuperclass(); - // } - // - // return false; - // } catch (NullPointerException e) { - // return true; - // } - // } - // - // public static boolean changedHeldItemTooltips = false; - // // used in hostwapping the item to dig with, to remember where to return the main slot to - // public static int prevOffhandOffset; - // - // public static boolean isItemBlock(Item item) { - // return item instanceof ItemBlock || item instanceof ItemDoor - // || item instanceof ItemSign - // || item instanceof ItemReed - // || item instanceof ItemSeedFood - // || item instanceof ItemRedstone - // || item instanceof ItemBucket - // || item instanceof ItemSkull; - // } - // - // @SideOnly(Side.CLIENT) - // public static void sendOffSwingEventNoCheck(EntityPlayer player, ItemStack mainHandItem, ItemStack offhandItem) { - // ((IBackhandPlayer) player).swingOffItem(); - // Backhand.proxy.sendAnimationPacket(EnumAnimations.OffHandSwing, player); - // } - // - // // @SideOnly(Side.CLIENT) - // // @SubscribeEvent(priority = EventPriority.HIGHEST) - // // public void onOffhandSwing(PlayerEventChild.OffhandSwingEvent event) {} - // - // public boolean interactWithNoEvent(EntityPlayer pl, Entity p_70998_1_) { - // ItemStack itemstack = pl.getCurrentEquippedItem(); - // ItemStack itemstack1 = ItemStack.copyItemStack(itemstack); - // - // if (!p_70998_1_.interactFirst(pl)) { - // if (itemstack != null && p_70998_1_ instanceof EntityLivingBase) { - // if (pl.capabilities.isCreativeMode) { - // itemstack = itemstack1; - // } - // - // if (itemstack.interactWithEntity(pl, (EntityLivingBase) p_70998_1_)) { - // if (itemstack.stackSize <= 0 && !pl.capabilities.isCreativeMode) { - // pl.destroyCurrentEquippedItem(); - // } - // - // return true; - // } - // } - // - // return false; - // } else { - // if (itemstack != null && itemstack == pl.getCurrentEquippedItem()) { - // if (itemstack.stackSize <= 0 && !pl.capabilities.isCreativeMode) { - // pl.destroyCurrentEquippedItem(); - // } else if (itemstack.stackSize < itemstack1.stackSize && pl.capabilities.isCreativeMode) { - // itemstack.stackSize = itemstack1.stackSize; - // } - // } - // - // return true; - // } - // } - // - // // @SubscribeEvent(priority = EventPriority.HIGHEST) - // // public void onOffhandAttack(PlayerEventChild.OffhandAttackEvent event) { - // // if (event.offHand != null) { - // // if (hasEntityInteraction( - // // event.getPlayer().capabilities.isCreativeMode ? event.offHand.copy() : event.offHand, - // // event.getTarget(), - // // event.getPlayer(), - // // false)) { - // // event.setCanceled(true); - // // if (event.offHand.stackSize <= 0 && !event.getPlayer().capabilities.isCreativeMode) { - // // ItemStack orig = event.offHand; - // // BackhandUtils.setPlayerOffhandItem(event.getPlayer(), null); - // // MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(event.getPlayer(), orig)); - // // } - // // } - // // } - // // } - // - // /** - // * Check if a stack has a specific interaction with an entity. - // * Use a call to {@link net.minecraft.item.ItemStack#interactWithEntity(EntityPlayer, EntityLivingBase)} - // * - // * @param itemStack to interact last with - // * @param entity to interact first with - // * @param entityPlayer holding the stack - // * @param asTest if data should be cloned before testing - // * @return true if a specific interaction exist (and has been done if asTest is false) - // */ - // private boolean hasEntityInteraction(ItemStack itemStack, Entity entity, EntityPlayer entityPlayer, - // boolean asTest) { - // if (asTest) { - // Entity clone = EntityList.createEntityByName(EntityList.getEntityString(entity), entity.worldObj); - // if (clone != null) { - // clone.copyDataFrom(entity, true); - // return !clone.interactFirst(entityPlayer) && clone instanceof EntityLivingBase - // && itemStack.copy() - // .interactWithEntity(entityPlayer, (EntityLivingBase) clone); - // } - // } else if (!entity.interactFirst(entityPlayer) && entity instanceof EntityLivingBase) { - // return itemStack.interactWithEntity(entityPlayer, (EntityLivingBase) entity); - // } - // return false; - // } - - // @SubscribeEvent - // public void addTracking(PlayerEvent.StartTracking event) { - // if (event.target instanceof EntityPlayer && !isFake(event.target)) { - // ((EntityPlayerMP) event.entityPlayer).playerNetServerHandler - // .sendPacket(new OffhandSyncItemPacket((EntityPlayer) event.target).generatePacket()); - // } - // } -} diff --git a/src/main/java/xonin/backhand/mixins/Mixins.java b/src/main/java/xonin/backhand/mixins/Mixins.java index bb06405..25ad06c 100644 --- a/src/main/java/xonin/backhand/mixins/Mixins.java +++ b/src/main/java/xonin/backhand/mixins/Mixins.java @@ -20,7 +20,6 @@ public enum Mixins { "minecraft.MixinItemBow", "minecraft.MixinEntityFishHook", "minecraft.MixinInventoryPlayer", - "minecraft.MixinEntityTrackerEntry", "minecraft.MixinContainerPlayer", "minecraft.MixinItemStack", "minecraft.MixinEntityLivingBase") diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java index 1e942c8..55b9351 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityLivingBase.java @@ -34,7 +34,7 @@ public MixinEntityLivingBase(World worldIn) { value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;areItemStacksEqual(Lnet/minecraft/item/ItemStack;Lnet/minecraft/item/ItemStack;)Z", ordinal = 0)) - private void backhand$isItemInUseHook(CallbackInfo ci, @Local(name = "j") int index) { + private void backhand$updateOffhandItem(CallbackInfo ci, @Local(name = "j") int index) { if (!((EntityLivingBase) (Object) this instanceof EntityPlayer player) || index > 0) return; ItemStack offhand = BackhandUtils.getOffhandItem(player); if (ItemStack.areItemStacksEqual(backhand$previousOffhandStack, offhand)) return; diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java index 1b22306..bfd108c 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java @@ -74,7 +74,7 @@ private MixinEntityPlayer(World p_i1594_1_) { @Inject( method = "setItemInUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;setEating(Z)V")) - private void backhand$onUpdate$getCurrentItem(ItemStack p_71008_1_, int p_71008_2_, CallbackInfo ci) { + private void backhand$setItemInUse(ItemStack p_71008_1_, int p_71008_2_, CallbackInfo ci) { EntityPlayer player = (EntityPlayer) (Object) this; if (Objects.equals(p_71008_1_, BackhandUtils.getOffhandItem(player))) { Backhand.packetHandler @@ -90,7 +90,7 @@ private MixinEntityPlayer(World p_i1594_1_) { @Inject( method = "clearItemInUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;setEating(Z)V")) - private void backhand$onUpdate$getCurrentItem(CallbackInfo ci) { + private void backhand$clearOffhand(CallbackInfo ci) { EntityPlayer player = (EntityPlayer) (Object) this; if (isUsingOffhand()) { Backhand.packetHandler @@ -99,6 +99,23 @@ private MixinEntityPlayer(World p_i1594_1_) { } } + @Inject(method = "updateEntityActionState", at = @At(value = "TAIL")) + private void backhand$updateOffhandSwingProgress(CallbackInfo ci) { + this.backhand$prevOffHandSwingProgress = this.backhand$offHandSwingProgress; + int var1 = this.getArmSwingAnimationEnd(); + if (this.backhand$isOffHandSwingInProgress) { + ++this.backhand$offHandSwingProgressInt; + if (this.backhand$offHandSwingProgressInt >= var1) { + this.backhand$offHandSwingProgressInt = 0; + this.backhand$isOffHandSwingInProgress = false; + } + } else { + this.backhand$offHandSwingProgressInt = 0; + } + + this.backhand$offHandSwingProgress = (float) this.backhand$offHandSwingProgressInt / (float) var1; + } + @Override public void swingItem() { if (inventory.currentItem == IOffhandInventory.OFFHAND_HOTBAR_SLOT) { @@ -116,7 +133,6 @@ public void swingOffItem() { this.backhand$offHandSwingProgressInt = -1; this.backhand$isOffHandSwingInProgress = true; } - } @Override @@ -129,24 +145,6 @@ public float getOffSwingProgress(float frame) { return this.backhand$prevOffHandSwingProgress + diff * frame; } - @Override - protected void updateArmSwingProgress() { - super.updateArmSwingProgress(); - this.backhand$prevOffHandSwingProgress = this.backhand$offHandSwingProgress; - int var1 = this.getArmSwingAnimationEnd(); - if (this.backhand$isOffHandSwingInProgress) { - ++this.backhand$offHandSwingProgressInt; - if (this.backhand$offHandSwingProgressInt >= var1) { - this.backhand$offHandSwingProgressInt = 0; - this.backhand$isOffHandSwingInProgress = false; - } - } else { - this.backhand$offHandSwingProgressInt = 0; - } - - this.backhand$offHandSwingProgress = (float) this.backhand$offHandSwingProgressInt / (float) var1; - } - @Override public void setUsingOffhand(boolean usingOffhand) { this.backhand$isUsingOffhand = usingOffhand; diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityTrackerEntry.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityTrackerEntry.java deleted file mode 100644 index 6f1d03a..0000000 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityTrackerEntry.java +++ /dev/null @@ -1,39 +0,0 @@ -package xonin.backhand.mixins.early.minecraft; - -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityTrackerEntry; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.ItemStack; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import com.llamalad7.mixinextras.sugar.Local; - -import xonin.backhand.Backhand; -import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.packet.OffhandSyncItemPacket; - -@Mixin(EntityTrackerEntry.class) -public abstract class MixinEntityTrackerEntry { - - @Shadow - public Entity myEntity; - - @Inject( - method = "tryStartWachingThis", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/entity/EntityLivingBase;getEquipmentInSlot(I)Lnet/minecraft/item/ItemStack;")) - private void backhand$syncOffhand(EntityPlayerMP receivingPlayer, CallbackInfo ci, @Local(name = "i") int i) { - if (!(myEntity instanceof EntityPlayerMP playerMP) || i > 0) return; - ItemStack offhand = BackhandUtils.getOffhandItem(playerMP); - if (offhand != null) { - Backhand.packetHandler - .sendPacketToPlayer(new OffhandSyncItemPacket(playerMP).generatePacket(), receivingPlayer); - } - } -} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java index 17eef70..eaaebdd 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java @@ -7,7 +7,6 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; @@ -22,9 +21,6 @@ public abstract class MixinNetHandlerPlayServer { @Shadow public EntityPlayerMP playerEntity; - @Unique - private boolean backhand$swappedOffhand = false; - @ModifyExpressionValue( method = "processHeldItemChange", at = @At( diff --git a/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java b/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java index 471083a..65ce08e 100644 --- a/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java +++ b/src/main/java/xonin/backhand/packet/BackhandPacketHandler.java @@ -25,7 +25,6 @@ public final class BackhandPacketHandler { public BackhandPacketHandler() { map.put(OffhandSyncItemPacket.packetName, new OffhandSyncItemPacket()); - map.put(OffhandAnimationPacket.packetName, new OffhandAnimationPacket()); map.put(OffhandSwapPacket.packetName, new OffhandSwapPacket()); map.put(OffhandSwapClientPacket.packetName, new OffhandSwapClientPacket()); map.put(OffhandConfigSyncPacket.packetName, new OffhandConfigSyncPacket()); diff --git a/src/main/java/xonin/backhand/packet/OffhandAnimationPacket.java b/src/main/java/xonin/backhand/packet/OffhandAnimationPacket.java deleted file mode 100644 index d71eff8..0000000 --- a/src/main/java/xonin/backhand/packet/OffhandAnimationPacket.java +++ /dev/null @@ -1,60 +0,0 @@ -package xonin.backhand.packet; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.world.WorldServer; - -import cpw.mods.fml.common.network.ByteBufUtils; -import io.netty.buffer.ByteBuf; -import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.utils.EnumAnimations; - -/** - * User: nerd-boy - * Date: 26/06/13 - * Time: 1:47 PM - */ -public final class OffhandAnimationPacket extends AbstractPacket { - - public static final String packetName = "MB2|Animation"; - private EnumAnimations animation; - private String username; - - public OffhandAnimationPacket(EnumAnimations animation, EntityPlayer user) { - this.animation = animation; - this.username = user.getCommandSenderName(); - } - - public OffhandAnimationPacket() {} - - @Override - public void process(ByteBuf in, EntityPlayer player) { - try { - animation = EnumAnimations.values()[in.readInt()]; - username = ByteBufUtils.readUTF8String(in); - } catch (Exception e) { - e.printStackTrace(); - return; - } - if (username != null && animation != null) { - EntityPlayer entity = player.worldObj.getPlayerEntityByName(username); - if (entity != null) { - if (entity.worldObj instanceof WorldServer) { - ((WorldServer) entity.worldObj).getEntityTracker() - .func_151247_a(entity, this.generatePacket()); - } - animation.processAnimation((IBackhandPlayer) entity); - } - } - } - - @Override - public String getChannel() { - return packetName; - } - - @Override - public void write(ByteBuf out) { - out.writeInt(animation.ordinal()); - ByteBufUtils.writeUTF8String(out, username); - } -} diff --git a/src/main/java/xonin/backhand/utils/EnumAnimations.java b/src/main/java/xonin/backhand/utils/EnumAnimations.java deleted file mode 100644 index 336f52e..0000000 --- a/src/main/java/xonin/backhand/utils/EnumAnimations.java +++ /dev/null @@ -1,17 +0,0 @@ -package xonin.backhand.utils; - -import xonin.backhand.api.core.IBackhandPlayer; - -public enum EnumAnimations { - - OffHandSwing { - - @Override - public void processAnimation(IBackhandPlayer entity) { - entity.swingOffItem(); - } - }; - - public abstract void processAnimation(IBackhandPlayer entity); - -} From 8b5b33ee53391badcf3135ec3b4317bd8db84c2c Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Thu, 16 Jan 2025 22:41:23 +0100 Subject: [PATCH 15/38] slight optimization --- .../minecraft/MixinEntityOtherPlayerMP.java | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java index 5498e6d..ffc9c73 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java @@ -5,11 +5,10 @@ import net.minecraft.item.ItemStack; import net.minecraft.world.World; +import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.Redirect; import com.mojang.authlib.GameProfile; @@ -19,31 +18,19 @@ @Mixin(EntityOtherPlayerMP.class) public abstract class MixinEntityOtherPlayerMP extends AbstractClientPlayer implements IBackhandPlayer { - @Shadow - private boolean isItemInUse; - private MixinEntityOtherPlayerMP(World p_i45074_1_, GameProfile p_i45074_2_) { super(p_i45074_1_, p_i45074_2_); } - @Inject( + @Redirect( method = "onUpdate", - cancellable = true, at = @At( value = "FIELD", - target = "Lnet/minecraft/client/entity/EntityOtherPlayerMP;isItemInUse:Z", - ordinal = 0)) - private void backhand$isItemInUseHook(CallbackInfo ci) { - if (!isUsingOffhand()) return; - ItemStack offhand = BackhandUtils.getOffhandItem(this); - if (!isItemInUse && isEating() && offhand != null) { - setItemInUse(offhand, offhand.getMaxItemUseDuration()); - isItemInUse = true; - } else if (isItemInUse && !isEating()) { - clearItemInUse(); - isItemInUse = false; - } - ci.cancel(); + opcode = Opcodes.GETFIELD, + args = "array=get", + target = "Lnet/minecraft/entity/player/InventoryPlayer;mainInventory:[Lnet/minecraft/item/ItemStack;")) + private ItemStack backhand$isItemInUseHook(ItemStack[] array, int index) { + if (isUsingOffhand()) return BackhandUtils.getOffhandItem(this); + return array[index]; } - } From dbff6c89a5697d9810dfb5b412e3866b629252fe Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:33:21 +0100 Subject: [PATCH 16/38] oops didn't mean to remove that --- .../java/xonin/backhand/mixins/Mixins.java | 1 + .../minecraft/MixinEntityPlayerClient.java | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java diff --git a/src/main/java/xonin/backhand/mixins/Mixins.java b/src/main/java/xonin/backhand/mixins/Mixins.java index 25ad06c..185ba45 100644 --- a/src/main/java/xonin/backhand/mixins/Mixins.java +++ b/src/main/java/xonin/backhand/mixins/Mixins.java @@ -29,6 +29,7 @@ public enum Mixins { MINECRAFT_CLIENT(new Builder("Client MC Mixins") .addMixinClasses( "minecraft.MixinEntityOtherPlayerMP", + "minecraft.MixinEntityPlayerClient", "minecraft.MixinItemRenderer", "minecraft.MixinModelBiped", "minecraft.MixinNetHandlerPlayClient", diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java new file mode 100644 index 0000000..cbbca32 --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java @@ -0,0 +1,31 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.world.World; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import xonin.backhand.api.core.IBackhandPlayer; +import xonin.backhand.api.core.IOffhandInventory; + +@Mixin(EntityPlayer.class) +public abstract class MixinEntityPlayerClient extends EntityLivingBase implements IBackhandPlayer { + + @Shadow + public InventoryPlayer inventory; + + public MixinEntityPlayerClient(World p_i1594_1_) { + super(p_i1594_1_); + } + + @Override + public float getSwingProgress(float p_70678_1_) { + if (inventory.currentItem == IOffhandInventory.OFFHAND_HOTBAR_SLOT) { + return getOffSwingProgress(p_70678_1_); + } + return super.getSwingProgress(p_70678_1_); + } +} From bccda1f4388e11ebc5e9519e506eb5b24e5807dd Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:44:33 +0100 Subject: [PATCH 17/38] fix a few issues with the offhand swing --- .../backhand/client/ClientEventHandler.java | 2 - .../java/xonin/backhand/mixins/Mixins.java | 3 +- .../early/minecraft/MixinEntityPlayer.java | 42 +++++++++++++------ .../early/minecraft/MixinEntityRenderer.java | 26 ++++++++++++ .../early/minecraft/MixinItemRenderer.java | 26 +----------- .../early/minecraft/MixinMinecraft.java | 26 ++++++++---- .../minecraft/MixinNetHandlerPlayClient.java | 16 +++++++ 7 files changed, 94 insertions(+), 47 deletions(-) create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityRenderer.java diff --git a/src/main/java/xonin/backhand/client/ClientEventHandler.java b/src/main/java/xonin/backhand/client/ClientEventHandler.java index ae2e61e..d16b492 100644 --- a/src/main/java/xonin/backhand/client/ClientEventHandler.java +++ b/src/main/java/xonin/backhand/client/ClientEventHandler.java @@ -37,7 +37,6 @@ @EventBusSubscriber(side = Side.CLIENT) public class ClientEventHandler { - public static EntityPlayer renderingPlayer; public static boolean prevInvTweaksAutoRefill; public static boolean prevInvTweaksBreakRefill; @@ -150,7 +149,6 @@ public static void onRenderHand(RenderHandEvent event) { @SubscribeEvent(priority = EventPriority.LOW) public static void renderPlayerLeftItemUsage(RenderLivingEvent.Pre event) { if (event.entity instanceof EntityPlayer entityPlayer) { - renderingPlayer = entityPlayer; ItemStack offhand = BackhandUtils.getOffhandItem(entityPlayer); if (offhand != null && event.renderer instanceof RenderPlayer renderer) { renderer.modelArmorChestplate.heldItemLeft = renderer.modelArmor.heldItemLeft = renderer.modelBipedMain.heldItemLeft = 1; diff --git a/src/main/java/xonin/backhand/mixins/Mixins.java b/src/main/java/xonin/backhand/mixins/Mixins.java index 185ba45..41beef5 100644 --- a/src/main/java/xonin/backhand/mixins/Mixins.java +++ b/src/main/java/xonin/backhand/mixins/Mixins.java @@ -34,7 +34,8 @@ public enum Mixins { "minecraft.MixinModelBiped", "minecraft.MixinNetHandlerPlayClient", "minecraft.MixinMinecraft", - "minecraft.MixinGuiInventory") + "minecraft.MixinGuiInventory", + "minecraft.MixinEntityRenderer") .setPhase(Phase.EARLY) .setSide(Side.CLIENT) .addTargetedMod(TargetedMod.VANILLA)), diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java index bfd108c..e87de91 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java @@ -6,7 +6,9 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.network.play.server.S0BPacketAnimation; import net.minecraft.world.World; +import net.minecraft.world.WorldServer; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -75,15 +77,10 @@ private MixinEntityPlayer(World p_i1594_1_) { method = "setItemInUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;setEating(Z)V")) private void backhand$setItemInUse(ItemStack p_71008_1_, int p_71008_2_, CallbackInfo ci) { - EntityPlayer player = (EntityPlayer) (Object) this; - if (Objects.equals(p_71008_1_, BackhandUtils.getOffhandItem(player))) { - Backhand.packetHandler - .sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, true).generatePacket()); - setUsingOffhand(true); + if (Objects.equals(p_71008_1_, BackhandUtils.getOffhandItem((EntityPlayer) (Object) this))) { + backhand$updateOffhandUse(true); } else if (isUsingOffhand()) { - Backhand.packetHandler - .sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, false).generatePacket()); - setUsingOffhand(false); + backhand$updateOffhandUse(false); } } @@ -91,11 +88,8 @@ private MixinEntityPlayer(World p_i1594_1_) { method = "clearItemInUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;setEating(Z)V")) private void backhand$clearOffhand(CallbackInfo ci) { - EntityPlayer player = (EntityPlayer) (Object) this; if (isUsingOffhand()) { - Backhand.packetHandler - .sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, false).generatePacket()); - setUsingOffhand(false); + backhand$updateOffhandUse(false); } } @@ -116,6 +110,14 @@ private MixinEntityPlayer(World p_i1594_1_) { this.backhand$offHandSwingProgress = (float) this.backhand$offHandSwingProgressInt / (float) var1; } + @Unique + private void backhand$updateOffhandUse(boolean state) { + EntityPlayer player = (EntityPlayer) (Object) this; + Backhand.packetHandler + .sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, state).generatePacket()); + setUsingOffhand(state); + } + @Override public void swingItem() { if (inventory.currentItem == IOffhandInventory.OFFHAND_HOTBAR_SLOT) { @@ -127,11 +129,27 @@ public void swingItem() { @Override public void swingOffItem() { + EntityPlayer player = (EntityPlayer) (Object) this; + ItemStack stack = BackhandUtils.getOffhandItem(player); + if (stack != null && stack.getItem() != null + && BackhandUtils.useOffhandItem( + player, + false, + () -> stack.getItem() + .onEntitySwing(player, stack))) { + return; + } + if (!this.backhand$isOffHandSwingInProgress || this.backhand$offHandSwingProgressInt >= this.getArmSwingAnimationEnd() / 2 || this.backhand$offHandSwingProgressInt < 0) { this.backhand$offHandSwingProgressInt = -1; this.backhand$isOffHandSwingInProgress = true; + + if (worldObj instanceof WorldServer world) { + world.getEntityTracker() + .func_151247_a(this, new S0BPacketAnimation(this, IOffhandInventory.OFFHAND_HOTBAR_SLOT)); + } } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityRenderer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityRenderer.java new file mode 100644 index 0000000..1263a1a --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityRenderer.java @@ -0,0 +1,26 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.EntityRenderer; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import xonin.backhand.api.core.BackhandUtils; +import xonin.backhand.client.utils.BackhandRenderHelper; + +@Mixin(EntityRenderer.class) +public class MixinEntityRenderer { + + @Inject( + method = "updateRenderer", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/ItemRenderer;updateEquippedItem()V")) + private void backhand$updateOffhandItem(CallbackInfo ci) { + BackhandUtils.useOffhandItem( + Minecraft.getMinecraft().thePlayer, + false, + BackhandRenderHelper.itemRenderer::updateEquippedItem); + } +} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java index 084b2f7..bb7efeb 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java @@ -8,14 +8,13 @@ import org.lwjgl.opengl.GL11; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.client.ClientEventHandler; +import xonin.backhand.api.core.IOffhandInventory; import xonin.backhand.client.utils.BackhandRenderHelper; import xonin.backhand.utils.BackhandConfig; import xonin.backhand.utils.BackhandConfigClient; @@ -23,18 +22,10 @@ @Mixin(ItemRenderer.class) public abstract class MixinItemRenderer { - @Unique - private static boolean backhand$isUpdatingOffhand; - - @Unique - private static boolean backhand$isRenderingOffhand; - @Inject(method = "renderItemInFirstPerson", at = @At("RETURN")) private void backhand$renderItemInFirstPerson(float frame, CallbackInfo ci) { - if (backhand$isRenderingOffhand) return; - EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer; - ClientEventHandler.renderingPlayer = player; + if (player.inventory.currentItem == IOffhandInventory.OFFHAND_HOTBAR_SLOT) return; ItemStack mainhandItem = player.getCurrentEquippedItem(); ItemStack offhandItem = BackhandUtils.getOffhandItem(player); @@ -50,7 +41,6 @@ public abstract class MixinItemRenderer { } BackhandRenderHelper.firstPersonFrame = frame; - backhand$isRenderingOffhand = true; GL11.glEnable(GL11.GL_CULL_FACE); GL11.glCullFace(GL11.GL_FRONT); GL11.glPushMatrix(); @@ -63,18 +53,6 @@ public abstract class MixinItemRenderer { .useOffhandItem(player, false, () -> BackhandRenderHelper.itemRenderer.renderItemInFirstPerson(frame)); GL11.glPopMatrix(); GL11.glCullFace(GL11.GL_BACK); - backhand$isRenderingOffhand = false; - } - - @Inject(method = "updateEquippedItem", at = @At("RETURN")) - private void backhand$updateOffhandItem(CallbackInfo ci) { - if (backhand$isUpdatingOffhand) return; - backhand$isUpdatingOffhand = true; - BackhandUtils.useOffhandItem( - Minecraft.getMinecraft().thePlayer, - false, - BackhandRenderHelper.itemRenderer::updateEquippedItem); - backhand$isUpdatingOffhand = false; } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index b920207..a0c8968 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -28,6 +28,7 @@ import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IBackhandPlayer; import xonin.backhand.api.core.IOffhandInventory; +import xonin.backhand.client.utils.BackhandRenderHelper; import xonin.backhand.utils.BackhandConfig; @Mixin(Minecraft.class) @@ -93,7 +94,8 @@ private void func_147121_ag() { if (!theWorld.getBlock(x, y, z) .isAir(theWorld, x, y, z)) { - int l = mainHandItem != null ? mainHandItem.stackSize : 0; + int mainOriginalSize = mainHandItem != null ? mainHandItem.stackSize : 0; + int offhandOriginalSize = offhandItem != null ? offhandItem.stackSize : 0; boolean result = !net.minecraftforge.event.ForgeEventFactory .onPlayerInteract( @@ -145,16 +147,20 @@ private void func_147121_ag() { if (mainHandItem != null) { if (mainHandItem.stackSize == 0) { thePlayer.setCurrentItemOrArmor(0, null); - } else if (mainHandItem.stackSize != l || playerController.isInCreativeMode()) { - entityRenderer.itemRenderer.resetEquippedProgress(); - } + } else + if (mainHandItem.stackSize != mainOriginalSize || playerController.isInCreativeMode()) { + entityRenderer.itemRenderer.resetEquippedProgress(); + } } if (offhandItem != null) { if (offhandItem.stackSize == 0) { thePlayer.inventory .setInventorySlotContents(IOffhandInventory.OFFHAND_HOTBAR_SLOT, null); - } + } else if (offhandItem.stackSize != offhandOriginalSize + || playerController.isInCreativeMode()) { + BackhandRenderHelper.itemRenderer.resetEquippedProgress(); + } } } } @@ -188,13 +194,16 @@ private void func_147121_ag() { 0, theWorld); if (!MinecraftForge.EVENT_BUS.post(useItemEvent)) { - playerController.sendUseItem(thePlayer, theWorld, offhandItem); + if (playerController.sendUseItem(thePlayer, theWorld, offhandItem)) { + BackhandRenderHelper.itemRenderer.resetEquippedProgress(); + } + return thePlayer.getItemInUse() != null; } return false; }); - if (trySecondaryAction && offhandItem.getItemUseAction() == EnumAction.none) { + if (trySecondaryAction) { switch (objectMouseOver.typeOfHit) { case ENTITY -> { if (BackhandConfig.OffhandAttack) { @@ -205,7 +214,8 @@ private void func_147121_ag() { } } case BLOCK -> { - if (BackhandConfig.OffhandBreakBlocks) { + if (BackhandConfig.OffhandBreakBlocks + && offhandItem.getItemUseAction() == EnumAction.none) { BackhandUtils.useOffhandItem(thePlayer, () -> { backhand$breakBlockTimer = 5; playerController.clickBlock( diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java index 872b730..ffda6d9 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java @@ -1,12 +1,18 @@ package xonin.backhand.mixins.early.minecraft; import net.minecraft.client.network.NetHandlerPlayClient; +import net.minecraft.entity.Entity; +import net.minecraft.network.play.server.S0BPacketAnimation; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.sugar.Local; +import xonin.backhand.api.core.IBackhandPlayer; import xonin.backhand.api.core.IOffhandInventory; @Mixin(NetHandlerPlayClient.class) @@ -22,4 +28,14 @@ public abstract class MixinNetHandlerPlayClient { // return a valid int e.g. between 0 and < 9 return IOffhandInventory.isValidSwitch(original) ? 0 : -1; } + + @Inject( + method = "handleAnimation", + at = @At(value = "INVOKE", target = "Lnet/minecraft/network/play/server/S0BPacketAnimation;func_148977_d()I")) + private void backhand$handleOffhandSwing(S0BPacketAnimation packetIn, CallbackInfo ci, @Local Entity entity) { + if (!(entity instanceof IBackhandPlayer player)) return; + if (packetIn.func_148978_c() == IOffhandInventory.OFFHAND_HOTBAR_SLOT) { + player.swingOffItem(); + } + } } From a4d42a0476b697cf06b46e186a661abaf26cfde1 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:53:05 +0100 Subject: [PATCH 18/38] reduce some CallbackInfo allocations --- .../early/minecraft/MixinModelBiped.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java index 4ba3936..52c89ac 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java @@ -8,13 +8,17 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.MathHelper; +import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import xonin.backhand.api.core.BackhandUtils; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.sugar.Local; + +import xonin.backhand.api.core.IBackhandPlayer; import xonin.backhand.client.utils.BackhandRenderHelper; @Mixin(ModelBiped.class) @@ -35,19 +39,16 @@ public abstract class MixinModelBiped extends ModelBase { BackhandRenderHelper.moveOffHandArm(entity, (ModelBiped) (Object) this, f6); } - @Inject( + @ModifyExpressionValue( method = "setRotationAngles", at = @At( value = "FIELD", - target = "Lnet/minecraft/client/model/ModelBiped;aimedBow:Z", - shift = At.Shift.BY, - by = 2), - cancellable = true) - private void backhand$moveOffhandAimedBow(float f1, float f2, float f3, float f4, float f5, float f6, Entity entity, - CallbackInfo ci) { + opcode = Opcodes.GETFIELD, + target = "Lnet/minecraft/client/model/ModelBiped;aimedBow:Z")) + private boolean backhand$moveOffhandAimedBow(boolean original, @Local(name = "p_78087_3_") float f3, + @Local(argsOnly = true) Entity entity) { if (entity instanceof EntityPlayer player && entity == Minecraft.getMinecraft().thePlayer - && BackhandUtils.getOffhandItem(player) != null - && player.getItemInUse() == BackhandUtils.getOffhandItem(player)) { + && ((IBackhandPlayer) player).isUsingOffhand()) { bipedLeftArm.rotateAngleZ = 0.0F; bipedRightArm.rotateAngleZ = 0.0F; bipedLeftArm.rotateAngleY = 0.1F + bipedHead.rotateAngleY; @@ -60,8 +61,9 @@ public abstract class MixinModelBiped extends ModelBase { bipedRightArm.rotateAngleZ += MathHelper.cos(f3 * 0.09F) * 0.05F + 0.05F; bipedLeftArm.rotateAngleX -= MathHelper.sin(f3 * 0.067F) * 0.05F; bipedRightArm.rotateAngleX += MathHelper.sin(f3 * 0.067F) * 0.05F; - ci.cancel(); + return false; } + return original; } } From c0827957c7807ebaa880fa1843085a249ca95d27 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Fri, 17 Jan 2025 00:07:40 +0100 Subject: [PATCH 19/38] more accurate name --- .../java/xonin/backhand/api/core/IBackhandPlayer.java | 4 ++-- .../early/minecraft/MixinEntityOtherPlayerMP.java | 2 +- .../mixins/early/minecraft/MixinEntityPlayer.java | 10 +++++----- .../backhand/mixins/early/minecraft/MixinItemBow.java | 2 +- .../mixins/early/minecraft/MixinModelBiped.java | 2 +- .../xonin/backhand/packet/OffhandSyncOffhandUse.java | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java b/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java index f9112c3..b353796 100644 --- a/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java +++ b/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java @@ -17,8 +17,8 @@ public interface IBackhandPlayer { */ float getOffSwingProgress(float frame); - void setUsingOffhand(boolean usingOffhand); + void setOffhandItemInUse(boolean usingOffhand); - boolean isUsingOffhand(); + boolean isOffhandItemInUse(); } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java index ffc9c73..1702f1f 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityOtherPlayerMP.java @@ -30,7 +30,7 @@ private MixinEntityOtherPlayerMP(World p_i45074_1_, GameProfile p_i45074_2_) { args = "array=get", target = "Lnet/minecraft/entity/player/InventoryPlayer;mainInventory:[Lnet/minecraft/item/ItemStack;")) private ItemStack backhand$isItemInUseHook(ItemStack[] array, int index) { - if (isUsingOffhand()) return BackhandUtils.getOffhandItem(this); + if (isOffhandItemInUse()) return BackhandUtils.getOffhandItem(this); return array[index]; } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java index e87de91..7c83cc1 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java @@ -79,7 +79,7 @@ private MixinEntityPlayer(World p_i1594_1_) { private void backhand$setItemInUse(ItemStack p_71008_1_, int p_71008_2_, CallbackInfo ci) { if (Objects.equals(p_71008_1_, BackhandUtils.getOffhandItem((EntityPlayer) (Object) this))) { backhand$updateOffhandUse(true); - } else if (isUsingOffhand()) { + } else if (isOffhandItemInUse()) { backhand$updateOffhandUse(false); } } @@ -88,7 +88,7 @@ private MixinEntityPlayer(World p_i1594_1_) { method = "clearItemInUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;setEating(Z)V")) private void backhand$clearOffhand(CallbackInfo ci) { - if (isUsingOffhand()) { + if (isOffhandItemInUse()) { backhand$updateOffhandUse(false); } } @@ -115,7 +115,7 @@ private MixinEntityPlayer(World p_i1594_1_) { EntityPlayer player = (EntityPlayer) (Object) this; Backhand.packetHandler .sendPacketToAllTracking(player, new OffhandSyncOffhandUse(player, state).generatePacket()); - setUsingOffhand(state); + setOffhandItemInUse(state); } @Override @@ -164,12 +164,12 @@ public float getOffSwingProgress(float frame) { } @Override - public void setUsingOffhand(boolean usingOffhand) { + public void setOffhandItemInUse(boolean usingOffhand) { this.backhand$isUsingOffhand = usingOffhand; } @Override - public boolean isUsingOffhand() { + public boolean isOffhandItemInUse() { return this.backhand$isUsingOffhand; } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemBow.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemBow.java index 91c5ab7..0e80e8d 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemBow.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemBow.java @@ -23,7 +23,7 @@ public abstract class MixinItemBow extends Item { at = @At( value = "INVOKE", target = "Lnet/minecraft/entity/player/InventoryPlayer;hasItem(Lnet/minecraft/item/Item;)Z")) - private boolean backhand$cancelAnimation(boolean original, @Local(argsOnly = true) EntityPlayer player) { + private boolean backhand$checkOffhand(boolean original, @Local(argsOnly = true) EntityPlayer player) { if (!original) { ItemStack offhand = BackhandUtils.getOffhandItem(player); return offhand != null diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java index 52c89ac..3868a12 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java @@ -48,7 +48,7 @@ public abstract class MixinModelBiped extends ModelBase { private boolean backhand$moveOffhandAimedBow(boolean original, @Local(name = "p_78087_3_") float f3, @Local(argsOnly = true) Entity entity) { if (entity instanceof EntityPlayer player && entity == Minecraft.getMinecraft().thePlayer - && ((IBackhandPlayer) player).isUsingOffhand()) { + && ((IBackhandPlayer) player).isOffhandItemInUse()) { bipedLeftArm.rotateAngleZ = 0.0F; bipedRightArm.rotateAngleZ = 0.0F; bipedLeftArm.rotateAngleY = 0.1F + bipedHead.rotateAngleY; diff --git a/src/main/java/xonin/backhand/packet/OffhandSyncOffhandUse.java b/src/main/java/xonin/backhand/packet/OffhandSyncOffhandUse.java index 49e986f..37c1734 100644 --- a/src/main/java/xonin/backhand/packet/OffhandSyncOffhandUse.java +++ b/src/main/java/xonin/backhand/packet/OffhandSyncOffhandUse.java @@ -23,7 +23,7 @@ public OffhandSyncOffhandUse() {} public void process(ByteBuf inputStream, EntityPlayer player) { this.player = player.worldObj.getPlayerEntityByName(ByteBufUtils.readUTF8String(inputStream)); if (this.player == null) return; - ((IBackhandPlayer) this.player).setUsingOffhand(inputStream.readBoolean()); + ((IBackhandPlayer) this.player).setOffhandItemInUse(inputStream.readBoolean()); } @Override From 247a0768be8b21753ce8e1a9c15995418407305b Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Fri, 17 Jan 2025 00:11:09 +0100 Subject: [PATCH 20/38] yeet --- .../backhand/api/core/BackhandTranslator.java | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 src/main/java/xonin/backhand/api/core/BackhandTranslator.java diff --git a/src/main/java/xonin/backhand/api/core/BackhandTranslator.java b/src/main/java/xonin/backhand/api/core/BackhandTranslator.java deleted file mode 100644 index 0c6845d..0000000 --- a/src/main/java/xonin/backhand/api/core/BackhandTranslator.java +++ /dev/null @@ -1,32 +0,0 @@ -package xonin.backhand.api.core; - -import net.minecraft.launchwrapper.Launch; - -public class BackhandTranslator { - - static { - obfuscatedEnv = (Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment"); - } - - public static boolean obfuscatedEnv; - - public static String getMapedClassName(String className) { - return "net/minecraft/" + className.replace(".", "/"); - } - - /** @deprecated */ - @Deprecated - public static String getMapedMethodName(String className, String methodName, String devName) { - return getMapedMethodName(methodName, devName); - } - - public static String getMapedMethodName(String methodName, String devName) { - return obfuscatedEnv ? devName : methodName; - } - - /** @deprecated */ - @Deprecated - public static String getMapedMethodDesc(String className, String methodName, String devDesc) { - return devDesc; - } -} From b0c585e330698416ec4335709fe616fd40b91111 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Fri, 17 Jan 2025 00:24:23 +0100 Subject: [PATCH 21/38] fix obf crash --- .../xonin/backhand/mixins/early/minecraft/MixinModelBiped.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java index 3868a12..ed44b62 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinModelBiped.java @@ -45,7 +45,7 @@ public abstract class MixinModelBiped extends ModelBase { value = "FIELD", opcode = Opcodes.GETFIELD, target = "Lnet/minecraft/client/model/ModelBiped;aimedBow:Z")) - private boolean backhand$moveOffhandAimedBow(boolean original, @Local(name = "p_78087_3_") float f3, + private boolean backhand$moveOffhandAimedBow(boolean original, @Local(argsOnly = true, ordinal = 2) float f3, @Local(argsOnly = true) Entity entity) { if (entity instanceof EntityPlayer player && entity == Minecraft.getMinecraft().thePlayer && ((IBackhandPlayer) player).isOffhandItemInUse()) { From c819fb17280df429a9e9f550b06ac1961242190f Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Fri, 17 Jan 2025 15:00:19 +0100 Subject: [PATCH 22/38] compat with double wide surprise & other inv extending mods --- src/main/java/xonin/backhand/CommonProxy.java | 11 +++- .../backhand/api/core/BackhandUtils.java | 28 +++++----- .../backhand/api/core/IBackhandPlayer.java | 3 ++ .../backhand/api/core/IOffhandInventory.java | 9 ++-- .../backhand/client/ClientEventHandler.java | 10 ++-- .../early/minecraft/MixinContainerPlayer.java | 4 +- .../early/minecraft/MixinEntityPlayer.java | 15 ++++-- .../minecraft/MixinEntityPlayerClient.java | 3 +- .../early/minecraft/MixinInventoryPlayer.java | 53 ++++++++++++++----- .../early/minecraft/MixinItemRenderer.java | 3 +- .../early/minecraft/MixinItemStack.java | 3 +- .../minecraft/MixinNetHandlerPlayClient.java | 9 +++- .../minecraft/MixinNetHandlerPlayServer.java | 9 ++-- .../packet/OffhandSwapClientPacket.java | 2 +- .../backhand/packet/OffhandSwapPacket.java | 2 +- src/main/java/xonin/backhand/utils/Mods.java | 3 +- 16 files changed, 108 insertions(+), 59 deletions(-) diff --git a/src/main/java/xonin/backhand/CommonProxy.java b/src/main/java/xonin/backhand/CommonProxy.java index e279d30..7454f82 100644 --- a/src/main/java/xonin/backhand/CommonProxy.java +++ b/src/main/java/xonin/backhand/CommonProxy.java @@ -1,8 +1,12 @@ package xonin.backhand; +import javax.annotation.Nullable; + +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; +import net.minecraftforge.common.util.FakePlayer; import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber; @@ -34,9 +38,14 @@ public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) { @SubscribeEvent public static void addTracking(net.minecraftforge.event.entity.player.PlayerEvent.StartTracking event) { - if (event.entityPlayer instanceof EntityPlayerMP playerMP && BackhandUtils.isValidPlayer(event.target)) { + if (event.entityPlayer instanceof EntityPlayerMP playerMP && isValidPlayer(event.target)) { Backhand.packetHandler .sendPacketToPlayer(new OffhandSyncItemPacket((EntityPlayer) event.target).generatePacket(), playerMP); } } + + private static boolean isValidPlayer(@Nullable Entity entity) { + return entity instanceof EntityPlayerMP playerMP + && !(entity instanceof FakePlayer || playerMP.playerNetServerHandler == null); + } } diff --git a/src/main/java/xonin/backhand/api/core/BackhandUtils.java b/src/main/java/xonin/backhand/api/core/BackhandUtils.java index 8f1d145..766104c 100644 --- a/src/main/java/xonin/backhand/api/core/BackhandUtils.java +++ b/src/main/java/xonin/backhand/api/core/BackhandUtils.java @@ -3,34 +3,29 @@ import java.util.function.BooleanSupplier; import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import net.minecraft.client.Minecraft; -import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; -import net.minecraftforge.common.util.FakePlayer; /** * Store commonly used method, mostly for the {@link EntityPlayer} {@link ItemStack}s management */ -public class BackhandUtils { +@ParametersAreNonnullByDefault +public final class BackhandUtils { public static void swapOffhandItem(EntityPlayer player) { + ItemStack mainHand = player.getCurrentEquippedItem(); player.setCurrentItemOrArmor(0, BackhandUtils.getOffhandItem(player)); - BackhandUtils.setPlayerOffhandItem(player, BackhandUtils.getOffhandItem(player)); + BackhandUtils.setPlayerOffhandItem(player, mainHand); } - public static void setPlayerCurrentItem(EntityPlayer player, ItemStack stack) { - player.inventory.setInventorySlotContents(player.inventory.currentItem, stack); - } - - public static void setPlayerOffhandItem(EntityPlayer player, ItemStack stack) { + public static void setPlayerOffhandItem(EntityPlayer player, @Nullable ItemStack stack) { ((IOffhandInventory) player.inventory).backhand$setOffhandItem(stack); } public static @Nullable ItemStack getOffhandItem(EntityPlayer player) { - if (player instanceof FakePlayer) return null; return ((IOffhandInventory) player.inventory).backhand$getOffhandItem(); } @@ -51,7 +46,7 @@ public static boolean useOffhandItem(EntityPlayer player, BooleanSupplier action public static boolean useOffhandItem(EntityPlayer player, boolean syncSlot, BooleanSupplier action) { int oldSlot = player.inventory.currentItem; - player.inventory.currentItem = IOffhandInventory.OFFHAND_HOTBAR_SLOT; + player.inventory.currentItem = ((IOffhandInventory) player.inventory).backhand$getOffhandSlot(); boolean result = action.getAsBoolean(); player.inventory.currentItem = oldSlot; if (syncSlot && player.worldObj.isRemote) { @@ -60,8 +55,11 @@ public static boolean useOffhandItem(EntityPlayer player, boolean syncSlot, Bool return result; } - public static boolean isValidPlayer(Entity entity) { - return entity instanceof EntityPlayerMP playerMP - && !(entity instanceof FakePlayer || playerMP.playerNetServerHandler == null); + public static boolean isUsingOffhand(EntityPlayer player) { + return ((IBackhandPlayer) player).isUsingOffhand(); + } + + public static int getOffhandSlot(EntityPlayer player) { + return ((IOffhandInventory) player.inventory).backhand$getOffhandSlot(); } } diff --git a/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java b/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java index b353796..f536948 100644 --- a/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java +++ b/src/main/java/xonin/backhand/api/core/IBackhandPlayer.java @@ -5,6 +5,7 @@ * * @author GotoLink */ + public interface IBackhandPlayer { /** @@ -21,4 +22,6 @@ public interface IBackhandPlayer { boolean isOffhandItemInUse(); + boolean isUsingOffhand(); + } diff --git a/src/main/java/xonin/backhand/api/core/IOffhandInventory.java b/src/main/java/xonin/backhand/api/core/IOffhandInventory.java index dd19da6..2830433 100644 --- a/src/main/java/xonin/backhand/api/core/IOffhandInventory.java +++ b/src/main/java/xonin/backhand/api/core/IOffhandInventory.java @@ -1,17 +1,18 @@ package xonin.backhand.api.core; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; public interface IOffhandInventory { - int OFFHAND_HOTBAR_SLOT = 36; - ItemStack backhand$getOffhandItem(); void backhand$setOffhandItem(ItemStack stack); - static boolean isValidSwitch(int id) { - return (id >= 0 && id < InventoryPlayer.getHotbarSize()) || id == OFFHAND_HOTBAR_SLOT; + int backhand$getOffhandSlot(); + + static boolean isValidSwitch(int id, EntityPlayer player) { + return (id >= 0 && id < InventoryPlayer.getHotbarSize()) || id == BackhandUtils.getOffhandSlot(player); } } diff --git a/src/main/java/xonin/backhand/client/ClientEventHandler.java b/src/main/java/xonin/backhand/client/ClientEventHandler.java index d16b492..fa9d1ab 100644 --- a/src/main/java/xonin/backhand/client/ClientEventHandler.java +++ b/src/main/java/xonin/backhand/client/ClientEventHandler.java @@ -1,5 +1,7 @@ package xonin.backhand.client; +import static xonin.backhand.utils.Mods.DOUBLE_WIDE_SURPRISE; + import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.gui.GuiIngame; @@ -93,14 +95,14 @@ private static void renderHotbar(GuiIngame gui, int width, int height, float par GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); mc.renderEngine.bindTexture(new ResourceLocation("textures/gui/widgets.png")); - gui.drawTexturedModalRect(width / 2 - 125, height - 22, 0, 0, 11, 22); - gui.drawTexturedModalRect(width / 2 - 125 + 11, height - 22, 182 - 11, 0, 11, 22); - + int offset = DOUBLE_WIDE_SURPRISE.isLoaded() ? 212 : 125; + gui.drawTexturedModalRect(width / 2 - offset, height - 22, 0, 0, 11, 22); + gui.drawTexturedModalRect(width / 2 - offset + 11, height - 22, 182 - 11, 0, 11, 22); GL11.glDisable(GL11.GL_BLEND); GL11.glEnable(GL12.GL_RESCALE_NORMAL); RenderHelper.enableGUIStandardItemLighting(); - int x = width / 2 - 122; + int x = width / 2 - offset + 3; int z = height - 16 - 3; renderOffhandInventorySlot(x, z, partialTicks); diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java index 2e36ab1..a6d22bd 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinContainerPlayer.java @@ -11,7 +11,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import xonin.backhand.api.core.IOffhandInventory; +import xonin.backhand.api.core.BackhandUtils; @Mixin(ContainerPlayer.class) public abstract class MixinContainerPlayer extends Container { @@ -19,6 +19,6 @@ public abstract class MixinContainerPlayer extends Container { @Inject(method = "", at = @At(value = "TAIL")) private void backhand2$addOffhandSlot(InventoryPlayer p_i1819_1_, boolean p_i1819_2_, EntityPlayer p_i1819_3_, CallbackInfo ci) { - addSlotToContainer(new Slot(p_i1819_1_, IOffhandInventory.OFFHAND_HOTBAR_SLOT, 80, 65)); + addSlotToContainer(new Slot(p_i1819_1_, BackhandUtils.getOffhandSlot(p_i1819_3_), 80, 65)); } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java index 7c83cc1..21a5091 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java @@ -43,7 +43,7 @@ public abstract class MixinEntityPlayer extends EntityLivingBase implements IBac @Unique private boolean backhand$isOffHandSwingInProgress = false; @Unique - private boolean backhand$isUsingOffhand = false; + private boolean backhand$isOffhandItemInUs = false; private MixinEntityPlayer(World p_i1594_1_) { super(p_i1594_1_); @@ -120,7 +120,7 @@ private MixinEntityPlayer(World p_i1594_1_) { @Override public void swingItem() { - if (inventory.currentItem == IOffhandInventory.OFFHAND_HOTBAR_SLOT) { + if (isUsingOffhand()) { this.swingOffItem(); } else { super.swingItem(); @@ -148,7 +148,7 @@ public void swingOffItem() { if (worldObj instanceof WorldServer world) { world.getEntityTracker() - .func_151247_a(this, new S0BPacketAnimation(this, IOffhandInventory.OFFHAND_HOTBAR_SLOT)); + .func_151247_a(this, new S0BPacketAnimation(this, 99)); } } } @@ -165,11 +165,16 @@ public float getOffSwingProgress(float frame) { @Override public void setOffhandItemInUse(boolean usingOffhand) { - this.backhand$isUsingOffhand = usingOffhand; + this.backhand$isOffhandItemInUs = usingOffhand; } @Override public boolean isOffhandItemInUse() { - return this.backhand$isUsingOffhand; + return this.backhand$isOffhandItemInUs; + } + + @Override + public boolean isUsingOffhand() { + return inventory.currentItem == ((IOffhandInventory) inventory).backhand$getOffhandSlot(); } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java index cbbca32..6d1c8b9 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayerClient.java @@ -9,7 +9,6 @@ import org.spongepowered.asm.mixin.Shadow; import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.api.core.IOffhandInventory; @Mixin(EntityPlayer.class) public abstract class MixinEntityPlayerClient extends EntityLivingBase implements IBackhandPlayer { @@ -23,7 +22,7 @@ public MixinEntityPlayerClient(World p_i1594_1_) { @Override public float getSwingProgress(float p_70678_1_) { - if (inventory.currentItem == IOffhandInventory.OFFHAND_HOTBAR_SLOT) { + if (isUsingOffhand()) { return getOffSwingProgress(p_70678_1_); } return super.getSwingProgress(p_70678_1_); diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java index ec3c769..055efd5 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java @@ -3,16 +3,18 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagList; +import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.llamalad7.mixinextras.injector.ModifyReturnValue; -import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IOffhandInventory; import xonin.backhand.utils.BackhandConfig; @@ -26,24 +28,46 @@ public abstract class MixinInventoryPlayer implements IOffhandInventory { public EntityPlayer player; @Shadow - public ItemStack[] mainInventory = new ItemStack[37]; + public ItemStack[] mainInventory; - @ModifyConstant(method = "readFromNBT", constant = @Constant(intValue = 36)) - private int backhand$correctInventorySize(int constant) { - return 37; + @Unique + private int backhand$offhandSlot; + + @Inject( + method = "readFromNBT", + at = @At( + value = "FIELD", + opcode = Opcodes.PUTFIELD, + target = "Lnet/minecraft/entity/player/InventoryPlayer;mainInventory:[Lnet/minecraft/item/ItemStack;", + shift = At.Shift.AFTER)) + private void backhand$addOffhandSlot(NBTTagList p_70443_1_, CallbackInfo ci) { + backhand$offhandSlot = mainInventory.length; + mainInventory = new ItemStack[mainInventory.length + 1]; + } + + @Inject( + method = "", + at = @At( + value = "FIELD", + opcode = Opcodes.PUTFIELD, + target = "Lnet/minecraft/entity/player/InventoryPlayer;mainInventory:[Lnet/minecraft/item/ItemStack;", + shift = At.Shift.AFTER)) + private void backhand$addOffhandSlot(EntityPlayer p_i1750_1_, CallbackInfo ci) { + backhand$offhandSlot = mainInventory.length; + mainInventory = new ItemStack[mainInventory.length + 1]; } @ModifyReturnValue(method = "getCurrentItem", at = @At("RETURN")) private ItemStack backhand$getOffhandItem(ItemStack original) { - if (currentItem == OFFHAND_HOTBAR_SLOT) { - return BackhandUtils.getOffhandItem(player); + if (currentItem == backhand$getOffhandSlot()) { + return backhand$getOffhandItem(); } return original; } @ModifyReturnValue(method = "getFirstEmptyStack", at = @At("RETURN")) private int backhand$checkOffhandPickup(int original) { - if (!BackhandConfig.OffhandPickup && original == OFFHAND_HOTBAR_SLOT) { + if (!BackhandConfig.OffhandPickup && original == backhand$getOffhandSlot()) { return -1; } return original; @@ -51,11 +75,16 @@ public abstract class MixinInventoryPlayer implements IOffhandInventory { @Override public ItemStack backhand$getOffhandItem() { - return mainInventory[OFFHAND_HOTBAR_SLOT]; + return mainInventory[backhand$getOffhandSlot()]; } @Override public void backhand$setOffhandItem(ItemStack stack) { - mainInventory[OFFHAND_HOTBAR_SLOT] = stack; + mainInventory[backhand$getOffhandSlot()] = stack; + } + + @Override + public int backhand$getOffhandSlot() { + return backhand$offhandSlot; } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java index bb7efeb..05096fb 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemRenderer.java @@ -14,7 +14,6 @@ import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.api.core.IOffhandInventory; import xonin.backhand.client.utils.BackhandRenderHelper; import xonin.backhand.utils.BackhandConfig; import xonin.backhand.utils.BackhandConfigClient; @@ -25,7 +24,7 @@ public abstract class MixinItemRenderer { @Inject(method = "renderItemInFirstPerson", at = @At("RETURN")) private void backhand$renderItemInFirstPerson(float frame, CallbackInfo ci) { EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer; - if (player.inventory.currentItem == IOffhandInventory.OFFHAND_HOTBAR_SLOT) return; + if (BackhandUtils.isUsingOffhand(player)) return; ItemStack mainhandItem = player.getCurrentEquippedItem(); ItemStack offhandItem = BackhandUtils.getOffhandItem(player); diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java index b02732c..6c243dc 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java @@ -12,7 +12,6 @@ import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.api.core.IOffhandInventory; @Mixin(ItemStack.class) public class MixinItemStack { @@ -24,7 +23,7 @@ public class MixinItemStack { target = "Lnet/minecraft/item/Item;onUpdate(Lnet/minecraft/item/ItemStack;Lnet/minecraft/world/World;Lnet/minecraft/entity/Entity;IZ)V")) private boolean backhand$updateOffhand(Item item, ItemStack stack, World worldIn, Entity entityIn, int index, boolean p_77663_5_) { - if (index == IOffhandInventory.OFFHAND_HOTBAR_SLOT && entityIn instanceof EntityPlayer player) { + if (entityIn instanceof EntityPlayer player && index == BackhandUtils.getOffhandSlot(player)) { BackhandUtils.useOffhandItem(player, () -> item.onUpdate(stack, worldIn, entityIn, index, p_77663_5_)); return false; } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java index ffda6d9..4c8f443 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayClient.java @@ -1,10 +1,12 @@ package xonin.backhand.mixins.early.minecraft; +import net.minecraft.client.Minecraft; import net.minecraft.client.network.NetHandlerPlayClient; import net.minecraft.entity.Entity; import net.minecraft.network.play.server.S0BPacketAnimation; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -18,6 +20,9 @@ @Mixin(NetHandlerPlayClient.class) public abstract class MixinNetHandlerPlayClient { + @Shadow + private Minecraft gameController; + @ModifyExpressionValue( method = "handleHeldItemChange", at = @At( @@ -26,7 +31,7 @@ public abstract class MixinNetHandlerPlayClient { ordinal = 1)) private int backhand$isValidInventorySlot(int original) { // return a valid int e.g. between 0 and < 9 - return IOffhandInventory.isValidSwitch(original) ? 0 : -1; + return IOffhandInventory.isValidSwitch(original, gameController.thePlayer) ? 0 : -1; } @Inject( @@ -34,7 +39,7 @@ public abstract class MixinNetHandlerPlayClient { at = @At(value = "INVOKE", target = "Lnet/minecraft/network/play/server/S0BPacketAnimation;func_148977_d()I")) private void backhand$handleOffhandSwing(S0BPacketAnimation packetIn, CallbackInfo ci, @Local Entity entity) { if (!(entity instanceof IBackhandPlayer player)) return; - if (packetIn.func_148978_c() == IOffhandInventory.OFFHAND_HOTBAR_SLOT) { + if (packetIn.func_148978_c() == 99) { player.swingOffItem(); } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java index eaaebdd..df1b92e 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinNetHandlerPlayServer.java @@ -12,6 +12,7 @@ import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; +import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IOffhandInventory; import xonin.backhand.utils.BackhandConfig; @@ -29,7 +30,7 @@ public abstract class MixinNetHandlerPlayServer { ordinal = 1)) private int backhand$isValidInventorySlot(int original) { // return a valid int e.g. between 0 and < 9 - return IOffhandInventory.isValidSwitch(original) ? 0 : -1; + return IOffhandInventory.isValidSwitch(original, playerEntity) ? 0 : -1; } @WrapWithCondition( @@ -38,8 +39,7 @@ public abstract class MixinNetHandlerPlayServer { value = "INVOKE", target = "Lnet/minecraft/server/management/ItemInWorldManager;uncheckedTryHarvestBlock(III)V")) private boolean backhand$playerDigging(ItemInWorldManager instance, int l, int block, int i) { - return BackhandConfig.OffhandBreakBlocks - || playerEntity.inventory.currentItem != IOffhandInventory.OFFHAND_HOTBAR_SLOT; + return BackhandConfig.OffhandBreakBlocks || !BackhandUtils.isUsingOffhand(playerEntity); } @WrapWithCondition( @@ -48,7 +48,6 @@ public abstract class MixinNetHandlerPlayServer { value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayerMP;attackTargetEntityWithCurrentItem(Lnet/minecraft/entity/Entity;)V")) private boolean backhand$checkOffhandAttack(EntityPlayerMP instance, Entity entity) { - return BackhandConfig.OffhandAttack - || playerEntity.inventory.currentItem != IOffhandInventory.OFFHAND_HOTBAR_SLOT; + return BackhandConfig.OffhandAttack || !BackhandUtils.isUsingOffhand(playerEntity); } } diff --git a/src/main/java/xonin/backhand/packet/OffhandSwapClientPacket.java b/src/main/java/xonin/backhand/packet/OffhandSwapClientPacket.java index 887f28a..04cb8eb 100644 --- a/src/main/java/xonin/backhand/packet/OffhandSwapClientPacket.java +++ b/src/main/java/xonin/backhand/packet/OffhandSwapClientPacket.java @@ -33,7 +33,7 @@ public void process(ByteBuf inputStream, EntityPlayer player) { this.player = player.worldObj.getPlayerEntityByName(user); if (this.player != null) { int slot = inputStream.readInt(); - if (IOffhandInventory.isValidSwitch(slot)) this.player.inventory.currentItem = slot; + if (IOffhandInventory.isValidSwitch(slot, this.player)) this.player.inventory.currentItem = slot; BackhandUtils.swapOffhandItem(player); } ClientEventHandler.allowSwap = true; diff --git a/src/main/java/xonin/backhand/packet/OffhandSwapPacket.java b/src/main/java/xonin/backhand/packet/OffhandSwapPacket.java index 1021566..acaba55 100644 --- a/src/main/java/xonin/backhand/packet/OffhandSwapPacket.java +++ b/src/main/java/xonin/backhand/packet/OffhandSwapPacket.java @@ -43,7 +43,7 @@ public void process(ByteBuf inputStream, EntityPlayer player) { || Backhand.isOffhandBlacklisted(offhandItem)) return; BackhandUtils.setPlayerOffhandItem(this.player, this.player.getCurrentEquippedItem()); - BackhandUtils.setPlayerCurrentItem(this.player, offhandItem); + player.setCurrentItemOrArmor(0, offhandItem); Backhand.packetHandler .sendPacketToPlayer(new OffhandSwapClientPacket(this.player).generatePacket(), (EntityPlayerMP) player); } diff --git a/src/main/java/xonin/backhand/utils/Mods.java b/src/main/java/xonin/backhand/utils/Mods.java index 8c40512..ed67e0e 100644 --- a/src/main/java/xonin/backhand/utils/Mods.java +++ b/src/main/java/xonin/backhand/utils/Mods.java @@ -4,7 +4,8 @@ public enum Mods { - INV_TWEAKS("inventorytweaks"),; + INV_TWEAKS("inventorytweaks"), + DOUBLE_WIDE_SURPRISE("dws"),; private final String modId; private Boolean loaded; From 8017460ca2e63f46fc04d76cdbfaab88be85b890 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Fri, 17 Jan 2025 15:01:36 +0100 Subject: [PATCH 23/38] fix offhand being used when placing mainhand blocks --- .../backhand/mixins/early/minecraft/MixinMinecraft.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index a0c8968..34a69bf 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -27,7 +27,6 @@ import xonin.backhand.api.core.BackhandUtils; import xonin.backhand.api.core.IBackhandPlayer; -import xonin.backhand.api.core.IOffhandInventory; import xonin.backhand.client.utils.BackhandRenderHelper; import xonin.backhand.utils.BackhandConfig; @@ -116,7 +115,7 @@ private void func_147121_ag() { z, objectMouseOver.sideHit, objectMouseOver.hitVec)) { - useMainhand = false; + useMainhand = useOffhand = false; thePlayer.swingItem(); } else if (offhandItem != null) { PlayerInteractEvent event = new PlayerInteractEvent( @@ -155,8 +154,7 @@ private void func_147121_ag() { if (offhandItem != null) { if (offhandItem.stackSize == 0) { - thePlayer.inventory - .setInventorySlotContents(IOffhandInventory.OFFHAND_HOTBAR_SLOT, null); + BackhandUtils.setPlayerOffhandItem(thePlayer, null); } else if (offhandItem.stackSize != offhandOriginalSize || playerController.isInCreativeMode()) { BackhandRenderHelper.itemRenderer.resetEquippedProgress(); From 1e1bda933fb070dc99c21a0cf7bd71a934815f9a Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 12:15:04 +0100 Subject: [PATCH 24/38] fix efr totem check --- .../mixins/late/etfuturum/MixinServerEventHandler.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java b/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java index faba504..2551901 100644 --- a/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java +++ b/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java @@ -38,10 +38,8 @@ public abstract class MixinServerEventHandler { if (backhand$skipCheck || !(entity instanceof EntityPlayer player)) return; ItemStack offhand = BackhandUtils.getOffhandItem(player); if (offhand == null) return; - ItemStack mainhand = player.getHeldItem(); Item totem = ModItems.TOTEM_OF_UNDYING.get(); - - if (totem.equals(offhand.getItem()) && (mainhand == null || totem.equals(mainhand.getItem()))) { + if (totem.equals(offhand.getItem())) { ci.cancel(); backhand$skipCheck = true; BackhandUtils.useOffhandItem(player, () -> handleTotemCheck(entity, event)); From 98b75342671365d1646a5d1020792db96aa476ab Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 13:29:21 +0100 Subject: [PATCH 25/38] prioritize offhand block place when holding TiC tools --- .../xonin/backhand/client/ClientProxy.java | 11 ++ .../early/minecraft/MixinMinecraft.java | 131 ++++++++++-------- src/main/java/xonin/backhand/utils/Mods.java | 3 +- 3 files changed, 83 insertions(+), 62 deletions(-) diff --git a/src/main/java/xonin/backhand/client/ClientProxy.java b/src/main/java/xonin/backhand/client/ClientProxy.java index b2212f4..cdefad9 100644 --- a/src/main/java/xonin/backhand/client/ClientProxy.java +++ b/src/main/java/xonin/backhand/client/ClientProxy.java @@ -1,11 +1,16 @@ package xonin.backhand.client; +import java.util.ArrayList; +import java.util.List; + import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; import cpw.mods.fml.client.registry.ClientRegistry; +import tconstruct.library.tools.HarvestTool; import xonin.backhand.CommonProxy; +import xonin.backhand.utils.Mods; public class ClientProxy extends CommonProxy { @@ -14,8 +19,14 @@ public class ClientProxy extends CommonProxy { Keyboard.KEY_F, "key.categories.gameplay"); + public static final List> offhandPriorityItems = new ArrayList<>(); + @Override public void load() { ClientRegistry.registerKeyBinding(swapOffhand); + + if (Mods.TINKERS_CONSTRUCT.isLoaded()) { + offhandPriorityItems.add(HarvestTool.class); + } } } diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index 34a69bf..b677018 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -26,7 +26,7 @@ import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; import xonin.backhand.api.core.BackhandUtils; -import xonin.backhand.api.core.IBackhandPlayer; +import xonin.backhand.client.ClientProxy; import xonin.backhand.client.utils.BackhandRenderHelper; import xonin.backhand.utils.BackhandConfig; @@ -69,8 +69,7 @@ private void func_147121_ag() { rightClickDelayTimer = 4; ItemStack mainHandItem = thePlayer.inventory.getCurrentItem(); ItemStack offhandItem = BackhandUtils.getOffhandItem(thePlayer); - boolean useMainhand = true; - boolean useOffhand = true; + boolean useAction = true; if (objectMouseOver == null) { logger.warn("Null returned as \'hitResult\', this shouldn\'t happen!"); @@ -78,11 +77,11 @@ private void func_147121_ag() { switch (objectMouseOver.typeOfHit) { case ENTITY: if (playerController.interactWithEntitySendPacket(thePlayer, objectMouseOver.entityHit)) { - useMainhand = false; + useAction = false; } else if (BackhandUtils.useOffhandItem( thePlayer, () -> playerController.interactWithEntitySendPacket(thePlayer, objectMouseOver.entityHit))) { - useOffhand = false; + useAction = false; } break; @@ -96,52 +95,7 @@ private void func_147121_ag() { int mainOriginalSize = mainHandItem != null ? mainHandItem.stackSize : 0; int offhandOriginalSize = offhandItem != null ? offhandItem.stackSize : 0; - boolean result = !net.minecraftforge.event.ForgeEventFactory - .onPlayerInteract( - thePlayer, - net.minecraftforge.event.entity.player.PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK, - x, - y, - z, - objectMouseOver.sideHit, - theWorld) - .isCanceled(); - if (result && playerController.onPlayerRightClick( - thePlayer, - theWorld, - mainHandItem, - x, - y, - z, - objectMouseOver.sideHit, - objectMouseOver.hitVec)) { - useMainhand = useOffhand = false; - thePlayer.swingItem(); - } else if (offhandItem != null) { - PlayerInteractEvent event = new PlayerInteractEvent( - thePlayer, - PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK, - x, - y, - z, - objectMouseOver.sideHit, - theWorld); - - if (!MinecraftForge.EVENT_BUS.post(event) && BackhandUtils.useOffhandItem( - thePlayer, - () -> playerController.onPlayerRightClick( - thePlayer, - theWorld, - offhandItem, - x, - y, - z, - objectMouseOver.sideHit, - objectMouseOver.hitVec))) { - useOffhand = false; - ((IBackhandPlayer) thePlayer).swingOffItem(); - } - } + useAction = !backhand$rightClickBlock(mainHandItem, offhandItem); if (mainHandItem != null) { if (mainHandItem.stackSize == 0) { @@ -163,9 +117,7 @@ private void func_147121_ag() { } } - if (useMainhand) { - ItemStack itemstack1 = thePlayer.inventory.getCurrentItem(); - + if (useAction) { boolean result = !net.minecraftforge.event.ForgeEventFactory .onPlayerInteract( thePlayer, @@ -176,13 +128,12 @@ private void func_147121_ag() { -1, theWorld) .isCanceled(); - if (result && itemstack1 != null && playerController.sendUseItem(thePlayer, theWorld, itemstack1)) { + if (result && mainHandItem != null && playerController.sendUseItem(thePlayer, theWorld, mainHandItem)) { entityRenderer.itemRenderer.resetEquippedProgress2(); } - } - if (useOffhand && offhandItem != null && thePlayer.getItemInUse() == null) { - boolean trySecondaryAction = !BackhandUtils.useOffhandItem(thePlayer, () -> { + if (offhandItem == null || thePlayer.getItemInUse() != null) return; + useAction = !BackhandUtils.useOffhandItem(thePlayer, () -> { PlayerInteractEvent useItemEvent = new PlayerInteractEvent( thePlayer, PlayerInteractEvent.Action.RIGHT_CLICK_AIR, @@ -201,7 +152,7 @@ private void func_147121_ag() { return false; }); - if (trySecondaryAction) { + if (useAction) { switch (objectMouseOver.typeOfHit) { case ENTITY -> { if (BackhandConfig.OffhandAttack) { @@ -235,7 +186,7 @@ private void func_147121_ag() { at = @At( value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/PlayerControllerMP;resetBlockRemoving()V")) - protected boolean backhand$pauseReset(PlayerControllerMP instance) { + private boolean backhand$pauseReset(PlayerControllerMP instance) { if (backhand$breakBlockTimer > 0) { backhand$breakBlockTimer--; return false; @@ -244,7 +195,7 @@ private void func_147121_ag() { } @Inject(method = "func_147115_a", at = @At(value = "HEAD")) - protected void backhand$breakBlockOffhand(boolean leftClick, CallbackInfo ci) { + private void backhand$breakBlockOffhand(boolean leftClick, CallbackInfo ci) { if (backhand$breakBlockTimer > 0) { BackhandUtils.useOffhandItem(thePlayer, () -> { int i = objectMouseOver.blockX; @@ -263,4 +214,62 @@ private void func_147121_ag() { }); } } + + @Unique + private boolean backhand$rightClickBlock(ItemStack mainHandItem, ItemStack offhandItem) { + int x = objectMouseOver.blockX; + int y = objectMouseOver.blockY; + int z = objectMouseOver.blockZ; + + boolean result; + + if (offhandItem != null && backhand$doesOffhandNeedPriority(mainHandItem)) { + result = BackhandUtils.useOffhandItem(thePlayer, () -> backhand$rightClickBlock(offhandItem, x, y, z)); + if (!result) { + result = backhand$rightClickBlock(mainHandItem, x, y, z); + } + } else { + result = backhand$rightClickBlock(mainHandItem, x, y, z); + if (!result && offhandItem != null) { + result = BackhandUtils.useOffhandItem(thePlayer, () -> backhand$rightClickBlock(offhandItem, x, y, z)); + } + } + + return result; + } + + @Unique + private boolean backhand$rightClickBlock(ItemStack stack, int x, int y, int z) { + boolean result = !net.minecraftforge.event.ForgeEventFactory + .onPlayerInteract( + thePlayer, + net.minecraftforge.event.entity.player.PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK, + x, + y, + z, + objectMouseOver.sideHit, + theWorld) + .isCanceled(); + if (result && playerController + .onPlayerRightClick(thePlayer, theWorld, stack, x, y, z, objectMouseOver.sideHit, objectMouseOver.hitVec)) { + thePlayer.swingItem(); + return true; + } + return false; + } + + @Unique + private boolean backhand$doesOffhandNeedPriority(ItemStack mainHandItem) { + if (mainHandItem == null) return false; + + for (Class clazz : ClientProxy.offhandPriorityItems) { + if (clazz.isAssignableFrom( + mainHandItem.getItem() + .getClass())) { + return true; + } + } + + return false; + } } diff --git a/src/main/java/xonin/backhand/utils/Mods.java b/src/main/java/xonin/backhand/utils/Mods.java index ed67e0e..60b62d1 100644 --- a/src/main/java/xonin/backhand/utils/Mods.java +++ b/src/main/java/xonin/backhand/utils/Mods.java @@ -5,7 +5,8 @@ public enum Mods { INV_TWEAKS("inventorytweaks"), - DOUBLE_WIDE_SURPRISE("dws"),; + DOUBLE_WIDE_SURPRISE("dws"), + TINKERS_CONSTRUCT("TConstruct"),; private final String modId; private Boolean loaded; From f13046467d473cde3fbc55eb9baa0b1cfa2a3dde Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 13:32:15 +0100 Subject: [PATCH 26/38] stop constant reset animation when placing blocks in creative --- .../mixins/early/minecraft/MixinMinecraft.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index b677018..e11854d 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -100,19 +100,17 @@ private void func_147121_ag() { if (mainHandItem != null) { if (mainHandItem.stackSize == 0) { thePlayer.setCurrentItemOrArmor(0, null); - } else - if (mainHandItem.stackSize != mainOriginalSize || playerController.isInCreativeMode()) { - entityRenderer.itemRenderer.resetEquippedProgress(); - } + } else if (mainHandItem.stackSize != mainOriginalSize) { + entityRenderer.itemRenderer.resetEquippedProgress(); + } } if (offhandItem != null) { if (offhandItem.stackSize == 0) { BackhandUtils.setPlayerOffhandItem(thePlayer, null); - } else if (offhandItem.stackSize != offhandOriginalSize - || playerController.isInCreativeMode()) { - BackhandRenderHelper.itemRenderer.resetEquippedProgress(); - } + } else if (offhandItem.stackSize != offhandOriginalSize) { + BackhandRenderHelper.itemRenderer.resetEquippedProgress(); + } } } } From da47385046886655f434a1465654ef606e8b4681 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 14:43:04 +0100 Subject: [PATCH 27/38] remove the weird creative survival inv offhand slot --- .../java/xonin/backhand/mixins/Mixins.java | 3 +- .../early/minecraft/MixinEntityRenderer.java | 2 +- .../minecraft/MixinGuiContainerCreative.java | 29 +++++++++++++++++++ .../early/minecraft/MixinItemStack.java | 2 +- src/main/resources/META-INF/backhand_at.cfg | 2 ++ 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiContainerCreative.java diff --git a/src/main/java/xonin/backhand/mixins/Mixins.java b/src/main/java/xonin/backhand/mixins/Mixins.java index 41beef5..959a914 100644 --- a/src/main/java/xonin/backhand/mixins/Mixins.java +++ b/src/main/java/xonin/backhand/mixins/Mixins.java @@ -35,7 +35,8 @@ public enum Mixins { "minecraft.MixinNetHandlerPlayClient", "minecraft.MixinMinecraft", "minecraft.MixinGuiInventory", - "minecraft.MixinEntityRenderer") + "minecraft.MixinEntityRenderer", + "minecraft.MixinGuiContainerCreative") .setPhase(Phase.EARLY) .setSide(Side.CLIENT) .addTargetedMod(TargetedMod.VANILLA)), diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityRenderer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityRenderer.java index 1263a1a..d3545da 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityRenderer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityRenderer.java @@ -12,7 +12,7 @@ import xonin.backhand.client.utils.BackhandRenderHelper; @Mixin(EntityRenderer.class) -public class MixinEntityRenderer { +public abstract class MixinEntityRenderer { @Inject( method = "updateRenderer", diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiContainerCreative.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiContainerCreative.java new file mode 100644 index 0000000..5723300 --- /dev/null +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiContainerCreative.java @@ -0,0 +1,29 @@ +package xonin.backhand.mixins.early.minecraft; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.inventory.GuiContainerCreative; +import net.minecraft.creativetab.CreativeTabs; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.llamalad7.mixinextras.sugar.Local; + +import xonin.backhand.api.core.BackhandUtils; + +@Mixin(GuiContainerCreative.class) +public abstract class MixinGuiContainerCreative { + + @Inject( + method = "setCurrentCreativeTab", + at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z", ordinal = 1)) + protected void backhand$removeOffhandSlot(CreativeTabs p_147050_1_, CallbackInfo ci, + @Local GuiContainerCreative.ContainerCreative container) { + GuiContainerCreative.CreativeSlot slot = (GuiContainerCreative.CreativeSlot) container.inventorySlots + .get(BackhandUtils.getOffhandSlot(Minecraft.getMinecraft().thePlayer)); + slot.xDisplayPosition = -2000; + slot.yDisplayPosition = -2000; + } +} diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java index 6c243dc..c2047c7 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinItemStack.java @@ -14,7 +14,7 @@ import xonin.backhand.api.core.BackhandUtils; @Mixin(ItemStack.class) -public class MixinItemStack { +public abstract class MixinItemStack { @WrapWithCondition( method = "updateAnimation", diff --git a/src/main/resources/META-INF/backhand_at.cfg b/src/main/resources/META-INF/backhand_at.cfg index 3bfd01e..f74a6bf 100644 --- a/src/main/resources/META-INF/backhand_at.cfg +++ b/src/main/resources/META-INF/backhand_at.cfg @@ -5,3 +5,5 @@ public net.minecraft.client.gui.inventory.GuiContainer field_147009_r # guiTop ( public net.minecraft.client.renderer.ItemRenderer field_78453_b # itemToRender public net.minecraft.client.renderer.ItemRenderer field_78454_c # equippedProgress public net.minecraft.client.renderer.ItemRenderer field_78451_d # prevEquippedProgress +public net.minecraft.client.gui.inventory.GuiContainerCreative$ContainerCreative +public net.minecraft.client.gui.inventory.GuiContainerCreative$CreativeSlot From 7c37e7d4523bab5b2d3cb83af4038cd4c2e7d838 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:02:49 +0100 Subject: [PATCH 28/38] less indents --- .../early/minecraft/MixinMinecraft.java | 182 +++++++++--------- 1 file changed, 90 insertions(+), 92 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index e11854d..748b4bf 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -72,108 +72,106 @@ private void func_147121_ag() { boolean useAction = true; if (objectMouseOver == null) { - logger.warn("Null returned as \'hitResult\', this shouldn\'t happen!"); - } else { - switch (objectMouseOver.typeOfHit) { - case ENTITY: - if (playerController.interactWithEntitySendPacket(thePlayer, objectMouseOver.entityHit)) { - useAction = false; - } else if (BackhandUtils.useOffhandItem( - thePlayer, - () -> playerController.interactWithEntitySendPacket(thePlayer, objectMouseOver.entityHit))) { - useAction = false; - } + logger.warn("Null returned as 'hitResult', this shouldn't happen!"); + return; + } - break; - case BLOCK: - int x = objectMouseOver.blockX; - int y = objectMouseOver.blockY; - int z = objectMouseOver.blockZ; - - if (!theWorld.getBlock(x, y, z) - .isAir(theWorld, x, y, z)) { - int mainOriginalSize = mainHandItem != null ? mainHandItem.stackSize : 0; - int offhandOriginalSize = offhandItem != null ? offhandItem.stackSize : 0; - - useAction = !backhand$rightClickBlock(mainHandItem, offhandItem); - - if (mainHandItem != null) { - if (mainHandItem.stackSize == 0) { - thePlayer.setCurrentItemOrArmor(0, null); - } else if (mainHandItem.stackSize != mainOriginalSize) { - entityRenderer.itemRenderer.resetEquippedProgress(); - } + switch (objectMouseOver.typeOfHit) { + case ENTITY -> { + if (playerController.interactWithEntitySendPacket(thePlayer, objectMouseOver.entityHit)) { + useAction = false; + } else if (BackhandUtils.useOffhandItem( + thePlayer, + () -> playerController.interactWithEntitySendPacket(thePlayer, objectMouseOver.entityHit))) { + useAction = false; + } + } + case BLOCK -> { + int x = objectMouseOver.blockX; + int y = objectMouseOver.blockY; + int z = objectMouseOver.blockZ; + + if (!theWorld.getBlock(x, y, z) + .isAir(theWorld, x, y, z)) { + int mainOriginalSize = mainHandItem != null ? mainHandItem.stackSize : 0; + int offhandOriginalSize = offhandItem != null ? offhandItem.stackSize : 0; + + useAction = !backhand$rightClickBlock(mainHandItem, offhandItem); + + if (mainHandItem != null) { + if (mainHandItem.stackSize == 0) { + thePlayer.setCurrentItemOrArmor(0, null); + } else if (mainHandItem.stackSize != mainOriginalSize) { + entityRenderer.itemRenderer.resetEquippedProgress(); } + } - if (offhandItem != null) { - if (offhandItem.stackSize == 0) { - BackhandUtils.setPlayerOffhandItem(thePlayer, null); - } else if (offhandItem.stackSize != offhandOriginalSize) { - BackhandRenderHelper.itemRenderer.resetEquippedProgress(); - } + if (offhandItem != null) { + if (offhandItem.stackSize == 0) { + BackhandUtils.setPlayerOffhandItem(thePlayer, null); + } else if (offhandItem.stackSize != offhandOriginalSize) { + BackhandRenderHelper.itemRenderer.resetEquippedProgress(); } } + } } + } + + if (!useAction) return; + boolean result = !net.minecraftforge.event.ForgeEventFactory + .onPlayerInteract( + thePlayer, + net.minecraftforge.event.entity.player.PlayerInteractEvent.Action.RIGHT_CLICK_AIR, + 0, + 0, + 0, + -1, + theWorld) + .isCanceled(); + if (result && mainHandItem != null && playerController.sendUseItem(thePlayer, theWorld, mainHandItem)) { + entityRenderer.itemRenderer.resetEquippedProgress2(); + } - if (useAction) { - boolean result = !net.minecraftforge.event.ForgeEventFactory - .onPlayerInteract( - thePlayer, - net.minecraftforge.event.entity.player.PlayerInteractEvent.Action.RIGHT_CLICK_AIR, - 0, - 0, - 0, - -1, - theWorld) - .isCanceled(); - if (result && mainHandItem != null && playerController.sendUseItem(thePlayer, theWorld, mainHandItem)) { - entityRenderer.itemRenderer.resetEquippedProgress2(); + if (offhandItem == null || thePlayer.getItemInUse() != null) return; + useAction = !BackhandUtils.useOffhandItem(thePlayer, () -> { + PlayerInteractEvent useItemEvent = new PlayerInteractEvent( + thePlayer, + PlayerInteractEvent.Action.RIGHT_CLICK_AIR, + 0, + 0, + -1, + 0, + theWorld); + if (!MinecraftForge.EVENT_BUS.post(useItemEvent)) { + if (playerController.sendUseItem(thePlayer, theWorld, offhandItem)) { + BackhandRenderHelper.itemRenderer.resetEquippedProgress(); } - if (offhandItem == null || thePlayer.getItemInUse() != null) return; - useAction = !BackhandUtils.useOffhandItem(thePlayer, () -> { - PlayerInteractEvent useItemEvent = new PlayerInteractEvent( - thePlayer, - PlayerInteractEvent.Action.RIGHT_CLICK_AIR, - 0, - 0, - -1, - 0, - theWorld); - if (!MinecraftForge.EVENT_BUS.post(useItemEvent)) { - if (playerController.sendUseItem(thePlayer, theWorld, offhandItem)) { - BackhandRenderHelper.itemRenderer.resetEquippedProgress(); - } + return thePlayer.getItemInUse() != null; + } + return false; + }); - return thePlayer.getItemInUse() != null; - } - return false; - }); - - if (useAction) { - switch (objectMouseOver.typeOfHit) { - case ENTITY -> { - if (BackhandConfig.OffhandAttack) { - BackhandUtils.useOffhandItem(thePlayer, () -> { - thePlayer.swingItem(); - playerController.attackEntity(thePlayer, objectMouseOver.entityHit); - }); - } - } - case BLOCK -> { - if (BackhandConfig.OffhandBreakBlocks - && offhandItem.getItemUseAction() == EnumAction.none) { - BackhandUtils.useOffhandItem(thePlayer, () -> { - backhand$breakBlockTimer = 5; - playerController.clickBlock( - objectMouseOver.blockX, - objectMouseOver.blockY, - objectMouseOver.blockZ, - objectMouseOver.sideHit); - }); - } - } - } + if (!useAction) return; + switch (objectMouseOver.typeOfHit) { + case ENTITY -> { + if (BackhandConfig.OffhandAttack) { + BackhandUtils.useOffhandItem(thePlayer, () -> { + thePlayer.swingItem(); + playerController.attackEntity(thePlayer, objectMouseOver.entityHit); + }); + } + } + case BLOCK -> { + if (BackhandConfig.OffhandBreakBlocks && offhandItem.getItemUseAction() == EnumAction.none) { + BackhandUtils.useOffhandItem(thePlayer, () -> { + backhand$breakBlockTimer = 5; + playerController.clickBlock( + objectMouseOver.blockX, + objectMouseOver.blockY, + objectMouseOver.blockZ, + objectMouseOver.sideHit); + }); } } } From 00d9cd853dc3a5678284291f76c9351a16359ce1 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:17:36 +0100 Subject: [PATCH 29/38] remove the correct creative inv slot --- .../mixins/early/minecraft/MixinGuiContainerCreative.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiContainerCreative.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiContainerCreative.java index 5723300..07cdfd5 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiContainerCreative.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinGuiContainerCreative.java @@ -1,6 +1,5 @@ package xonin.backhand.mixins.early.minecraft; -import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiContainerCreative; import net.minecraft.creativetab.CreativeTabs; @@ -11,8 +10,6 @@ import com.llamalad7.mixinextras.sugar.Local; -import xonin.backhand.api.core.BackhandUtils; - @Mixin(GuiContainerCreative.class) public abstract class MixinGuiContainerCreative { @@ -22,7 +19,7 @@ public abstract class MixinGuiContainerCreative { protected void backhand$removeOffhandSlot(CreativeTabs p_147050_1_, CallbackInfo ci, @Local GuiContainerCreative.ContainerCreative container) { GuiContainerCreative.CreativeSlot slot = (GuiContainerCreative.CreativeSlot) container.inventorySlots - .get(BackhandUtils.getOffhandSlot(Minecraft.getMinecraft().thePlayer)); + .get(container.inventorySlots.size() - 1); slot.xDisplayPosition = -2000; slot.yDisplayPosition = -2000; } From 53426879e92162436e2e0341ac12270cda25a795 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:50:49 +0100 Subject: [PATCH 30/38] prioritise using arrows held in offhand when shooting bow --- .../early/minecraft/MixinEntityPlayer.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java index 21a5091..82ca2cc 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinEntityPlayer.java @@ -2,11 +2,14 @@ import java.util.Objects; +import net.minecraft.block.BlockDispenser; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.item.EnumAction; import net.minecraft.item.ItemStack; import net.minecraft.network.play.server.S0BPacketAnimation; +import net.minecraft.util.RegistrySimple; import net.minecraft.world.World; import net.minecraft.world.WorldServer; @@ -18,6 +21,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; @@ -110,6 +114,29 @@ private MixinEntityPlayer(World p_i1594_1_) { this.backhand$offHandSwingProgress = (float) this.backhand$offHandSwingProgressInt / (float) var1; } + @WrapWithCondition( + method = "stopUsingItem", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/item/ItemStack;onPlayerStoppedUsing(Lnet/minecraft/world/World;Lnet/minecraft/entity/player/EntityPlayer;I)V")) + private boolean backhand$stopUsingItem(ItemStack stack, World world, EntityPlayer player, int p_77974_3_) { + ItemStack offhand = BackhandUtils.getOffhandItem(player); + if (offhand != null && !isUsingOffhand() + && stack.getItemUseAction() == EnumAction.bow + && ((RegistrySimple) BlockDispenser.dispenseBehaviorRegistry).containsKey(offhand.getItem())) { + // Swap the offhand item into the first available slot to give it usage priority + int slot = (inventory.currentItem == 0) ? 1 : 0; + ItemStack swappedStack = player.inventory.mainInventory[slot]; + inventory.mainInventory[slot] = offhand; + BackhandUtils.setPlayerOffhandItem(player, swappedStack); + stack.onPlayerStoppedUsing(world, player, p_77974_3_); + player.inventory.mainInventory[slot] = backhand$getLegalStack(swappedStack); + BackhandUtils.setPlayerOffhandItem(player, backhand$getLegalStack(offhand)); + return false; + } + return true; + } + @Unique private void backhand$updateOffhandUse(boolean state) { EntityPlayer player = (EntityPlayer) (Object) this; @@ -177,4 +204,10 @@ public boolean isOffhandItemInUse() { public boolean isUsingOffhand() { return inventory.currentItem == ((IOffhandInventory) inventory).backhand$getOffhandSlot(); } + + @Unique + private ItemStack backhand$getLegalStack(ItemStack stack) { + if (stack == null || stack.stackSize == 0) return null; + return stack; + } } From 567a793bfde92fd6ea78e9dbf39ed41deaca1622 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 19:21:30 +0100 Subject: [PATCH 31/38] fix pickblock placing block in wrong slot --- .../backhand/mixins/early/minecraft/MixinMinecraft.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index 748b4bf..7e164a7 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -20,7 +20,9 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Constant; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyConstant; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; @@ -211,6 +213,11 @@ private void func_147121_ag() { } } + @ModifyConstant(method = "func_147112_ai", constant = @Constant(intValue = 9)) + private int backhand$adjustSlotOffset(int constant) { + return 10; + } + @Unique private boolean backhand$rightClickBlock(ItemStack mainHandItem, ItemStack offhandItem) { int x = objectMouseOver.blockX; From 839300e88ae4ec4b56b4b33c6ed2a245853f3442 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 21:13:57 +0100 Subject: [PATCH 32/38] import items from bg2 slots --- .../early/minecraft/MixinInventoryPlayer.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java index 055efd5..9407263 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java @@ -1,5 +1,8 @@ package xonin.backhand.mixins.early.minecraft; +import java.util.ArrayList; +import java.util.List; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; @@ -14,6 +17,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import com.llamalad7.mixinextras.sugar.Local; import xonin.backhand.api.core.IOffhandInventory; import xonin.backhand.utils.BackhandConfig; @@ -30,9 +34,15 @@ public abstract class MixinInventoryPlayer implements IOffhandInventory { @Shadow public ItemStack[] mainInventory; + @Shadow + public abstract boolean addItemStackToInventory(ItemStack p_70441_1_); + @Unique private int backhand$offhandSlot; + @Unique + private List backhand$bg2Stacks = new ArrayList<>(); + @Inject( method = "readFromNBT", at = @At( @@ -45,6 +55,31 @@ public abstract class MixinInventoryPlayer implements IOffhandInventory { mainInventory = new ItemStack[mainInventory.length + 1]; } + @Inject( + method = "readFromNBT", + at = @At( + value = "FIELD", + opcode = Opcodes.GETFIELD, + target = "Lnet/minecraft/entity/player/InventoryPlayer;mainInventory:[Lnet/minecraft/item/ItemStack;", + ordinal = 0)) + private void backhand$importBG2Items(NBTTagList p_70443_1_, CallbackInfo ci, @Local ItemStack stack, + @Local(name = "j") int index) { + if (index >= 150 && index < 168) { + backhand$bg2Stacks.add(stack); + } + } + + @Inject(method = "readFromNBT", at = @At(value = "TAIL")) + private void backhand$giveBG2Items(NBTTagList p_70443_1_, CallbackInfo ci) { + for (ItemStack stack : backhand$bg2Stacks) { + if (!addItemStackToInventory(stack)) { + player.entityDropItem(stack, 0.0F); + } + } + + backhand$bg2Stacks = null; + } + @Inject( method = "", at = @At( From 3e3d241cc917243089e46ddee47c8efca367794b Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 21:17:08 +0100 Subject: [PATCH 33/38] fix efr remap --- .../mixins/late/etfuturum/MixinServerEventHandler.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java b/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java index 2551901..753b308 100644 --- a/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java +++ b/src/main/java/xonin/backhand/mixins/late/etfuturum/MixinServerEventHandler.java @@ -17,10 +17,10 @@ import ganymedes01.etfuturum.core.handlers.ServerEventHandler; import xonin.backhand.api.core.BackhandUtils; -@Mixin(ServerEventHandler.class) +@Mixin(value = ServerEventHandler.class, remap = false) public abstract class MixinServerEventHandler { - @Shadow(remap = false) + @Shadow public abstract void handleTotemCheck(EntityLivingBase entity, LivingHurtEvent event); @Unique @@ -31,8 +31,8 @@ public abstract class MixinServerEventHandler { at = @At( value = "INVOKE", target = "Lnet/minecraft/entity/EntityLivingBase;getHeldItem()Lnet/minecraft/item/ItemStack;", + remap = true, ordinal = 0), - remap = false, cancellable = true) private void backhand$useOffhandTotem(EntityLivingBase entity, LivingHurtEvent event, CallbackInfo ci) { if (backhand$skipCheck || !(entity instanceof EntityPlayer player)) return; From 32b532cb9490d35d81ded0dd093d781cafe86cf4 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 21:30:04 +0100 Subject: [PATCH 34/38] stack picked up items with offhand first if they're the same item --- .../early/minecraft/MixinInventoryPlayer.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java index 9407263..56f79fc 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java @@ -37,6 +37,9 @@ public abstract class MixinInventoryPlayer implements IOffhandInventory { @Shadow public abstract boolean addItemStackToInventory(ItemStack p_70441_1_); + @Shadow + public abstract int getInventoryStackLimit(); + @Unique private int backhand$offhandSlot; @@ -108,6 +111,24 @@ public abstract class MixinInventoryPlayer implements IOffhandInventory { return original; } + @ModifyReturnValue(method = "storeItemStack", at = @At("RETURN")) + private int backhand$getOffhandItem(int original, @Local(argsOnly = true) ItemStack stack) { + ItemStack offhand = backhand$getOffhandItem(); + if (original == backhand$getOffhandSlot() || original == -1 || offhand == null) { + return original; + } + + if (offhand.getItem() == stack.getItem() && offhand.isStackable() + && offhand.stackSize < offhand.getMaxStackSize() + && offhand.stackSize < getInventoryStackLimit() + && (!offhand.getHasSubtypes() || offhand.getItemDamage() == stack.getItemDamage()) + && ItemStack.areItemStackTagsEqual(offhand, stack)) { + return backhand$getOffhandSlot(); + } + + return original; + } + @Override public ItemStack backhand$getOffhandItem() { return mainInventory[backhand$getOffhandSlot()]; From 0e22d7c57d398f8890cc37dcdb18d7aaf405189c Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sat, 18 Jan 2025 21:37:39 +0100 Subject: [PATCH 35/38] stack with mainhand first --- .../backhand/mixins/early/minecraft/MixinInventoryPlayer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java index 56f79fc..f0bf887 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java @@ -114,7 +114,7 @@ public abstract class MixinInventoryPlayer implements IOffhandInventory { @ModifyReturnValue(method = "storeItemStack", at = @At("RETURN")) private int backhand$getOffhandItem(int original, @Local(argsOnly = true) ItemStack stack) { ItemStack offhand = backhand$getOffhandItem(); - if (original == backhand$getOffhandSlot() || original == -1 || offhand == null) { + if (original == backhand$getOffhandSlot() || original == currentItem || original == -1 || offhand == null) { return original; } From 3f01a92343e514a88ea883a0e280ad45bdceb6d1 Mon Sep 17 00:00:00 2001 From: Caedis Date: Sat, 18 Jan 2025 15:16:58 -0600 Subject: [PATCH 36/38] Simplify player inventory insert --- .../early/minecraft/MixinInventoryPlayer.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java index f0bf887..fecd645 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java @@ -15,6 +15,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.llamalad7.mixinextras.injector.ModifyReturnValue; import com.llamalad7.mixinextras.sugar.Local; @@ -110,23 +111,26 @@ public abstract class MixinInventoryPlayer implements IOffhandInventory { } return original; } - - @ModifyReturnValue(method = "storeItemStack", at = @At("RETURN")) - private int backhand$getOffhandItem(int original, @Local(argsOnly = true) ItemStack stack) { - ItemStack offhand = backhand$getOffhandItem(); - if (original == backhand$getOffhandSlot() || original == currentItem || original == -1 || offhand == null) { - return original; + @Inject(method = "storeItemStack", at = @At("HEAD"), cancellable = true) + private void backhand$storeItemStack(ItemStack checkStack, CallbackInfoReturnable cir) { + ItemStack stack = player.getHeldItem(); + if (stack != null && checkStack(stack, checkStack)) { + cir.setReturnValue(currentItem); } - if (offhand.getItem() == stack.getItem() && offhand.isStackable() - && offhand.stackSize < offhand.getMaxStackSize() - && offhand.stackSize < getInventoryStackLimit() - && (!offhand.getHasSubtypes() || offhand.getItemDamage() == stack.getItemDamage()) - && ItemStack.areItemStackTagsEqual(offhand, stack)) { - return backhand$getOffhandSlot(); + stack = backhand$getOffhandItem(); + if (stack != null && checkStack(stack, checkStack)) { + cir.setReturnValue(backhand$getOffhandSlot()); } + } - return original; + @Unique + private boolean checkStack(ItemStack a, ItemStack b) { + return a.getItem() == b.getItem() && a.isStackable() + && a.stackSize < a.getMaxStackSize() + && a.stackSize < getInventoryStackLimit() + && (!a.getHasSubtypes() || a.getItemDamage() == b.getItemDamage()) + && ItemStack.areItemStackTagsEqual(a, b); } @Override From 1cc193fbec1885842e22bebc633affa19eb7d0a1 Mon Sep 17 00:00:00 2001 From: Caedis Date: Sat, 18 Jan 2025 15:23:26 -0600 Subject: [PATCH 37/38] spotless --- .../backhand/mixins/early/minecraft/MixinInventoryPlayer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java index fecd645..61e8614 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinInventoryPlayer.java @@ -111,6 +111,7 @@ public abstract class MixinInventoryPlayer implements IOffhandInventory { } return original; } + @Inject(method = "storeItemStack", at = @At("HEAD"), cancellable = true) private void backhand$storeItemStack(ItemStack checkStack, CallbackInfoReturnable cir) { ItemStack stack = player.getHeldItem(); From 94a079ed75d35ff57a5bd01da0cabb3476f56ee8 Mon Sep 17 00:00:00 2001 From: Lyfts <127234178+Lyfts@users.noreply.github.com> Date: Sun, 19 Jan 2025 10:25:21 +0100 Subject: [PATCH 38/38] upgrade method visibility --- .../xonin/backhand/mixins/early/minecraft/MixinMinecraft.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java index 7e164a7..0a60b6a 100644 --- a/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java +++ b/src/main/java/xonin/backhand/mixins/early/minecraft/MixinMinecraft.java @@ -65,9 +65,11 @@ public abstract class MixinMinecraft { /** * @author Lyft * @reason Offhand support + * Don't change this methods visibility despite what mixin debug says. + * Some mods AT this and changing the visibility will break them. */ @Overwrite - private void func_147121_ag() { + public void func_147121_ag() { rightClickDelayTimer = 4; ItemStack mainHandItem = thePlayer.inventory.getCurrentItem(); ItemStack offhandItem = BackhandUtils.getOffhandItem(thePlayer);