Skip to content

Commit

Permalink
Fix multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
RubixDev committed Aug 25, 2022
1 parent 3c5fd26 commit 987034b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 42 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_mappings=1.19.2+build.1
loader_version=0.14.9

# Mod Properties
mod_version = 1.0.1
mod_version = 1.0.2
maven_group = de.rubixdev
archives_base_name = enchantedshulkers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,55 +34,39 @@ public boolean isTreasure() {
return true;
}

private static final Previous previous = new Previous();

private static class Previous {
int slot = -1;
ItemStack mainStack = ItemStack.EMPTY;
ItemStack offStack = ItemStack.EMPTY;
}

public static void onPlayerTick(ServerPlayerEntity player) {
int currentSlot = player.getInventory().selectedSlot;
ItemStack currentMainStack = player.getInventory().getMainHandStack();
ItemStack currentOffStack = player.getInventory().getStack(PlayerInventory.OFF_HAND_SLOT);

boolean swappedHands = ItemStack.areEqual(previous.mainStack, currentOffStack)
&& ItemStack.areNbtEqual(previous.mainStack, currentOffStack)
&& ItemStack.areEqual(currentMainStack, previous.offStack)
&& ItemStack.areNbtEqual(currentMainStack, previous.offStack);
boolean wasMainEmptied = previous.mainStack.getCount() > 0 && currentMainStack.isEmpty() && !swappedHands;
boolean wasOffEmptied = previous.offStack.getCount() > 0 && currentOffStack.isEmpty() && !swappedHands;
boolean shouldRefillMain = (wasMainEmptied || ItemStack.canCombine(currentMainStack, previous.mainStack))
&& currentMainStack.getCount() < previous.mainStack.getCount()
&& (WorldConfig.refillNonStackables() || previous.mainStack.isStackable());
boolean shouldRefillOff = (wasOffEmptied || ItemStack.canCombine(currentOffStack, previous.offStack))
&& currentOffStack.getCount() < previous.offStack.getCount()
&& (WorldConfig.refillNonStackables() || previous.offStack.isStackable())
public static void onPlayerTick(
ServerPlayerEntity player,
int currentSlot,
ItemStack currentMainStack,
ItemStack currentOffStack,
int previousSlot,
ItemStack previousMainStack,
ItemStack previousOffStack) {
boolean swappedHands = ItemStack.areEqual(previousMainStack, currentOffStack)
&& ItemStack.areNbtEqual(previousMainStack, currentOffStack)
&& ItemStack.areEqual(currentMainStack, previousOffStack)
&& ItemStack.areNbtEqual(currentMainStack, previousOffStack);
boolean wasMainEmptied = previousMainStack.getCount() > 0 && currentMainStack.isEmpty() && !swappedHands;
boolean wasOffEmptied = previousOffStack.getCount() > 0 && currentOffStack.isEmpty() && !swappedHands;
boolean shouldRefillMain = (wasMainEmptied || ItemStack.canCombine(currentMainStack, previousMainStack))
&& currentMainStack.getCount() < previousMainStack.getCount()
&& (WorldConfig.refillNonStackables() || previousMainStack.isStackable());
boolean shouldRefillOff = (wasOffEmptied || ItemStack.canCombine(currentOffStack, previousOffStack))
&& currentOffStack.getCount() < previousOffStack.getCount()
&& (WorldConfig.refillNonStackables() || previousOffStack.isStackable())
&& WorldConfig.refillOffhand();

if (currentSlot != previous.slot || swappedHands || !(shouldRefillMain || shouldRefillOff)) {
previous.slot = currentSlot;
previous.mainStack = currentMainStack.copy();
previous.offStack = currentOffStack.copy();
return;
}
if (currentSlot != previousSlot || swappedHands || !(shouldRefillMain || shouldRefillOff)) return;

if (shouldRefillMain) {
refill(
player,
currentSlot,
previous.mainStack,
previous.mainStack.getCount() - currentMainStack.getCount());
refill(player, currentSlot, previousMainStack, previousMainStack.getCount() - currentMainStack.getCount());
} else {
refill(
player,
PlayerInventory.OFF_HAND_SLOT,
previous.offStack,
previous.offStack.getCount() - currentOffStack.getCount());
previousOffStack,
previousOffStack.getCount() - currentOffStack.getCount());
}
previous.mainStack = currentMainStack.copy();
previous.offStack = currentOffStack.copy();
}

static void refill(ServerPlayerEntity player, int slot, ItemStack itemType, int amount) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
package de.rubixdev.enchantedshulkers.mixin;

import de.rubixdev.enchantedshulkers.enchantment.RefillEnchantment;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
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.callback.CallbackInfo;

@Mixin(ServerPlayerEntity.class)
public class ServerPlayerEntityMixin {
@Unique
private int previousSlot = -1;

@Unique
private ItemStack previousMainStack = ItemStack.EMPTY;

@Unique
private ItemStack previousOffStack = ItemStack.EMPTY;

@Inject(
method = "playerTick",
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;tick()V"))
public void playerTick(CallbackInfo ci) {
RefillEnchantment.onPlayerTick((ServerPlayerEntity) (Object) this);
ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;

int currentSlot = player.getInventory().selectedSlot;
ItemStack currentMainStack = player.getInventory().getMainHandStack();
ItemStack currentOffStack = player.getInventory().getStack(PlayerInventory.OFF_HAND_SLOT);

RefillEnchantment.onPlayerTick(
(ServerPlayerEntity) (Object) this,
currentSlot,
currentMainStack,
currentOffStack,
previousSlot,
previousMainStack,
previousOffStack);

previousSlot = currentSlot;
previousMainStack = currentMainStack.copy();
previousOffStack = currentOffStack.copy();
}
}

0 comments on commit 987034b

Please sign in to comment.