Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit feb1ebc

Browse files
committed
A billion fixes.
1 parent f7c5ee5 commit feb1ebc

File tree

11 files changed

+118
-134
lines changed

11 files changed

+118
-134
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2023
3+
Copyright (c) 2024
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies {
4242

4343
// Fabric API. This is technically optional, but you probably want it anyway.
4444
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
45+
implementation(annotationProcessor("io.github.llamalad7:mixinextras-fabric:${project.mixin_extras_version}"))
4546
}
4647

4748
processResources {

gradle.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ org.gradle.jvmargs=-Xmx1G
44
# check these on https://fabricmc.net/versions.html
55
minecraft_version=1.20.2
66
yarn_mappings=1.20.2+build.4
7-
loader_version=0.14.22
7+
loader_version=0.15.3
88
# Mod Properties
99
mod_version=5.3.0-1.20.2
1010
maven_group=com.github.vini2003.linkart
1111
archives_base_name=linkart
1212
# Dependencies
1313
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api (or https://fabricmc.net/versions.html)
1414
fabric_version=0.90.0+1.20.2
15+
mixin_extras_version=0.3.2
1516

1617
# Publishing
17-
game_versions=>=1.19.2 <=1.20.2
18+
game_versions=>=1.19.2 <=1.20.4

src/main/java/com/github/vini2003/linkart/Linkart.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.google.gson.GsonBuilder;
99
import net.fabricmc.api.ModInitializer;
1010
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
11+
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
1112
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
1213
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
1314
import net.fabricmc.loader.api.FabricLoader;
@@ -22,18 +23,19 @@
2223
import java.io.IOException;
2324
import java.nio.file.Files;
2425
import java.nio.file.Path;
25-
import java.util.HashMap;
26+
import java.util.IdentityHashMap;
2627
import java.util.Map;
2728

2829
public class Linkart implements ModInitializer {
2930

3031
public static final String ID = "linkart";
3132
public static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("linkart.json");
3233
public static LinkartConfiguration CONFIG;
33-
public static final Map<PlayerEntity, AbstractMinecartEntity> LINKING_CARTS = new HashMap<>();
34-
public static final Map<PlayerEntity, AbstractMinecartEntity> UNLINKING_CARTS = new HashMap<>();
3534
public static final TagKey<Item> LINKERS = TagKey.of(itemKey(), new Identifier(ID, "linkers"));
3635

36+
public static final Map<PlayerEntity, AbstractMinecartEntity> LINKING_CARTS = new IdentityHashMap<>();
37+
public static final Map<PlayerEntity, AbstractMinecartEntity> UNLINKING_CARTS = new IdentityHashMap<>();
38+
3739
static {
3840
loadConfig();
3941
}
@@ -47,6 +49,11 @@ public void onInitialize() {
4749
if (CONFIG.chunkloading) LoadingCarts.getOrCreate(world);
4850
});
4951

52+
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
53+
LINKING_CARTS.clear();
54+
UNLINKING_CARTS.clear();
55+
});
56+
5057
ServerTickEvents.START_WORLD_TICK.register(world -> {
5158
if (CONFIG.chunkloading && ((PersistentStateAccessor)world.getPersistentStateManager()).linkart$loadedStates().containsKey("linkart_loading_carts")) {
5259
LoadingCarts.getOrCreate(world).tick(world);

src/main/java/com/github/vini2003/linkart/api/LinkableMinecart.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,28 @@
44
import net.minecraft.item.ItemStack;
55

66
public interface LinkableMinecart {
7-
AbstractMinecartEntity linkart$getFollowing();
87

9-
void linkart$setFollowing(AbstractMinecartEntity following);
8+
default AbstractMinecartEntity linkart$getFollowing() {
9+
throw new IllegalStateException("Implemented via mixin");
10+
}
1011

11-
AbstractMinecartEntity linkart$getFollower();
12+
default void linkart$setFollowing(AbstractMinecartEntity following) {
13+
throw new IllegalStateException("Implemented via mixin");
14+
}
1215

13-
void linkart$setFollower(AbstractMinecartEntity follower);
16+
default AbstractMinecartEntity linkart$getFollower() {
17+
throw new IllegalStateException("Implemented via mixin");
18+
}
1419

15-
ItemStack linkart$getLinkItem();
20+
default void linkart$setFollower(AbstractMinecartEntity follower) {
21+
throw new IllegalStateException("Implemented via mixin");
22+
}
1623

17-
void linkart$setLinkItem(ItemStack linkItem);
24+
default ItemStack linkart$getLinkItem() {
25+
throw new IllegalStateException("Implemented via mixin");
26+
}
27+
28+
default void linkart$setLinkItem(ItemStack linkItem) {
29+
throw new IllegalStateException("Implemented via mixin");
30+
}
1831
}

src/main/java/com/github/vini2003/linkart/mixin/AbstractMinecartEntityMixin.java

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
import com.github.vini2003.linkart.Linkart;
44
import com.github.vini2003.linkart.api.LinkableMinecart;
5+
import com.github.vini2003.linkart.utility.CartUtils;
56
import com.github.vini2003.linkart.utility.CollisionUtils;
67
import com.github.vini2003.linkart.utility.LoadingCarts;
78
import net.minecraft.entity.Entity;
89
import net.minecraft.entity.EntityType;
9-
import net.minecraft.entity.ItemEntity;
1010
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
1111
import net.minecraft.item.ItemStack;
12-
import net.minecraft.item.Items;
1312
import net.minecraft.nbt.NbtCompound;
14-
import net.minecraft.particle.ItemStackParticleEffect;
15-
import net.minecraft.particle.ParticleTypes;
1613
import net.minecraft.server.world.ChunkTicketType;
1714
import net.minecraft.server.world.ServerWorld;
1815
import net.minecraft.util.math.Vec3d;
@@ -42,27 +39,11 @@ public AbstractMinecartEntityMixin(EntityType<?> type, World world) {
4239
super(type, world);
4340
}
4441

45-
@Unique
46-
private static void linkart$spawnChainParticles(AbstractMinecartEntity entity, LinkableMinecart duck) {
47-
if (!entity.getWorld().isClient()) {
48-
((ServerWorld) entity.getWorld()).spawnParticles(new ItemStackParticleEffect(ParticleTypes.ITEM, duck.linkart$getLinkItem()), entity.getX(), entity.getY() + 0.3, entity.getZ(), 15, 0.2, 0.2, 0.2, 0.2);
49-
}
50-
}
51-
52-
@Unique
53-
private static boolean linkart$approximatelyZero(double a) {
54-
return Math.abs(0 - a) < 0.00029146489604938;
55-
}
56-
5742
@Inject(at = @At("HEAD"), method = "tick")
5843
private void linkart$tick(CallbackInfo ci) {
5944
if (!getWorld().isClient()) {
45+
AbstractMinecartEntity cast = (AbstractMinecartEntity) (Object) this;
6046
if (linkart$getFollowing() != null) {
61-
if (linkart$getFollowing().isRemoved() || this.isRemoved()) {
62-
linkart$unlink();
63-
return;
64-
}
65-
6647
Vec3d pos = getPos();
6748
Vec3d pos2 = linkart$getFollowing().getPos();
6849
double dist = Math.abs(pos.distanceTo(pos2)) - 1.2;
@@ -95,39 +76,22 @@ public AbstractMinecartEntityMixin(EntityType<?> type, World world) {
9576
if (dist <= Linkart.CONFIG.pathfindingDistance) {
9677
setVelocity(vec3d);
9778
} else {
98-
linkart$unlink();
79+
CartUtils.unlink(cast);
9980
}
10081
}
10182
}
10283

10384
if (Linkart.CONFIG.chunkloading) {
104-
if (linkart$getFollower() != null && !linkart$approximatelyZero(this.getVelocity().length())) {
85+
if (linkart$getFollower() != null && !CartUtils.approximatelyZero(this.getVelocity().length())) {
10586
((ServerWorld) this.getWorld()).getChunkManager().addTicket(ChunkTicketType.PORTAL, this.getChunkPos(), Linkart.CONFIG.chunkloadingRadius, this.getBlockPos());
106-
LoadingCarts.getOrCreate((ServerWorld) getWorld()).addCart((AbstractMinecartEntity) (Object) this);
87+
LoadingCarts.getOrCreate((ServerWorld) getWorld()).addCart(cast);
10788
} else {
108-
LoadingCarts.getOrCreate((ServerWorld) getWorld()).removeCart((AbstractMinecartEntity) (Object) this);
89+
LoadingCarts.getOrCreate((ServerWorld) getWorld()).removeCart(cast);
10990
}
11091
}
11192
}
11293
}
11394

114-
@Unique
115-
private void linkart$unlink() {
116-
LinkableMinecart duck = (LinkableMinecart) linkart$getFollowing();
117-
118-
duck.linkart$setFollower(null);
119-
linkart$setFollowing(null);
120-
setVelocity(0, 0, 0);
121-
122-
ItemEntity itemEntity = new ItemEntity(getWorld(), getX(), getY(), getZ(), Items.CHAIN.getDefaultStack());
123-
itemEntity.setToDefaultPickupDelay();
124-
getWorld().spawnEntity(itemEntity);
125-
126-
linkart$spawnChainParticles((AbstractMinecartEntity) (Object) this, this);
127-
128-
duck.linkart$setLinkItem(ItemStack.EMPTY);
129-
}
130-
13195
@Inject(at = @At("HEAD"), method = "pushAwayFrom", cancellable = true)
13296
void onPushAway(Entity entity, CallbackInfo ci) {
13397
if (!CollisionUtils.shouldCollide(this, entity)) {
Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.github.vini2003.linkart.mixin;
22

3-
import com.github.vini2003.linkart.api.LinkableMinecart;
3+
import com.github.vini2003.linkart.utility.CartUtils;
44
import com.github.vini2003.linkart.utility.CollisionUtils;
5+
import com.llamalad7.mixinextras.sugar.Local;
56
import net.minecraft.block.AbstractRailBlock;
67
import net.minecraft.entity.Entity;
7-
import net.minecraft.entity.ItemEntity;
88
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
9-
import net.minecraft.item.ItemStack;
109
import net.minecraft.util.math.Box;
1110
import net.minecraft.util.math.Vec3d;
1211
import net.minecraft.world.World;
13-
import org.jetbrains.annotations.Nullable;
1412
import org.spongepowered.asm.mixin.Mixin;
1513
import org.spongepowered.asm.mixin.Shadow;
1614
import org.spongepowered.asm.mixin.injection.At;
@@ -28,30 +26,19 @@ public abstract class EntityMixin {
2826
@Shadow
2927
public abstract Box getBoundingBox();
3028

31-
@Shadow @Nullable public abstract ItemEntity dropStack(ItemStack stack);
32-
3329
@Inject(at = @At("HEAD"), method = "remove")
34-
void linkart$removeLink(CallbackInfo callbackInformation) {
35-
if ((Entity) (Object) this instanceof AbstractMinecartEntity && !world.isClient()) {
36-
LinkableMinecart accessor = (LinkableMinecart) this;
37-
LinkableMinecart follower = (LinkableMinecart) accessor.linkart$getFollower();
38-
LinkableMinecart following = (LinkableMinecart) accessor.linkart$getFollowing();
39-
40-
if (follower != null) {
41-
follower.linkart$setFollowing(null);
42-
}
43-
44-
if (following != null) {
45-
following.linkart$setFollower(null);
46-
}
30+
void linkart$removeLink(CallbackInfo callbackInformation, @Local Entity.RemovalReason reason) {
31+
if ((Entity) (Object) this instanceof AbstractMinecartEntity minecart && !world.isClient() && reason.shouldDestroy()) {
32+
if (minecart.linkart$getFollowing() != null) CartUtils.unlink(minecart);
33+
if (minecart.linkart$getFollower() != null) CartUtils.unlink(minecart.linkart$getFollower());
4734
}
4835
}
4936

5037
@Inject(at = @At("HEAD"), method = "adjustMovementForCollisions(Lnet/minecraft/util/math/Vec3d;)Lnet/minecraft/util/math/Vec3d;", cancellable = true)
5138
void linkart$onRecalculateVelocity(Vec3d movement, CallbackInfoReturnable<Vec3d> cir) {
52-
List<Entity> collisions = this.world.getOtherEntities((Entity) (Object) this, getBoundingBox().stretch(movement));
39+
if ((Object) this instanceof AbstractMinecartEntity minecart) {
40+
List<Entity> collisions = this.world.getOtherEntities((Entity) (Object) this, getBoundingBox().stretch(movement));
5341

54-
if ((Entity) (Object) this instanceof AbstractMinecartEntity minecart) {
5542
for (Entity entity : collisions) {
5643
if (!CollisionUtils.shouldCollide((Entity) (Object) this, entity) && world.getBlockState(minecart.getBlockPos()).getBlock() instanceof AbstractRailBlock) {
5744
cir.setReturnValue(movement);
@@ -60,11 +47,4 @@ public abstract class EntityMixin {
6047
}
6148
}
6249
}
63-
64-
@Inject(at = @At("HEAD"), method = "kill")
65-
private void linkart$kill(CallbackInfo ci) {
66-
if ((Object) this instanceof LinkableMinecart minecart) {
67-
this.dropStack(minecart.linkart$getLinkItem());
68-
}
69-
}
7050
}

0 commit comments

Comments
 (0)