-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Entity kill and Item change preview (#241)
- Loading branch information
Showing
19 changed files
with
329 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/github/quiltservertools/ledger/mixin/preview/ChestBlockMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.quiltservertools.ledger.mixin.preview; | ||
|
||
import com.github.quiltservertools.ledger.utility.HandlerWithContext; | ||
import net.minecraft.block.entity.ChestBlockEntity; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.entity.player.PlayerInventory; | ||
import net.minecraft.screen.ScreenHandler; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
// An anonymous inner class inside an anonymous inner class | ||
@Mixin(targets = "net/minecraft/block/ChestBlock$2$1") | ||
public abstract class ChestBlockMixin { | ||
|
||
@Shadow | ||
ChestBlockEntity field_17358; | ||
|
||
@Inject( | ||
method = "createMenu(ILnet/minecraft/entity/player/PlayerInventory;Lnet/minecraft/entity/player/PlayerEntity;)Lnet/minecraft/screen/ScreenHandler;", | ||
at = @At("RETURN") | ||
) | ||
private void addPositionContext(int i, PlayerInventory playerInventory, PlayerEntity playerEntity, CallbackInfoReturnable<ScreenHandler> cir) { | ||
ScreenHandler screenHandler = cir.getReturnValue(); | ||
if (screenHandler != null) { | ||
((HandlerWithContext) screenHandler).setPos(this.field_17358.getPos()); | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...a/com/github/quiltservertools/ledger/mixin/preview/LockableContainerBlockEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.quiltservertools.ledger.mixin.preview; | ||
|
||
import com.github.quiltservertools.ledger.utility.HandlerWithContext; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.block.entity.BlockEntityType; | ||
import net.minecraft.block.entity.LockableContainerBlockEntity; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.entity.player.PlayerInventory; | ||
import net.minecraft.screen.ScreenHandler; | ||
import net.minecraft.util.math.BlockPos; | ||
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(LockableContainerBlockEntity.class) | ||
public abstract class LockableContainerBlockEntityMixin extends BlockEntity { | ||
|
||
public LockableContainerBlockEntityMixin(BlockEntityType<?> type, BlockPos pos, BlockState state) { | ||
super(type, pos, state); | ||
} | ||
|
||
@Inject(method = "createMenu", at = @At("RETURN")) | ||
private void addPositionContext(int i, PlayerInventory playerInventory, PlayerEntity playerEntity, CallbackInfoReturnable<ScreenHandler> cir) { | ||
ScreenHandler screenHandler = cir.getReturnValue(); | ||
if (screenHandler != null) { | ||
((HandlerWithContext) screenHandler).setPos(this.getPos()); | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...a/com/github/quiltservertools/ledger/mixin/preview/LootableContainerBlockEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.quiltservertools.ledger.mixin.preview; | ||
|
||
import com.github.quiltservertools.ledger.utility.HandlerWithContext; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.entity.BlockEntityType; | ||
import net.minecraft.block.entity.LockableContainerBlockEntity; | ||
import net.minecraft.block.entity.LootableContainerBlockEntity; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.entity.player.PlayerInventory; | ||
import net.minecraft.screen.ScreenHandler; | ||
import net.minecraft.util.math.BlockPos; | ||
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(LootableContainerBlockEntity.class) | ||
public abstract class LootableContainerBlockEntityMixin extends LockableContainerBlockEntity { | ||
|
||
protected LootableContainerBlockEntityMixin(BlockEntityType<?> blockEntityType, BlockPos blockPos, BlockState blockState) { | ||
super(blockEntityType, blockPos, blockState); | ||
} | ||
|
||
@Inject(method = "createMenu", at = @At("RETURN")) | ||
private void addPositionContext(int i, PlayerInventory playerInventory, PlayerEntity playerEntity, CallbackInfoReturnable<ScreenHandler> cir) { | ||
ScreenHandler screenHandler = cir.getReturnValue(); | ||
if (screenHandler != null) { | ||
((HandlerWithContext) screenHandler).setPos(this.getPos()); | ||
} | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/github/quiltservertools/ledger/mixin/preview/ServerPlayerEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.github.quiltservertools.ledger.mixin.preview; | ||
|
||
import com.github.quiltservertools.ledger.Ledger; | ||
import com.github.quiltservertools.ledger.actionutils.Preview; | ||
import com.github.quiltservertools.ledger.utility.HandlerWithContext; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import kotlin.Pair; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.screen.ScreenHandler; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.collection.DefaultedList; | ||
import net.minecraft.util.math.BlockPos; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.ModifyArg; | ||
|
||
import java.util.List; | ||
|
||
@Mixin(targets = "net.minecraft.server.network.ServerPlayerEntity$1") | ||
public abstract class ServerPlayerEntityMixin { | ||
|
||
// synthetic field ServerPlayerEntity from the outer class | ||
@Final | ||
@Shadow | ||
ServerPlayerEntity field_29182; | ||
|
||
@ModifyArg( | ||
method = "updateState", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/network/packet/s2c/play/InventoryS2CPacket;<init>(IILnet/minecraft/util/collection/DefaultedList;Lnet/minecraft/item/ItemStack;)V" | ||
), index = 2 | ||
) | ||
private DefaultedList<ItemStack> modifyStacks(DefaultedList<ItemStack> stacks, @Local(argsOnly = true) ScreenHandler handler) { | ||
BlockPos pos = ((HandlerWithContext) handler).getPos(); | ||
if (pos == null) return stacks; | ||
Preview preview = Ledger.previewCache.get(field_29182.getUuid()); | ||
if (preview == null) return stacks; | ||
List<Pair<ItemStack, Boolean>> modifiedItems = preview.getModifiedItems().get(pos); | ||
if (modifiedItems == null) return stacks; | ||
// Copy original list | ||
DefaultedList<ItemStack> previewStacks = DefaultedList.of(); | ||
previewStacks.addAll(stacks); | ||
for (Pair<ItemStack, Boolean> modifiedItem : modifiedItems) { | ||
if (modifiedItem.component2()) { | ||
// Add item | ||
for (int i = 0; i < previewStacks.size(); i++) { | ||
if (previewStacks.get(i).isEmpty()) { | ||
previewStacks.set(i, modifiedItem.component1()); | ||
break; | ||
} | ||
} | ||
} else { | ||
// Remove item | ||
for (int i = 0; i < previewStacks.size(); i++) { | ||
if (ItemStack.areItemsEqual(previewStacks.get(i), modifiedItem.component1())) { | ||
previewStacks.set(i, ItemStack.EMPTY); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
return previewStacks; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/github/quiltservertools/ledger/actions/EntityKillActionType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.