tomdodd4598

Untitled

Oct 4th, 2023
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. import net.minecraft.item.ItemStack;
  4. import net.minecraft.util.NonNullList;
  5.  
  6. public class InventoryStackList extends NonNullList<ItemStack> {
  7.    
  8.     public InventoryStackList() {
  9.         super();
  10.     }
  11.    
  12.     public InventoryStackList(List<ItemStack> other) {
  13.         super(other, null);
  14.     }
  15.    
  16.     @Override
  17.     public List<ItemStack> subList(int fromIndex, int toIndex) {
  18.         return new SubList<>(this, fromIndex, toIndex);
  19.     }
  20.    
  21.     protected class SubList<E> extends NonNullList<E> {
  22.        
  23.         private final AbstractList<E> internal;
  24.         private final int offset;
  25.         private int size;
  26.        
  27.         SubList(AbstractList<E> list, int fromIndex, int toIndex) {
  28.             if (fromIndex < 0) {
  29.                 throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
  30.             }
  31.             else if (toIndex > list.size()) {
  32.                 throw new IndexOutOfBoundsException("toIndex = " + toIndex);
  33.             }
  34.             else if (fromIndex > toIndex) {
  35.                 throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
  36.             }
  37.             internal = list;
  38.             offset = fromIndex;
  39.             size = toIndex - fromIndex;
  40.             // this.modCount = internal.modCount;
  41.         }
  42.        
  43.         @Override
  44.         public E set(int index, E element) {
  45.             rangeCheck(index);
  46.             checkForComodification();
  47.             return internal.set(index + offset, element);
  48.         }
  49.        
  50.         @Override
  51.         public E get(int index) {
  52.             rangeCheck(index);
  53.             checkForComodification();
  54.             return internal.get(index + offset);
  55.         }
  56.        
  57.         @Override
  58.         public int size() {
  59.             checkForComodification();
  60.             return size;
  61.         }
  62.        
  63.         @Override
  64.         public void add(int index, E element) {
  65.             rangeCheckForAdd(index);
  66.             checkForComodification();
  67.             internal.add(index + offset, element);
  68.             // this.modCount = internal.modCount;
  69.             ++size;
  70.         }
  71.        
  72.         @Override
  73.         public E remove(int index) {
  74.             rangeCheck(index);
  75.             checkForComodification();
  76.             E result = internal.remove(index + offset);
  77.             // this.modCount = internal.modCount;
  78.             --size;
  79.             return result;
  80.         }
  81.        
  82.         @Override
  83.         protected void removeRange(int fromIndex, int toIndex) {
  84.             /*checkForComodification();
  85.             removeRangeMethod.invoke(internal, fromIndex + offset, toIndex + offset);
  86.             // this.modCount = internal.modCount;
  87.             size -= (toIndex - fromIndex);*/
  88.             throw new UnsupportedOperationException();
  89.         }
  90.        
  91.         @Override
  92.         public boolean addAll(Collection<? extends E> c) {
  93.             return addAll(size, c);
  94.         }
  95.        
  96.         @Override
  97.         public boolean addAll(int index, Collection<? extends E> c) {
  98.             rangeCheckForAdd(index);
  99.             int cSize = c.size();
  100.             if (cSize == 0) {
  101.                 return false;
  102.             }
  103.            
  104.             checkForComodification();
  105.             internal.addAll(offset + index, c);
  106.             // this.modCount = internal.modCount;
  107.             size += cSize;
  108.             return true;
  109.         }
  110.        
  111.         @Override
  112.         public Iterator<E> iterator() {
  113.             return listIterator();
  114.         }
  115.        
  116.         @Override
  117.         public ListIterator<E> listIterator(final int index) {
  118.             checkForComodification();
  119.             rangeCheckForAdd(index);
  120.            
  121.             return new ListIterator<E>() {
  122.                
  123.                 private final ListIterator<E> internalIter = internal.listIterator(index + offset);
  124.                
  125.                 @Override
  126.                 public boolean hasNext() {
  127.                     return nextIndex() < size;
  128.                 }
  129.                
  130.                 @Override
  131.                 public E next() {
  132.                     if (hasNext()) {
  133.                         return internalIter.next();
  134.                     }
  135.                     else {
  136.                         throw new NoSuchElementException();
  137.                     }
  138.                 }
  139.                
  140.                 @Override
  141.                 public boolean hasPrevious() {
  142.                     return previousIndex() >= 0;
  143.                 }
  144.                
  145.                 @Override
  146.                 public E previous() {
  147.                     if (hasPrevious()) {
  148.                         return internalIter.previous();
  149.                     }
  150.                     else {
  151.                         throw new NoSuchElementException();
  152.                     }
  153.                 }
  154.                
  155.                 @Override
  156.                 public int nextIndex() {
  157.                     return internalIter.nextIndex() - offset;
  158.                 }
  159.                
  160.                 @Override
  161.                 public int previousIndex() {
  162.                     return internalIter.previousIndex() - offset;
  163.                 }
  164.                
  165.                 @Override
  166.                 public void remove() {
  167.                     internalIter.remove();
  168.                     // SubList.this.modCount = internal.modCount;
  169.                     --size;
  170.                 }
  171.                
  172.                 @Override
  173.                 public void set(E e) {
  174.                     internalIter.set(e);
  175.                 }
  176.                
  177.                 @Override
  178.                 public void add(E e) {
  179.                     internalIter.add(e);
  180.                     // SubList.this.modCount = internal.modCount;
  181.                     ++size;
  182.                 }
  183.             };
  184.         }
  185.        
  186.         @Override
  187.         public List<E> subList(int fromIndex, int toIndex) {
  188.             return new SubList<>(this, fromIndex, toIndex);
  189.         }
  190.        
  191.         private void rangeCheck(int index) {
  192.             if (index < 0 || index >= size) {
  193.                 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
  194.             }
  195.         }
  196.        
  197.         private void rangeCheckForAdd(int index) {
  198.             if (index < 0 || index > size) {
  199.                 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
  200.             }
  201.         }
  202.        
  203.         private String outOfBoundsMsg(int index) {
  204.             return "Index: " + index + ", Size: " + size;
  205.         }
  206.        
  207.         private void checkForComodification() {
  208.             /*if (this.modCount != internal.modCount) {
  209.                 throw new ConcurrentModificationException();
  210.             }*/
  211.         }
  212.     }
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment