-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send clients with mod installed extra metadata for the custom info li…
…nes included in the sent item stacks to allow for item stack syncing. Add new player filter feature for database querying
- Loading branch information
1 parent
75fce98
commit 804a389
Showing
18 changed files
with
309 additions
and
61 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...client/java/xd/arkosammy/publicenderchest/mixin/client/ClientPlayNetworkHandlerMixin.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,55 @@ | ||
package xd.arkosammy.publicenderchest.mixin.client; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import net.minecraft.client.network.ClientPlayNetworkHandler; | ||
import net.minecraft.component.DataComponentTypes; | ||
import net.minecraft.component.type.LoreComponent; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Formatting; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import xd.arkosammy.publicenderchest.PublicEnderChestKt; | ||
import xd.arkosammy.publicenderchest.util.ducks.ItemStackDuck; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
@Mixin(ClientPlayNetworkHandler.class) | ||
public abstract class ClientPlayNetworkHandlerMixin { | ||
|
||
@WrapOperation(method = "onScreenHandlerSlotUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/ScreenHandlerSlotUpdateS2CPacket;getStack()Lnet/minecraft/item/ItemStack;")) | ||
private ItemStack restoreOriginalItemStackAndSaveCustomInfo(ScreenHandlerSlotUpdateS2CPacket instance, Operation<ItemStack> original) { | ||
ItemStack itemStack = original.call(instance); | ||
LoreComponent loreComponent = itemStack.get(DataComponentTypes.LORE); | ||
if (loreComponent == null) { | ||
return itemStack; | ||
} | ||
String expectedMetadata = PublicEnderChestKt.formatInfoTextMetadata(instance.getSyncId(), instance.getRevision()); | ||
List<Text> originalLines = new ArrayList<>(loreComponent.lines()); | ||
List<Text> originalStyledLines = new ArrayList<>(loreComponent.styledLines()); | ||
ItemStack actualItemStack = itemStack.copy(); | ||
Predicate<Text> lineMetadataFilter = (line) -> { | ||
boolean containsMetadata = line.getString().contains(expectedMetadata); | ||
if (containsMetadata) { | ||
Text customInfoLine = Text.literal(line.getString().replace(expectedMetadata, "")).formatted(Formatting.ITALIC, Formatting.DARK_PURPLE); | ||
List<Text> currentCustomInfoLines = ((ItemStackDuck) (Object) actualItemStack).publicenderchest$getCustomInfoLines(); | ||
if (!currentCustomInfoLines.contains(customInfoLine)) { | ||
((ItemStackDuck) (Object) actualItemStack).publicenderchest$addCustomInfoLine(customInfoLine); | ||
} | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
}; | ||
List<Text> actualLines = originalLines.stream().filter(lineMetadataFilter).toList(); | ||
List<Text> actualStyledLines = originalStyledLines.stream().filter(lineMetadataFilter).toList(); | ||
LoreComponent actualLoreComponent = new LoreComponent(actualLines, actualStyledLines); | ||
actualItemStack.set(DataComponentTypes.LORE, actualLoreComponent); | ||
return actualItemStack; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/client/java/xd/arkosammy/publicenderchest/mixin/client/HandledScreenMixin.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,25 @@ | ||
package xd.arkosammy.publicenderchest.mixin.client; | ||
|
||
import com.llamalad7.mixinextras.injector.ModifyReturnValue; | ||
import net.minecraft.client.gui.screen.ingame.HandledScreen; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.text.Text; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import xd.arkosammy.publicenderchest.util.ducks.ItemStackDuck; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Mixin(HandledScreen.class) | ||
public abstract class HandledScreenMixin { | ||
|
||
@ModifyReturnValue(method = "getTooltipFromItem", at = @At("RETURN")) | ||
private List<Text> addCustomInfoLines(List<Text> original, ItemStack stack) { | ||
List<Text> originalTooltipLines = new ArrayList<>(original); | ||
List<Text> customInfoLines = ((ItemStackDuck) (Object) stack).publicenderchest$getCustomInfoLines(); | ||
originalTooltipLines.addAll(customInfoLines); | ||
return originalTooltipLines; | ||
} | ||
|
||
} |
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
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
69 changes: 69 additions & 0 deletions
69
src/main/java/xd/arkosammy/publicenderchest/mixin/ServerPlayerEntitySyncHandlerMixin.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,69 @@ | ||
package xd.arkosammy.publicenderchest.mixin; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import net.minecraft.component.DataComponentTypes; | ||
import net.minecraft.component.type.LoreComponent; | ||
import net.minecraft.inventory.Inventory; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket; | ||
import net.minecraft.screen.ScreenHandler; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.text.Text; | ||
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 xd.arkosammy.publicenderchest.PublicEnderChestKt; | ||
import xd.arkosammy.publicenderchest.inventory.CustomGenericContainerScreenHandler; | ||
import xd.arkosammy.publicenderchest.inventory.PublicInventory; | ||
import xd.arkosammy.publicenderchest.inventory.PublicInventoryKt; | ||
import xd.arkosammy.publicenderchest.util.CustomMutableText; | ||
import xd.arkosammy.publicenderchest.util.ducks.ServerPlayerEntityDuck; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Mixin(targets = "net.minecraft.server.network.ServerPlayerEntity$1") | ||
public abstract class ServerPlayerEntitySyncHandlerMixin { | ||
|
||
@Shadow @Final private ServerPlayerEntity field_29182; | ||
|
||
@WrapOperation(method = "updateSlot", at = @At(value = "NEW", target = "net/minecraft/network/packet/s2c/play/ScreenHandlerSlotUpdateS2CPacket")) | ||
private ScreenHandlerSlotUpdateS2CPacket modifySentItemStack(int syncId, int revision, int slot, ItemStack itemStack, Operation<ScreenHandlerSlotUpdateS2CPacket> original, ScreenHandler screenHandler) { | ||
ServerPlayerEntity player = this.field_29182; | ||
if (!(screenHandler instanceof CustomGenericContainerScreenHandler customScreenHandler)) { | ||
return original.call(syncId, revision, slot, itemStack); | ||
} | ||
Inventory inventory = customScreenHandler.getInventory(); | ||
if (!(inventory instanceof PublicInventory publicInventory)) { | ||
return original.call(syncId, revision, slot, itemStack); | ||
} | ||
List<CustomMutableText> customInfoLines = PublicInventoryKt.getCustomInfoLines(publicInventory.getStack(slot)); | ||
if (customInfoLines.isEmpty()) { | ||
return original.call(syncId, revision, slot, itemStack); | ||
} | ||
if (((ServerPlayerEntityDuck) player).publicenderchest$hasMod()) { | ||
for (CustomMutableText line : customInfoLines) { | ||
line.getText().append(" " + PublicEnderChestKt.formatInfoTextMetadata(syncId, revision)); | ||
} | ||
} | ||
LoreComponent loreComponent = itemStack.get(DataComponentTypes.LORE); | ||
List<Text> sentLines = new ArrayList<>(); | ||
if (loreComponent != null) { | ||
sentLines.addAll(loreComponent.lines()); | ||
} | ||
sentLines.addAll(customInfoLines); | ||
LoreComponent sentLoreComponent; | ||
if (loreComponent != null) { | ||
sentLoreComponent = new LoreComponent(ImmutableList.copyOf(sentLines), ImmutableList.copyOf(loreComponent.styledLines())); | ||
} else { | ||
sentLoreComponent = new LoreComponent(ImmutableList.copyOf(sentLines)); | ||
} | ||
ItemStack sentItemStack = itemStack.copy(); | ||
sentItemStack.set(DataComponentTypes.LORE, sentLoreComponent); | ||
return original.call(syncId, revision, slot, sentItemStack); | ||
} | ||
|
||
} |
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
Oops, something went wrong.