Skip to content

Commit

Permalink
[StAPI] Attempt at getting multiplayer to work
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Oct 28, 2023
1 parent ddee889 commit 86f2f50
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package io.github.betterthanupdates.apron.stapi.mixin;

import io.github.betterthanupdates.forge.block.ForgeBlock;
import io.github.betterthanupdates.stapi.StAPIBlock;
import io.github.betterthanupdates.apron.stapi.ApronStAPICompat;
import io.github.betterthanupdates.apron.stapi.ModContents;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.modificationstation.stationapi.api.block.BlockState;
import net.modificationstation.stationapi.api.block.StationFlatteningBlock;
import net.modificationstation.stationapi.api.registry.BlockRegistry;
import net.modificationstation.stationapi.api.registry.Identifier;
import net.modificationstation.stationapi.api.registry.ItemRegistry;
import net.modificationstation.stationapi.api.template.block.BlockTemplate;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -24,7 +32,7 @@
import java.util.Random;

@Mixin(Block.class)
public class BlockMixin implements StAPIBlock {
public abstract class BlockMixin implements StAPIBlock, StationFlatteningBlock, ForgeBlock {
@Shadow
@Final
public int id;
Expand Down Expand Up @@ -93,4 +101,16 @@ private int fixBlockDrop(Block instance, int i, Random random) {

return originalId;
}

@Override
public float calcBlockBreakingDelta(BlockState state, PlayerEntity player, BlockView world, BlockPos pos) {
float apronHardness = this.blockStrength((World) world, player, pos.x, pos.y, pos.z);

float hardness = this.getHardness(state, world, pos);
if (hardness < 0.0F) {
return Math.max(0.0F, apronHardness);
} else {
return !player.canHarvest(state) ? Math.max(1.0F / hardness / 100.0F, apronHardness) : Math.max(player.getBlockBreakingSpeed(state) / hardness / 30.0F, apronHardness);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.betterthanupdates.forge.mixin.client;
package io.github.betterthanupdates.forge.mixin.client.nostation;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.betterthanupdates.forge.mixin.client;
package io.github.betterthanupdates.forge.mixin.client.nostation;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.betterthanupdates.forge.mixin.server.server.player;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import forge.ForgeHooks;
import forge.IUseItemFirst;
Expand All @@ -19,32 +21,6 @@

@Mixin(ServerInteractionManager.class)
public class ServerInteractionManagerMixin {
@Shadow
private ServerWorld world;

@Shadow
private int field_2318;

@Shadow
private int field_2319;

@Shadow
private int field_2320;

@Redirect(method = "method_1828", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;getHardness(Lnet/minecraft/entity/player/PlayerEntity;)F"))
private float forge$method_1828$blockStrength(Block instance, PlayerEntity playerEntity) {
return ((ForgeBlock) instance).blockStrength(this.world, playerEntity, this.field_2318, this.field_2319, this.field_2320);
}

@Redirect(method = {"method_1830", "method_1829"}, at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;getHardness(Lnet/minecraft/entity/player/PlayerEntity;)F", ordinal = 0))
private float forge$blockStrength(Block instance, PlayerEntity playerEntity, @Local(ordinal = 0) int i, @Local(ordinal = 1) int j, @Local(ordinal = 2) int k) {
return ((ForgeBlock) instance).blockStrength(this.world, playerEntity, i, j, k);
}

@Redirect(method = "method_1834", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;canRemoveBlock(Lnet/minecraft/block/Block;)Z"))
private boolean forge$canHarvestBlock(PlayerEntity instance, Block block, @Local(ordinal = 4) int meta) {
return ((ForgeBlock) block).canHarvestBlock(instance, meta);
}

@Inject(method = "method_1831", at = @At(value = "RETURN", ordinal = 0))
private void forge$method_1831(PlayerEntity player, World arg3, ItemStack par3, CallbackInfoReturnable<Boolean> cir, @Local(ordinal = 1) ItemStack usedStack) {
Expand All @@ -63,9 +39,9 @@ public class ServerInteractionManagerMixin {
}
}

@Redirect(method = "activateBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;useOnBlock(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/world/World;IIII)Z"))
private boolean forge$activateBlock$destroyItemStack(ItemStack instance, PlayerEntity player, World world, int i, int j, int k, int l) {
if (!instance.useOnBlock(player, world, i, j, k, l)) {
@WrapOperation(method = "activateBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;useOnBlock(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/world/World;IIII)Z"))
private boolean forge$activateBlock$destroyItemStack(ItemStack instance, PlayerEntity player, World world, int i, int j, int k, int l, Operation<Boolean> operation) {
if (!operation.call(instance, player, world, i, j, k, l)) {
return false;
} else {
if (instance.count == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.betterthanupdates.forge.mixin.server.server.player.nostation;

import com.llamalad7.mixinextras.sugar.Local;
import io.github.betterthanupdates.forge.block.ForgeBlock;
import net.minecraft.block.Block;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.player.ServerInteractionManager;
import net.minecraft.server.world.ServerWorld;
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.Redirect;

@Mixin(ServerInteractionManager.class)
public class ServerInteractionManagerMixin {
@Shadow
private ServerWorld world;

@Shadow
private int field_2318;

@Shadow
private int field_2319;

@Shadow
private int field_2320;

@Redirect(method = "method_1828", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;getHardness(Lnet/minecraft/entity/player/PlayerEntity;)F"))
private float forge$method_1828$blockStrength(Block instance, PlayerEntity playerEntity) {
return ((ForgeBlock) instance).blockStrength(this.world, playerEntity, this.field_2318, this.field_2319, this.field_2320);
}

@Redirect(method = {"method_1830", "method_1829"}, at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;getHardness(Lnet/minecraft/entity/player/PlayerEntity;)F", ordinal = 0))
private float forge$blockStrength(Block instance, PlayerEntity playerEntity, @Local(ordinal = 0) int i, @Local(ordinal = 1) int j, @Local(ordinal = 2) int k) {
return ((ForgeBlock) instance).blockStrength(this.world, playerEntity, i, j, k);
}

@Redirect(method = "method_1834", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;canRemoveBlock(Lnet/minecraft/block/Block;)Z"))
private boolean forge$canHarvestBlock(PlayerEntity instance, Block block, @Local(ordinal = 4) int meta) {
return ((ForgeBlock) block).canHarvestBlock(instance, meta);
}
}
11 changes: 6 additions & 5 deletions apron/src/main/resources/forge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,27 @@
"item.BucketItemMixin",
"item.ItemMixin",
"util.hit.HitResultMixin",
"world.explosion.ExplosionMixin",
"world.WorldMixin",
"world.chunk.ChunkMixin"
"world.chunk.ChunkMixin",
"world.explosion.ExplosionMixin"
],
"client": [
"client.BlockRendererMixin",
"client.ClientInteractionManagerMixin",
"client.GameRendererMixin",
"client.MultiplayerClientInteractionManagerMixin",
"client.ParticleManagerMixin",
"client.PistonRendererMixin",
"client.PlayerRendererMixin",
"client.TessellatorMixin",
"client.TextureManagerMixin"
"client.TextureManagerMixin",
"client.nostation.MultiplayerClientInteractionManagerMixin"
],
"server": [
"server.class_417Mixin",
"server.item.ToolItemMixin",
"server.server.nostation.ServerPlayerView_class_514Mixin",
"server.server.player.ServerInteractionManagerMixin"
"server.server.player.ServerInteractionManagerMixin",
"server.server.player.nostation.ServerInteractionManagerMixin"
],
"plugin": "io.github.betterthanupdates.apron.ApronMixinPlugin",
"parent": "apron.parent.mixins.json"
Expand Down

0 comments on commit 86f2f50

Please sign in to comment.