Skip to content

Commit

Permalink
Improved Item Exchanging and Refactoring in that Area (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
hammy275 committed Oct 10, 2024
1 parent 44bcf3b commit b75d996
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface ItemSwapAmount {

/**
* Get the number of items to swap based on the input item stack size.
* @param stackSize The size of the item stack being swapped.
* @param stackSize The size of the item stack being swapped from the player.
* @return The amount of the stack to ideally swap.
*/
public int getNumItemsToSwap(int stackSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.hammy275.immersivemc.api.common.ImmersiveLogicHelpers;
import com.hammy275.immersivemc.api.server.ItemSwapAmount;
import com.hammy275.immersivemc.api.server.SwapResult;
import com.hammy275.immersivemc.common.util.Util;
import com.hammy275.immersivemc.server.api_impl.SwapResultImpl;
import com.hammy275.immersivemc.server.swap.Swap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.player.Player;
Expand All @@ -30,32 +29,6 @@ public Direction getHorizontalBlockForward(Player player, BlockPos blockPos) {

@Override
public SwapResult swapItems(ItemStack stackFromPlayer, ItemStack stackInImmersive, ItemSwapAmount swapAmount) {
int toPlace = swapAmount.getNumItemsToSwap(stackFromPlayer.getCount());

// Swap toPlace from stackFromPlayer to otherIn
ItemStack toHand;
ItemStack toOther;
ItemStack leftovers;
if (Util.stacksEqualBesidesCount(stackFromPlayer, stackInImmersive) && !stackFromPlayer.isEmpty() && !stackInImmersive.isEmpty()) {
ItemStack stackFromPlayerCountAdjusted = stackFromPlayer.copy();
stackFromPlayerCountAdjusted.setCount(toPlace);
Util.ItemStackMergeResult mergeResult = Util.mergeStacks(stackInImmersive.copy(), stackFromPlayerCountAdjusted, false);
toOther = mergeResult.mergedInto;
// Take our original hand, shrink by all of the amount to be moved, then grow by the amount
// that didn't get moved
toHand = stackFromPlayer.copy();
toHand.shrink(toPlace);
toHand.grow(mergeResult.mergedFrom.getCount());
leftovers = ItemStack.EMPTY;
} else if (stackFromPlayer.isEmpty()) { // We grab the items from the immersive into our hand
return new SwapResultImpl(stackInImmersive.copy(), ItemStack.EMPTY, ItemStack.EMPTY);
} else { // We're placing into a slot of air OR the other slot contains something that isn't what we have
toOther = stackFromPlayer.copy();
toOther.setCount(toPlace);
toHand = stackFromPlayer.copy();
toHand.shrink(toPlace);
leftovers = stackInImmersive.copy();
}
return new SwapResultImpl(toHand, toOther, leftovers);
return Swap.swapItems(stackFromPlayer, stackInImmersive, swapAmount, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AnvilStorage getEmptyNetworkStorage() {
public void swap(int slot, InteractionHand hand, BlockPos pos, ServerPlayer player, ItemSwapAmount amount) {
AnvilStorage storage = (AnvilStorage) WorldStoragesImpl.getOrCreateS(pos, player.getLevel());
if (slot != 2) {
storage.placeItem(player, hand, amount.getNumItemsToSwap(player.getItemInHand(hand).getCount()), slot);
storage.placeItem(player, hand, slot, amount);
storage.setItem(2, ItemStack.EMPTY);
storage.xpLevels = 0;
if (!storage.getItem(0).isEmpty() && !storage.getItem(1).isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hammy275.immersivemc.common.immersive.storage.dual.impl.BeaconStorage;
import com.hammy275.immersivemc.common.util.Util;
import com.hammy275.immersivemc.api.server.WorldStorage;
import com.hammy275.immersivemc.server.api_impl.ConstantItemSwapAmount;
import com.hammy275.immersivemc.server.storage.world.WorldStoragesImpl;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -37,7 +38,7 @@ public void swap(int slot, InteractionHand hand, BlockPos pos, ServerPlayer play
Util.placeLeftovers(player, beaconItem);
beaconStorage.setItem(0, ItemStack.EMPTY);
}
beaconStorage.placeItem(player, hand, 1, 0);
beaconStorage.placeItem(player, hand, 0, new ConstantItemSwapAmount(1));
beaconStorage.setDirty(player.getLevel());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import com.hammy275.immersivemc.api.server.SwapResult;
import com.hammy275.immersivemc.common.config.ActiveConfig;
import com.hammy275.immersivemc.common.immersive.storage.network.impl.ListOfItemsStorage;
import com.hammy275.immersivemc.common.util.Util;
import com.hammy275.immersivemc.server.swap.Swap;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -43,9 +41,8 @@ public void swap(int slot, InteractionHand hand, BlockPos pos, ServerPlayer play
} else { // Ingredient and Fuel
if (!stand.canPlaceItem(slot, playerItem) && playerItem != ItemStack.EMPTY) return;
SwapResult result = ImmersiveLogicHelpers.instance().swapItems(playerItem, standItem, amount);
Swap.givePlayerItemSwap(result.playerHandStack(), playerItem, player, hand);
result.giveToPlayer(player, hand);
stand.setItem(slot, result.immersiveStack());
Util.placeLeftovers(player, result.leftoverStack());
}
stand.setChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public CraftingTableStorage getEmptyNetworkStorage() {
public void swap(int slot, InteractionHand hand, BlockPos pos, ServerPlayer player, ItemSwapAmount amount) {
CraftingTableStorage storage = (CraftingTableStorage) WorldStoragesImpl.getOrCreateS(pos, player.getLevel());
if (slot < 9) {
storage.placeItem(player, hand,
amount.getNumItemsToSwap(player.getItemInHand(hand).getCount()),
slot);
storage.placeItem(player, hand, slot, amount);
storage.setItem(9, Swap.getRecipeOutput(player, storage.getItemsRaw()));
} else {
Swap.handleDoCraft(player, storage.getItemsRaw(), pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.hammy275.immersivemc.common.immersive.storage.network.impl.ETableStorage;
import com.hammy275.immersivemc.common.vr.VRRumble;
import com.hammy275.immersivemc.api.server.WorldStorage;
import com.hammy275.immersivemc.server.api_impl.ConstantItemSwapAmount;
import com.hammy275.immersivemc.server.storage.world.WorldStoragesImpl;
import com.hammy275.immersivemc.server.storage.world.impl.ETableWorldStorage;
import com.hammy275.immersivemc.server.swap.Swap;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void swap(int slot, InteractionHand hand, BlockPos pos, ServerPlayer play
if (slot == 0) {
ItemStack toEnchant = player.getItemInHand(hand);
if (!toEnchant.isEmpty() && !toEnchant.isEnchantable()) return;
enchStorage.placeItem(player, hand, 1, slot);
enchStorage.placeItem(player, hand, slot, new ConstantItemSwapAmount(1));
} else if (player.getItemInHand(hand).isEmpty()) {
boolean res = Swap.doEnchanting(slot, pos, player, hand);
if (res) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.hammy275.immersivemc.common.config.ActiveConfig;
import com.hammy275.immersivemc.common.immersive.storage.network.impl.ListOfItemsStorage;
import com.hammy275.immersivemc.common.util.Util;
import com.hammy275.immersivemc.server.swap.Swap;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -39,9 +38,8 @@ public void swap(int slot, InteractionHand hand, BlockPos pos, ServerPlayer play
if (slot != 2) {
if (slot != 1 || furnace.canPlaceItem(1, playerItem) || playerItem.isEmpty()) {
SwapResult result = ImmersiveLogicHelpers.instance().swapItems(playerItem, furnaceItem, amount);
Swap.givePlayerItemSwap(result.playerHandStack(), playerItem, player, hand);
result.giveToPlayer(player, hand);
furnace.setItem(slot, result.immersiveStack());
Util.placeLeftovers(player, result.leftoverStack());
}
} else {
boolean itemTaken = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public SmithingTableStorage getEmptyNetworkStorage() {
@Override
public void swap(int slot, InteractionHand hand, BlockPos pos, ServerPlayer player, ItemSwapAmount amount) {
SmithingTableStorage storage = (SmithingTableStorage) WorldStoragesImpl.getOrCreateS(pos, player.getLevel());
if (slot != 2) {
storage.placeItem(player, hand, amount.getNumItemsToSwap(player.getItemInHand(hand).getCount()), slot);
storage.setItem(2, ItemStack.EMPTY);
if (!storage.getItem(0).isEmpty() && !storage.getItem(1).isEmpty()) {
if (slot != 3) {
storage.placeItem(player, hand, slot, amount);
storage.setItem(3, ItemStack.EMPTY);
if (!storage.getItem(0).isEmpty() && !storage.getItem(1).isEmpty() && !storage.getItem(2).isEmpty()) {
ItemStack output = Swap.getSmithingTableOutput(storage.getItem(0),
storage.getItem(1), player);
storage.setItem(2, output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.hammy275.immersivemc.common.compat.TinkersConstruct;
import com.hammy275.immersivemc.common.config.ActiveConfig;
import com.hammy275.immersivemc.common.immersive.storage.network.impl.ListOfItemsStorage;
import com.hammy275.immersivemc.common.util.Util;
import com.hammy275.immersivemc.server.swap.Swap;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -55,9 +54,8 @@ public void swap(int slot, InteractionHand hand, BlockPos pos, ServerPlayer play
// Just place the item in. Recipe result is calculated in makeInventoryContents() to show the client
// and at actual crafting time (else block below).
SwapResult result = ImmersiveLogicHelpers.instance().swapItems(playerItem, craftingItem, amount);
Swap.givePlayerItemSwap(result.playerHandStack(), playerItem, player, hand);
result.giveToPlayer(player, hand);
table.setItem(slot, result.immersiveStack());
Util.placeLeftovers(player, result.leftoverStack());
} else {
// Get the items into an array, do the craft, then put the items back.
ItemStack[] items = new ItemStack[10];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package com.hammy275.immersivemc.common.immersive.storage.dual.impl;

import com.hammy275.immersivemc.common.config.ActiveConfig;
import com.hammy275.immersivemc.api.common.immersive.NetworkStorage;
import com.hammy275.immersivemc.common.util.Util;
import com.hammy275.immersivemc.api.server.ItemSwapAmount;
import com.hammy275.immersivemc.api.server.SwapResult;
import com.hammy275.immersivemc.api.server.WorldStorage;
import com.hammy275.immersivemc.server.ServerSubscriber;
import com.hammy275.immersivemc.common.config.ActiveConfig;
import com.hammy275.immersivemc.common.util.Util;
import com.hammy275.immersivemc.server.ServerUtil;
import com.hammy275.immersivemc.server.storage.world.WorldStoragesImpl;
import com.hammy275.immersivemc.server.swap.Swap;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

import java.util.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Stack;
import java.util.UUID;

/**
* Functions both as WorldStorage for saving server side and as a NetworkStorage for sending items
Expand Down Expand Up @@ -105,56 +113,40 @@ public void shrinkCountsOnly(int slot, int amount) {
* @param player Player to get items from
* @param hand Hand to get item from
* @param slot Slot to merge into
* @param amount Amount of items to swap
*
*/
public void placeItem(Player player, InteractionHand hand, int amountToPlace, int slot) {
public void placeItem(Player player, InteractionHand hand, int slot, ItemSwapAmount amount) {
ItemStack playerStack = player.getItemInHand(hand);
ItemStack otherStack = this.getItem(slot);
SwapResult result = Swap.swapItems(playerStack, otherStack, amount,
incrementAmount -> incrementCountForPlayer(player, incrementAmount, slot),
ignored -> this.itemCounts[slot].clear());
result.giveToPlayer(player, hand);
this.items[slot] = result.immersiveStack(); // Set without clearing item counts, since those are updated above
if (player instanceof ServerPlayer sp) {
setDirty(sp.getLevel());
}
}

/**
* Increments the return item count for the provided player.
* @param player Player to increment for.
* @param amount Amount to increment.
* @param slot Slot to increment in.
*/
public void incrementCountForPlayer(Player player, int amount, int slot) {
boolean shouldReturnItems = ActiveConfig.getConfigForPlayer(player).returnItemsWhenLeavingImmersives;
ItemStack toHand;
ItemStack toImmersive;
ItemStack leftovers;
ItemStack handStack = player.getItemInHand(hand);
ItemStack immersiveStack = this.items[slot];
if (Util.stacksEqualBesidesCount(handStack, this.items[slot]) && !handStack.isEmpty()) {
ItemStack handStackToPlace = handStack.copy();
handStackToPlace.setCount(amountToPlace);
int oldImmersiveCount = immersiveStack.getCount();
Util.ItemStackMergeResult mergeResult = Util.mergeStacks(immersiveStack, handStackToPlace, false);
toImmersive = immersiveStack;
toHand = handStack.copy();
toHand.shrink(amountToPlace);
// Add anything that wasn't transferred due to stack size back
toHand.grow(mergeResult.mergedFrom.getCount());
leftovers = ItemStack.EMPTY;
// Always place only in last slot. If Player A places, then Player B, then A places again, order is
// A-B-A, rather than all of A then B.
PlayerItemCounts last = this.itemCounts[slot].get(this.itemCounts[slot].size() - 1);
int itemsMoved = immersiveStack.getCount() - oldImmersiveCount;
if (shouldReturnItems && last.uuid.isPresent() && last.uuid.get().equals(player.getUUID())) {
last.count += itemsMoved;
} else if (shouldReturnItems) {
this.itemCounts[slot].add(new PlayerItemCounts(Optional.of(player.getUUID()), itemsMoved));
} else if (last.uuid.isEmpty()) {
last.count += itemsMoved;
} else {
this.itemCounts[slot].add(new PlayerItemCounts(Optional.empty(), itemsMoved));
}
} else if (handStack.isEmpty()) {
toHand = immersiveStack;
toImmersive = ItemStack.EMPTY;
leftovers = ItemStack.EMPTY;
this.itemCounts[slot].clear();
} else { // Slots contain different item types and hand isn't air (place new stack in and old items go somewhere)
toHand = handStack.copy();
toHand.shrink(amountToPlace);
toImmersive = handStack.copy();
toImmersive.setCount(amountToPlace);
leftovers = immersiveStack.copy();
this.itemCounts[slot].add(
new PlayerItemCounts(Optional.ofNullable(shouldReturnItems ? player.getUUID() : null), amountToPlace));
ItemStorage.PlayerItemCounts last = this.itemCounts[slot].isEmpty() ? null : this.itemCounts[slot].get(this.itemCounts[slot].size() - 1);
if (last != null && shouldReturnItems && last.uuid.isPresent() && last.uuid.get().equals(player.getUUID())) {
last.count += amount;
} else if (shouldReturnItems) {
this.itemCounts[slot].add(new ItemStorage.PlayerItemCounts(Optional.of(player.getUUID()), amount));
} else if (last != null && last.uuid.isEmpty()) {
last.count += amount;
} else {
this.itemCounts[slot].add(new ItemStorage.PlayerItemCounts(Optional.empty(), amount));
}
this.items[slot] = toImmersive;
player.setItemInHand(hand, toHand);
Util.placeLeftovers(player, leftovers);
}

public ItemStack getItem(int slot) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hammy275.immersivemc.server.api_impl;

import com.hammy275.immersivemc.api.server.ItemSwapAmount;

public class ConstantItemSwapAmount implements ItemSwapAmount {

private final int amountToSwap;

public ConstantItemSwapAmount(int amountToSwap) {
this.amountToSwap = amountToSwap;
}

@Override
public int getNumItemsToSwap(int stackSize) {
return amountToSwap;
}
}
Loading

0 comments on commit b75d996

Please sign in to comment.