Skip to content

Commit

Permalink
QOL code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Mar 16, 2023
1 parent 32a47d2 commit 83a2c17
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.samo_lego.taterzens.api.TaterzensAPI;
import org.samo_lego.taterzens.commands.edit.EditCommand;
import org.samo_lego.taterzens.interfaces.ITaterzenEditor;
import org.samo_lego.taterzens.mixin.accessors.CommandSourceStackAccessor;
import org.samo_lego.taterzens.mixin.accessors.ACommandSourceStack;
import org.samo_lego.taterzens.npc.TaterzenNPC;

import java.util.ArrayList;
Expand Down Expand Up @@ -397,7 +397,7 @@ private static int spawnTaterzen(CommandContext<CommandSourceStack> context) thr

TaterzenNPC taterzen = TaterzensAPI.createTaterzen(player, taterzenName);
// Making sure permission level is as high as owner's, to prevent permission bypassing.
taterzen.setPermissionLevel(((CommandSourceStackAccessor) source).getPermissionLevel());
taterzen.setPermissionLevel(((ACommandSourceStack) source).getPermissionLevel());

// Lock if needed
if (config.lockAfterCreation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import static org.samo_lego.taterzens.Taterzens.GSON;
import static org.samo_lego.taterzens.Taterzens.config;
import static org.samo_lego.taterzens.compatibility.ModDiscovery.FABRICTAILOR_LOADED;
import static org.samo_lego.taterzens.mixin.accessors.PlayerAccessor.getPLAYER_MODE_CUSTOMISATION;
import static org.samo_lego.taterzens.mixin.accessors.APlayer.getPLAYER_MODE_CUSTOMISATION;
import static org.samo_lego.taterzens.util.TextUtil.*;
import static org.samo_lego.taterzens.util.WebUtil.urlRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ChunkMap.class)
public interface ChunkMapAccessor {
public interface AChunkMap {
@Accessor("entityMap")
Int2ObjectMap<EntityTrackerEntryAccessor> getEntityMap();
Int2ObjectMap<AEntityTrackerEntry> getEntityMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import java.util.UUID;

@Mixin(ClientboundAddPlayerPacket.class)
public interface ClientboundAddPlayerPacketAccessor {
public interface AClientboundAddPlayerPacket {
@Mutable
@Accessor("entityId")
void setId(int id);

@Mutable
@Accessor("entityId")
int getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;

@Mixin(ClientboundPlayerInfoUpdatePacket.class)
public interface ClientboundPlayerInfoPacketAccessor {
public interface AClientboundPlayerInfoPacket {
@Mutable
@Accessor("entries")
void setEntries(List<ClientboundPlayerInfoUpdatePacket.Entry> entries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;

@Mixin(ClientboundSetEntityDataPacket.class)
public interface ClientboundSetEntityDataPacketAccessor {
public interface AClientboundSetEntityDataPacket {
@Mutable
@Accessor("id")
int getEntityId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(CommandSourceStack.class)
public interface CommandSourceStackAccessor {
public interface ACommandSourceStack {
@Accessor("permissionLevel")
int getPermissionLevel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(Entity.class)
public interface EntityAccessor {
public interface AEntity {
@Accessor("DATA_SHARED_FLAGS_ID")
static EntityDataAccessor<Byte> getFLAGS() {
throw new AssertionError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import java.util.Set;

@Mixin(targets = "net.minecraft.server.level.ChunkMap$TrackedEntity")
public interface EntityTrackerEntryAccessor {
public interface AEntityTrackerEntry {
@Accessor("serverEntity")
ServerEntity getPlayer();

@Accessor("seenBy")
Set<ServerPlayerConnection> getSeenBy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(Player.class)
public interface PlayerAccessor {
public interface APlayer {
@Accessor("DATA_PLAYER_MODE_CUSTOMISATION")
static EntityDataAccessor<Byte> getPLAYER_MODE_CUSTOMISATION() {
throw new AssertionError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.network.PacketSendListener;
import net.minecraft.network.protocol.BundlePacket;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundAddPlayerPacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import net.minecraft.network.syncher.SynchedEntityData;
Expand All @@ -14,8 +15,8 @@
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import org.samo_lego.taterzens.interfaces.ITaterzenEditor;
import org.samo_lego.taterzens.mixin.accessors.ClientboundSetEntityDataPacketAccessor;
import org.samo_lego.taterzens.mixin.accessors.EntityAccessor;
import org.samo_lego.taterzens.mixin.accessors.AClientboundSetEntityDataPacket;
import org.samo_lego.taterzens.mixin.accessors.AEntity;
import org.samo_lego.taterzens.npc.TaterzenNPC;
import org.samo_lego.taterzens.util.NpcPlayerUpdate;
import org.spongepowered.asm.mixin.Final;
Expand Down Expand Up @@ -61,27 +62,23 @@ public abstract class ServerGamePacketListenerImplMixin_PacketFaker {
*/
@Inject(method = "send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;)V",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/Connection;send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;)V"),
cancellable = true)
target = "Lnet/minecraft/network/Connection;send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;)V"))
private void changeEntityType(Packet<?> packet, PacketSendListener listener, CallbackInfo ci) {
Level world = player.getLevel();
if (packet instanceof BundlePacket<?> && !this.taterzens$skipCheck) {
System.out.println("BundlePacket, todo - remove from tablist");
/*if (!(entity instanceof TaterzenNPC npc))
return;
// And now we can remove it from tablist
// we must delay the tablist packet so as to allow
// the client to fetch skin.
// If player is immediately removed from the tablist,
// client doesn't care about the skin.
if (config.taterzenTablistTimeout != -1) {
var uuid = npc.getGameProfile().getId();
taterzens$tablistQueue.remove(uuid);
taterzens$tablistQueue.put(uuid, new NpcPlayerUpdate(npc.getGameProfile(), npc.getTabListName(), taterzens$queueTick + config.taterzenTablistTimeout));
}*/
if (packet instanceof BundlePacket<?> bPacket && !this.taterzens$skipCheck) {
for (Packet<?> subPacket : bPacket.subPackets()) {
if (subPacket instanceof ClientboundAddPlayerPacket playerAddPacket) {
Entity entity = player.getLevel().getEntity(playerAddPacket.getEntityId());

if (entity instanceof TaterzenNPC npc) {
var uuid = npc.getGameProfile().getId();
this.taterzens$tablistQueue.remove(uuid);
this.taterzens$tablistQueue.put(uuid, new NpcPlayerUpdate(npc.getGameProfile(), npc.getTabListName(), taterzens$queueTick + config.taterzenTablistTimeout));
}
}
}
} else if (packet instanceof ClientboundSetEntityDataPacket) {
Entity entity = world.getEntity(((ClientboundSetEntityDataPacketAccessor) packet).getEntityId());
Entity entity = world.getEntity(((AClientboundSetEntityDataPacket) packet).getEntityId());

if (!(entity instanceof TaterzenNPC taterzen))
return;
Expand All @@ -90,15 +87,15 @@ private void changeEntityType(Packet<?> packet, PacketSendListener listener, Cal

if (taterzen.equals(((ITaterzenEditor) this.player).getNpc()) && trackedValues != null && config.glowSelectedNpc) {
trackedValues.removeIf(value -> value.id() == 0);
Byte flags = fakePlayer.getEntityData().get(EntityAccessor.getFLAGS());
Byte flags = fakePlayer.getEntityData().get(AEntity.getFLAGS());
// Modify Taterzen to have fake glowing effect for the player
flags = (byte) (flags | 1 << EntityAccessor.getFLAG_GLOWING());
flags = (byte) (flags | 1 << AEntity.getFLAG_GLOWING());

SynchedEntityData.DataValue<Byte> glowingTag = SynchedEntityData.DataValue.create(EntityAccessor.getFLAGS(), flags);
SynchedEntityData.DataValue<Byte> glowingTag = SynchedEntityData.DataValue.create(AEntity.getFLAGS(), flags);
trackedValues.add(glowingTag);
}

((ClientboundSetEntityDataPacketAccessor) packet).setPackedItems(trackedValues);
((AClientboundSetEntityDataPacket) packet).setPackedItems(trackedValues);
}
}

Expand Down
29 changes: 15 additions & 14 deletions common/src/main/java/org/samo_lego/taterzens/npc/TaterzenNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
import org.samo_lego.taterzens.api.professions.TaterzenProfession;
import org.samo_lego.taterzens.interfaces.ITaterzenEditor;
import org.samo_lego.taterzens.interfaces.ITaterzenPlayer;
import org.samo_lego.taterzens.mixin.accessors.ChunkMapAccessor;
import org.samo_lego.taterzens.mixin.accessors.ClientboundAddPlayerPacketAccessor;
import org.samo_lego.taterzens.mixin.accessors.ClientboundPlayerInfoPacketAccessor;
import org.samo_lego.taterzens.mixin.accessors.EntityTrackerEntryAccessor;
import org.samo_lego.taterzens.mixin.accessors.AChunkMap;
import org.samo_lego.taterzens.mixin.accessors.AClientboundAddPlayerPacket;
import org.samo_lego.taterzens.mixin.accessors.AClientboundPlayerInfoPacket;
import org.samo_lego.taterzens.mixin.accessors.AEntityTrackerEntry;
import org.samo_lego.taterzens.npc.ai.goal.*;
import org.samo_lego.taterzens.npc.commands.AbstractTaterzenCommand;
import org.samo_lego.taterzens.npc.commands.CommandGroups;
Expand All @@ -78,7 +78,7 @@

import static net.minecraft.world.InteractionHand.MAIN_HAND;
import static org.samo_lego.taterzens.Taterzens.*;
import static org.samo_lego.taterzens.mixin.accessors.PlayerAccessor.getPLAYER_MODE_CUSTOMISATION;
import static org.samo_lego.taterzens.mixin.accessors.APlayer.getPLAYER_MODE_CUSTOMISATION;
import static org.samo_lego.taterzens.util.TextUtil.errorText;
import static org.samo_lego.taterzens.util.TextUtil.successText;

Expand Down Expand Up @@ -587,12 +587,12 @@ public Packet<ClientGamePacketListener> getAddEntityPacket() {
final var playerAddPacket = new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, this.fakePlayer);
//noinspection ConstantConditions
var entry = new ClientboundPlayerInfoUpdatePacket.Entry(this.gameProfile.getId(), this.gameProfile, false, 0, GameType.SURVIVAL, this.getDisplayName(), null);
((ClientboundPlayerInfoPacketAccessor) playerAddPacket).setEntries(Collections.singletonList(entry));
((AClientboundPlayerInfoPacket) playerAddPacket).setEntries(Collections.singletonList(entry));
packets.add(playerAddPacket);

// Spawn player
final var spawnPlayerPacket = new ClientboundAddPlayerPacket(this.fakePlayer);
ClientboundAddPlayerPacketAccessor addPlayerPacketAccessor = (ClientboundAddPlayerPacketAccessor) spawnPlayerPacket;
AClientboundAddPlayerPacket addPlayerPacketAccessor = (AClientboundAddPlayerPacket) spawnPlayerPacket;
addPlayerPacketAccessor.setId(this.getId());
addPlayerPacketAccessor.setUuid(this.getUUID());
addPlayerPacketAccessor.setX(this.getX());
Expand Down Expand Up @@ -655,7 +655,7 @@ public void sendProfileUpdates() {

ServerChunkCache manager = (ServerChunkCache) this.level.getChunkSource();
ChunkMap storage = manager.chunkMap;
EntityTrackerEntryAccessor trackerEntry = ((ChunkMapAccessor) storage).getEntityMap().get(this.getId());
AEntityTrackerEntry trackerEntry = ((AChunkMap) storage).getEntityMap().get(this.getId());
if(trackerEntry != null)
trackerEntry.getSeenBy().forEach(tracking -> trackerEntry.getPlayer().addPairing(tracking.getPlayer()));
}
Expand Down Expand Up @@ -728,7 +728,7 @@ public void readAdditionalSaveData(CompoundTag tag) {
super.readAdditionalSaveData(tag);

// Has a "preset" tag
// We want to override other data
// We want to overwrite self data from that provided by preset
if (tag.contains("PresetOverride")) {
this.loadPresetTag(tag);
return; // Other data doesn't need to be loaded as it will be handled by preset
Expand Down Expand Up @@ -1617,8 +1617,9 @@ public void addProfession(ResourceLocation professionId, TaterzenProfession prof
}

/**
* GetsTaterzen's professions.
* @return all professions ids of Taterzen's professions.
* Gets taterzen's professions.
*
* @return all professions ids of taterzen's professions.
*/
public Collection<ResourceLocation> getProfessionIds() {
return this.professions.keySet();
Expand All @@ -1629,7 +1630,7 @@ public Collection<ResourceLocation> getProfessionIds() {
* @param professionId id of the profession that is in Taterzen's profession map.
*/
public void removeProfession(ResourceLocation professionId) {
TaterzenProfession toRemove = this.professions.getOrDefault(professionId, null);
TaterzenProfession toRemove = this.professions.get(professionId);

if (toRemove != null) {
toRemove.onProfessionRemoved();
Expand All @@ -1643,7 +1644,7 @@ public void removeProfession(ResourceLocation professionId) {
*/
@Nullable
public TaterzenProfession getProfession(ResourceLocation professionId) {
return this.professions.getOrDefault(professionId, null);
return this.professions.get(professionId);
}

@Override
Expand All @@ -1659,7 +1660,7 @@ public boolean canPickUpLoot() {
protected void pickUpItem(ItemEntity item) {
// Profession event
ItemStack stack = item.getItem();
for(TaterzenProfession profession : this.professions.values()) {
for (TaterzenProfession profession : this.professions.values()) {
if (profession.tryPickupItem(item)) {
this.onItemPickup(item);
this.take(item, stack.getCount());
Expand Down
16 changes: 8 additions & 8 deletions common/src/main/resources/taterzens.common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"ChunkMapMixin_TaterzenList",
"CommandSourceStackMixin_HideTaterzenExecutor",
"ServerPlayInteractionManagerMixin",
"accessors.ChunkMapAccessor",
"accessors.ClientboundAddPlayerPacketAccessor",
"accessors.ClientboundPlayerInfoPacketAccessor",
"accessors.ClientboundSetEntityDataPacketAccessor",
"accessors.CommandSourceStackAccessor",
"accessors.EntityAccessor",
"accessors.EntityTrackerEntryAccessor",
"accessors.PlayerAccessor",
"accessors.AChunkMap",
"accessors.AClientboundAddPlayerPacket",
"accessors.AClientboundPlayerInfoPacket",
"accessors.AClientboundSetEntityDataPacket",
"accessors.ACommandSourceStack",
"accessors.AEntity",
"accessors.AEntityTrackerEntry",
"accessors.APlayer",
"network.ServerGamePacketListenerImplMixin_BungeeListener",
"network.ServerGamePacketListenerImplMixin_MsgEditor",
"network.ServerGamePacketListenerImplMixin_PacketFaker",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.samo_lego.taterzens.fabric.mixin;

import it.unimi.dsi.fastutil.objects.ObjectList;
import net.minecraft.core.Holder;
import net.minecraft.core.MappedRegistry;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(MappedRegistry.class)
public interface MappedRegistryAccessor {
public interface AMappedRegistry<T> {
@Accessor("byId")
ObjectList<?> getById();
ObjectList<Holder.Reference<T>> getById();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import org.samo_lego.taterzens.fabric.mixin.MappedRegistryAccessor;
import org.samo_lego.taterzens.fabric.mixin.AMappedRegistry;
import org.samo_lego.taterzens.npc.TaterzenNPC;
import org.samo_lego.taterzens.platform.Platform;

Expand All @@ -25,7 +25,7 @@

public class FabricPlatform extends Platform {

private static final int REGISTRY_ITEMS_SIZE = ((MappedRegistryAccessor) BuiltInRegistries.ITEM).getById().size();
private static final int REGISTRY_ITEMS_SIZE = ((AMappedRegistry<?>) BuiltInRegistries.ITEM).getById().size();

@Override
public Path getConfigDirPath() {
Expand Down
15 changes: 14 additions & 1 deletion fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"taterzens.mixins.json",
"taterzens.common.mixins.json"
],

"depends": {
"fabricloader": ">=0.7.4",
"fabric": ">=0.48.0"
Expand All @@ -39,5 +38,19 @@
},
"breaks": {
"disguiselib": "<1.2.1"
},
"custom": {
"loom:injected_interfaces": {
"net.minecraft.class_2370": [
"org/samo_lego/taterzens/fabric/mixin/AMappedRegistry"
],
"net.minecraft.class_3222": [
"org/samo_lego/taterzens/interfaces/ITaterzenEditor",
"org/samo_lego/taterzens/interfaces/ITaterzenPlayer"
],
"net.minecraft.class_2613": [
"org/samo_lego/taterzens/mixin/accessors/AClientboundAddPlayerPacket"
]
}
}
}
2 changes: 1 addition & 1 deletion fabric/src/main/resources/taterzens.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"package": "org.samo_lego.taterzens.fabric.mixin",
"compatibilityLevel": "JAVA_16",
"mixins": [
"MappedRegistryAccessor"
"AMappedRegistry"
],
"client": [],
"injectors": {
Expand Down

0 comments on commit 83a2c17

Please sign in to comment.