format code

This commit is contained in:
TONY_All 2024-08-08 20:12:34 +08:00
parent ff96cf59da
commit ef8b5c973f
Signed by: tony_all
GPG Key ID: 08A2261D5D6F746A
7 changed files with 328 additions and 329 deletions

View File

@ -25,8 +25,8 @@
package cc.maxmc.blastingcrisis.misc; package cc.maxmc.blastingcrisis.misc;
import java.util.function.Consumer;
import java.util.*; import java.util.*;
import java.util.function.Consumer;
/** /**
* Doubly-linked list implementation of the {@code List} and {@code Deque} * Doubly-linked list implementation of the {@code List} and {@code Deque}
@ -73,30 +73,28 @@ import java.util.*;
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> * <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>. * Java Collections Framework</a>.
* *
* @author Josh Bloch
* @see List
* @see ArrayList
* @since 1.2
* @param <E> the type of elements held in this collection * @param <E> the type of elements held in this collection
* @author Josh Bloch
* @see List
* @see ArrayList
* @since 1.2
*/ */
public class EnhancedLinkedList<E> public class EnhancedLinkedList<E>
extends AbstractSequentialList<E> extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
{ private static final long serialVersionUID = 876323262645176354L;
transient int size = 0; transient int size = 0;
/** /**
* Pointer to first node. * Pointer to first node.
* Invariant: (first == null && last == null) || * Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null) * (first.prev == null && first.item != null)
*/ */
transient Node<E> first; transient Node<E> first;
/** /**
* Pointer to last node. * Pointer to last node.
* Invariant: (first == null && last == null) || * Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null) * (last.next == null && last.item != null)
*/ */
transient Node<E> last; transient Node<E> last;
@ -111,7 +109,7 @@ public class EnhancedLinkedList<E>
* collection, in the order they are returned by the collection's * collection, in the order they are returned by the collection's
* iterator. * iterator.
* *
* @param c the collection whose elements are to be placed into this list * @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null * @throws NullPointerException if the specified collection is null
*/ */
public EnhancedLinkedList(Collection<? extends E> c) { public EnhancedLinkedList(Collection<? extends E> c) {
@ -397,10 +395,10 @@ public class EnhancedLinkedList<E>
* *
* @param index index at which to insert the first element * @param index index at which to insert the first element
* from the specified collection * from the specified collection
* @param c collection containing elements to be added to this list * @param c collection containing elements to be added to this list
* @return {@code true} if this list changed as a result of the call * @return {@code true} if this list changed as a result of the call
* @throws IndexOutOfBoundsException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc}
* @throws NullPointerException if the specified collection is null * @throws NullPointerException if the specified collection is null
*/ */
public boolean addAll(int index, Collection<? extends E> c) { public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index); checkPositionIndex(index);
@ -441,6 +439,9 @@ public class EnhancedLinkedList<E>
return true; return true;
} }
// Positional Access Operations
/** /**
* Removes all of the elements from this list. * Removes all of the elements from this list.
* The list will be empty after this call returns. * The list will be empty after this call returns.
@ -462,9 +463,6 @@ public class EnhancedLinkedList<E>
modCount++; modCount++;
} }
// Positional Access Operations
/** /**
* Returns the element at the specified position in this list. * Returns the element at the specified position in this list.
* *
@ -481,7 +479,7 @@ public class EnhancedLinkedList<E>
* Replaces the element at the specified position in this list with the * Replaces the element at the specified position in this list with the
* specified element. * specified element.
* *
* @param index index of the element to replace * @param index index of the element to replace
* @param element element to be stored at the specified position * @param element element to be stored at the specified position
* @return the element previously at the specified position * @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc}
@ -499,7 +497,7 @@ public class EnhancedLinkedList<E>
* Shifts the element currently at that position (if any) and any * Shifts the element currently at that position (if any) and any
* subsequent elements to the right (adds one to their indices). * subsequent elements to the right (adds one to their indices).
* *
* @param index index at which the specified element is to be inserted * @param index index at which the specified element is to be inserted
* @param element element to be inserted * @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc}
*/ */
@ -565,7 +563,7 @@ public class EnhancedLinkedList<E>
* this "outlining" performs best with both server and client VMs. * this "outlining" performs best with both server and client VMs.
*/ */
private String outOfBoundsMsg(int index) { private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size; return "Index: " + index + ", Size: " + size;
} }
private void checkElementIndex(int index) { private void checkElementIndex(int index) {
@ -578,6 +576,8 @@ public class EnhancedLinkedList<E>
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
} }
// Search Operations
/** /**
* Returns the (non-null) Node at the specified element index. * Returns the (non-null) Node at the specified element index.
*/ */
@ -597,8 +597,6 @@ public class EnhancedLinkedList<E>
} }
} }
// Search Operations
/** /**
* Returns the index of the first occurrence of the specified element * Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element. * in this list, or -1 if this list does not contain the element.
@ -608,7 +606,7 @@ public class EnhancedLinkedList<E>
* *
* @param o element to search for * @param o element to search for
* @return the index of the first occurrence of the specified element in * @return the index of the first occurrence of the specified element in
* this list, or -1 if this list does not contain the element * this list, or -1 if this list does not contain the element
*/ */
public int indexOf(Object o) { public int indexOf(Object o) {
int index = 0; int index = 0;
@ -643,6 +641,8 @@ public class EnhancedLinkedList<E>
return null; return null;
} }
// Queue operations.
/** /**
* Returns the index of the last occurrence of the specified element * Returns the index of the last occurrence of the specified element
* in this list, or -1 if this list does not contain the element. * in this list, or -1 if this list does not contain the element.
@ -652,7 +652,7 @@ public class EnhancedLinkedList<E>
* *
* @param o element to search for * @param o element to search for
* @return the index of the last occurrence of the specified element in * @return the index of the last occurrence of the specified element in
* this list, or -1 if this list does not contain the element * this list, or -1 if this list does not contain the element
*/ */
public int lastIndexOf(Object o) { public int lastIndexOf(Object o) {
int index = size; int index = size;
@ -672,8 +672,6 @@ public class EnhancedLinkedList<E>
return -1; return -1;
} }
// Queue operations.
/** /**
* Retrieves, but does not remove, the head (first element) of this list. * Retrieves, but does not remove, the head (first element) of this list.
* *
@ -718,6 +716,8 @@ public class EnhancedLinkedList<E>
return removeFirst(); return removeFirst();
} }
// Deque operations
/** /**
* Adds the specified element as the tail (last element) of this list. * Adds the specified element as the tail (last element) of this list.
* *
@ -729,7 +729,6 @@ public class EnhancedLinkedList<E>
return add(e); return add(e);
} }
// Deque operations
/** /**
* Inserts the specified element at the front of this list. * Inserts the specified element at the front of this list.
* *
@ -759,20 +758,20 @@ public class EnhancedLinkedList<E>
* or returns {@code null} if this list is empty. * or returns {@code null} if this list is empty.
* *
* @return the first element of this list, or {@code null} * @return the first element of this list, or {@code null}
* if this list is empty * if this list is empty
* @since 1.6 * @since 1.6
*/ */
public E peekFirst() { public E peekFirst() {
final Node<E> f = first; final Node<E> f = first;
return (f == null) ? null : f.item; return (f == null) ? null : f.item;
} }
/** /**
* Retrieves, but does not remove, the last element of this list, * Retrieves, but does not remove, the last element of this list,
* or returns {@code null} if this list is empty. * or returns {@code null} if this list is empty.
* *
* @return the last element of this list, or {@code null} * @return the last element of this list, or {@code null}
* if this list is empty * if this list is empty
* @since 1.6 * @since 1.6
*/ */
public E peekLast() { public E peekLast() {
@ -785,7 +784,7 @@ public class EnhancedLinkedList<E>
* or returns {@code null} if this list is empty. * or returns {@code null} if this list is empty.
* *
* @return the first element of this list, or {@code null} if * @return the first element of this list, or {@code null} if
* this list is empty * this list is empty
* @since 1.6 * @since 1.6
*/ */
public E pollFirst() { public E pollFirst() {
@ -798,7 +797,7 @@ public class EnhancedLinkedList<E>
* or returns {@code null} if this list is empty. * or returns {@code null} if this list is empty.
* *
* @return the last element of this list, or {@code null} if * @return the last element of this list, or {@code null} if
* this list is empty * this list is empty
* @since 1.6 * @since 1.6
*/ */
public E pollLast() { public E pollLast() {
@ -826,7 +825,7 @@ public class EnhancedLinkedList<E>
* <p>This method is equivalent to {@link #removeFirst()}. * <p>This method is equivalent to {@link #removeFirst()}.
* *
* @return the element at the front of this list (which is the top * @return the element at the front of this list (which is the top
* of the stack represented by this list) * of the stack represented by this list)
* @throws NoSuchElementException if this list is empty * @throws NoSuchElementException if this list is empty
* @since 1.6 * @since 1.6
*/ */
@ -879,7 +878,7 @@ public class EnhancedLinkedList<E>
* Returns a list-iterator of the elements in this list (in proper * Returns a list-iterator of the elements in this list (in proper
* sequence), starting at the specified position in the list. * sequence), starting at the specified position in the list.
* Obeys the general contract of {@code List.listIterator(int)}.<p> * Obeys the general contract of {@code List.listIterator(int)}.<p>
* * <p>
* The list-iterator is <i>fail-fast</i>: if the list is structurally * The list-iterator is <i>fail-fast</i>: if the list is structurally
* modified at any time after the Iterator is created, in any way except * modified at any time after the Iterator is created, in any way except
* through the list-iterator's own {@code remove} or {@code add} * through the list-iterator's own {@code remove} or {@code add}
@ -892,7 +891,7 @@ public class EnhancedLinkedList<E>
* @param index index of the first element to be returned from the * @param index index of the first element to be returned from the
* list-iterator (by a call to {@code next}) * list-iterator (by a call to {@code next})
* @return a ListIterator of the elements in this list (in proper * @return a ListIterator of the elements in this list (in proper
* sequence), starting at the specified position in the list * sequence), starting at the specified position in the list
* @throws IndexOutOfBoundsException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc}
* @see List#listIterator(int) * @see List#listIterator(int)
*/ */
@ -901,6 +900,286 @@ public class EnhancedLinkedList<E>
return new ListItr(index); return new ListItr(index);
} }
/**
* @since 1.6
*/
public Iterator<E> descendingIterator() {
return new DescendingIterator();
}
@SuppressWarnings("unchecked")
private EnhancedLinkedList<E> superClone() {
try {
return (EnhancedLinkedList<E>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
/**
* Returns a shallow copy of this {@code LinkedList}. (The elements
* themselves are not cloned.)
*
* @return a shallow copy of this {@code LinkedList} instance
*/
@Override
public Object clone() {
EnhancedLinkedList<E> clone = superClone();
// Put clone into "virgin" state
clone.first = clone.last = null;
clone.size = 0;
clone.modCount = 0;
// Initialize clone with our elements
for (Node<E> x = first; x != null; x = x.next)
clone.add(x.item);
return clone;
}
/**
* Returns an array containing all of the elements in this list
* in proper sequence (from first to last element).
*
* <p>The returned array will be "safe" in that no references to it are
* maintained by this list. (In other words, this method must allocate
* a new array). The caller is thus free to modify the returned array.
*
* <p>This method acts as bridge between array-based and collection-based
* APIs.
*
* @return an array containing all of the elements in this list
* in proper sequence
*/
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
/**
* Returns an array containing all of the elements in this list in
* proper sequence (from first to last element); the runtime type of
* the returned array is that of the specified array. If the list fits
* in the specified array, it is returned therein. Otherwise, a new
* array is allocated with the runtime type of the specified array and
* the size of this list.
*
* <p>If the list fits in the specified array with room to spare (i.e.,
* the array has more elements than the list), the element in the array
* immediately following the end of the list is set to {@code null}.
* (This is useful in determining the length of the list <i>only</i> if
* the caller knows that the list does not contain any null elements.)
*
* <p>Like the {@link #toArray()} method, this method acts as bridge between
* array-based and collection-based APIs. Further, this method allows
* precise control over the runtime type of the output array, and may,
* under certain circumstances, be used to save allocation costs.
*
* <p>Suppose {@code x} is a list known to contain only strings.
* The following code can be used to dump the list into a newly
* allocated array of {@code String}:
*
* <pre>
* String[] y = x.toArray(new String[0]);</pre>
* <p>
* Note that {@code toArray(new Object[0])} is identical in function to
* {@code toArray()}.
*
* @param a the array into which the elements of the list are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of the list
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this list
* @throws NullPointerException if the specified array is null
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[]) java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
/**
* Saves the state of this {@code LinkedList} instance to a stream
* (that is, serializes it).
*
* @serialData The size of the list (the number of elements it
* contains) is emitted (int), followed by all of its
* elements (each an Object) in the proper order.
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out size
s.writeInt(size);
// Write out all elements in the proper order.
for (Node<E> x = first; x != null; x = x.next)
s.writeObject(x.item);
}
/**
* Reconstitutes this {@code LinkedList} instance from a stream
* (that is, deserializes it).
*/
@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in size
int size = s.readInt();
// Read in all elements in the proper order.
for (int i = 0; i < size; i++)
linkLast((E) s.readObject());
}
/**
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
* and <em>fail-fast</em> {@link Spliterator} over the elements in this
* list.
*
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
* {@link Spliterator#ORDERED}. Overriding implementations should document
* the reporting of additional characteristic values.
*
* @return a {@code Spliterator} over the elements in this list
* @implNote The {@code Spliterator} additionally reports {@link Spliterator#SUBSIZED}
* and implements {@code trySplit} to permit limited parallelism..
* @since 1.8
*/
@Override
public Spliterator<E> spliterator() {
return new LLSpliterator<>(this, -1, 0);
}
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
/**
* A customized variant of Spliterators.IteratorSpliterator
*/
static final class LLSpliterator<E> implements Spliterator<E> {
static final int BATCH_UNIT = 1 << 10; // batch array size increment
static final int MAX_BATCH = 1 << 25; // max batch array size;
final EnhancedLinkedList<E> list; // null OK unless traversed
Node<E> current; // current node; null until initialized
int est; // size estimate; -1 until first needed
int expectedModCount; // initialized when est set
int batch; // batch size for splits
LLSpliterator(EnhancedLinkedList<E> list, int est, int expectedModCount) {
this.list = list;
this.est = est;
this.expectedModCount = expectedModCount;
}
int getEst() {
int s; // force initialization
final EnhancedLinkedList<E> lst;
if ((s = est) < 0) {
if ((lst = list) == null)
s = est = 0;
else {
expectedModCount = lst.modCount;
current = lst.first;
s = est = lst.size;
}
}
return s;
}
public long estimateSize() {
return getEst();
}
public Spliterator<E> trySplit() {
Node<E> p;
int s = getEst();
if (s > 1 && (p = current) != null) {
int n = batch + BATCH_UNIT;
if (n > s)
n = s;
if (n > MAX_BATCH)
n = MAX_BATCH;
Object[] a = new Object[n];
int j = 0;
do {
a[j++] = p.item;
} while ((p = p.next) != null && j < n);
current = p;
batch = j;
est = s - j;
return Spliterators.spliterator(a, 0, j, Spliterator.ORDERED);
}
return null;
}
public void forEachRemaining(Consumer<? super E> action) {
Node<E> p;
int n;
if (action == null) throw new NullPointerException();
if ((n = getEst()) > 0 && (p = current) != null) {
current = null;
est = 0;
do {
E e = p.item;
p = p.next;
action.accept(e);
} while (p != null && --n > 0);
}
if (list.modCount != expectedModCount)
throw new ConcurrentModificationException();
}
public boolean tryAdvance(Consumer<? super E> action) {
Node<E> p;
if (action == null) throw new NullPointerException();
if (getEst() > 0 && (p = current) != null) {
--est;
E e = p.item;
current = p.next;
action.accept(e);
if (list.modCount != expectedModCount)
throw new ConcurrentModificationException();
return true;
}
return false;
}
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
}
}
private class ListItr implements ListIterator<E> { private class ListItr implements ListIterator<E> {
private Node<E> lastReturned; private Node<E> lastReturned;
private Node<E> next; private Node<E> next;
@ -1000,297 +1279,23 @@ public class EnhancedLinkedList<E>
} }
} }
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
/**
* @since 1.6
*/
public Iterator<E> descendingIterator() {
return new DescendingIterator();
}
/** /**
* Adapter to provide descending iterators via ListItr.previous * Adapter to provide descending iterators via ListItr.previous
*/ */
private class DescendingIterator implements Iterator<E> { private class DescendingIterator implements Iterator<E> {
private final ListItr itr = new ListItr(size()); private final ListItr itr = new ListItr(size());
public boolean hasNext() { public boolean hasNext() {
return itr.hasPrevious(); return itr.hasPrevious();
} }
public E next() { public E next() {
return itr.previous(); return itr.previous();
} }
public void remove() { public void remove() {
itr.remove(); itr.remove();
} }
} }
@SuppressWarnings("unchecked")
private EnhancedLinkedList<E> superClone() {
try {
return (EnhancedLinkedList<E>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
/**
* Returns a shallow copy of this {@code LinkedList}. (The elements
* themselves are not cloned.)
*
* @return a shallow copy of this {@code LinkedList} instance
*/
@Override
public Object clone() {
EnhancedLinkedList<E> clone = superClone();
// Put clone into "virgin" state
clone.first = clone.last = null;
clone.size = 0;
clone.modCount = 0;
// Initialize clone with our elements
for (Node<E> x = first; x != null; x = x.next)
clone.add(x.item);
return clone;
}
/**
* Returns an array containing all of the elements in this list
* in proper sequence (from first to last element).
*
* <p>The returned array will be "safe" in that no references to it are
* maintained by this list. (In other words, this method must allocate
* a new array). The caller is thus free to modify the returned array.
*
* <p>This method acts as bridge between array-based and collection-based
* APIs.
*
* @return an array containing all of the elements in this list
* in proper sequence
*/
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
/**
* Returns an array containing all of the elements in this list in
* proper sequence (from first to last element); the runtime type of
* the returned array is that of the specified array. If the list fits
* in the specified array, it is returned therein. Otherwise, a new
* array is allocated with the runtime type of the specified array and
* the size of this list.
*
* <p>If the list fits in the specified array with room to spare (i.e.,
* the array has more elements than the list), the element in the array
* immediately following the end of the list is set to {@code null}.
* (This is useful in determining the length of the list <i>only</i> if
* the caller knows that the list does not contain any null elements.)
*
* <p>Like the {@link #toArray()} method, this method acts as bridge between
* array-based and collection-based APIs. Further, this method allows
* precise control over the runtime type of the output array, and may,
* under certain circumstances, be used to save allocation costs.
*
* <p>Suppose {@code x} is a list known to contain only strings.
* The following code can be used to dump the list into a newly
* allocated array of {@code String}:
*
* <pre>
* String[] y = x.toArray(new String[0]);</pre>
*
* Note that {@code toArray(new Object[0])} is identical in function to
* {@code toArray()}.
*
* @param a the array into which the elements of the list are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of the list
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this list
* @throws NullPointerException if the specified array is null
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
private static final long serialVersionUID = 876323262645176354L;
/**
* Saves the state of this {@code LinkedList} instance to a stream
* (that is, serializes it).
*
* @serialData The size of the list (the number of elements it
* contains) is emitted (int), followed by all of its
* elements (each an Object) in the proper order.
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out size
s.writeInt(size);
// Write out all elements in the proper order.
for (Node<E> x = first; x != null; x = x.next)
s.writeObject(x.item);
}
/**
* Reconstitutes this {@code LinkedList} instance from a stream
* (that is, deserializes it).
*/
@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in size
int size = s.readInt();
// Read in all elements in the proper order.
for (int i = 0; i < size; i++)
linkLast((E)s.readObject());
}
/**
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
* and <em>fail-fast</em> {@link Spliterator} over the elements in this
* list.
*
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
* {@link Spliterator#ORDERED}. Overriding implementations should document
* the reporting of additional characteristic values.
*
* @implNote
* The {@code Spliterator} additionally reports {@link Spliterator#SUBSIZED}
* and implements {@code trySplit} to permit limited parallelism..
*
* @return a {@code Spliterator} over the elements in this list
* @since 1.8
*/
@Override
public Spliterator<E> spliterator() {
return new LLSpliterator<>(this, -1, 0);
}
/** A customized variant of Spliterators.IteratorSpliterator */
static final class LLSpliterator<E> implements Spliterator<E> {
static final int BATCH_UNIT = 1 << 10; // batch array size increment
static final int MAX_BATCH = 1 << 25; // max batch array size;
final EnhancedLinkedList<E> list; // null OK unless traversed
Node<E> current; // current node; null until initialized
int est; // size estimate; -1 until first needed
int expectedModCount; // initialized when est set
int batch; // batch size for splits
LLSpliterator(EnhancedLinkedList<E> list, int est, int expectedModCount) {
this.list = list;
this.est = est;
this.expectedModCount = expectedModCount;
}
int getEst() {
int s; // force initialization
final EnhancedLinkedList<E> lst;
if ((s = est) < 0) {
if ((lst = list) == null)
s = est = 0;
else {
expectedModCount = lst.modCount;
current = lst.first;
s = est = lst.size;
}
}
return s;
}
public long estimateSize() { return getEst(); }
public Spliterator<E> trySplit() {
Node<E> p;
int s = getEst();
if (s > 1 && (p = current) != null) {
int n = batch + BATCH_UNIT;
if (n > s)
n = s;
if (n > MAX_BATCH)
n = MAX_BATCH;
Object[] a = new Object[n];
int j = 0;
do { a[j++] = p.item; } while ((p = p.next) != null && j < n);
current = p;
batch = j;
est = s - j;
return Spliterators.spliterator(a, 0, j, Spliterator.ORDERED);
}
return null;
}
public void forEachRemaining(Consumer<? super E> action) {
Node<E> p; int n;
if (action == null) throw new NullPointerException();
if ((n = getEst()) > 0 && (p = current) != null) {
current = null;
est = 0;
do {
E e = p.item;
p = p.next;
action.accept(e);
} while (p != null && --n > 0);
}
if (list.modCount != expectedModCount)
throw new ConcurrentModificationException();
}
public boolean tryAdvance(Consumer<? super E> action) {
Node<E> p;
if (action == null) throw new NullPointerException();
if (getEst() > 0 && (p = current) != null) {
--est;
E e = p.item;
current = p.next;
action.accept(e);
if (list.modCount != expectedModCount)
throw new ConcurrentModificationException();
return true;
}
return false;
}
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
}
}
} }

View File

@ -2,7 +2,6 @@ package cc.maxmc.blastingcrisis.command
import cc.maxmc.blastingcrisis.game.Game import cc.maxmc.blastingcrisis.game.Game
import cc.maxmc.blastingcrisis.game.GameOreGenerator import cc.maxmc.blastingcrisis.game.GameOreGenerator
import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule
import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule.MatchResult import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule.MatchResult
import cc.maxmc.blastingcrisis.game.GameState import cc.maxmc.blastingcrisis.game.GameState
import cc.maxmc.blastingcrisis.misc.Area import cc.maxmc.blastingcrisis.misc.Area
@ -18,10 +17,9 @@ import taboolib.common.platform.function.adaptCommandSender
import taboolib.common.platform.function.getDataFolder import taboolib.common.platform.function.getDataFolder
import taboolib.module.configuration.Configuration import taboolib.module.configuration.Configuration
import taboolib.platform.BukkitPlugin import taboolib.platform.BukkitPlugin
import taboolib.platform.util.broadcast
import taboolib.platform.util.sendActionBar import taboolib.platform.util.sendActionBar
import java.io.File import java.io.File
import java.util.UUID import java.util.*
import cc.maxmc.blastingcrisis.misc.debug as debuglog import cc.maxmc.blastingcrisis.misc.debug as debuglog
object DebugCommand { object DebugCommand {
@ -29,7 +27,7 @@ object DebugCommand {
lateinit var villager: BEntityVillager lateinit var villager: BEntityVillager
var autoRespawn = false var autoRespawn = false
val builders = ArrayList<UUID>() val builders = ArrayList<UUID>()
private val init by lazy{ private val init by lazy {
// allow builders // allow builders
debuglog("init builder rule") debuglog("init builder rule")
GameManager.currentGame.placeBreakRule.addRuleFirst("builder") { (player, _, _, _) -> GameManager.currentGame.placeBreakRule.addRuleFirst("builder") { (player, _, _, _) ->
@ -157,7 +155,7 @@ object DebugCommand {
} }
literal("build_mode") { literal("build_mode") {
execute<Player> { sender, _ ,_ -> execute<Player> { sender, _, _ ->
init init
if (builders.contains(sender.uniqueId)) { if (builders.contains(sender.uniqueId)) {
builders.remove(sender.uniqueId) builders.remove(sender.uniqueId)

View File

@ -1,6 +1,5 @@
package cc.maxmc.blastingcrisis.game package cc.maxmc.blastingcrisis.game
import cc.maxmc.blastingcrisis.command.DebugCommand
import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule.ActionType.BREAK import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule.ActionType.BREAK
import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule.ActionType.PLACE import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule.ActionType.PLACE
import cc.maxmc.blastingcrisis.misc.EnhancedLinkedList import cc.maxmc.blastingcrisis.misc.EnhancedLinkedList
@ -98,9 +97,9 @@ class GamePlaceBreakRule(val game: Game) {
addRuleLast("allow_wall_conditional") { (player, actionType, loc, _) -> addRuleLast("allow_wall_conditional") { (player, actionType, loc, _) ->
val team = player.team ?: return@addRuleLast MatchResult.DENY val team = player.team ?: return@addRuleLast MatchResult.DENY
val isWall = game.teams.map { val isWall = game.teams.map {
it.teamInfo.wall.containsBlock(loc) it.teamInfo.wall.containsBlock(loc)
.also { result -> debug("area ${it.teamInfo.wall} contains $loc is $result") } .also { result -> debug("area ${it.teamInfo.wall} contains $loc is $result") }
}.reduce { a, b -> a || b } }.reduce { a, b -> a || b }
if (!isWall) return@addRuleLast MatchResult.DEFAULT if (!isWall) return@addRuleLast MatchResult.DEFAULT
val isHome = team.teamInfo.wall.containsBlock(loc) val isHome = team.teamInfo.wall.containsBlock(loc)
if ((actionType == BREAK) xor isHome) MatchResult.ALLOW else MatchResult.DENY if ((actionType == BREAK) xor isHome) MatchResult.ALLOW else MatchResult.DENY
@ -111,9 +110,9 @@ class GamePlaceBreakRule(val game: Game) {
if (type != XMaterial.TNT) return@addRuleFirst MatchResult.DEFAULT if (type != XMaterial.TNT) return@addRuleFirst MatchResult.DEFAULT
val team = player.team ?: return@addRuleFirst MatchResult.DENY val team = player.team ?: return@addRuleFirst MatchResult.DENY
val isHome = game.teams.map { val isHome = game.teams.map {
it.teamInfo.home.containsBlock(loc) it.teamInfo.home.containsBlock(loc)
.also { result -> debug("area ${it.teamInfo.home} contains $loc is $result") } .also { result -> debug("area ${it.teamInfo.home} contains $loc is $result") }
}.reduce { a, b -> a || b } }.reduce { a, b -> a || b }
if (!isHome) return@addRuleFirst MatchResult.DENY if (!isHome) return@addRuleFirst MatchResult.DENY
val isSelf = team.teamInfo.home.containsBlock(loc) val isSelf = team.teamInfo.home.containsBlock(loc)
if ((actionType == PLACE) xor isSelf) MatchResult.ALLOW else MatchResult.DENY if ((actionType == PLACE) xor isSelf) MatchResult.ALLOW else MatchResult.DENY

View File

@ -96,7 +96,7 @@ object GameListener {
GameManager.currentGame.respawnPlayer(respawnEvent.player) GameManager.currentGame.respawnPlayer(respawnEvent.player)
} }
// @SubscribeEvent // @SubscribeEvent
fun protectDamageBeforeGame(damageEvent: EntityDamageEvent) { fun protectDamageBeforeGame(damageEvent: EntityDamageEvent) {
if (damageEvent.entity !is Player) return // ignore none player entity if (damageEvent.entity !is Player) return // ignore none player entity
if (GameManager.currentGame.state.isStarted()) return if (GameManager.currentGame.state.isStarted()) return

View File

@ -7,7 +7,6 @@ import gnu.trove.map.hash.TIntObjectHashMap
import net.minecraft.server.v1_8_R3.* import net.minecraft.server.v1_8_R3.*
import org.bukkit.Location import org.bukkit.Location
import org.bukkit.entity.Player import org.bukkit.entity.Player
import taboolib.common.platform.function.submit
import taboolib.common.util.random import taboolib.common.util.random
import taboolib.library.reflex.Reflex.Companion.getProperty import taboolib.library.reflex.Reflex.Companion.getProperty
import taboolib.library.reflex.Reflex.Companion.setProperty import taboolib.library.reflex.Reflex.Companion.setProperty

View File

@ -31,7 +31,6 @@ class BEntityVillager private constructor(loc: Location) : BEntity(loc, 120) {
} }
override fun DataWatcher.initEntity() { override fun DataWatcher.initEntity() {
a(6, 1.0f) // Health a(6, 1.0f) // Health
a(7, 0) // Potion Effect Color a(7, 0) // Potion Effect Color

View File

@ -1,4 +1,3 @@
# Time before game start # Time before game start
# Unit: second # Unit: second
time_to_start: 15 time_to_start: 15