Guest User

Untitled

a guest
Dec 16th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. public abstract class TheContainerBase
  2. extends Container
  3. {
  4. /**
  5. * Maps slotNumber -> Slot
  6. */
  7. private final HashMap<Integer, Slot> slotMap = new HashMap<Integer, Slot>();
  8.  
  9. /**
  10. * Set if the player is an MP player.
  11. */
  12. private final EntityPlayerMP playerMP;
  13.  
  14. /**
  15. * The player interacting with this container.
  16. */
  17. protected final EntityPlayer player;
  18.  
  19. public TheContainerBase( final EntityPlayer player )
  20. {
  21. // Set the player
  22. this.player = player;
  23. if( player instanceof EntityPlayerMP )
  24. {
  25. this.playerMP = (EntityPlayerMP)player;
  26. }
  27. else
  28. {
  29. this.playerMP = null;
  30. }
  31. }
  32.  
  33. /**
  34. * Adds a slot to the container and the slot map.
  35. */
  36. @Override
  37. protected Slot addSlotToContainer( @Nonnull final Slot slot )
  38. {
  39. // Call super
  40. super.addSlotToContainer( slot );
  41.  
  42. // Map the slot
  43. if( this.slotMap.put( slot.slotNumber, slot ) != null )
  44. {
  45. ThELog.warning( "Duplicate Slot Number Detected: %d", slot.slotNumber );
  46. }
  47.  
  48. return slot;
  49. }
  50.  
  51. /**
  52. * Detects server side changes to send to the player.<br/>
  53. * When modifying slots, return true to set {@code playerMP.isChangingQuantityOnly} to {@code false},
  54. * or set it directly with the player argument.
  55. *
  56. * @param playerMP
  57. *
  58. * @return
  59. */
  60. protected boolean detectAndSendChangesMP( @Nonnull final EntityPlayerMP playerMP )
  61. {
  62. return false;
  63. }
  64.  
  65. @Override
  66. public void detectAndSendChanges()
  67. {
  68. // Call super
  69. super.detectAndSendChanges();
  70.  
  71. // MP player?
  72. if( this.playerMP != null )
  73. {
  74. if( this.detectAndSendChangesMP( this.playerMP ) )
  75. {
  76. this.playerMP.isChangingQuantityOnly = false;
  77.  
  78. // Call super
  79. super.detectAndSendChanges();
  80. }
  81. }
  82. }
  83.  
  84. /**
  85. * Use getSlotOrNull.
  86. */
  87. @Override
  88. @Deprecated
  89. public Slot getSlot( final int slotNumber )
  90. {
  91. return super.getSlot( slotNumber );
  92. }
  93.  
  94. /**
  95. * Returns the slot with the specified slot number or null.
  96. *
  97. * @param slotNumber
  98. * @return
  99. */
  100. @Nullable
  101. public Slot getSlotOrNull( final int slotNumber )
  102. {
  103. return this.slotMap.getOrDefault( slotNumber, null );
  104. }
  105.  
  106. /**
  107. * Clears the slot map.
  108. */
  109. @Override
  110. public void onContainerClosed( @Nonnull final EntityPlayer player )
  111. {
  112. // Call super
  113. super.onContainerClosed( player );
  114.  
  115. // Clear the map
  116. this.slotMap.clear();
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment