Skip to content

Instantly share code, notes, and snippets.

@Devan-Kerman
Last active June 11, 2020 18:29
Show Gist options
  • Save Devan-Kerman/59e8bf09394c9c18eba4b69e07b7aebc to your computer and use it in GitHub Desktop.
Save Devan-Kerman/59e8bf09394c9c18eba4b69e07b7aebc to your computer and use it in GitHub Desktop.
Stacc mixin
package net.devtech.stacc.mixin;
import io.netty.buffer.ByteBuf;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
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;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.PacketByteBuf;
/**
* by HalfOf2
*
* this code is licenced under CC0
* https://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
@Mixin (PacketByteBuf.class)
public abstract class StaccMixin {
@Unique private static final int VAL = 512;
@Shadow public abstract int readInt();
@Redirect (method = "writeItemStack",
at = @At (value = "INVOKE",
target = "Lnet/minecraft/util/PacketByteBuf;writeByte(I)Lio/netty/buffer/ByteBuf;"))
private ByteBuf write(PacketByteBuf buf, int i) {
return buf.writeInt(i);
}
@Redirect (method = "readItemStack",
at = @At (value = "INVOKE",
target = "Lnet/minecraft/util/PacketByteBuf;readByte()B"))
private byte read(PacketByteBuf buf) {
return 0; // no-op moved to #doThing
}
@ModifyArg(method = "readItemStack", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;<init>(Lnet/minecraft/item/ItemConvertible;I)V"), index = 1)
private int doThing(int amount) {
return this.readInt();
}
@Mixin(ItemStack.class)
private static class ItemStackMixin {
@Shadow private int count;
@Inject(at = @At("TAIL"), method = "<init>(Lnet/minecraft/nbt/CompoundTag;)V")
void onDeserialization(CompoundTag tag, CallbackInfo callbackInformation) {
if (tag.contains("countInteger")) {
this.count = tag.getInt("countInteger");
}
}
@Inject(at = @At("HEAD"), method = "toTag(Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/nbt/CompoundTag;")
void onSerialization(CompoundTag tag, CallbackInfoReturnable<CompoundTag> callbackInformationReturnable) {
if (this.count > Byte.MAX_VALUE) {
tag.putInt("countInteger", this.count);
// allow this to be cross compatible with vanilla
this.count = 64;
}
}
}
@Mixin(Inventory.class)
public interface InventoryMixin {
/**
* @author HalfOf2
* @reason increase max stack size
*/
@Overwrite
default int getInvMaxStackAmount() {
return VAL;
}
}
@Mixin(Item.class)
private static class ItemMixin {
@Mutable @Shadow @Final private int maxCount;
@Inject (method = "<init>", at = @At("TAIL"))
private void init(Item.Settings settings, CallbackInfo ci) {
this.maxCount = VAL;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment