diff --git a/gradle.properties b/gradle.properties index a69571d..b153781 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=b1.7.3-build.2 loader_version=0.14.24-babric.1 # Mod Properties -mod_version=2.4.1 +mod_version=2.4.2 maven_group=com.github.telvarost archives_base_name=AnnoyanceFix diff --git a/src/main/java/com/github/telvarost/annoyancefix/ModHelper.java b/src/main/java/com/github/telvarost/annoyancefix/ModHelper.java index ea76870..eca3dd7 100644 --- a/src/main/java/com/github/telvarost/annoyancefix/ModHelper.java +++ b/src/main/java/com/github/telvarost/annoyancefix/ModHelper.java @@ -22,5 +22,12 @@ public static class ModHelperFields { /** @brief - Special data for flint and steel fixes */ public static Boolean isFireLit = false; + + /** @brief - Special data for finding vehicle on login */ + public static Boolean isVehicleSaved = false; + public static Class savedVehicleClass = null; + public static Double savedVehicleX = 0.0D; + public static Double savedVehicleY = 0.0D; + public static Double savedVehicleZ = 0.0D; } } diff --git a/src/main/java/com/github/telvarost/annoyancefix/mixin/LevelMixin.java b/src/main/java/com/github/telvarost/annoyancefix/mixin/LevelMixin.java new file mode 100644 index 0000000..d93f95f --- /dev/null +++ b/src/main/java/com/github/telvarost/annoyancefix/mixin/LevelMixin.java @@ -0,0 +1,54 @@ +package com.github.telvarost.annoyancefix.mixin; + +import com.github.telvarost.annoyancefix.Config; +import com.github.telvarost.annoyancefix.ModHelper; +import net.minecraft.entity.EntityBase; +import net.minecraft.entity.player.PlayerBase; +import net.minecraft.level.Level; +import net.modificationstation.stationapi.api.entity.player.PlayerHelper; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.util.List; + +@Mixin(Level.class) +public class LevelMixin { + + @Shadow public boolean isServerSide; + + @Shadow public List entities; + + @Inject( + method = "spawnEntity", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/level/Level;method_219(Lnet/minecraft/entity/EntityBase;)V" + ) + ) + public void spawnEntity(EntityBase arg, CallbackInfoReturnable cir) { + if ( (Config.config.boatLogoutLoginFixesEnabled) + && (ModHelper.ModHelperFields.isVehicleSaved) + && (!this.isServerSide) + ) { + /** - Find saved vehicle if on single player */ + for (int entityIndex = 0; entityIndex < this.entities.size(); entityIndex++) { + EntityBase entityToCheck = (EntityBase) this.entities.get(entityIndex); + + if ( (entityToCheck.getClass().equals(ModHelper.ModHelperFields.savedVehicleClass)) + && (1 > Math.abs(entityToCheck.x - ModHelper.ModHelperFields.savedVehicleX)) + && (1 > Math.abs(entityToCheck.y - ModHelper.ModHelperFields.savedVehicleY)) + && (1 > Math.abs(entityToCheck.z - ModHelper.ModHelperFields.savedVehicleZ)) + ) { + PlayerBase player = PlayerHelper.getPlayerFromGame(); + if (null != player) { + player.startRiding(entityToCheck); + } + ModHelper.ModHelperFields.isVehicleSaved = false; + } + } + } + } +} diff --git a/src/main/java/com/github/telvarost/annoyancefix/mixin/PlayerBaseMixin.java b/src/main/java/com/github/telvarost/annoyancefix/mixin/PlayerBaseMixin.java index efba86e..6b65af2 100644 --- a/src/main/java/com/github/telvarost/annoyancefix/mixin/PlayerBaseMixin.java +++ b/src/main/java/com/github/telvarost/annoyancefix/mixin/PlayerBaseMixin.java @@ -1,6 +1,7 @@ package com.github.telvarost.annoyancefix.mixin; import com.github.telvarost.annoyancefix.Config; +import com.github.telvarost.annoyancefix.ModHelper; import com.github.telvarost.annoyancefix.interfaces.VehicleInterface; import net.minecraft.entity.EntityBase; import net.minecraft.entity.EntityRegistry; @@ -102,7 +103,7 @@ private void betaTweaks_readCustomDataFromTag(CompoundTag tag, CallbackInfo info _vehicleTag = tag.getCompoundTag("VehicleTag"); } - /** - Spawn saved vehicle if on single player (and remove old vehicle) */ + /** - Find saved vehicle if on single player */ if (level.isServerSide) return; PlayerBase singlePlayer = PlayerHelper.getPlayerFromGame(); if (null == singlePlayer) return; @@ -110,6 +111,8 @@ private void betaTweaks_readCustomDataFromTag(CompoundTag tag, CallbackInfo info if (_vehicleName.equals(NULL_AS_STRING)) return; EntityBase vehicle = EntityRegistry.create(_vehicleName, level); if (null != vehicle) { + ModHelper.ModHelperFields.isVehicleSaved = true; + EntityBase savedVehicle = null; try { vehicle.fromTag(_vehicleTag); @@ -118,22 +121,28 @@ private void betaTweaks_readCustomDataFromTag(CompoundTag tag, CallbackInfo info System.out.println("Failed to read vehicle data"); } - /** - Remove old entity if there is one */ + ModHelper.ModHelperFields.savedVehicleClass = vehicle.getClass(); + ModHelper.ModHelperFields.savedVehicleX = vehicle.x; + ModHelper.ModHelperFields.savedVehicleY = vehicle.y; + ModHelper.ModHelperFields.savedVehicleZ = vehicle.z; + + /** - Search for vehicle */ for (int entityIndex = 0; entityIndex < level.entities.size(); entityIndex++) { EntityBase entityToCheck = (EntityBase)level.entities.get(entityIndex); - if ( (entityToCheck.getClass().equals(vehicle.getClass())) - && (entityToCheck.x == vehicle.x) - && (entityToCheck.y == vehicle.y) - && (entityToCheck.z == vehicle.z) + if ( (entityToCheck.getClass().equals(ModHelper.ModHelperFields.savedVehicleClass)) + && (1 > Math.abs(entityToCheck.x - vehicle.x)) + && (1 > Math.abs(entityToCheck.y - vehicle.y)) + && (1 > Math.abs(entityToCheck.z - vehicle.z)) ) { - //level.removeEntity(entityToCheck); - startRiding(entityToCheck); + savedVehicle = entityToCheck; } } -// level.spawnEntity(vehicle); -// startRiding(vehicle); + if (null != savedVehicle) { + startRiding(savedVehicle); + ModHelper.ModHelperFields.isVehicleSaved = false; + } } } } diff --git a/src/main/resources/annoyancefix.mixins.json b/src/main/resources/annoyancefix.mixins.json index 02806ef..c81def1 100644 --- a/src/main/resources/annoyancefix.mixins.json +++ b/src/main/resources/annoyancefix.mixins.json @@ -13,6 +13,7 @@ "FlowingFluidMixin", "HatchetMixin", "ItemBaseMixin", + "LevelMixin", "MinecraftMixin", "PickaxeMixin", "PigMixin",