-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAbstractMinecartEntityMixin.java
More file actions
162 lines (130 loc) · 4.9 KB
/
AbstractMinecartEntityMixin.java
File metadata and controls
162 lines (130 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package io.nihlen.scriptschunkloaders.mixin;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.vehicle.*;
import net.minecraft.world.TeleportTarget;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
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 io.nihlen.scriptschunkloaders.ScriptsChunkLoadersMod;
import io.nihlen.scriptschunkloaders.MinecartEntityExt;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
@Mixin(AbstractMinecartEntity.class)
public abstract class AbstractMinecartEntityMixin extends Entity implements MinecartEntityExt {
@Unique
private boolean isChunkLoader = false;
@Unique
private int particleTicker = 0;
@Unique
private final int particleInterval = 3;
@Unique
private ChunkPos lastChunkPos = null;
public AbstractMinecartEntityMixin(EntityType<?> type, World world) {
super(type, world);
}
@Inject(method = "<init>(Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;DDD)V", at = @At("TAIL"))
private void injectConstructor(CallbackInfo callbackInfo) {
if (isChunkLoader) {
scripts_chunk_loaders$startChunkLoader();
}
}
public boolean scripts_chunk_loaders$isChunkLoader() {
return this.isChunkLoader;
}
public void scripts_chunk_loaders$startChunkLoader() {
if (this.getWorld().isClient) return;
this.isChunkLoader = true;
ScriptsChunkLoadersMod.LOGGER.info("Starting chunk loader in {}", this.getWorld().getRegistryKey().getValue());
}
public void scripts_chunk_loaders$setChunkLoaderNameFromInventory() {
EntityType<?> minecartType = this.getType();
if (minecartType == EntityType.CHEST_MINECART) {
//noinspection DataFlowIssue - We're sure this is a chest because of the if statement.
var entity = (ChestMinecartEntity)(Object)this;
var firstSlot = entity.getInventory().get(0);
var hasCustomName = firstSlot.get(DataComponentTypes.CUSTOM_NAME) != null;
if (!firstSlot.isEmpty() && hasCustomName) {
var name = firstSlot.getName().getString();
scripts_chunk_loaders$setChunkLoaderName(name);
return;
}
};
scripts_chunk_loaders$setChunkLoaderName("Chunk Loader");
}
public void scripts_chunk_loaders$setChunkLoaderName(String name) {
var nameText = Text.literal(name);
this.setCustomName(nameText);
this.setCustomNameVisible(true);
}
public void scripts_chunk_loaders$stopChunkLoader() {
scripts_chunk_loaders$stopChunkLoader(false);
this.lastChunkPos = null;
}
public void scripts_chunk_loaders$stopChunkLoader(Boolean keepName) {
this.isChunkLoader = false;
ScriptsChunkLoadersMod.CHUNK_LOADER_MANAGER.removeChunkLoader(this);
if (!keepName) {
this.setCustomName(null);
this.setCustomNameVisible(false);
}
}
@Inject(method = "writeCustomDataToNbt", at = @At("RETURN"))
public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
nbt.putBoolean("chunkLoader", this.isChunkLoader);
}
@Inject(method = "readCustomDataFromNbt", at = @At("RETURN"))
public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
this.isChunkLoader = nbt.getBoolean("chunkLoader").orElse(false);
}
@Inject(method = "tick", at = @At("TAIL"))
public void tick(CallbackInfo ci) {
if (!isChunkLoader) return;
var chunkPos = this.getChunkPos();
if (lastChunkPos == null || lastChunkPos != chunkPos) {
lastChunkPos = chunkPos;
ScriptsChunkLoadersMod.LOGGER.info("Re-registering chunk loader");
ScriptsChunkLoadersMod.CHUNK_LOADER_MANAGER.registerChunkLoader(this);
}
this.tickParticles();
}
@Override
public void remove(Entity.RemovalReason reason) {
if (isChunkLoader) {
this.scripts_chunk_loaders$stopChunkLoader();
}
super.remove(reason);
}
@Override
public Entity teleportTo(TeleportTarget teleportTarget) {
var wasChunkLoader = isChunkLoader;
if (wasChunkLoader)
this.scripts_chunk_loaders$stopChunkLoader(true);
var newEntity = super.teleportTo(teleportTarget);
if (wasChunkLoader && newEntity != null)
((AbstractMinecartEntityMixin)newEntity).scripts_chunk_loaders$startChunkLoader();
return newEntity;
}
@Unique
private void tickParticles() {
this.particleTicker += 1;
if (this.particleTicker >= particleInterval) {
this.particleTicker = 0;
this.spawnParticles();
}
}
@Unique
private void spawnParticles() {
AbstractMinecartEntity entity = (AbstractMinecartEntity)(Object)this;
ServerWorld world = (ServerWorld)entity.getWorld();
world.spawnParticles(ParticleTypes.HAPPY_VILLAGER, entity.getX(), entity.getY(), entity.getZ(), 1, 0.25, 0.25, 0.25, 0.15f);
}
}