-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SyncableData And SyncableData$Registry
Breaking Changes: Remove LivingEntityNetworkingEvents And ClientLivingEntityNetworkingEvents Add TrackedDataHandlerUtils#createTrackedDataMapHandler Add MModdingPackets#SYNCABLE_DATA Change CommonOperations And ClientOperations Add EntitySyncableDataRegistry Change IdentifierUtils Add ObjectUtils
- Loading branch information
1 parent
5b11f80
commit 89a6554
Showing
18 changed files
with
183 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/mmodding/mmodding_lib/interface_injections/EntitySyncableDataRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.mmodding.mmodding_lib.interface_injections; | ||
|
||
import com.mmodding.mmodding_lib.library.entities.data.syncable.SyncableData; | ||
import net.minecraft.entity.Entity; | ||
import org.quiltmc.qsl.base.api.util.InjectedInterface; | ||
|
||
@InjectedInterface(Entity.class) | ||
public interface EntitySyncableDataRegistry { | ||
|
||
default SyncableData.Registry getSyncableDataRegistry() { | ||
throw new AssertionError(); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/com/mmodding/mmodding_lib/library/entities/data/syncable/SyncableData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.mmodding.mmodding_lib.library.entities.data.syncable; | ||
|
||
import com.mmodding.mmodding_lib.networking.MModdingPackets; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.entity.data.TrackedDataHandler; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.world.World; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.quiltmc.qsl.networking.api.PacketByteBufs; | ||
import org.quiltmc.qsl.networking.api.ServerPlayNetworking; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class SyncableData<V> extends AtomicReference<V> { | ||
|
||
private final Entity entity; | ||
private final Identifier identifier; | ||
private final TrackedDataHandler<V> handler; | ||
|
||
public SyncableData(V initialValue, Entity entity, Identifier identifier, TrackedDataHandler<V> handler) { | ||
super(initialValue); | ||
this.entity = entity; | ||
this.identifier = identifier; | ||
this.handler = handler; | ||
this.entity.getSyncableDataRegistry().put(this.identifier, this); | ||
} | ||
|
||
public SyncableData(Entity entity, Identifier identifier, TrackedDataHandler<V> handler) { | ||
super(); | ||
this.entity = entity; | ||
this.identifier = identifier; | ||
this.handler = handler; | ||
this.entity.getSyncableDataRegistry().put(this.identifier, this); | ||
} | ||
|
||
public void synchronize() { | ||
World world = this.entity.getWorld(); | ||
if (!world.isClient()) { | ||
PacketByteBuf buf = PacketByteBufs.create(); | ||
buf.writeVarInt(this.entity.getId()); | ||
buf.writeIdentifier(this.identifier); | ||
this.handler.write(buf, this.get()); | ||
this.entity.getWorld().getPlayers().forEach(playerEntity -> ServerPlayNetworking.send( | ||
(ServerPlayerEntity) playerEntity, MModdingPackets.SYNCABLE_DATA, buf | ||
)); | ||
} | ||
} | ||
|
||
public void accept(PacketByteBuf buf) { | ||
this.set(this.handler.read(buf)); | ||
} | ||
|
||
@ApiStatus.Internal | ||
public static class Registry extends HashMap<Identifier, SyncableData<?>> implements Map<Identifier, SyncableData<?>> { | ||
|
||
private final Entity syncedEntity; | ||
|
||
public Registry(Entity syncedEntity) { | ||
this.syncedEntity = syncedEntity; | ||
} | ||
|
||
public void accept(PacketByteBuf buf) { | ||
World world = this.syncedEntity.getWorld(); | ||
if (world.isClient()) { | ||
Identifier identifier = buf.readIdentifier(); | ||
this.get(identifier).accept(buf); | ||
} | ||
} | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
...ava/com/mmodding/mmodding_lib/library/events/networking/LivingEntityNetworkingEvents.java
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
...ing/mmodding_lib/library/events/networking/client/ClientLivingEntityNetworkingEvents.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/mmodding/mmodding_lib/library/utils/ObjectUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mmodding.mmodding_lib.library.utils; | ||
|
||
|
||
public class ObjectUtils { | ||
|
||
public static void load(Object ignored) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 25 additions & 29 deletions
54
src/main/java/com/mmodding/mmodding_lib/mixin/injectors/LivingEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,62 @@ | ||
package com.mmodding.mmodding_lib.mixin.injectors; | ||
|
||
import com.mmodding.mmodding_lib.ducks.LivingEntityDuckInterface; | ||
import com.mmodding.mmodding_lib.networking.CommonOperations; | ||
import com.mmodding.mmodding_lib.library.entities.data.MModdingTrackedDataHandlers; | ||
import com.mmodding.mmodding_lib.library.entities.data.syncable.SyncableData; | ||
import com.mmodding.mmodding_lib.library.utils.MModdingIdentifier; | ||
import com.mmodding.mmodding_lib.library.utils.ObjectUtils; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.Identifier; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Mixin(LivingEntity.class) | ||
public abstract class LivingEntityMixin extends EntityMixin implements LivingEntityDuckInterface { | ||
|
||
@Unique | ||
private Map<Integer, Identifier> stuckArrowTypes = new HashMap<>(); | ||
private final SyncableData<List<Identifier>> stuckArrowTypes = new SyncableData<>( | ||
new ArrayList<>(), | ||
(LivingEntity) (Object) this, | ||
new MModdingIdentifier("stuck_arrow_types"), | ||
MModdingTrackedDataHandlers.IDENTIFIER_LIST | ||
); | ||
|
||
@Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;setStuckArrowCount(I)V")) | ||
private void tick(CallbackInfo ci) { | ||
this.deleteStuckArrowType(); | ||
} | ||
|
||
@Override | ||
public Map<Integer, Identifier> mmodding_lib$getStuckArrowTypes() { | ||
return new HashMap<>(this.stuckArrowTypes); | ||
public List<Identifier> mmodding_lib$getStuckArrowTypes() { | ||
return new ArrayList<>(this.stuckArrowTypes.get()); | ||
} | ||
|
||
@Override | ||
public void mmodding_lib$setStuckArrowTypes(Map<Integer, Identifier> stuckArrowTypes) { | ||
this.stuckArrowTypes = stuckArrowTypes; | ||
this.syncStuckArrowTypes(); | ||
public void mmodding_lib$setStuckArrowTypes(List<Identifier> stuckArrowTypes) { | ||
this.stuckArrowTypes.set(stuckArrowTypes); | ||
this.stuckArrowTypes.synchronize(); | ||
} | ||
|
||
@Override | ||
public void mmodding_lib$putStuckArrowType(int index, Identifier arrowEntityId) { | ||
this.stuckArrowTypes.put(index, arrowEntityId); | ||
this.syncStuckArrowTypes(); | ||
public void mmodding_lib$addStuckArrowType(Identifier arrowEntityId) { | ||
this.stuckArrowTypes.get().add(arrowEntityId); | ||
this.stuckArrowTypes.synchronize(); | ||
} | ||
|
||
@Unique | ||
private void deleteStuckArrowType() { | ||
Map<Integer, Identifier> oldStuckArrowTypes = this.mmodding_lib$getStuckArrowTypes(); | ||
Map<Integer, Identifier> newStuckArrowTypes = new HashMap<>(); | ||
int smallest = !oldStuckArrowTypes.isEmpty() ? Integer.MAX_VALUE : 0; | ||
for (int current : oldStuckArrowTypes.keySet()) { | ||
smallest = Math.min(smallest, current); | ||
} | ||
oldStuckArrowTypes.remove(smallest); | ||
oldStuckArrowTypes.forEach((index, arrowEntityId) -> newStuckArrowTypes.put(index - 1, arrowEntityId)); | ||
this.mmodding_lib$setStuckArrowTypes(newStuckArrowTypes); | ||
List<Identifier> stuckArrowTypes = this.mmodding_lib$getStuckArrowTypes(); | ||
stuckArrowTypes.remove(stuckArrowTypes.size() - 1); | ||
this.mmodding_lib$setStuckArrowTypes(stuckArrowTypes); | ||
} | ||
|
||
@Unique | ||
private void syncStuckArrowTypes() { | ||
if (!this.world.isClient()) { | ||
this.world.getPlayers().forEach(playerEntity -> CommonOperations.sendLivingEntityStuckArrowTypesToClient( | ||
(LivingEntity) (Object) this, this.stuckArrowTypes, (ServerPlayerEntity) playerEntity | ||
)); | ||
} | ||
static { | ||
ObjectUtils.load(MModdingTrackedDataHandlers.IDENTIFIER_LIST); | ||
} | ||
} |
Oops, something went wrong.