generated from calmilamsy/stationapi-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from telvarost/75-stay-in-vehicle-when-logging…
…-out-and-back-in 75 stay in vehicle when logging out and back in
- Loading branch information
Showing
10 changed files
with
274 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/github/telvarost/annoyancefix/interfaces/VehicleInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.github.telvarost.annoyancefix.interfaces; | ||
|
||
import net.minecraft.util.io.CompoundTag; | ||
import net.modificationstation.stationapi.api.util.Util; | ||
|
||
public interface VehicleInterface { | ||
default String vehicle_getVehicleName() { | ||
return Util.assertImpl(); | ||
} | ||
|
||
default void vehicle_setVehicleName(String vehicleName) { | ||
Util.assertImpl(); | ||
} | ||
|
||
default CompoundTag vehicle_getVehicleTag() { | ||
return Util.assertImpl(); | ||
} | ||
|
||
default void vehicle_setVehicleTag(CompoundTag vehicleTag) { | ||
Util.assertImpl(); | ||
} | ||
} |
226 changes: 138 additions & 88 deletions
226
src/main/java/com/github/telvarost/annoyancefix/mixin/PlayerBaseMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,138 @@ | ||
//package com.github.telvarost.annoyancefix.mixin; | ||
// | ||
//import net.minecraft.entity.EntityBase; | ||
//import net.minecraft.entity.Living; | ||
//import net.minecraft.entity.player.PlayerBase; | ||
//import net.minecraft.entity.player.PlayerContainer; | ||
//import net.minecraft.item.ItemInstance; | ||
//import net.minecraft.util.io.CompoundTag; | ||
//import org.objectweb.asm.Opcodes; | ||
//import org.spongepowered.asm.mixin.Mixin; | ||
//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.Redirect; | ||
//import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
// | ||
//@Mixin(PlayerBase.class) | ||
//public class PlayerBaseMixin { | ||
// | ||
// @Unique | ||
// private String _vehicleName = null; | ||
// @Unique | ||
// private boolean _playerInVehicle = false; | ||
// @Unique | ||
// private double _vehicleX = 0; | ||
// @Unique | ||
// private double _vehicleY = 70; | ||
// @Unique | ||
// private double _vehicleZ = 0; | ||
// | ||
// @Redirect( | ||
// method = "interactWith", | ||
// at = @At( | ||
// value = "INVOKE", | ||
// target = "Lnet/minecraft/entity/EntityBase;interact(Lnet/minecraft/entity/player/PlayerBase;)Z" | ||
// ) | ||
// ) | ||
// public boolean interactWith(EntityBase instance, PlayerBase playerBase) { | ||
// boolean canInteract = instance.interact(playerBase); | ||
// | ||
// if (canInteract) { | ||
// _playerInVehicle = (instance.passenger != null); | ||
// | ||
// if (instance.passenger != null) { | ||
// _vehicleName = instance.getClass().toString(); | ||
// _vehicleX = Math.floor(instance.x); | ||
// _vehicleY = Math.floor(instance.y); | ||
// _vehicleZ = Math.floor(instance.z); | ||
// } | ||
// } | ||
// | ||
// return canInteract; | ||
// } | ||
// | ||
// | ||
// | ||
// @Inject(method = "writeCustomDataToTag", at = @At("HEAD")) | ||
// private void betaTweaks_writeCustomDataToTag(CompoundTag tag, CallbackInfo info) { | ||
// if (!true) { | ||
// return; | ||
// } | ||
// | ||
// tag.put("PlayerInVehicle", _playerInVehicle); | ||
// | ||
// if (_playerInVehicle) { | ||
// tag.put("VehicleName", _vehicleName); | ||
// tag.put("VehicleX", _vehicleX); | ||
// tag.put("VehicleY", _vehicleY); | ||
// tag.put("VehicleZ", _vehicleZ); | ||
// } | ||
// } | ||
// | ||
// @Inject(method = "readCustomDataFromTag", at = @At("HEAD")) | ||
// private void betaTweaks_readCustomDataFromTag(CompoundTag tag, CallbackInfo info) { | ||
// if (!true) { | ||
// return; | ||
// } | ||
// | ||
// _playerInVehicle = tag.getBoolean("PlayerInVehicle"); | ||
// | ||
// if (_playerInVehicle) { | ||
// _vehicleName = tag.getString("VehicleName"); | ||
// _vehicleX = tag.getDouble("VehicleX"); | ||
// _vehicleY = tag.getDouble("VehicleY"); | ||
// _vehicleZ = tag.getDouble("VehicleZ"); | ||
// } | ||
// } | ||
//} | ||
package com.github.telvarost.annoyancefix.mixin; | ||
|
||
import com.github.telvarost.annoyancefix.Config; | ||
import com.github.telvarost.annoyancefix.interfaces.VehicleInterface; | ||
import net.minecraft.entity.EntityBase; | ||
import net.minecraft.entity.EntityRegistry; | ||
import net.minecraft.entity.Living; | ||
import net.minecraft.entity.player.PlayerBase; | ||
import net.minecraft.level.Level; | ||
import net.minecraft.util.io.CompoundTag; | ||
import net.modificationstation.stationapi.api.entity.player.PlayerHelper; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
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.Redirect; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(PlayerBase.class) | ||
public abstract class PlayerBaseMixin extends Living implements VehicleInterface { | ||
|
||
@Unique | ||
private static String NULL_AS_STRING = "null"; | ||
|
||
@Unique | ||
public String _vehicleName = NULL_AS_STRING; | ||
|
||
@Unique | ||
public CompoundTag _vehicleTag = new CompoundTag(); | ||
|
||
public PlayerBaseMixin(Level arg) { | ||
super(arg); | ||
} | ||
|
||
@Override | ||
public String vehicle_getVehicleName() { | ||
return _vehicleName; | ||
} | ||
|
||
@Override | ||
public void vehicle_setVehicleName(String vehicleName) { | ||
_vehicleName = vehicleName; | ||
} | ||
|
||
@Override | ||
public CompoundTag vehicle_getVehicleTag() { | ||
return _vehicleTag; | ||
} | ||
|
||
@Override | ||
public void vehicle_setVehicleTag(CompoundTag vehicleTag) { | ||
_vehicleTag = vehicleTag; | ||
} | ||
|
||
@Redirect( | ||
method = "interactWith", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/entity/EntityBase;interact(Lnet/minecraft/entity/player/PlayerBase;)Z" | ||
) | ||
) | ||
public boolean interactWith(EntityBase instance, PlayerBase playerBase) { | ||
boolean canInteract = instance.interact(playerBase); | ||
|
||
if (Config.config.boatLogoutLoginFixesEnabled && canInteract) { | ||
/** - Set vehicle data */ | ||
_vehicleName = (instance.passenger != null) ? EntityRegistry.getStringId(instance) : NULL_AS_STRING; | ||
if (!_vehicleName.equals(NULL_AS_STRING)) { | ||
instance.toTag(_vehicleTag); | ||
} | ||
} | ||
|
||
return canInteract; | ||
} | ||
|
||
|
||
|
||
@Inject(method = "writeCustomDataToTag", at = @At("HEAD")) | ||
private void betaTweaks_writeCustomDataToTag(CompoundTag tag, CallbackInfo info) { | ||
if (!Config.config.boatLogoutLoginFixesEnabled) { | ||
return; | ||
} | ||
|
||
/** - Save vehicle data */ | ||
_vehicleName = (this.vehicle != null) ? EntityRegistry.getStringId(this.vehicle) : NULL_AS_STRING; | ||
tag.put("VehicleName", _vehicleName); | ||
if (!_vehicleName.equals(NULL_AS_STRING)) { | ||
this.vehicle.toTag(_vehicleTag); | ||
tag.put("VehicleTag", _vehicleTag); | ||
} | ||
} | ||
|
||
@Inject(method = "readCustomDataFromTag", at = @At("HEAD")) | ||
private void betaTweaks_readCustomDataFromTag(CompoundTag tag, CallbackInfo info) { | ||
if (!Config.config.boatLogoutLoginFixesEnabled) { | ||
return; | ||
} | ||
|
||
/** - Get vehicle data */ | ||
_vehicleName = tag.getString("VehicleName"); | ||
if (!_vehicleName.equals(NULL_AS_STRING)) { | ||
_vehicleTag = tag.getCompoundTag("VehicleTag"); | ||
} | ||
|
||
/** - Spawn saved vehicle if on single player (and remove old vehicle) */ | ||
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) { | ||
|
||
try { | ||
vehicle.fromTag(_vehicleTag); | ||
} catch(Exception ex) { | ||
vehicle.setPositionAndAngles(x, y, z, yaw, pitch); | ||
System.out.println("Failed to read vehicle data"); | ||
} | ||
|
||
/** - Remove old entity if there is one */ | ||
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) | ||
) { | ||
level.removeEntity(entityToCheck); | ||
} | ||
} | ||
|
||
level.spawnEntity(vehicle); | ||
startRiding(vehicle); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/github/telvarost/annoyancefix/mixin/ServerPacketHandlerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.github.telvarost.annoyancefix.mixin; | ||
|
||
import com.github.telvarost.annoyancefix.Config; | ||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.Environment; | ||
import net.minecraft.entity.EntityBase; | ||
import net.minecraft.entity.EntityRegistry; | ||
import net.minecraft.entity.player.ServerPlayer; | ||
import net.minecraft.server.network.ServerPacketHandler; | ||
import net.minecraft.util.io.CompoundTag; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
@Environment(EnvType.SERVER) | ||
@Mixin(ServerPacketHandler.class) | ||
public abstract class ServerPacketHandlerMixin { | ||
|
||
@Redirect( | ||
method = "complete", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/entity/player/ServerPlayer;method_317()V" | ||
) | ||
) | ||
public void annoyanceFix_completeLoadVehicle(ServerPlayer instance) { | ||
instance.method_317(); | ||
|
||
if (Config.config.boatLogoutLoginFixesEnabled) { | ||
/** - Spawn saved vehicle if on multiplayer */ | ||
String vehicleName = instance.vehicle_getVehicleName(); | ||
if (!vehicleName.equals("null")) { | ||
CompoundTag vehicleTag = instance.vehicle_getVehicleTag(); | ||
if (vehicleTag != null) { | ||
EntityBase vehicle = EntityRegistry.create(vehicleName, instance.level); | ||
if (null != vehicle) { | ||
try { | ||
vehicle.fromTag(vehicleTag); | ||
} catch(Exception ex) { | ||
vehicle.setPositionAndAngles(instance.x, instance.y, instance.z, instance.yaw, instance.pitch); | ||
System.out.println("Failed to read vehicle data"); | ||
} | ||
instance.level.spawnEntity(vehicle); | ||
instance.startRiding(vehicle); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...main/java/com/github/telvarost/annoyancefix/mixin/ServerPlayerConnectionManagerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.github.telvarost.annoyancefix.mixin; | ||
|
||
import com.github.telvarost.annoyancefix.Config; | ||
import net.minecraft.entity.player.PlayerBase; | ||
import net.minecraft.server.PlayerHandler; | ||
import net.minecraft.server.ServerPlayerConnectionManager; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
@Mixin(ServerPlayerConnectionManager.class) | ||
public abstract class ServerPlayerConnectionManagerMixin { | ||
|
||
@Redirect( | ||
method = "updateDimension", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/server/PlayerHandler;savePlayer(Lnet/minecraft/entity/player/PlayerBase;)V" | ||
) | ||
) | ||
public void updateDimension(PlayerHandler instance, PlayerBase playerBase) { | ||
instance.savePlayer(playerBase); | ||
|
||
if (Config.config.boatLogoutLoginFixesEnabled) { | ||
/** - Remove vehicle on logout */ | ||
if (null != playerBase.vehicle) { | ||
playerBase.level.removeEntity(playerBase.vehicle); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.