Skip to content

Commit

Permalink
Fixes #12 and #18
Browse files Browse the repository at this point in the history
  • Loading branch information
TechPizzaDev committed Jul 29, 2023
1 parent 68fe9aa commit 80b6a36
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.adventurecraft.awakening.extension.container;

public interface ExPlayerContainer {

boolean getAllowsCrafting();

void setAllowsCrafting(boolean value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.adventurecraft.awakening.extension.container.slot;

public interface ExSlot {

boolean getEnabled();

void setEnabled(boolean value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.adventurecraft.awakening.mixin.client.gui.screen.container;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import dev.adventurecraft.awakening.extension.container.slot.ExSlot;
import dev.adventurecraft.awakening.mixin.client.gui.screen.MixinScreen;
import net.minecraft.client.gui.screen.container.ContainerScreen;
import net.minecraft.container.slot.Slot;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(ContainerScreen.class)
public abstract class MixinContainerScreen extends MixinScreen {

@WrapOperation(
method = "render",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/gui/screen/container/ContainerScreen;isMouseOverSlot(Lnet/minecraft/container/slot/Slot;II)Z"))
private boolean wrapSlotHover(ContainerScreen instance, Slot slot, int x, int y, Operation<Boolean> original) {
if (((ExSlot) slot).getEnabled()) {
return original.call(instance, slot, x, y);
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.adventurecraft.awakening.mixin.client.gui.screen.container;

import com.llamalad7.mixinextras.injector.WrapWithCondition;
import dev.adventurecraft.awakening.extension.container.ExPlayerContainer;
import dev.adventurecraft.awakening.extension.entity.player.ExPlayerEntity;
import net.minecraft.client.gui.screen.container.ContainerScreen;
import net.minecraft.client.gui.screen.container.PlayerInventoryScreen;
import net.minecraft.client.render.TextRenderer;
import net.minecraft.container.Container;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -17,8 +19,16 @@ public MixinPlayerInventoryScreen(Container arg) {
super(arg);
}

@Overwrite
public void renderForeground() {
@WrapWithCondition(
method = "renderForeground",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/render/TextRenderer;drawText(Ljava/lang/String;III)V"))
private boolean renderCraftingLabel(TextRenderer renderer, String string, int i, int j, int k) {
if (container instanceof ExPlayerContainer exContainer) {
return exContainer.getAllowsCrafting();
}
return true;
}

@Inject(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package dev.adventurecraft.awakening.mixin.container;

import com.llamalad7.mixinextras.injector.WrapWithCondition;
import dev.adventurecraft.awakening.extension.world.ExWorldProperties;
import net.minecraft.client.Minecraft;
import dev.adventurecraft.awakening.extension.container.ExPlayerContainer;
import dev.adventurecraft.awakening.extension.container.slot.ExSlot;
import net.minecraft.container.Container;
import net.minecraft.container.PlayerContainer;
import net.minecraft.container.slot.Slot;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.Slice;

import java.util.ArrayList;

@Mixin(PlayerContainer.class)
public abstract class MixinPlayerContainer {
public abstract class MixinPlayerContainer extends Container implements ExPlayerContainer {

private boolean allowsCrafting = true;
private final ArrayList<Slot> craftingSlots = new ArrayList<>();

@WrapWithCondition(
@Redirect(
method = "<init>(Lnet/minecraft/inventory/PlayerInventory;Z)V",
at = @At(
value = "INVOKE",
Expand All @@ -23,8 +29,9 @@ public abstract class MixinPlayerContainer {
value = "INVOKE",
target = "Lnet/minecraft/container/PlayerContainer;addSlot(Lnet/minecraft/container/slot/Slot;)V",
ordinal = 1)))
private boolean conditionalCraftingSlots(PlayerContainer instance, Slot slot) {
return ((ExWorldProperties) Minecraft.instance.world.properties).getAllowsInventoryCrafting();
private void recordCraftingSlots(PlayerContainer instance, Slot slot) {
instance.addSlot(slot);
this.craftingSlots.add(slot);
}

@ModifyArg(
Expand All @@ -48,4 +55,20 @@ private int modifyCraftingResultSlotY(int y) {
private int modifyCraftingSlotY(int y) {
return y + 16;
}

@Override
public boolean getAllowsCrafting() {
return this.allowsCrafting;
}

@Override
public void setAllowsCrafting(boolean value) {
if (this.allowsCrafting != value) {
this.allowsCrafting = value;

for (Slot slot : this.craftingSlots) {
((ExSlot) slot).setEnabled(this.allowsCrafting);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.adventurecraft.awakening.mixin.container;

import dev.adventurecraft.awakening.mixin.container.slot.MixinSlot;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(targets = "net.minecraft.container.PlayerContainer$1")
public abstract class MixinPlayerContainerSlot extends MixinSlot {

@Inject(method = "canInsert", at = @At("HEAD"), cancellable = true)
private void cancelCanInsert(ItemStack item, CallbackInfoReturnable<Boolean> cir) {
if (!this.getEnabled()) {
cir.setReturnValue(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.adventurecraft.awakening.mixin.container.slot;

import dev.adventurecraft.awakening.extension.container.slot.ExSlot;
import net.minecraft.container.slot.Slot;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Slot.class)
public abstract class MixinSlot implements ExSlot {

private boolean enabled = true;

@Override
public boolean getEnabled() {
return this.enabled;
}

@Override
public void setEnabled(boolean value) {
this.enabled = value;
}

@Inject(method = "canInsert", at = @At("HEAD"), cancellable = true)
private void cancelCanInsert(ItemStack item, CallbackInfoReturnable<Boolean> cir) {
if (!this.enabled) {
cir.setReturnValue(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package dev.adventurecraft.awakening.mixin.entity.player;

import dev.adventurecraft.awakening.common.*;
import dev.adventurecraft.awakening.extension.container.ExPlayerContainer;
import dev.adventurecraft.awakening.extension.entity.player.ExPlayerEntity;
import dev.adventurecraft.awakening.extension.inventory.ExPlayerInventory;
import dev.adventurecraft.awakening.extension.world.ExWorldProperties;
import dev.adventurecraft.awakening.mixin.entity.MixinLivingEntity;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.container.Container;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.monster.MonsterEntity;
Expand All @@ -29,6 +33,9 @@ public abstract class MixinPlayerEntity extends MixinLivingEntity implements ExP
@Shadow
public PlayerInventory inventory;

@Shadow
public Container playerContainer;

@Shadow
public int handSwingTicks;

Expand All @@ -42,6 +49,7 @@ public abstract class MixinPlayerEntity extends MixinLivingEntity implements ExP
private boolean swappedItems;
private int numHeartPieces;
public String cloakTexture;
private boolean allowsCrafting;

@Shadow
public abstract ItemStack getHeldItem();
Expand All @@ -65,6 +73,7 @@ public abstract class MixinPlayerEntity extends MixinLivingEntity implements ExP
private void init(World var1, CallbackInfo ci) {
this.health = 12;
this.maxHealth = 12;
this.updateContainer();
}

@ModifyConstant(method = "afterSpawn", constant = @Constant(intValue = 20))
Expand Down Expand Up @@ -106,10 +115,18 @@ public void tickHandSwing() {

@Override
public void baseTick() {
this.updateContainer();
this.prevSwingProgressOffhand = this.swingProgressOffhand;
super.baseTick();
}

private void updateContainer() {
if (playerContainer instanceof ExPlayerContainer exContainer) {
boolean allowsCrafting = ((ExWorldProperties) Minecraft.instance.world.properties).getAllowsInventoryCrafting();
exContainer.setAllowsCrafting(allowsCrafting);
}
}

@ModifyConstant(
method = "updateDespawnCounter",
constant = @Constant(intValue = 20, ordinal = 0))
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/adventurecraft.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ accessible method net/minecraft/entity/animal/WolfEntity spawnBoneParticles (Z)V

accessible method net/minecraft/inventory/PlayerInventory getSlotWithItem (I)I

accessible method net/minecraft/container/Container addSlot (Lnet/minecraft/container/slot/Slot;)V

accessible field net/minecraft/entity/projectile/ArrowEntity blockX I
accessible field net/minecraft/entity/projectile/ArrowEntity blockY I
accessible field net/minecraft/entity/projectile/ArrowEntity blockZ I
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/adventurecraft.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"client.gui.MixinInGameHud",
"client.gui.screen.MixinScreen",
"client.gui.screen.MixinSelectWorldScreen",
"client.gui.screen.container.MixinContainerScreen",
"client.gui.screen.container.MixinPlayerInventoryScreen",
"client.gui.screen.ingame.MixinChatScreen",
"client.gui.screen.ingame.MixinDeathScreen",
Expand Down Expand Up @@ -81,6 +82,8 @@
"client.resource.language.MixinTranslationStorage",
"client.texture.MixinTextureManager",
"container.MixinPlayerContainer",
"container.MixinPlayerContainerSlot",
"container.slot.MixinSlot",
"dimension.MixinDimension",
"entity.MixinBlockEntity",
"entity.MixinBoatEntity",
Expand Down

0 comments on commit 80b6a36

Please sign in to comment.