Skip to content

Commit

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

75 stay in vehicle when logging out and back in
  • Loading branch information
telvarost authored Mar 16, 2024
2 parents e287479 + 6f89612 commit c3e5fa2
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 95 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ The aim of this mod is to fix (major) annoyances or backport useful features tha
- Axes are also effective against: crafting tables, wooden slabs (experimental), wooden stairs, fences, wooden doors, ladders, signs, pumpkins, wooden pressure plates, jukeboxes, and noteblocks
- Fences are placeable like normal and bounding box was minimized when appropriate
- Stairs drop themselves
- Boats drop themselves
- Pigs drop their saddles if they have a saddle equipped
- Bookshelves drop three books
- Boats don't break due to high velocity
- Pigs drop their saddles if they have a saddle equipped
- Boats drop themselves
- Boats don't break due to high velocity (or drop boat, or vanilla, toggle with config)
- Never fall through boats anymore when getting out
- Logout/login with vehicle (no longer exit vehicle upon logout)
- Pick block (middle mouse button click) works for blocks that are in the player's inventory, but not in their hotbar
- Pick block also works on: paintings, minecarts (all three types), wooden doors, iron doors, signs, crops, redstone repeaters, redstone wire, beds, piston heads and cake
- Add missing wooden items as fuel sources in furnace: wooden tools, wooden doors, boats, bowls, signs, and ladders
Expand Down
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.3.0
mod_version=2.4.0
maven_group=com.github.telvarost
archives_base_name=AnnoyanceFix

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/github/telvarost/annoyancefix/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,20 @@ public static class ConfigFields {
@Comment("Options here require restart to take effect")
public final RecipesConfig RECIPES_CONFIG = new RecipesConfig();

@ConfigName("Boat Collision Behavior")
@MultiplayerSynced
@ValueOnVanillaServer(integerValue = 0)
public BoatCollisionEnum boatCollisionBehavior = BoatCollisionEnum.INVINCIBLE;

@ConfigName("Boat Drop Fixes Enabled")
@MultiplayerSynced
@ValueOnVanillaServer(booleanValue = TriBoolean.FALSE)
public Boolean boatDropFixesEnabled = true;

@ConfigName("Boat Speed Collision Behavior")
@ConfigName("Boat Logout/Login Fixes Enabled")
@MultiplayerSynced
@ValueOnVanillaServer(integerValue = 0)
public BoatCollisionEnum boatCollisionBehavior = BoatCollisionEnum.INVINCIBLE;
@ValueOnVanillaServer(booleanValue = TriBoolean.FALSE)
public Boolean boatLogoutLoginFixesEnabled = true;

@ConfigName("Bookshelf Fixes Enabled")
@MultiplayerSynced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ public enum BlockTypeEnum {
SLAB_BLOCK_IS_WOODEN
}

public static byte toByte(boolean value) {
return (byte) (value ? 1 : 0);
}

public static boolean toBool(byte value) {
return value > 0;
}

public static class ModHelperFields {

/** @brief - Special data for remembering block type */
Expand Down
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();
}
}
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);
}
}
}
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);
}
}
}
}
}

}
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);
}
}
}
}
Loading

0 comments on commit c3e5fa2

Please sign in to comment.