Skip to content

Commit

Permalink
Merge pull request #79 from telvarost/75-stay-in-vehicle-when-logging…
Browse files Browse the repository at this point in the history
…-out-and-back-in

ISSUE-75 - Fix finding saved vehicle on login
  • Loading branch information
telvarost authored Apr 13, 2024
2 parents 260a1d5 + ab9d25c commit c6e2f9a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -102,14 +103,16 @@ 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;
if (null == _vehicleTag) return;
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);
Expand All @@ -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;
}
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/annoyancefix.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"FlowingFluidMixin",
"HatchetMixin",
"ItemBaseMixin",
"LevelMixin",
"MinecraftMixin",
"PickaxeMixin",
"PigMixin",
Expand Down

0 comments on commit c6e2f9a

Please sign in to comment.