Skip to content

Commit

Permalink
Merge branch 'master' into 1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD committed Dec 3, 2024
2 parents 91a3fdc + fdccae2 commit 5796b0a
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 90 deletions.
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ jobs:
files: |
build/libs/!(*-@(dev-all|sources|javadoc)).jar
build/libs/*-@(dev-all|sources|javadocs).jar
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ kotlin.code.style=official
org.gradle.jvmargs=-Xmx2G

# Mod Properties
modVersion = 1.3.4
modVersion = 1.3.6
mavenGroup = com.github.quiltservertools
modId = ledger
modName = Ledger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package com.github.quiltservertools.ledger.mixin;

import com.github.quiltservertools.ledger.callbacks.ItemInsertCallback;
import com.github.quiltservertools.ledger.callbacks.ItemRemoveCallback;
import com.github.quiltservertools.ledger.utility.HandledSlot;
import com.github.quiltservertools.ledger.utility.HandlerWithContext;
import com.github.quiltservertools.ledger.utility.ItemData;
import com.github.quiltservertools.ledger.utility.Sources;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -20,6 +31,8 @@

@Mixin(ScreenHandler.class)
public abstract class ScreenHandlerMixin implements HandlerWithContext {
@Unique
Map<ItemData, Integer> changedStacks = new HashMap<>();
@Unique
private ServerPlayerEntity player = null;

Expand Down Expand Up @@ -51,6 +64,35 @@ private void ledgerDropInventoryGetPlayer(PlayerEntity player, Inventory invento
this.player = (ServerPlayerEntity) player;
}

@Inject(method = "onClosed", at = @At(value = "RETURN"))
private void ledgerCloseScreenLogChanges(PlayerEntity player, CallbackInfo ci) {
if (!player.getWorld().isClient) {
for (var pair : changedStacks.keySet()) {
ItemStack stack = new ItemStack(Registries.ITEM.getEntry(pair.getItem()), 1, pair.getChanges());
if (stack.isEmpty()) {
continue;
}
int count = changedStacks.get(pair);
int countAbs = Math.abs(count);
List<ItemStack> splitStacks = new ArrayList<>();
while (countAbs > 0) {
ItemStack addStack = stack.copyWithCount(Math.min(countAbs, stack.getMaxCount()));
splitStacks.add(addStack);
countAbs -= addStack.getCount();
}
if (count > 0) {
for (ItemStack splitStack : splitStacks) {
ItemInsertCallback.EVENT.invoker().insert(splitStack, pos, (ServerWorld) player.getWorld(), Sources.PLAYER, (ServerPlayerEntity) player);
}
} else {
for (ItemStack splitStack : splitStacks) {
ItemRemoveCallback.EVENT.invoker().remove(splitStack, pos, (ServerWorld) player.getWorld(), Sources.PLAYER, (ServerPlayerEntity) player);
}
}
}
}
}

@Nullable
@Override
public ServerPlayerEntity getPlayer() {
Expand All @@ -67,4 +109,29 @@ public BlockPos getPos() {
public void setPos(@NotNull BlockPos pos) {
this.pos = pos;
}

@Override
public void onStackChanged(@NotNull ItemStack old, @NotNull ItemStack itemStack, @NotNull BlockPos pos) {
if (old.isEmpty() && !itemStack.isEmpty()) {
// Add item
var key = new ItemData(itemStack.getItem(), itemStack.getComponentChanges());
if (changedStacks.containsKey(key)) {
changedStacks.put(key, changedStacks.get(key) + itemStack.getCount());
} else {
changedStacks.put(key, itemStack.getCount());
}
} else if (!old.isEmpty() && itemStack.isEmpty()) {
// Remove item
var key = new ItemData(old.getItem(), old.getComponentChanges());
if (changedStacks.containsKey(key)) {
changedStacks.put(key, changedStacks.get(key) - old.getCount());
} else {
changedStacks.put(key, -old.getCount());
}
} else if (!old.isEmpty() && !itemStack.isEmpty()) {
// Item changed
onStackChanged(old, ItemStack.EMPTY, pos);
onStackChanged(ItemStack.EMPTY, itemStack, pos);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

import com.github.quiltservertools.ledger.actionutils.DoubleInventoryHelper;
import com.github.quiltservertools.ledger.actionutils.LocationalInventory;
import com.github.quiltservertools.ledger.callbacks.ItemInsertCallback;
import com.github.quiltservertools.ledger.callbacks.ItemRemoveCallback;
import com.github.quiltservertools.ledger.utility.HandledSlot;
import com.github.quiltservertools.ledger.utility.HandlerWithContext;
import com.github.quiltservertools.ledger.utility.Sources;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -58,7 +52,7 @@ private void ledgerLogChanges(CallbackInfo ci) {
HandlerWithContext handlerWithContext = (HandlerWithContext) handler;

if (pos != null && handlerWithContext.getPlayer() != null) {
logChange(handlerWithContext.getPlayer(), oldStack, this.getStack().copy(), pos);
handlerWithContext.onStackChanged(oldStack, this.getStack().copy(), pos);
}

oldStack = this.getStack().copy();
Expand All @@ -77,34 +71,4 @@ private BlockPos getInventoryLocation() {

return null;
}

@Unique
private void logChange(PlayerEntity player, ItemStack stack, ItemStack newStack, BlockPos pos) {
if (stack.isEmpty() && newStack.isEmpty()) return; // nothing to do

if (!stack.isEmpty() && !newStack.isEmpty()) { // 2 non-empty stacks
if (stack.getItem() == newStack.getItem()) { // add or remove to stack of same type
int newCount = newStack.getCount();
int oldCount = stack.getCount();
if (newCount > oldCount) { // add items
logChange(player, ItemStack.EMPTY, new ItemStack(newStack.getItem(), newCount - oldCount), pos);
} else { // remove items
logChange(player, new ItemStack(newStack.getItem(), oldCount - newCount), ItemStack.EMPTY, pos);
}
} else { // split up the actions
logChange(player, stack, ItemStack.EMPTY, pos); // log taking out the old stack
logChange(player, ItemStack.EMPTY, newStack, pos); // log putting in the new stack
}
return;
}

boolean oldEmpty = stack.isEmpty(); // we know only one is empty
ItemStack changedStack = oldEmpty ? newStack : stack;

if (oldEmpty) {
ItemInsertCallback.EVENT.invoker().insert(changedStack, pos, (ServerWorld) player.getWorld(), Sources.PLAYER, (ServerPlayerEntity) player);
} else {
ItemRemoveCallback.EVENT.invoker().remove(changedStack, pos, (ServerWorld) player.getWorld(), Sources.PLAYER, (ServerPlayerEntity) player);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,19 @@ import net.minecraft.block.LecternBlock
import net.minecraft.block.entity.ChestBlockEntity
import net.minecraft.block.entity.LecternBlockEntity
import net.minecraft.inventory.Inventory
import net.minecraft.item.BlockItem
import net.minecraft.item.ItemStack
import net.minecraft.item.Items
import net.minecraft.registry.Registries
import net.minecraft.server.MinecraftServer
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.server.world.ServerWorld
import net.minecraft.text.HoverEvent
import net.minecraft.text.Text
import net.minecraft.util.Util
import net.minecraft.util.math.BlockPos

abstract class ItemChangeActionType : AbstractActionType() {
override fun getTranslationType(): String {
val item = Registries.ITEM.get(objectIdentifier)
return if (item is BlockItem) {
"block"
} else {
"item"
}
}
// Not used
override fun getTranslationType(): String = "item"

private fun getStack(server: MinecraftServer) = NbtUtils.itemFromProperties(
extraData,
Expand All @@ -48,12 +39,7 @@ abstract class ItemChangeActionType : AbstractActionType() {
val stack = getStack(source.server)

return "${stack.count} ".literal().append(
Text.translatable(
Util.createTranslationKey(
getTranslationType(),
objectIdentifier
)
)
stack.itemName
).setStyle(TextColorPallet.secondaryVariant).styled {
it.withHoverEvent(
HoverEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,18 @@ import com.github.quiltservertools.ledger.utility.literal
import net.minecraft.entity.Entity
import net.minecraft.entity.EntityType
import net.minecraft.entity.ItemEntity
import net.minecraft.item.BlockItem
import net.minecraft.nbt.StringNbtReader
import net.minecraft.registry.Registries
import net.minecraft.server.MinecraftServer
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.HoverEvent
import net.minecraft.text.Text
import net.minecraft.util.Util

// TODO remove duplication from ItemPickUpActionType and ItemDropActionType
open class ItemDropActionType : AbstractActionType() {
override val identifier = "item-drop"

override fun getTranslationType(): String {
val item = Registries.ITEM.get(objectIdentifier)
return if (item is BlockItem) {
"block"
} else {
"item"
}
}
// Not used
override fun getTranslationType(): String = "item"

private fun getStack(server: MinecraftServer) = NbtUtils.itemFromProperties(
extraData,
Expand All @@ -39,12 +31,7 @@ open class ItemDropActionType : AbstractActionType() {
val stack = getStack(source.server)

return "${stack.count} ".literal().append(
Text.translatable(
Util.createTranslationKey(
getTranslationType(),
objectIdentifier
)
)
stack.itemName
).setStyle(TextColorPallet.secondaryVariant).styled {
it.withHoverEvent(
HoverEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,17 @@ import com.github.quiltservertools.ledger.utility.literal
import net.minecraft.entity.Entity
import net.minecraft.entity.EntityType
import net.minecraft.entity.ItemEntity
import net.minecraft.item.BlockItem
import net.minecraft.nbt.StringNbtReader
import net.minecraft.registry.Registries
import net.minecraft.server.MinecraftServer
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.HoverEvent
import net.minecraft.text.Text
import net.minecraft.util.Util

open class ItemPickUpActionType : AbstractActionType() {
override val identifier = "item-pick-up"

override fun getTranslationType(): String {
val item = Registries.ITEM.get(objectIdentifier)
return if (item is BlockItem) {
"block"
} else {
"item"
}
}
// Not used
override fun getTranslationType(): String = "item"

private fun getStack(server: MinecraftServer) = NbtUtils.itemFromProperties(
extraData,
Expand All @@ -39,12 +30,7 @@ open class ItemPickUpActionType : AbstractActionType() {
val stack = getStack(source.server)

return "${stack.count} ".literal().append(
Text.translatable(
Util.createTranslationKey(
getTranslationType(),
objectIdentifier
)
)
stack.itemName
).setStyle(TextColorPallet.secondaryVariant).styled {
it.withHoverEvent(
HoverEvent(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.quiltservertools.ledger.utility

import net.minecraft.item.ItemStack
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.math.BlockPos

interface HandlerWithContext {
fun getPlayer(): ServerPlayerEntity?
fun getPos(): BlockPos?
fun setPos(pos: BlockPos)
fun onStackChanged(old: ItemStack, new: ItemStack, pos: BlockPos)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.quiltservertools.ledger.utility

import net.minecraft.component.ComponentChanges
import net.minecraft.item.Item

data class ItemData(val item: Item, val changes: ComponentChanges)

0 comments on commit 5796b0a

Please sign in to comment.