Skip to content

Commit

Permalink
improved networking, content component & other stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Uraneptus committed Oct 15, 2024
1 parent cf6f9f5 commit 31009e5
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 71 deletions.
14 changes: 13 additions & 1 deletion src/main/java/vazkii/morphtool/AttachementRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public ItemStack assemble(CraftingInput input, HolderLookup.Provider provider) {
ItemStack copy = tool.copy();
String mod = MorphingHandler.getModFromStack(target);
ToolContentComponent contents = copy.get(Registries.TOOL_CONTENT);
List<ItemStack> contentStacks = new ArrayList<>(List.copyOf(copy.get(Registries.TOOL_CONTENT).contents()));
if (contents == null) {
return ItemStack.EMPTY;
}
/*
List<ItemStack> contentStacks = new ArrayList<>(List.copyOf(copy.get(Registries.TOOL_CONTENT).getItems()));
//This assures that only one item of a mod is in the tool
if (!contentStacks.isEmpty()) {
Expand All @@ -79,6 +83,14 @@ public ItemStack assemble(CraftingInput input, HolderLookup.Provider provider) {
copy.set(Registries.TOOL_CONTENT, new ToolContentComponent(contentStacks));
*/
ToolContentComponent.Mutable mutable = new ToolContentComponent.Mutable(contents);
if (!target.isEmpty()) {
mutable.tryInsert(target);
}
copy.set(Registries.TOOL_CONTENT, mutable.toImmutable());


return copy;
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/vazkii/morphtool/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.neoforged.neoforge.network.PacketDistributor;
import vazkii.morphtool.data_components.ToolContentComponent;
import vazkii.morphtool.network.MessageMorphTool;
import vazkii.morphtool.network.NetworkHandler;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void onTick(ClientTickEvent.Post event) {
if (newStack != mainHandItem && !ItemStack.isSameItemSameComponents(newStack, mainHandItem)) {
var inventory = player.getInventory();
inventory.setItem(ConfigHandler.invertHandShift.get() ? inventory.getContainerSize() - 1 : inventory.selected, newStack);
PacketDistributor.sendToServer(new MessageMorphTool(newStack, inventory.selected));
NetworkHandler.sendToServer(new MessageMorphTool(newStack, inventory.selected));
MorphTool.proxy.updateEquippedItem();
}
}
Expand Down Expand Up @@ -84,7 +85,7 @@ public void onMouseEvent(InputEvent.MouseScrollingEvent event) {
if (newStack != mainHandItem && !ItemStack.isSameItemSameComponents(newStack, mainHandItem)) {
var inventory = player.getInventory();
inventory.setItem(ConfigHandler.invertHandShift.get() ? inventory.getContainerSize() - 1 : inventory.selected, newStack);
PacketDistributor.sendToServer(new MessageMorphTool(newStack, inventory.selected));
NetworkHandler.sendToServer(new MessageMorphTool(newStack, inventory.selected));
MorphTool.proxy.updateEquippedItem();
}
}
Expand All @@ -93,7 +94,7 @@ public void onMouseEvent(InputEvent.MouseScrollingEvent event) {

public static List<String> getModsFromStacks(ToolContentComponent toolContents) {
List<String> mods = new ArrayList<>();
for (ItemStack stack : toolContents.contents()) {
for (ItemStack stack : toolContents.getItems()) {
mods.add(MorphingHandler.getModFromStack(stack));
}
return mods;
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/vazkii/morphtool/MorphTool.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package vazkii.morphtool;

import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.registration.PayloadRegistrar;
import vazkii.morphtool.network.MessageMorphTool;
import vazkii.morphtool.proxy.ClientProxy;
import vazkii.morphtool.network.NetworkHandler;
import vazkii.morphtool.proxy.CommonProxy;

@Mod(MorphTool.MOD_ID)
@EventBusSubscriber(modid = MorphTool.MOD_ID, bus = EventBusSubscriber.Bus.MOD)
public class MorphTool {
public static final String MOD_ID = "morphtool";
public static CommonProxy proxy;

public MorphTool(IEventBus bus, ModContainer modContainer) {
bus.addListener(NetworkHandler::registerPayloadHandler);

Registries.DATA_COMPONENTS.register(bus);
Registries.ITEMS.register(bus);
Registries.SERIALIZERS.register(bus);
Expand All @@ -30,13 +26,4 @@ public MorphTool(IEventBus bus, ModContainer modContainer) {
proxy.preInit();
}

@SubscribeEvent
public static void registerPayloadHandler(final RegisterPayloadHandlersEvent event) {
final PayloadRegistrar registrar = event.registrar("1");
registrar.playToServer(
MessageMorphTool.TYPE,
MessageMorphTool.STREAM_CODEC,
new MessageMorphTool.Handler()
);
}
}
13 changes: 10 additions & 3 deletions src/main/java/vazkii/morphtool/MorphToolItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,18 @@ public void appendHoverText(ItemStack stack, Item.TooltipContext tooltipContext,
*/

if (Screen.hasShiftDown()) {
for (ItemStack contentStack : contents.contents()) {
for (ItemStack contentStack : contents.getItems()) {
if (!contentStack.isEmpty()) {
String name = contentStack.getHoverName().getString();
Component name;
if (contentStack.has(Registries.OG_DISPLAY_NAME)) {
name = contentStack.get(Registries.OG_DISPLAY_NAME);
} else {
name = contentStack.getHoverName();
}


String mod = MorphingHandler.getModFromStack(contentStack);
tooltip.add(Component.literal(" " + mod + " : " + name));
tooltip.add(Component.literal(" " + mod + " : " + name.getString()));
}
}
/*
Expand Down
77 changes: 49 additions & 28 deletions src/main/java/vazkii/morphtool/MorphingHandler.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package vazkii.morphtool;

import net.minecraft.ChatFormatting;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
Expand All @@ -20,6 +20,7 @@
import net.neoforged.neoforge.event.entity.item.ItemTossEvent;
import net.neoforged.neoforge.event.entity.player.PlayerDestroyItemEvent;
import net.neoforged.neoforgespi.language.IModInfo;
import org.jetbrains.annotations.NotNull;
import vazkii.morphtool.data_components.ToolContentComponent;

import java.util.*;
Expand All @@ -46,14 +47,15 @@ public void onItemBroken(PlayerDestroyItemEvent event) {
removeItemFromTool(event.getEntity(), event.getOriginal(), true, (ItemStack morph) -> event.getEntity().setItemInHand(event.getHand(), morph));
}

//TODO next fix this
public static void removeItemFromTool(Entity e, ItemStack stack, boolean itemBroken, Consumer<ItemStack> consumer) {
if (stack != null && !stack.isEmpty() && isMorphTool(stack) && !stack.is(Registries.MORPH_TOOL.get())) {
ToolContentComponent contents = stack.get(Registries.TOOL_CONTENT);
//CompoundTag morphData = stack.getTag().getCompound(TAG_MORPH_TOOL_DATA).copy();

ItemStack morph = makeMorphedStack(stack, MINECRAFT, contents);
ItemStack morph = makeMorphedStack(stack, MINECRAFT);
String mod = getModFromStack(stack);
List<ItemStack> newStacks = new ArrayList<>(List.copyOf(contents.contents()));
List<ItemStack> newStacks = new ArrayList<>(List.copyOf(contents.getItems()));
newStacks.remove(getStackFromMod(contents, mod));
ToolContentComponent newContents = new ToolContentComponent(newStacks);

Expand Down Expand Up @@ -126,34 +128,22 @@ public static ItemStack getShiftStackForMod(ItemStack stack, String mod) {
return stack;
}

ToolContentComponent contentComponent = stack.get(Registries.TOOL_CONTENT);
return makeMorphedStack(stack, mod, contentComponent);
return makeMorphedStack(stack, mod);
}

public static ItemStack makeMorphedStack(ItemStack currentStack, String targetMod, ToolContentComponent contentComponent) {
public static ItemStack makeMorphedStack(ItemStack currentStack, String targetMod) {
String currentMod = getModFromStack(currentStack);
ToolContentComponent currentContent = currentStack.get(Registries.TOOL_CONTENT);
ToolContentComponent newStackComponent = new ToolContentComponent(List.of(currentStack));
if (currentContent == null) return ItemStack.EMPTY;

ToolContentComponent currentStackComponent = new ToolContentComponent(List.of(currentStack));

/*
CompoundTag currentCmp = new CompoundTag();
currentStack.save(currentCmp);
currentCmp = currentCmp.copy();
if (currentCmp.contains("tag")) {
currentCmp.getCompound("tag").remove(TAG_MORPH_TOOL_DATA);
}
*/

if (!currentMod.equalsIgnoreCase(MINECRAFT) && !currentMod.equalsIgnoreCase(MorphTool.MOD_ID)) {
contentComponent = currentStackComponent;
}
ToolContentComponent.Mutable mutable = getMutable(currentContent, newStackComponent, currentMod);

ItemStack stack;
if (targetMod.equals(MINECRAFT)) {
stack = new ItemStack(Registries.MORPH_TOOL.get());
} else {
stack = getStackFromMod(contentComponent, targetMod);
stack = getStackFromMod(currentContent, targetMod);

if (stack.isEmpty()) {
stack = new ItemStack(Registries.MORPH_TOOL.get());
Expand All @@ -164,10 +154,11 @@ public static ItemStack makeMorphedStack(ItemStack currentStack, String targetMo
if (!stack.hasTag()) {
stack.setTag(new CompoundTag());
}
*/

stack.set(Registries.TOOL_CONTENT, contentComponent);
mutable.remove(stack);

stack.set(Registries.TOOL_CONTENT, mutable.toImmutable());
stack.set(Registries.IS_MORPH_TOOL, true);

/*
Expand All @@ -183,7 +174,7 @@ public static ItemStack makeMorphedStack(ItemStack currentStack, String targetMo
CompoundTag displayName = new CompoundTag();
CompoundTag ogDisplayName = displayName;
displayName.putString("text", Component.Serializer.toJson(stack.getHoverName()));
if (stackCmp.contains(TAG_MORPH_TOOL_DISPLAY_NAME)) {
displayName = (CompoundTag) stackCmp.get(TAG_MORPH_TOOL_DISPLAY_NAME);
} else {
Expand All @@ -197,19 +188,49 @@ public static ItemStack makeMorphedStack(ItemStack currentStack, String targetMo
}
*/


Component hoverName = stack.getHoverName();

if (!stack.has(Registries.OG_DISPLAY_NAME)) {
stack.set(Registries.OG_DISPLAY_NAME, hoverName);
} else {
hoverName = stack.get(Registries.OG_DISPLAY_NAME);
}

Component stackName = Component.literal(stack.getDisplayName().getString()).setStyle(Style.EMPTY.applyFormats(ChatFormatting.GREEN));
Component stackName = Component.literal(hoverName.getString()).setStyle(Style.EMPTY.applyFormats(ChatFormatting.GREEN));
Component comp = Component.translatable("morphtool.sudo_name", stackName);
stack.set(DataComponents.ITEM_NAME, comp);
stack.set(DataComponents.CUSTOM_NAME, comp);

}

stack.setCount(1);
return stack;
}

private static ToolContentComponent.Mutable getMutable(ToolContentComponent currentContent, ToolContentComponent newStackComponent, String currentMod) {
ToolContentComponent.Mutable currentContentMutable = new ToolContentComponent.Mutable(currentContent);
ToolContentComponent.Mutable newStackComponentMutable = new ToolContentComponent.Mutable(newStackComponent);

/*
CompoundTag currentCmp = new CompoundTag();
currentStack.save(currentCmp);
currentCmp = currentCmp.copy();
if (currentCmp.contains("tag")) {
currentCmp.getCompound("tag").remove(TAG_MORPH_TOOL_DATA);
}
*/

if (!currentMod.equalsIgnoreCase(MINECRAFT) && !currentMod.equalsIgnoreCase(MorphTool.MOD_ID)) {
currentContentMutable.tryInsert(newStackComponent.getItems().getFirst());
}
return currentContentMutable;
}

public static ItemStack getStackFromMod(ToolContentComponent component, String mod) {
if (component != null && !component.isEmpty()) {
for (ItemStack contentStack : component.contents()) {
for (ItemStack contentStack : component.getItems()) {
if (BuiltInRegistries.ITEM.getKey(contentStack.getItem()).getNamespace().equals(mod)) {
return contentStack;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/vazkii/morphtool/Registries.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mojang.serialization.Codec;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.RecipeSerializer;
Expand All @@ -19,7 +21,8 @@ public final class Registries {
public static final DeferredRegister.DataComponents DATA_COMPONENTS = DeferredRegister.createDataComponents(MorphTool.MOD_ID);

public static final Supplier<DataComponentType<Boolean>> IS_MORPH_TOOL = DATA_COMPONENTS.registerComponentType("is_morph_tool", builder -> builder.persistent(Codec.BOOL).networkSynchronized(ByteBufCodecs.BOOL));
public static final Supplier<DataComponentType<ToolContentComponent>> TOOL_CONTENT = DATA_COMPONENTS.registerComponentType("tool_content", builder -> builder.persistent(ToolContentComponent.CODEC).networkSynchronized(ToolContentComponent.STREAM_CODEC));
public static final Supplier<DataComponentType<ToolContentComponent>> TOOL_CONTENT = DATA_COMPONENTS.registerComponentType("tool_content", builder -> builder.persistent(ToolContentComponent.CODEC).networkSynchronized(ToolContentComponent.STREAM_CODEC).cacheEncoding());
public static final Supplier<DataComponentType<Component>> OG_DISPLAY_NAME = DATA_COMPONENTS.register("og_display_name", () -> DataComponentType.<Component>builder().persistent(ComponentSerialization.FLAT_CODEC).networkSynchronized(ComponentSerialization.STREAM_CODEC).build());

public static final DeferredItem<Item> MORPH_TOOL = ITEMS.registerItem("tool", MorphToolItem::new, new Item.Properties().stacksTo(1));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,68 @@
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.item.ItemStack;

import java.util.ArrayList;
import java.util.List;

public record ToolContentComponent(List<ItemStack> contents) {
public class ToolContentComponent {
public static final ToolContentComponent EMPTY = new ToolContentComponent(List.of());
public static final Codec<ToolContentComponent> CODEC = ItemStack.CODEC.listOf().xmap(ToolContentComponent::new, component -> component.contents);
public static final Codec<ToolContentComponent> CODEC = ItemStack.CODEC.listOf().xmap(ToolContentComponent::new, component -> component.items);
public static final StreamCodec<RegistryFriendlyByteBuf, ToolContentComponent> STREAM_CODEC = ItemStack.STREAM_CODEC
.apply(ByteBufCodecs.list())
.map(ToolContentComponent::new, component -> component.contents);
.map(ToolContentComponent::new, component -> component.items);
final List<ItemStack> items;

public ToolContentComponent(List<ItemStack> contents) {
this.items = contents;
}

public boolean isEmpty() {
return this.contents.isEmpty();
return this.items.isEmpty();
}

public List<ItemStack> getItems() {
return this.items;
}

@Override
public boolean equals(Object object) {
if (this == object) {
return true;
} else {
return object instanceof ToolContentComponent component && ItemStack.listMatches(this.items, component.items);
}
}

@Override
public int hashCode() {
return ItemStack.hashStackList(this.items);
}

@Override
public String toString() {
return "ToolContents" + this.items;
}

public static class Mutable {
private final List<ItemStack> items;

public Mutable(ToolContentComponent component) {
this.items = new ArrayList<>(component.items);
}

public void tryInsert(ItemStack stack) {
if (!stack.isEmpty()) {
ItemStack itemstack1 = stack.copy();
this.items.add(itemstack1);
}
}

public void remove(ItemStack stack) {
this.items.remove(stack);
}

public ToolContentComponent toImmutable() {
return new ToolContentComponent(List.copyOf(this.items));
}
}
}
Loading

0 comments on commit 31009e5

Please sign in to comment.