Skip to content

Commit 4f5ab9e

Browse files
committed
Wrap and unwrap values in Optionals
1 parent f51427b commit 4f5ab9e

File tree

1 file changed

+63
-76
lines changed

1 file changed

+63
-76
lines changed

modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java

Lines changed: 63 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
2424
import com.comphenix.protocol.utility.MinecraftReflection;
2525
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
26+
import com.google.common.base.Optional;
2627

2728
/**
2829
* Represents a DataWatcher Item in 1.9.
@@ -96,43 +97,6 @@ public Object getRawValue() {
9697
return modifier.readSafely(1);
9798
}
9899

99-
/**
100-
* Retrieve the wrapped object value, if needed.
101-
*
102-
* @param value - the raw NMS object to wrap.
103-
* @return The wrapped object.
104-
*/
105-
@SuppressWarnings("rawtypes")
106-
static Object getWrapped(Object value) {
107-
// Handle the special cases
108-
if (MinecraftReflection.isItemStack(value)) {
109-
return BukkitConverters.getItemStackConverter().getSpecific(value);
110-
} else if (MinecraftReflection.isChunkCoordinates(value)) {
111-
return new WrappedChunkCoordinate((Comparable) value);
112-
} else {
113-
return value;
114-
}
115-
}
116-
117-
/**
118-
* Retrieve the wrapped type, if needed.
119-
*
120-
* @param wrapped - the wrapped class type.
121-
* @return The wrapped class type.
122-
*/
123-
static Class<?> getWrappedType(Class<?> unwrapped) {
124-
if (unwrapped.equals(MinecraftReflection.getChunkPositionClass()))
125-
return ChunkPosition.class;
126-
else if (unwrapped.equals(MinecraftReflection.getBlockPositionClass()))
127-
return BlockPosition.class;
128-
else if (unwrapped.equals(MinecraftReflection.getChunkCoordinatesClass()))
129-
return WrappedChunkCoordinate.class;
130-
else if (unwrapped.equals(MinecraftReflection.getItemStackClass()))
131-
return ItemStack.class;
132-
else
133-
return unwrapped;
134-
}
135-
136100
/**
137101
* Sets the value of this item.
138102
* @param value New value
@@ -154,45 +118,6 @@ public void setValue(Object value) {
154118
setValue(value, false);
155119
}
156120

157-
/**
158-
* Retrieve the raw NMS value.
159-
*
160-
* @param wrapped - the wrapped position.
161-
* @return The raw NMS object.
162-
*/
163-
static Object getUnwrapped(Object wrapped) {
164-
// Convert special cases
165-
if (wrapped instanceof ChunkPosition)
166-
return ChunkPosition.getConverter().getGeneric(MinecraftReflection.getChunkPositionClass(), (ChunkPosition) wrapped);
167-
else if (wrapped instanceof BlockPosition)
168-
return BlockPosition.getConverter().getGeneric(MinecraftReflection.getBlockPositionClass(), (BlockPosition) wrapped);
169-
else if (wrapped instanceof WrappedChunkCoordinate)
170-
return ((WrappedChunkCoordinate) wrapped).getHandle();
171-
else if (wrapped instanceof ItemStack)
172-
return BukkitConverters.getItemStackConverter().getGeneric(MinecraftReflection.getItemStackClass(), (ItemStack) wrapped);
173-
else
174-
return wrapped;
175-
}
176-
177-
/**
178-
* Retrieve the unwrapped type, if needed.
179-
*
180-
* @param wrapped - the unwrapped class type.
181-
* @return The unwrapped class type.
182-
*/
183-
static Class<?> getUnwrappedType(Class<?> wrapped) {
184-
if (wrapped.equals(ChunkPosition.class))
185-
return MinecraftReflection.getChunkPositionClass();
186-
else if (wrapped.equals(BlockPosition.class))
187-
return MinecraftReflection.getBlockPositionClass();
188-
else if (wrapped.equals(WrappedChunkCoordinate.class))
189-
return MinecraftReflection.getChunkCoordinatesClass();
190-
else if (ItemStack.class.isAssignableFrom(wrapped))
191-
return MinecraftReflection.getItemStackClass();
192-
else
193-
return wrapped;
194-
}
195-
196121
/**
197122
* Whether or not the value must be synchronized with the client.
198123
* @return True if it must, false if not
@@ -229,4 +154,66 @@ && getRawValue().equals(other.getRawValue())
229154
public String toString() {
230155
return "DataWatcherItem[object=" + getWatcherObject() + ", value=" + getValue() + ", dirty=" + getDirtyState() + "]";
231156
}
157+
158+
// ---- Wrapping
159+
160+
/**
161+
* Retrieve the wrapped object value, if needed.
162+
*
163+
* @param value - the raw NMS object to wrap.
164+
* @return The wrapped object.
165+
*/
166+
@SuppressWarnings("rawtypes")
167+
static Object getWrapped(Object value) {
168+
// Deal with optionals first
169+
if (value instanceof Optional) {
170+
Optional<?> optional = (Optional<?>) value;
171+
if (optional.isPresent()) {
172+
return Optional.of(getWrapped(optional.get()));
173+
} else {
174+
return Optional.absent();
175+
}
176+
}
177+
178+
if (MinecraftReflection.isItemStack(value)) {
179+
return BukkitConverters.getItemStackConverter().getSpecific(value);
180+
} else if (MinecraftReflection.isChunkCoordinates(value)) {
181+
return new WrappedChunkCoordinate((Comparable) value);
182+
} else if (MinecraftReflection.isBlockPosition(value)) {
183+
return BlockPosition.getConverter().getSpecific(value);
184+
} else if (MinecraftReflection.isChunkPosition(value)) {
185+
return ChunkPosition.getConverter().getSpecific(value);
186+
} else {
187+
return value;
188+
}
189+
}
190+
191+
/**
192+
* Retrieve the raw NMS value.
193+
*
194+
* @param wrapped - the wrapped position.
195+
* @return The raw NMS object.
196+
*/
197+
static Object getUnwrapped(Object wrapped) {
198+
if (wrapped instanceof Optional) {
199+
Optional<?> optional = (Optional<?>) wrapped;
200+
if (optional.isPresent()) {
201+
return Optional.of(getUnwrapped(optional.get()));
202+
} else {
203+
return Optional.absent();
204+
}
205+
}
206+
207+
if (wrapped instanceof ItemStack) {
208+
return BukkitConverters.getItemStackConverter().getGeneric(MinecraftReflection.getItemStackClass(), (ItemStack) wrapped);
209+
} else if (wrapped instanceof WrappedChunkCoordinate) {
210+
return ((WrappedChunkCoordinate) wrapped).getHandle();
211+
} else if (wrapped instanceof BlockPosition) {
212+
return BlockPosition.getConverter().getGeneric(MinecraftReflection.getBlockPositionClass(), (BlockPosition) wrapped);
213+
} else if (wrapped instanceof ChunkPosition) {
214+
return ChunkPosition.getConverter().getGeneric(MinecraftReflection.getChunkPositionClass(), (ChunkPosition) wrapped);
215+
} else {
216+
return wrapped;
217+
}
218+
}
232219
}

0 commit comments

Comments
 (0)