Skip to content

Commit 6ff55ab

Browse files
committed
Fix deselecting the npc not clearing path
1 parent e9ea9d8 commit 6ff55ab

2 files changed

Lines changed: 54 additions & 35 deletions

File tree

common/src/main/java/org/samo_lego/taterzens/mixin/network/ServerGamePacketListenerImplMixin_PacketFaker.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ private void changeEntityType(Packet<?> packet, PacketSendListener listener, Cal
9494
Arrays.asList(new ClientboundPlayerInfoPacket.PlayerUpdate(profile, 0, GameType.SURVIVAL, npc.getTabListName(), null))
9595
);
9696
this.send(playerAddPacket, listener);
97+
9798
// Vanilla sends the packet twice
99+
playerAddPacket = new ClientboundPlayerInfoPacket(ADD_PLAYER);
100+
//noinspection ConstantConditions
101+
((ClientboundPlayerInfoPacketAccessor) playerAddPacket).setEntries(
102+
Arrays.asList(new ClientboundPlayerInfoPacket.PlayerUpdate(profile, 0, GameType.SURVIVAL, npc.getTabListName(), null))
103+
);
98104
this.send(playerAddPacket, listener);
99105

100106
// Before we send this packet, we have

common/src/main/java/org/samo_lego/taterzens/mixin/player/ServerPlayerMixinCast_ITaterzenEditor.java

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,33 @@
2929
@Mixin(ServerPlayer.class)
3030
public abstract class ServerPlayerMixinCast_ITaterzenEditor implements ITaterzenEditor {
3131

32-
private final ServerPlayer player = (ServerPlayer) (Object) this;
32+
@Unique
33+
private final ServerPlayer self = (ServerPlayer) (Object) this;
3334

3435
@Unique
35-
private TaterzenNPC taterzens$selectedNpc;
36+
private TaterzenNPC selectedNpc;
3637
@Unique
37-
private int taterzens$selectedMsgId = -1; // -1 as no selected msg to edit
38+
private int selectedMsgId = -1; // -1 as no selected msg to edit
3839

3940
@Unique
40-
private byte taterzens$lastRenderTick;
41+
private byte lastRenderTick;
4142
@Unique
42-
private EditorMode taterzens$editorMode = EditorMode.NONE;
43+
private EditorMode editorMode = EditorMode.NONE;
4344

4445
/**
4546
* Used for showing the path particles.
4647
*/
4748
@Inject(method = "tick()V", at = @At("TAIL"))
4849
private void tick(CallbackInfo ci) {
49-
ITaterzenEditor editor = (ITaterzenEditor) this.player;
50-
if(editor.getNpc() != null && taterzens$lastRenderTick++ > 4) {
51-
if(this.taterzens$editorMode == EditorMode.PATH) {
50+
ITaterzenEditor editor = (ITaterzenEditor) this.self;
51+
if (editor.getNpc() != null && lastRenderTick++ > 4) {
52+
if (this.editorMode == EditorMode.PATH) {
5253
ArrayList<BlockPos> pathTargets = editor.getNpc().getPathTargets();
5354
DustParticleOptions effect = new DustParticleOptions(
5455
new Vector3f(
55-
config.path.color.red / 255.0F,
56-
config.path.color.green / 255.0F,
57-
config.path.color.blue / 255.0F
56+
config.path.color.red / 255.0F,
57+
config.path.color.green / 255.0F,
58+
config.path.color.blue / 255.0F
5859
),
5960
1.0F
6061
);
@@ -68,45 +69,50 @@ private void tick(CallbackInfo ci) {
6869
int deltaZ = pos.getZ() - nextPos.getZ();
6970

7071
double distance = Math.sqrt(pos.distSqr(nextPos));
71-
for(double j = 0; j < distance; j += 0.5D) {
72+
for (double j = 0; j < distance; j += 0.5D) {
7273
double x = pos.getX() - j / distance * deltaX;
7374
double y = pos.getY() - j / distance * deltaY;
7475
double z = pos.getZ() - j / distance * deltaZ;
7576
ClientboundLevelParticlesPacket packet = new ClientboundLevelParticlesPacket(effect, true, x + 0.5D, y + 1.5D, z + 0.5D, 0.1F, 0.1F, 0.1F, 1.0F, 1);
76-
this.player.connection.send(packet);
77+
this.self.connection.send(packet);
7778
}
7879
}
7980
}
80-
if(this.taterzens$editorMode != EditorMode.NONE) {
81-
player.displayClientMessage(successText("taterzens.tooltip.current_editor", String.valueOf(this.taterzens$editorMode)), true);
81+
if (this.editorMode != EditorMode.NONE) {
82+
self.displayClientMessage(successText("taterzens.tooltip.current_editor", String.valueOf(this.editorMode)), true);
8283
}
8384

84-
this.taterzens$lastRenderTick = 0;
85+
this.lastRenderTick = 0;
8586
}
8687
}
8788

8889
@Override
8990
public void setEditorMode(EditorMode mode) {
90-
ITaterzenEditor editor = (ITaterzenEditor) this.player;
91-
if(editor.getNpc() != null) {
92-
Level world = player.getLevel();
93-
if(this.taterzens$editorMode == EditorMode.PATH && mode != EditorMode.PATH) {
94-
editor.getNpc().getPathTargets().forEach(blockPos -> player.connection.send(
91+
ITaterzenEditor editor = (ITaterzenEditor) this.self;
92+
93+
if (editor.getNpc() != null) {
94+
Level world = self.getLevel();
95+
if (this.editorMode == EditorMode.PATH && mode != EditorMode.PATH) {
96+
editor.getNpc().getPathTargets().forEach(blockPos -> self.connection.send(
9597
new ClientboundBlockUpdatePacket(blockPos, world.getBlockState(blockPos))
9698
));
97-
} else if(this.taterzens$editorMode != EditorMode.PATH && mode == EditorMode.PATH) {
98-
editor.getNpc().getPathTargets().forEach(blockPos -> player.connection.send(
99+
} else if (this.editorMode != EditorMode.PATH && mode == EditorMode.PATH) {
100+
editor.getNpc().getPathTargets().forEach(blockPos -> self.connection.send(
99101
new ClientboundBlockUpdatePacket(blockPos, Blocks.REDSTONE_BLOCK.defaultBlockState())
100102
));
101103
}
104+
105+
if (this.editorMode == EditorMode.MESSAGES && mode != EditorMode.MESSAGES) {
106+
this.setEditingMessageIndex(-1);
107+
}
102108
}
103109

104-
this.taterzens$editorMode = mode;
110+
this.editorMode = mode;
105111
}
106112

107113
@Override
108114
public EditorMode getEditorMode() {
109-
return this.taterzens$editorMode;
115+
return this.editorMode;
110116
}
111117

112118
/**
@@ -116,35 +122,42 @@ public EditorMode getEditorMode() {
116122
@Nullable
117123
@Override
118124
public TaterzenNPC getNpc() {
119-
return this.taterzens$selectedNpc;
125+
return this.selectedNpc;
120126
}
121127

122128
@Override
123129
public boolean selectNpc(@Nullable TaterzenNPC npc) {
124-
if (npc != null && !npc.allowEditBy(this.player) &&
125-
!Taterzens.getInstance().getPlatform().checkPermission(
126-
this.player.createCommandSourceStack(), "taterzens.npc.select.bypass", config.perms.selectBypassLevel)) {
130+
if (npc != null && !npc.allowEditBy(this.self) &&
131+
!Taterzens.getInstance().getPlatform().checkPermission(
132+
this.self.createCommandSourceStack(), "taterzens.npc.select.bypass", config.perms.selectBypassLevel)) {
127133
return false;
128134
}
129135

130-
TaterzenNPC selectedNpc = this.taterzens$selectedNpc;
131-
this.taterzens$selectedNpc = npc;
136+
if (this.getEditorMode() != EditorMode.NONE) {
137+
this.setEditorMode(EditorMode.NONE);
138+
}
132139

133-
if(npc != null)
140+
TaterzenNPC selectedNpc = this.selectedNpc;
141+
this.selectedNpc = npc;
142+
143+
if (npc != null) {
134144
npc.sendProfileUpdates();
135-
if(selectedNpc != null)
145+
}
146+
147+
if (selectedNpc != null) {
136148
selectedNpc.sendProfileUpdates();
149+
}
137150

138151
return true;
139152
}
140153

141154
@Override
142155
public void setEditingMessageIndex(int selected) {
143-
this.taterzens$selectedMsgId = selected;
156+
this.selectedMsgId = selected;
144157
}
145158

146159
@Override
147160
public int getEditingMessageIndex() {
148-
return this.taterzens$selectedMsgId;
161+
return this.selectedMsgId;
149162
}
150163
}

0 commit comments

Comments
 (0)