Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Syncable Data Attachments #1823

Draft
wants to merge 15 commits into
base: 1.21.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,38 @@
throw EntityArgument.ERROR_SELECTORS_NOT_ALLOWED.create();
}
}
@@ -150,8 +_,9 @@
} else {
Predicate<Entity> predicate = this.getPredicate(vec3, aabb, p_121161_.enabledFeatures());
List<Entity> list = new ObjectArrayList<>();
+ // TODO: remove all this messy temp stuff
if (this.isWorldLimited()) {
- this.addEntities(list, p_121161_.getLevel(), aabb, predicate);
+ this.addEntities(list, p_121161_.getUnsidedLevel(), aabb, predicate);
} else {
for (ServerLevel serverlevel : p_121161_.getServer().getAllLevels()) {
this.addEntities(list, serverlevel, aabb, predicate);
@@ -163,13 +_,21 @@
}
}

- private void addEntities(List<Entity> p_121155_, ServerLevel p_121156_, @Nullable AABB p_352947_, Predicate<Entity> p_121158_) {
+ private void addEntities(List<Entity> p_121155_, net.minecraft.world.level.Level p_121156_, @Nullable AABB p_352947_, Predicate<Entity> p_121158_) {
int i = this.getResultLimit();
if (p_121155_.size() < i) {
if (p_352947_ != null) {
p_121156_.getEntities(this.type, p_352947_, p_121158_, p_121155_, i);
} else {
- p_121156_.getEntities(this.type, p_121158_, p_121155_, i);
+ if (p_121156_ instanceof ServerLevel serverLevel) {
+ serverLevel.getEntities(this.type, p_121158_, p_121155_, i);
+ } else {
+ ((net.minecraft.client.multiplayer.ClientLevel) p_121156_).entitiesForRendering().forEach(entity -> {
+ if (p_121158_.test(entity) && p_121155_.size() < i) {
+ p_121155_.add(entity);
+ }
+ });
+ }
}
}
}
21 changes: 21 additions & 0 deletions patches/net/minecraft/server/level/ChunkMap.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@
EntityType<?> entitytype = p_140200_.getType();
int i = entitytype.clientTrackingRange() * 16;
if (i != 0) {
@@ -1240,6 +_,20 @@
});
}

+ // Neo: Getter for players watching an entity.
+ public List<ServerPlayer> getPlayersWatching(Entity entity) {
+ var trackedEntity = entityMap.get(entity.getId());
+ if (trackedEntity != null) {
+ var ret = new java.util.ArrayList<ServerPlayer>(trackedEntity.seenBy.size());
+ for (var connection : trackedEntity.seenBy) {
+ ret.add(connection.getPlayer());
+ }
+ return List.copyOf(ret);
+ } else {
+ return List.of();
+ }
+ }
+
class DistanceManager extends net.minecraft.server.level.DistanceManager {
protected DistanceManager(Executor p_140459_, Executor p_140460_) {
super(p_140459_, p_140460_);
@@ -1354,5 +_,20 @@
this.updatePlayer(serverplayer);
}
Expand Down
20 changes: 14 additions & 6 deletions patches/net/minecraft/server/level/ServerEntity.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,39 @@
if (mapitemsaveddata != null) {
for (ServerPlayer serverplayer : this.level.players()) {
mapitemsaveddata.tickCarriedBy(serverplayer, itemstack);
@@ -273,22 +_,25 @@
@@ -273,6 +_,7 @@
public void removePairing(ServerPlayer p_8535_) {
this.entity.stopSeenByPlayer(p_8535_);
p_8535_.connection.send(new ClientboundRemoveEntitiesPacket(this.entity.getId()));
+ net.neoforged.neoforge.event.EventHooks.onStopEntityTracking(this.entity, p_8535_);
}

public void addPairing(ServerPlayer p_8542_) {
List<Packet<? super ClientGamePacketListener>> list = new ArrayList<>();
- this.sendPairingData(p_8542_, list::add);
+ this.sendPairingData(p_8542_, new net.neoforged.neoforge.network.bundle.PacketAndPayloadAcceptor<>(list::add));
@@ -280,15 +_,17 @@
this.sendPairingData(p_8542_, list::add);
p_8542_.connection.send(new ClientboundBundlePacket(list));
this.entity.startSeenByPlayer(p_8542_);
+ net.neoforged.neoforge.event.EventHooks.onStartEntityTracking(this.entity, p_8542_);
}

- public void sendPairingData(ServerPlayer p_289562_, Consumer<Packet<ClientGamePacketListener>> p_289563_) {
+ public void sendPairingData(ServerPlayer p_289562_, net.neoforged.neoforge.network.bundle.PacketAndPayloadAcceptor<net.minecraft.network.protocol.game.ClientGamePacketListener> p_289563_) {
+ public void sendPairingData(ServerPlayer p_289562_, Consumer<Packet<? super ClientGamePacketListener>> p_289563_) {
Shadows-of-Fire marked this conversation as resolved.
Show resolved Hide resolved
if (this.entity.isRemoved()) {
LOGGER.warn("Fetching packet for removed entity {}", this.entity);
}

Packet<ClientGamePacketListener> packet = this.entity.getAddEntityPacket(this);
p_289563_.accept(packet);
+ this.entity.sendPairingData(p_289562_, p_289563_::accept);
+ this.entity.sendPairingData(p_289562_, payload -> p_289563_.accept(payload.toVanillaClientbound()));
if (this.trackedDataValues != null) {
p_289563_.accept(new ClientboundSetEntityDataPacket(this.entity.getId(), this.trackedDataValues));
}
@@ -335,6 +_,8 @@
if (this.entity instanceof Leashable leashable && leashable.isLeashed()) {
p_289563_.accept(new ClientboundSetEntityLinkPacket(this.entity, leashable.getLeashHolder()));
}
+
+ net.neoforged.neoforge.attachment.AttachmentSync.sendEntityPairingData(this.entity, p_289562_, p_289563_);
}

public Vec3 getPositionBase() {
7 changes: 6 additions & 1 deletion patches/net/minecraft/server/level/ServerLevel.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
ServerLevel.this.dragonParts.put(enderdragonpart.getId(), enderdragonpart);
}
}
@@ -1783,24 +_,101 @@
@@ -1783,24 +_,106 @@
if (ServerLevel.this.isUpdatingNavigations) {
String s = "onTrackingStart called during navigation iteration";
Util.logAndPauseIfInIde(
Expand Down Expand Up @@ -273,6 +273,11 @@
}
}
+
+ @Override
+ public final void syncData(net.neoforged.neoforge.attachment.AttachmentType<?> type) {
+ net.neoforged.neoforge.attachment.AttachmentSync.syncLevelUpdate(this, type);
+ }
+
+ private final net.neoforged.neoforge.capabilities.CapabilityListenerHolder capListenerHolder = new net.neoforged.neoforge.capabilities.CapabilityListenerHolder();
+
+ @Override
Expand Down
8 changes: 8 additions & 0 deletions patches/net/minecraft/server/players/PlayerList.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@
p_11230_.connection.send(new ClientboundSetDefaultSpawnPositionPacket(p_11231_.getSharedSpawnPos(), p_11231_.getSharedSpawnAngle()));
if (p_11231_.isRaining()) {
p_11230_.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.START_RAINING, 0.0F));
@@ -670,6 +_,7 @@

p_11230_.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.LEVEL_CHUNKS_LOAD_START, 0.0F));
this.server.tickRateManager().updateJoiningPlayer(p_11230_);
+ net.neoforged.neoforge.attachment.AttachmentSync.sendLevelInfo(p_11231_, p_11230_);
}

public void sendAllPlayerInfo(ServerPlayer p_11293_) {
@@ -785,13 +_,6 @@
if (serverstatscounter == null) {
File file1 = this.server.getWorldPath(LevelResource.PLAYER_STATS_DIR).toFile();
Expand Down
7 changes: 6 additions & 1 deletion patches/net/minecraft/world/entity/Entity.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@
}

public void checkDespawn() {
@@ -3627,6 +_,128 @@
@@ -3627,6 +_,133 @@

public boolean mayInteract(ServerLevel p_376870_, BlockPos p_146844_) {
return true;
Expand Down Expand Up @@ -555,6 +555,11 @@
+ return super.setData(type, data);
+ }
+
+ @Override
+ public final void syncData(net.neoforged.neoforge.attachment.AttachmentType<?> type) {
+ net.neoforged.neoforge.attachment.AttachmentSync.syncEntityUpdate(this, type);
+ }
+
+ // Neo: Hookup Capabilities getters to entities
+ @Nullable
+ public final <T, C extends @org.jetbrains.annotations.Nullable Object> T getCapability(net.neoforged.neoforge.capabilities.EntityCapability<T, C> capability, C context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
}

public boolean triggerEvent(int p_58889_, int p_58890_) {
@@ -234,6 +_,27 @@
@@ -234,6 +_,32 @@
return this.type;
}

Expand All @@ -87,6 +87,11 @@
+ setChanged();
+ return super.removeData(type);
+ }
+
+ @Override
+ public final void syncData(net.neoforged.neoforge.attachment.AttachmentType<?> type) {
+ net.neoforged.neoforge.attachment.AttachmentSync.syncBlockEntityUpdate(this, type);
+ }
+
@Deprecated
public void setBlockState(BlockState p_155251_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
+ }
+
+ @org.jetbrains.annotations.ApiStatus.Internal
+ protected net.neoforged.neoforge.attachment.AttachmentHolder.AsField getAttachmentHolder() {
+ public net.neoforged.neoforge.attachment.AttachmentHolder.AsField getAttachmentHolder() {
+ return attachmentHolder;
+ }
+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
this.blockEntities.values().forEach(p_187988_ -> {
if (this.level instanceof ServerLevel serverlevel) {
this.addGameEventListener(p_187988_, serverlevel);
@@ -694,6 +_,14 @@
@@ -694,6 +_,19 @@
return new LevelChunk.BoundTickingBlockEntity<>(p_156376_, p_156377_);
}

Expand All @@ -131,6 +131,11 @@
+ public net.neoforged.neoforge.common.world.LevelChunkAuxiliaryLightManager getAuxLightManager(ChunkPos pos) {
+ return auxLightManager;
+ }
+
+ @Override
+ public final void syncData(net.neoforged.neoforge.attachment.AttachmentType<?> type) {
+ net.neoforged.neoforge.attachment.AttachmentSync.syncChunkUpdate(this, getAttachmentHolder(), type);
+ }
+
class BoundTickingBlockEntity<T extends BlockEntity> implements TickingBlockEntity {
private final T blockEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public final <T> T getData(AttachmentType<T> type) {
if (ret == null) {
ret = type.defaultValueSupplier.apply(getExposedHolder());
attachments.put(type, ret);
syncData(type);
}
return ret;
}
Expand All @@ -96,7 +97,9 @@ public <T> Optional<T> getExistingData(AttachmentType<T> type) {
public <T> @Nullable T setData(AttachmentType<T> type, T data) {
validateAttachmentType(type);
Objects.requireNonNull(data);
return (T) getAttachmentMap().put(type, data);
var previousData = (T) getAttachmentMap().put(type, data);
syncData(type);
return previousData;
}

@Override
Expand All @@ -106,7 +109,9 @@ public <T> Optional<T> getExistingData(AttachmentType<T> type) {
if (attachments == null) {
return null;
}
return (T) attachments.remove(type);
var previousData = (T) attachments.remove(type);
syncData(type);
return previousData;
}

/**
Expand Down Expand Up @@ -179,5 +184,10 @@ IAttachmentHolder getExposedHolder() {
public void deserializeInternal(HolderLookup.Provider provider, CompoundTag tag) {
deserializeAttachments(provider, tag);
}

@Override
public void syncData(AttachmentType<?> type) {
exposedHolder.syncData(type);
}
}
}
Loading
Loading