Use primitive types in Position and BlockFace, remove world block connections on Sponge, clean code in 1.8 entity tracker, add constructor for cloning Item and Position

This commit is contained in:
creeper123123321 2019-11-22 15:13:02 -03:00
parent faaa252fa8
commit 062d3759be
17 changed files with 131 additions and 150 deletions

View File

@ -21,11 +21,11 @@ public class BukkitBlockConnectionProvider extends BlockConnectionProvider {
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
World world = player.getWorld();
int x = (int) (position.getX() >> 4);
int z = (int) (position.getZ() >> 4);
int x = position.getPosX() >> 4;
int z = position.getPosZ() >> 4;
if (world.isChunkLoaded(x, z)) {
Chunk c = getChunk(world, x, z);
Block b = c.getBlock(position.getX().intValue(), position.getY().intValue(), position.getZ().intValue());
Block b = c.getBlock(position.getPosX(), position.getPosY(), position.getPosZ());
return b.getTypeId() << 4 | b.getData();
}
}

View File

@ -9,12 +9,12 @@ import java.util.Map;
@Getter
@AllArgsConstructor
public enum BlockFace {
NORTH(0, 0, -1, EnumAxis.Z),
SOUTH(0, 0, 1, EnumAxis.Z),
EAST(1, 0, 0, EnumAxis.X),
WEST(-1, 0, 0, EnumAxis.X),
TOP(0, 1, 0, EnumAxis.Y),
BOTTOM(0, -1, 0, EnumAxis.Y);
NORTH((byte) 0, (byte) 0, (byte) -1, EnumAxis.Z),
SOUTH((byte) 0, (byte) 0, (byte) 1, EnumAxis.Z),
EAST((byte) 1, (byte) 0, (byte) 0, EnumAxis.X),
WEST((byte) -1, (byte) 0, (byte) 0, EnumAxis.X),
TOP((byte) 0, (byte) 1, (byte) 0, EnumAxis.Y),
BOTTOM((byte) 0, (byte) -1, (byte) 0, EnumAxis.Y);
private static Map<BlockFace, BlockFace> opposites = new HashMap<>();
@ -27,7 +27,7 @@ public enum BlockFace {
opposites.put(BlockFace.BOTTOM, BlockFace.TOP);
}
private int modX, modY, modZ;
private byte modX, modY, modZ;
private EnumAxis axis;
public BlockFace opposite() {
@ -35,6 +35,6 @@ public enum BlockFace {
}
public enum EnumAxis {
X, Y, Z;
X, Y, Z
}
}

View File

@ -10,18 +10,59 @@ import lombok.ToString;
@ToString
@EqualsAndHashCode
public class Position {
private Long x;
private Long y;
private Long z;
private int posX;
private short posY;
private int posZ;
@Deprecated
public Position(Long x, Long y, Long z) {
this.posX = x.intValue();
this.posY = y.shortValue();
this.posZ = z.intValue();
}
public Position(Position toCopy) {
this(toCopy.getPosX(), toCopy.getPosY(), toCopy.getPosZ());
}
@Deprecated
public void setX(Long x) {
this.posX = x.intValue();
}
@Deprecated
public void setY(Long y) {
this.posY = y.shortValue();
}
@Deprecated
public void setZ(Long z) {
this.posZ = z.intValue();
}
@Deprecated
public Long getX() {
return (long) this.posX;
}
@Deprecated
public Long getY() {
return (long) this.posY;
}
@Deprecated
public Long getZ() {
return (long) this.posZ;
}
public Position getRelative(BlockFace face) {
return new Position(this.x + face.getModX(), this.y + face.getModY(), this.z + face.getModZ());
return new Position(posX + face.getModX(), (short) (posY + face.getModY()), posZ + face.getModZ());
}
public Position shift(BlockFace face) {
this.x += face.getModX();
this.y += face.getModY();
this.z += face.getModZ();
this.posX += face.getModX();
this.posY += face.getModY();
this.posZ += face.getModZ();
return this;
}
}

View File

@ -34,4 +34,8 @@ public class Item {
this.data = data;
this.tag = tag;
}
public Item(Item toCopy) {
this(toCopy.getIdentifier(), toCopy.getAmount(), toCopy.getData(), toCopy.getTag());
}
}

View File

@ -17,11 +17,13 @@ public class Position1_14Type extends Type<Position> {
long y = val << 52 >> 52;
long z = val << 26 >> 38;
return new Position(x, y, z);
return new Position((int) x, (short) y, (int) z);
}
@Override
public void write(ByteBuf buffer, Position object) {
buffer.writeLong(((object.getX() & 0x3ffffff) << 38) | (object.getY() & 0xfff) | ((object.getZ() & 0x3ffffff) << 12));
buffer.writeLong((((long) object.getPosX() & 0x3ffffff) << 38)
| (object.getPosY() & 0xfff)
| ((object.getPosZ() & 0x3ffffff) << 12));
}
}

View File

@ -17,11 +17,13 @@ public class PositionType extends Type<Position> {
// this shifting madness is used to preserve sign
long z = (val << 38) >> 38; // signed
return new Position(x, y, z);
return new Position((int) x, (short) y, (int) z);
}
@Override
public void write(ByteBuf buffer, Position object) {
buffer.writeLong(((object.getX() & 0x3ffffff) << 38) | ((object.getY() & 0xfff) << 26) | (object.getZ() & 0x3ffffff));
buffer.writeLong((((long) object.getPosX() & 0x3ffffff) << 38)
| ((object.getPosY() & 0xfff) << 26)
| (object.getPosZ() & 0x3ffffff));
}
}

View File

@ -47,9 +47,9 @@ public class Protocol1_13To1_12_2 extends Protocol {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Position position = wrapper.read(Type.POSITION);
wrapper.write(Type.INT, position.getX().intValue());
wrapper.write(Type.INT, position.getY().intValue());
wrapper.write(Type.INT, position.getZ().intValue());
wrapper.write(Type.INT, position.getPosX());
wrapper.write(Type.INT, (int) position.getPosY());
wrapper.write(Type.INT, position.getPosZ());
}
};
@ -515,14 +515,11 @@ public class Protocol1_13To1_12_2 extends Protocol {
Item[] clone = ingredient.clone(); // Clone because array and item is mutable
for (int i = 0; i < clone.length; i++) {
if (clone[i] == null) continue;
clone[i] = new Item(clone[i].getId(), clone[i].getAmount(),
(short) 0, null);
clone[i] = new Item(new Item(clone[i]));
}
wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
}
wrapper.write(Type.FLAT_ITEM, new Item(
entry.getValue().getResult().getId(),
entry.getValue().getResult().getAmount(), (short) 0, null));
wrapper.write(Type.FLAT_ITEM, new Item(entry.getValue().getResult()));
break;
}
case "crafting_shaped": {
@ -533,14 +530,11 @@ public class Protocol1_13To1_12_2 extends Protocol {
Item[] clone = ingredient.clone(); // Clone because array and item is mutable
for (int i = 0; i < clone.length; i++) {
if (clone[i] == null) continue;
clone[i] = new Item(clone[i].getId(), clone[i].getAmount(),
(short) 0, null);
clone[i] = new Item(new Item(clone[i]));
}
wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
}
wrapper.write(Type.FLAT_ITEM, new Item(
entry.getValue().getResult().getId(),
entry.getValue().getResult().getAmount(), (short) 0, null));
wrapper.write(Type.FLAT_ITEM, new Item(entry.getValue().getResult()));
break;
}
case "smelting": {
@ -548,13 +542,10 @@ public class Protocol1_13To1_12_2 extends Protocol {
Item[] clone = entry.getValue().getIngredient().clone(); // Clone because array and item is mutable
for (int i = 0; i < clone.length; i++) {
if (clone[i] == null) continue;
clone[i] = new Item(clone[i].getId(), clone[i].getAmount(),
(short) 0, null);
clone[i] = new Item(clone[i]);
}
wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
wrapper.write(Type.FLAT_ITEM, new Item(
entry.getValue().getResult().getId(),
entry.getValue().getResult().getAmount(), (short) 0, null));
wrapper.write(Type.FLAT_ITEM, new Item(entry.getValue().getResult()));
wrapper.write(Type.FLOAT, entry.getValue().getExperience());
wrapper.write(Type.VAR_INT, entry.getValue().getCookingTime());
break;

View File

@ -30,11 +30,7 @@ public class ConnectionData {
public static void update(UserConnection user, Position position) {
BlockConnectionProvider connectionProvider = Via.getManager().getProviders().get(BlockConnectionProvider.class);
for (BlockFace face : BlockFace.values()) {
Position pos = new Position(
position.getX() + face.getModX(),
position.getY() + face.getModY(),
position.getZ() + face.getModZ()
);
Position pos = position.getRelative(face);
int blockState = connectionProvider.getBlockdata(user, pos);
ConnectionHandler handler = connectionHandlerMap.get(blockState);
if (handler == null) continue;
@ -64,9 +60,9 @@ public class ConnectionData {
int blockPosZ = chunkDeltaZ == 1 ? 0 : 15;
updateBlock(user,
new Position(
(long) ((chunkX + chunkDeltaX) << 4) + blockPosX,
(long) blockY,
(long) ((chunkZ + chunkDeltaZ) << 4) + blockPosZ
((chunkX + chunkDeltaX) << 4) + blockPosX,
(short) blockY,
((chunkZ + chunkDeltaZ) << 4) + blockPosZ
),
updates
);
@ -102,9 +98,9 @@ public class ConnectionData {
for (int blockZ = zStart; blockZ < zEnd; blockZ++) {
updateBlock(user,
new Position(
(long) ((chunkX + chunkDeltaX) << 4) + blockX,
(long) blockY,
(long) ((chunkZ + chunkDeltaZ) << 4) + blockZ),
((chunkX + chunkDeltaX) << 4) + blockX,
(short) blockY,
((chunkZ + chunkDeltaZ) << 4) + blockZ),
updates
);
}
@ -133,7 +129,7 @@ public class ConnectionData {
if (handler == null) return;
int newBlockState = handler.connect(user, pos, blockState);
records.add(new BlockChangeRecord((short) (((pos.getX() & 0xF) << 4) | (pos.getZ() & 0xF)), pos.getY().shortValue(), newBlockState));
records.add(new BlockChangeRecord((short) (((pos.getPosX() & 0xF) << 4) | (pos.getPosZ() & 0xF)), pos.getPosY(), newBlockState));
}
public static BlockConnectionProvider getProvider() {
@ -186,7 +182,11 @@ public class ConnectionData {
ConnectionHandler handler = ConnectionData.getConnectionHandler(block);
if (handler != null) {
block = handler.connect(user, new Position(xOff + x, yOff + y, zOff + z), block);
block = handler.connect(user, new Position(
(int) (xOff + x),
(short) (yOff + y),
(short) (zOff + z)
), block);
section.setFlatBlock(x, y, z, block);
}
}
@ -282,7 +282,7 @@ public class ConnectionData {
}
public static int getId(String key) {
return keyToId.containsKey(key) ? keyToId.get(key) : -1;
return keyToId.getOrDefault(key, -1);
}
public static String getKey(int id) {

View File

@ -152,7 +152,7 @@ public class WorldPackets {
if (blockId == 73) { // Note block
PacketWrapper blockChange = wrapper.create(0x0B); // block change
blockChange.write(Type.POSITION, new Position(pos.getX(), pos.getY(), pos.getZ())); // Clone because position is mutable
blockChange.write(Type.POSITION, new Position(pos)); // Clone because position is mutable
blockChange.write(Type.VAR_INT, 249 + (action * 24 * 2) + (param * 2));
blockChange.send(Protocol1_13To1_12_2.class, true, true);
}
@ -211,9 +211,9 @@ public class WorldPackets {
for (BlockChangeRecord record : records) {
int newBlock = toNewId(record.getBlockId());
Position position = new Position(
(long) (record.getHorizontal() >> 4 & 15) + (chunkX * 16),
(long) record.getY(),
(long) (record.getHorizontal() & 15) + (chunkZ * 16));
(record.getHorizontal() >> 4 & 15) + (chunkX * 16),
record.getY(),
(record.getHorizontal() & 15) + (chunkZ * 16));
if (Via.getConfig().isServersideBlockConnections()) {
ConnectionData.updateBlockStorage(userConnection, position, newBlock);
@ -339,9 +339,9 @@ public class WorldPackets {
int block = section.getFlatBlock(x, y, z);
if (storage.isWelcome(block)) {
storage.store(new Position(
(long) (x + (chunk.getX() << 4)),
(long) (y + (i << 4)),
(long) (z + (chunk.getZ() << 4))
(x + (chunk.getX() << 4)),
(short) (y + (i << 4)),
(z + (chunk.getZ() << 4))
), block);
}
}
@ -355,9 +355,9 @@ public class WorldPackets {
for (int x = 0; x < 16; x++) {
int block = section.getFlatBlock(x, y, z);
if (ConnectionData.isWelcome(block)) {
ConnectionData.getProvider().storeBlock(wrapper.user(), (long) (x + (chunk.getX() << 4)),
(long) (y + (i << 4)),
(long) (z + (chunk.getZ() << 4)),
ConnectionData.getProvider().storeBlock(wrapper.user(), x + (chunk.getX() << 4),
y + (i << 4),
z + (chunk.getZ() << 4),
block);
}
}

View File

@ -112,7 +112,7 @@ public class BlockConnectionStorage extends StoredObject {
}
private long getChunkSectionIndex(Position position) {
return getChunkSectionIndex(position.getX().intValue(), position.getY().intValue(), position.getZ().intValue());
return getChunkSectionIndex(position.getPosX(), position.getPosY(), position.getPosZ());
}
private short encodeBlockPos(int x, int y, int z) {
@ -120,7 +120,7 @@ public class BlockConnectionStorage extends StoredObject {
}
private short encodeBlockPos(Position pos) {
return encodeBlockPos(pos.getX().intValue(), pos.getY().intValue(), pos.getZ().intValue());
return encodeBlockPos(pos.getPosX(), pos.getPosY(), pos.getPosZ());
}
private <T> Map<Long, T> createLongObjectMap() {

View File

@ -55,9 +55,9 @@ public class Protocol1_9_3To1_9_1_2 extends Protocol {
//Create nbt
CompoundTag tag = new CompoundTag("");
tag.put(new StringTag("id", "Sign"));
tag.put(new IntTag("x", position.getX().intValue()));
tag.put(new IntTag("y", position.getY().intValue()));
tag.put(new IntTag("z", position.getZ().intValue()));
tag.put(new IntTag("x", position.getPosX()));
tag.put(new IntTag("y", position.getPosY()));
tag.put(new IntTag("z", position.getPosZ()));
for (int i = 0; i < lines.length; i++)
tag.put(new StringTag("Text" + (i + 1), lines[i]));

View File

@ -299,7 +299,7 @@ public class WorldPackets {
if (Via.getConfig().isShieldBlocking()) {
EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class);
if (item != null && Protocol1_9To1_8.isSword(item.getId())) {
if (item != null && Protocol1_9To1_8.isSword(item.getIdentifier())) {
if (hand == 0) {
if (!tracker.isBlocking()) {
tracker.setBlocking(true);
@ -363,9 +363,9 @@ public class WorldPackets {
if (face == 255)
return;
Position p = wrapper.get(Type.POSITION, 0);
long x = p.getX();
long y = p.getY();
long z = p.getZ();
int x = p.getPosX();
short y = p.getPosY();
int z = p.getPosZ();
switch (face) {
case 0:
y--;

View File

@ -32,7 +32,7 @@ public class CommandBlockStorage extends StoredObject {
Pair<Integer, Integer> chunkPos = getChunkCoords(position);
if (!storedCommandBlocks.containsKey(chunkPos))
storedCommandBlocks.put(chunkPos, new ConcurrentHashMap<Position, CompoundTag>());
storedCommandBlocks.put(chunkPos, new ConcurrentHashMap<>());
Map<Position, CompoundTag> blocks = storedCommandBlocks.get(chunkPos);
@ -44,8 +44,8 @@ public class CommandBlockStorage extends StoredObject {
}
private Pair<Integer, Integer> getChunkCoords(Position position) {
int chunkX = (int) Math.floor(position.getX() / 16);
int chunkZ = (int) Math.floor(position.getZ() / 16);
int chunkX = Math.floorDiv(position.getPosX(), 16);
int chunkZ = Math.floorDiv(position.getPosZ(), 16);
return new Pair<>(chunkX, chunkZ);
}

View File

@ -38,7 +38,11 @@ public class EntityTracker1_9 extends EntityTracker {
private final Map<Integer, BossBar> bossBarMap = new ConcurrentHashMap<>();
private final Set<Integer> validBlocking = Sets.newConcurrentHashSet();
private final Set<Integer> knownHolograms = Sets.newConcurrentHashSet();
private final Cache<Position, Integer> blockInteractions = CacheBuilder.newBuilder().maximumSize(10).expireAfterAccess(250, TimeUnit.MILLISECONDS).build();
private final Set<Position> blockInteractions = Collections.newSetFromMap(CacheBuilder.newBuilder()
.maximumSize(10)
.expireAfterAccess(250, TimeUnit.MILLISECONDS)
.<Position, Boolean>build()
.asMap());
@Setter
private boolean blocking = false;
@Setter
@ -100,19 +104,11 @@ public class EntityTracker1_9 extends EntityTracker {
}
public boolean interactedBlockRecently(int x, int y, int z) {
if (blockInteractions.size() == 0)
return false;
for (Position p : blockInteractions.asMap().keySet()) {
if (p.getX() == x)
if (p.getY() == y)
if (p.getZ() == z)
return true;
}
return false;
return blockInteractions.contains(new Position(x, (short) y , z));
}
public void addBlockInteraction(Position p) {
blockInteractions.put(p, 0);
blockInteractions.add(p);
}
public void handleMetadata(int entityId, List<Metadata> metadataList) {

View File

@ -12,7 +12,9 @@ import java.util.Map;
import java.util.Optional;
public class SpongeViaConfig extends AbstractViaConfig {
private static final List<String> UNSUPPORTED = Arrays.asList("anti-xray-patch", "bungee-ping-interval", "bungee-ping-save", "bungee-servers", "velocity-ping-interval", "velocity-ping-save", "velocity-servers", "quick-move-action-fix", "change-1_9-hitbox", "change-1_14-hitbox");
private static final List<String> UNSUPPORTED = Arrays.asList("anti-xray-patch", "bungee-ping-interval",
"bungee-ping-save", "bungee-servers", "velocity-ping-interval", "velocity-ping-save", "velocity-servers",
"quick-move-action-fix", "change-1_9-hitbox", "change-1_14-hitbox", "blockconnection-method");
private final PluginContainer pluginContainer;
public SpongeViaConfig(PluginContainer pluginContainer, File configFile) {
@ -59,4 +61,9 @@ public class SpongeViaConfig extends AbstractViaConfig {
public boolean is1_14HitboxFix() {
return false;
}
@Override
public String getBlockConnectionMethod() {
return "packet";
}
}

View File

@ -8,7 +8,6 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaPlatformLoader;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTranslatorProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider;
@ -19,7 +18,6 @@ import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.DeathListener;
import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.HandItemCache;
import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.sponge4.Sponge4ArmorListener;
import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.sponge5.Sponge5ArmorListener;
import us.myles.ViaVersion.sponge.providers.SpongeBlockConnectionProvider;
import us.myles.ViaVersion.sponge.providers.SpongeViaBulkChunkTranslator;
import us.myles.ViaVersion.sponge.providers.SpongeViaMovementTransmitter;
@ -80,9 +78,6 @@ public class SpongeViaLoader implements ViaPlatformLoader {
}
}
});
if (Via.getConfig().getBlockConnectionMethod().equalsIgnoreCase("world")) {
Via.getManager().getProviders().use(BlockConnectionProvider.class, new SpongeBlockConnectionProvider());
}
}
public void unload() {

View File

@ -1,57 +0,0 @@
package us.myles.ViaVersion.sponge.providers;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.world.Chunk;
import org.spongepowered.api.world.World;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.minecraft.Position;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider;
import us.myles.ViaVersion.util.ReflectionUtil;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public class SpongeBlockConnectionProvider extends BlockConnectionProvider {
private static Class block;
private static Map<Object, Integer> blockStateIds;
static {
try {
block = Class.forName("net.minecraft.block.Block");
blockStateIds = ReflectionUtil.get(
ReflectionUtil.getStatic(block, "field_176229_d", Object.class),
"field_148749_a", Map.class);
} catch (ClassNotFoundException e) {
Via.getPlatform().getLogger().warning("net.minecraft.block.Block not found! Are you using Lantern?");
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
}
@Override
public int getWorldBlockData(UserConnection user, Position position) {
if (blockStateIds != null) {
UUID uuid = user.get(ProtocolInfo.class).getUuid();
Optional<Player> player = Sponge.getServer().getPlayer(uuid);
if (player.isPresent()) {
World world = player.get().getWorld();
Optional<Chunk> chunk = world.getChunkAtBlock(position.getX().intValue(), position.getY().intValue(), position.getZ().intValue());
if (chunk.isPresent()) {
BlockState b = chunk.get().getBlock(position.getX().intValue(), position.getY().intValue(), position.getZ().intValue());
Integer id = blockStateIds.get(b);
if (id == null) {
System.out.println("id not found");
} else {
return id;
}
}
}
}
return 0;
}
}