2525
2626import com .fox2code .foxevents .EventHolder ;
2727import com .fox2code .foxloader .event .interaction .*;
28+ import com .fox2code .foxloader .event .inventory .PlayerClickSlotEvent ;
29+ import com .fox2code .foxloader .event .inventory .PlayerDropItemEvent ;
30+ import net .minecraft .common .block .container .Container ;
31+ import net .minecraft .common .block .container .Slot ;
2832import net .minecraft .common .entity .Entity ;
2933import net .minecraft .common .entity .player .EntityPlayer ;
3034import net .minecraft .common .item .ItemStack ;
35+ import net .minecraft .common .networking .Packet103SetSlot ;
3136import net .minecraft .common .util .math .Vec3D ;
3237import net .minecraft .common .world .World ;
38+ import net .minecraft .server .entity .player .EntityPlayerMP ;
3339
3440/**
3541 * Internal hook for player network event and interactions.
@@ -51,6 +57,10 @@ public final class InternalInteractionHooks {
5157 EventHolder .getHolderFromEvent (PlayerUseItemOnEntityEvent .class );
5258 private static final EventHolder <PlayerAttackEntityEvent > PLAYER_ATTACK_ENTITY_EVENT =
5359 EventHolder .getHolderFromEvent (PlayerAttackEntityEvent .class );
60+ private static final EventHolder <PlayerClickSlotEvent > PLAYER_CLICK_SLOT_EVENT =
61+ EventHolder .getHolderFromEvent (PlayerClickSlotEvent .class );
62+ private static final EventHolder <PlayerDropItemEvent > PLAYER_DROP_ITEM_EVENT =
63+ EventHolder .getHolderFromEvent (PlayerDropItemEvent .class );
5464
5565 public static boolean sendPlayerStartBreakBlockEvent (
5666 EntityPlayer entityPlayer , int x , int y , int z , int facing ) {
@@ -111,4 +121,91 @@ public static boolean sendPlayerAttackEntityEvent(EntityPlayer player, Entity ta
111121 PLAYER_ATTACK_ENTITY_EVENT .callEvent (playerAttackEntityEvent );
112122 return playerAttackEntityEvent .isCancelled ();
113123 }
124+
125+ public static boolean onHandleClickSlot (Container container , int slotId , int mouseButton ,
126+ int transferType , EntityPlayer player ) {
127+ if (!PLAYER_CLICK_SLOT_EVENT .isEmpty ()) {
128+ PlayerClickSlotEvent playerClickSlotEvent = new PlayerClickSlotEvent (
129+ container , slotId , mouseButton , transferType , player );
130+ PLAYER_CLICK_SLOT_EVENT .callEvent (playerClickSlotEvent );
131+ if (playerClickSlotEvent .isCancelled ()) {
132+ return true ;
133+ }
134+ }
135+ // Handle dropping manually there.
136+ if (PLAYER_DROP_ITEM_EVENT .isEmpty ()) {
137+ return false ;
138+ }
139+ if ((transferType == 0 || transferType == 1 ) && (mouseButton == 0 || mouseButton == 1 ) &&
140+ slotId == -999 && player .inventory .getCursorStack () != null ) {
141+ return mouseButton == 0 ? onDropCursorItemStack (player ) : onDropCursorItem (player );
142+ }
143+ Slot slot ;
144+ ItemStack slotStack ;
145+ if (transferType == 3 && (mouseButton == 0 || mouseButton == 1 ) && slotId >= 0 &&
146+ slotId < container .slots .size () && (slot = container .getSlot (slotId )) != null &&
147+ (slotStack = slot .getStack ()) != null ) {
148+ PlayerDropItemEvent playerDropItemEvent = new PlayerDropItemEvent (
149+ player , slotStack , slotId , mouseButton == 0 ? 64 : 1 , false );
150+ PLAYER_DROP_ITEM_EVENT .callEvent (playerDropItemEvent );
151+ updateSlotItemIfCancelled (playerDropItemEvent );
152+ return playerDropItemEvent .isCancelled ();
153+ }
154+ return false ;
155+ }
156+
157+ public static boolean onDropCurrentItem (EntityPlayer player ) {
158+ if (PLAYER_DROP_ITEM_EVENT .isEmpty ()) return false ;
159+ PlayerDropItemEvent playerDropItemEvent = new PlayerDropItemEvent (
160+ player , player .inventory .getCurrentItem (), player .inventory .currentItem , 1 , false );
161+ PLAYER_DROP_ITEM_EVENT .callEvent (playerDropItemEvent );
162+ updateSlotItemIfCancelled (playerDropItemEvent );
163+ return playerDropItemEvent .isCancelled ();
164+ }
165+
166+ public static boolean onDropCurrentItemStack (EntityPlayer player ) {
167+ if (PLAYER_DROP_ITEM_EVENT .isEmpty ()) return false ;
168+ PlayerDropItemEvent playerDropItemEvent = new PlayerDropItemEvent (
169+ player , player .inventory .getCurrentItem (), player .inventory .currentItem , 64 , false );
170+ PLAYER_DROP_ITEM_EVENT .callEvent (playerDropItemEvent );
171+ updateSlotItemIfCancelled (playerDropItemEvent );
172+ return playerDropItemEvent .isCancelled ();
173+ }
174+
175+ public static boolean onDropCursorItem (EntityPlayer player ) {
176+ if (PLAYER_DROP_ITEM_EVENT .isEmpty ()) return false ;
177+ PlayerDropItemEvent playerDropItemEvent = new PlayerDropItemEvent (
178+ player , player .inventory .getCursorStack (), -999 , 1 , false );
179+ PLAYER_DROP_ITEM_EVENT .callEvent (playerDropItemEvent );
180+ updateSlotItemIfCancelled (playerDropItemEvent );
181+ return playerDropItemEvent .isCancelled ();
182+ }
183+
184+ public static boolean onDropCursorItemStack (EntityPlayer player ) {
185+ if (PLAYER_DROP_ITEM_EVENT .isEmpty ()) return false ;
186+ PlayerDropItemEvent playerDropItemEvent = new PlayerDropItemEvent (
187+ player , player .inventory .getCursorStack (), -999 , 64 , false );
188+ PLAYER_DROP_ITEM_EVENT .callEvent (playerDropItemEvent );
189+ updateSlotItemIfCancelled (playerDropItemEvent );
190+ return playerDropItemEvent .isCancelled ();
191+ }
192+
193+ public static boolean onDropCreativeItemStack (EntityPlayer player , ItemStack itemStack ) {
194+ if (PLAYER_DROP_ITEM_EVENT .isEmpty ()) return false ;
195+ PlayerDropItemEvent playerDropItemEvent = new PlayerDropItemEvent (
196+ player , itemStack , -999 , 64 , true );
197+ PLAYER_DROP_ITEM_EVENT .callEvent (playerDropItemEvent );
198+ return playerDropItemEvent .isCancelled ();
199+ }
200+
201+ private static void updateSlotItemIfCancelled (PlayerDropItemEvent playerDropItemEvent ) {
202+ int slotId = playerDropItemEvent .getSlotId ();
203+ EntityPlayer entityPlayer = playerDropItemEvent .getEntityPlayer ();
204+ if (playerDropItemEvent .isCancelled () && entityPlayer instanceof EntityPlayerMP ) {
205+ ((EntityPlayerMP ) entityPlayer ).playerNetServerHandler .sendPacket (slotId == -999 ?
206+ new Packet103SetSlot (-1 , -1 , entityPlayer .inventory .getCursorStack ()) :
207+ new Packet103SetSlot (((EntityPlayerMP ) entityPlayer ).currentWindowId ,
208+ slotId , playerDropItemEvent .getItemToDrop ()));
209+ }
210+ }
114211}
0 commit comments