This commit is contained in:
Felix Cravic 2020-08-03 00:37:03 +02:00
parent 1b324624b9
commit 134173d0f1
13 changed files with 119 additions and 32 deletions

View File

@ -13,6 +13,7 @@ import java.util.regex.Pattern;
*/
public class ColoredText {
// the raw text
private String message;
// true if the compiled string is up-to-date, false otherwise
@ -63,7 +64,7 @@ public class ColoredText {
String result = "";
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
final char c = message.charAt(i);
if (c == colorChar) {
final boolean hasNextChar = i < message.length();
if (hasNextChar) {
@ -85,6 +86,16 @@ public class ColoredText {
return result;
}
public ColoredText appendLegacy(String message, char colorChar) {
String legacy = toLegacy(message, colorChar);
return appendFormat(legacy);
}
/**
* Get the raw text
*
* @return the raw text
*/
public String getMessage() {
return message;
}
@ -112,7 +123,7 @@ public class ColoredText {
* @return the Json representation of the text
*/
public JsonObject getJsonObject() {
List<JsonObject> components = getComponents();
final List<JsonObject> components = getComponents();
// No message, return empty object
if (components.isEmpty()) {
@ -155,9 +166,9 @@ public class ColoredText {
for (int i = 0; i < message.length(); i++) {
// Last char or null
Character p = i == 0 ? null : message.charAt(i - 1);
final Character p = i == 0 ? null : message.charAt(i - 1);
// Current char
char c = message.charAt(i);
final char c = message.charAt(i);
if ((p == null || (p != '/')) && c == '{' && !inFormat) {
formatEnd = formatEnd > 0 ? formatEnd + 1 : formatEnd;
@ -237,7 +248,7 @@ public class ColoredText {
}
// Keybind component
if (formatString.startsWith("&")) {
String keybindCode = formatString.substring(1);
final String keybindCode = formatString.substring(1);
objects.add(getMessagePart(MessageType.KEYBIND, keybindCode, currentColor, specialComponentContainer));
continue;
}
@ -246,7 +257,7 @@ public class ColoredText {
// Add the remaining of the message as a raw message when any
if (formatEnd < message.length()) {
String lastRawMessage = message.substring(formatEnd + 1);
final String lastRawMessage = message.substring(formatEnd + 1);
objects.add(getMessagePart(MessageType.RAW, lastRawMessage, currentColor, specialComponentContainer));
}
@ -301,23 +312,26 @@ public class ColoredText {
RAW, KEYBIND, TRANSLATABLE
}
public ColoredText appendLegacy(String message, char colorChar) {
String legacy = toLegacy(message, colorChar);
return appendFormat(legacy);
}
private static class SpecialComponentContainer {
boolean bold = false;
boolean italic = false;
boolean underlined = false;
boolean strikethrough = false;
boolean obfuscated = false;
private void reset() {
this.bold = false;
this.italic = false;
this.underlined = false;
this.strikethrough = false;
this.obfuscated = false;
}
}

View File

@ -32,6 +32,16 @@ public class BoundingBox {
(getMinZ() <= boundingBox.getMaxZ() && getMaxZ() >= boundingBox.getMinZ());
}
/**
* Used to know if this bounding box intersects with the bounding box of an entity
*
* @param entity the entity to check the bounding box
* @return true if this bounding box intersects with the entity, false otherwise
*/
public boolean intersect(Entity entity) {
return intersect(entity.getBoundingBox());
}
/**
* Used to know if the bounding box intersects with a block (can be air)
*
@ -192,7 +202,7 @@ public class BoundingBox {
@Override
public String toString() {
String result = "BoudingBox";
String result = "BoundingBox";
result += "\n";
result += "[" + getMinX() + " : " + getMaxX() + "]";
result += "\n";

View File

@ -86,8 +86,8 @@ public class CollisionUtils {
}
float sign = Math.signum(stepAmount);
int blockLength = (int) stepAmount;
float remainingLength = stepAmount - blockLength;
final int blockLength = (int) stepAmount;
final float remainingLength = stepAmount - blockLength;
// used to determine if 'remainingLength' should be used
boolean collisionFound = false;
for (int i = 0; i < Math.abs(blockLength); i++) {
@ -130,7 +130,7 @@ public class CollisionUtils {
* @return
*/
private static boolean stepOnce(Instance instance, Vector axis, float amount, Vector[] cornersCopy, BlockPosition[] cornerPositions) {
float sign = Math.signum(amount);
final float sign = Math.signum(amount);
for (int cornerIndex = 0; cornerIndex < cornersCopy.length; cornerIndex++) {
Vector corner = cornersCopy[cornerIndex];
BlockPosition blockPos = cornerPositions[cornerIndex];
@ -138,8 +138,8 @@ public class CollisionUtils {
blockPos.setX((int) Math.floor(corner.getX()));
blockPos.setY((int) Math.floor(corner.getY()));
blockPos.setZ((int) Math.floor(corner.getZ()));
short blockId = instance.getBlockId(blockPos);
Block block = Block.fromId(blockId);
final short blockId = instance.getBlockId(blockPos);
final Block block = Block.fromId(blockId);
// TODO: block collision boxes
// TODO: for the moment, always consider a full block

View File

@ -100,7 +100,7 @@ public class Data {
*/
public Data clone() {
Data data = new Data();
data.data = new ConcurrentHashMap<>(this.data);
data.data.putAll(this.data);
return data;
}

View File

@ -2,8 +2,18 @@ package net.minestom.server.data;
public interface DataContainer {
/**
* Get the data of this container
*
* @return the data of this container, can be null
*/
Data getData();
/**
* Set the data object of this container
*
* @param data the data of this container
*/
void setData(Data data);
}

View File

@ -60,6 +60,7 @@ public final class DataManager {
* @param clazz the data class
* @param dataType the data type associated
* @param <T> the data type
* @throws IllegalStateException if the type {@code clazz} is already registered
*/
public <T> void registerType(Class<T> clazz, DataType<T> dataType) {
clazz = PrimitiveConversion.getObjectClass(clazz);

View File

@ -5,8 +5,20 @@ import net.minestom.server.network.packet.PacketWriter;
public abstract class DataType<T> {
/**
* Encode the data type
*
* @param packetWriter the data writer
* @param value the value to encode
*/
public abstract void encode(PacketWriter packetWriter, T value);
/**
* Decode the data type
*
* @param packetReader the data readerr
* @return the decoded value
*/
public abstract T decode(PacketReader packetReader);
}

View File

@ -40,8 +40,8 @@ public class SerializableData extends Data {
@Override
public Data clone() {
SerializableData data = new SerializableData();
data.data = new ConcurrentHashMap<>(this.data);
data.dataType = new ConcurrentHashMap<>(this.dataType);
data.data.putAll(this.data);
data.dataType.putAll(this.dataType);
return data;
}
@ -59,22 +59,22 @@ public class SerializableData extends Data {
DataOutputStream dos = new DataOutputStream(output);
for (Map.Entry<String, Object> entry : data.entrySet()) {
String key = entry.getKey();
Class type = dataType.get(key);
Object value = entry.getValue();
DataType dataType = DATA_MANAGER.getDataType(type);
final String key = entry.getKey();
final Class type = dataType.get(key);
final Object value = entry.getValue();
final DataType dataType = DATA_MANAGER.getDataType(type);
byte[] encodedType = PrimitiveConversion.getObjectClassString(type.getName()).getBytes(); // Data type (fix for primitives)
final byte[] encodedType = PrimitiveConversion.getObjectClassString(type.getName()).getBytes(); // Data type (fix for primitives)
dos.writeShort(encodedType.length);
dos.write(encodedType);
byte[] encodedName = key.getBytes(); // Data name
final byte[] encodedName = key.getBytes(); // Data name
dos.writeShort(encodedName.length);
dos.write(encodedName);
PacketWriter packetWriter = new PacketWriter();
dataType.encode(packetWriter, value); // Encode
byte[] encodedValue = packetWriter.toByteArray(); // Retrieve bytes
final byte[] encodedValue = packetWriter.toByteArray(); // Retrieve bytes
dos.writeInt(encodedValue.length);
dos.write(encodedValue);
}

View File

@ -12,9 +12,10 @@ public abstract class ObjectEntity extends Entity {
}
/**
* Objects data can be found <a href="https://wiki.vg/Object_Data">here</a>
* Get the data of this object entity
*
* @return an object data
* @see <a href="https://wiki.vg/Object_Data">here</a>
*/
public abstract int getObjectData();
@ -34,7 +35,7 @@ public abstract class ObjectEntity extends Entity {
if (!result)
return false;
PlayerConnection playerConnection = player.getPlayerConnection();
final PlayerConnection playerConnection = player.getPlayerConnection();
SpawnEntityPacket spawnEntityPacket = new SpawnEntityPacket();
spawnEntityPacket.entityId = getEntityId();

View File

@ -51,8 +51,8 @@ public class PlayerSkin {
try {
final String response = URLUtils.getText(url);
JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject();
JsonArray propertiesArray = jsonObject.get("properties").getAsJsonArray();
final JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject();
final JsonArray propertiesArray = jsonObject.get("properties").getAsJsonArray();
Iterator<JsonElement> iterator = propertiesArray.iterator();
while (iterator.hasNext()) {

View File

@ -276,7 +276,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
/**
* Insert safely an item into the inventory
* <p>
* This will update the slot for all viewers
* This will update the slot for all viewers and warn the inventory that
* the window items packet is not up-to-date
*
* @param slot the internal slot id
* @param itemStack the item to insert
@ -294,6 +295,15 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
this.windowItemsBufferUpdated = false;
}
/**
* Insert an item into the inventory without notifying viewers
* <p>
* This will also warn the inventory that the cached window items packet is
* not up-to-date
*
* @param slot the internal slot
* @param itemStack the item to insert
*/
protected void setItemStackInternal(int slot, ItemStack itemStack) {
itemStacks[slot] = itemStack;
this.windowItemsBufferUpdated = false;
@ -337,6 +347,13 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
return windowItemsPacket;
}
/**
* Send a window property to all viewers
*
* @param property the property to send
* @param value the value of the property
* @see <a href="https://wiki.vg/Protocol#Window_Property</a>
*/
protected void sendProperty(InventoryProperty property, short value) {
WindowPropertyPacket windowPropertyPacket = new WindowPropertyPacket();
windowPropertyPacket.windowId = getWindowId();
@ -345,6 +362,14 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
sendPacketToViewers(windowPropertyPacket);
}
/**
* Change the internal player's cursor item
* <p>
* WARNING: the player will not be notified by the change
*
* @param player the player to change the cursor item
* @param itemStack the cursor item
*/
private void setCursorPlayerItem(Player player, ItemStack itemStack) {
this.cursorPlayersItem.put(player, itemStack);
}
@ -596,6 +621,14 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
return !clickResult.isCancel();
}
/**
* Refresh a slot for all viewers
* <p>
* WARNING: this does not update the items in the inventory, this is only visual
*
* @param slot the packet slot
* @param itemStack the item stack to set at the slot
*/
private void sendSlotRefresh(short slot, ItemStack itemStack) {
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = getWindowId();

View File

@ -4,5 +4,6 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.function.Consumer;
@FunctionalInterface
public interface NBTConsumer extends Consumer<NBTCompound> {
}

View File

@ -50,6 +50,11 @@ public abstract class StackingRule {
*/
public abstract int getAmount(ItemStack itemStack);
/**
* Get the max size of a stack
*
* @return the max size of a stack
*/
public int getMaxSize() {
return maxSize;
}