Jump to content

LYXHAHA

Members
  • Posts

    8
  • Joined

  • Last visited

Posts posted by LYXHAHA

  1. public static void onPlayerCloned(PlayerEvent.Clone event) {
            if (!event.getOriginal().world.isRemote() && !event.getPlayer().world.isRemote() && event.getOriginal() != null && event.getPlayer() != null) {
                LazyOptional<IMindCapability> oldMindCap = event.getOriginal().getCapability(LibCapabilities.MIND_CAPABILITY);
                LazyOptional<IMindCapability> newMindCap = event.getPlayer().getCapability(LibCapabilities.MIND_CAPABILITY);
                if (oldMindCap.isPresent() && newMindCap.isPresent()) {
                    newMindCap.ifPresent(capability -> {
                        oldMindCap.ifPresent(capabilityOld -> {
                            event.getPlayer().sendMessage(new StringTextComponent("Before: " + capability.getMindStrength()), CommonProxy.IN_GAME_UUID);
                            capability.deserializeNBT(capabilityOld.serializeNBT());
                            event.getPlayer().sendMessage(new StringTextComponent("After: " + capability.getMindStrength()), CommonProxy.IN_GAME_UUID);
                        });
                      // This don't work, the value on the client side is still the initial value
                        SCPacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) event.getPlayer()), new MSGMindCapabilitySync(capability.getMindStrength()));
                      // It works,the value on the client side is correct, but I don't know why, I think it's just a coincidence.
    //                    SCPacketHandler.INSTANCE.sendToServer(new MSGMindCapabilitySync(capability.getMindStrength()));
                    });
                }
            }
        }

     

    This is my message code.

    public class MSGMindCapabilitySync {
        private final int mindStrength;
    
        public MSGMindCapabilitySync(int mindStrength) {
            this.mindStrength = mindStrength;
    
        }
    
        public static void encodingMSG(MSGMindCapabilitySync msg, PacketBuffer buffer) {
            buffer.writeInt(msg.mindStrength);
        }
    
        public static MSGMindCapabilitySync decodingMSG(PacketBuffer buffer) {
            return new MSGMindCapabilitySync(buffer.readInt());
        }
    
        public static void handle(MSGMindCapabilitySync msg, Supplier<NetworkEvent.Context> ctx) {
            ctx.get().enqueueWork(() -> {
                // Form client to server
                ServerPlayerEntity serverPlayer = ctx.get().getSender();
                if (serverPlayer != null) {
                    serverPlayer.getCapability(LibCapabilities.MIND_CAPABILITY).ifPresent((capability -> {
                        capability.setMindStrength(msg.mindStrength);
                    }));
                    SaintChapter.LOGGER.info("Form client to server. Done");
                }
                // From server to client
                DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
                    if (Minecraft.getInstance().player == null) return;
                    Minecraft.getInstance().player.getCapability(LibCapabilities.MIND_CAPABILITY).ifPresent((capability -> {
                        capability.setMindStrength(msg.mindStrength);
                    }));
                    SaintChapter.LOGGER.info("From server to client. Done");
                });
            });
            ctx.get().setPacketHandled(true);
        }
    }
  2. Every time you restart the game or re-enter the map, the capability is reset. I don’t know if this is a problem with my code or is it the way it is.

    public class CapabilityAttachHandler {
        public CapabilityAttachHandler() {}
    
        public static void capabilityAttachHandler(AttachCapabilitiesEvent<Entity> event) {
            Entity entity = event.getObject();
            if (entity instanceof PlayerEntity){
                if (!entity.getCapability(LibCapabilities.MIND_CAPABILITY).isPresent()) {
                    event.addCapability(SaintChapter.key("mind_strength"), new MindCapabilityProvider());
                    SaintChapter.LOGGER.info("Mind Capability attached");
                }
                if (!entity.getCapability(LibCapabilities.SOUL_CAPABILITY).isPresent()){
                    event.addCapability(SaintChapter.key("soul_strength"), new SoulCapabilityProvider());
                    SaintChapter.LOGGER.info("Player Soul Capability attached");
                }
            }
            if (entity instanceof MobEntity) {
                if (!entity.getCapability(LibCapabilities.SOUL_CAPABILITY).isPresent()){
                    event.addCapability(SaintChapter.key("soul_strength"), new SoulCapabilityProvider());
                    SaintChapter.LOGGER.info("Mob Soul Capability attached");
                }
            }
        }
    }
    public interface ISoulCapability extends INBTSerializable<CompoundNBT> {
        int getSoulStrength ();
        boolean getInitialized();
    }
    public class SoulCapability implements ISoulCapability {
        private int soulStrength;
        private boolean initialized;
    
        public SoulCapability(int soulStrength, boolean isInitialized) {
            this.soulStrength = soulStrength;
            this.initialized = isInitialized;
        }
    
        @Override
        public boolean getInitialized() {
            return this.initialized;
        }
    
        @Override
        public int getSoulStrength() {
            return soulStrength;
        }
    
    
        @Override
        public CompoundNBT serializeNBT() {
            CompoundNBT compoundNBT = new CompoundNBT();
            compoundNBT.putInt("soulStrength", this.soulStrength);
            compoundNBT.putBoolean("initialized", this.initialized);
            return compoundNBT;
        }
    
        @Override
        public void deserializeNBT(CompoundNBT nbt) {
            this.soulStrength = nbt.getInt("soulStrength");
            this.initialized = nbt.getBoolean("initialized");
        }
    }
    public class SoulCapabilityProvider implements ICapabilityProvider, INBTSerializable<CompoundNBT> {
        private ISoulCapability soulCapability;
    
        @Nonnull
        @Override
        public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
            return cap == LibCapabilities.SOUL_CAPABILITY ? LazyOptional.of(this::getOrCreateCapability).cast() : LazyOptional.empty();
        }
    
        @Nonnull
        private ISoulCapability getOrCreateCapability() {
            if (soulCapability == null) {
                this.soulCapability = new SoulCapability(888, false);
            }
            return this.soulCapability;
        }
    
        @Override
        public CompoundNBT serializeNBT() {
            return getOrCreateCapability().serializeNBT();
        }
    
        @Override
        public void deserializeNBT(CompoundNBT nbt) {
            getOrCreateCapability().deserializeNBT(nbt);
        }
    }

     

  3. public class SCRecipeProvider extends RecipeProvider {
        public SCRecipeProvider(DataGenerator generator) {
            super(generator);
        }
    
        @Override
        protected void registerRecipes(@Nonnull Consumer<IFinishedRecipe> registrar) {
            VanillaTypeRecipeProvider.registerCookingRecipes(registrar);
            VanillaTypeRecipeProvider.registerCustomRecipes(registrar);
            registerShapedRecipes(registrar);
            VanillaTypeRecipeProvider.registerStoneCutterRecipes(registrar);
        }
    
        public static void registerShapedRecipes(Consumer<IFinishedRecipe> registrar) {
    
            ShapedRecipeBuilder.shapedRecipe(Objects.requireNonNull(ForgeRegistries.ITEMS.getValue(new ResourceLocation(SaintChapter.MODID, "book_of_truth"))))
                    .patternLine("MMM")
                    .patternLine("MBM")
                    .patternLine("MMM")
                    .key('M', RegistryItems.MIND_CRYSTAL.get())
                    .key('B', Items.BOOK)
                    .addCriterion("has_item", hasItem(RegistryItems.MIND_CRYSTAL.get()))
                    .build(registrar);
        }
    }

    I think it’s because Patchouli’s book was not registered during the data generation stage. But i don't know how to solve this problem.

    ForgeRegistries.ITEMS.getValue(new ResourceLocation(SaintChapter.MODID, "book_of_truth")

    This should return nothing, cause Patchouli’s book is registered to my mod's namespace during it is loaded, Patchouli API may not be loaded during the data generation stage.

    Sorry, I'm not good at English. I hope my statement is correct.

  4. I'm a Chinese modder, and I use language provider to provide the Chinese language file. This is the problem I encountered. The language file provided by LanguageProvider does not display Chinese correctly.

    there is my code

    public class SCLanguageProvider_en_us extends net.minecraftforge.common.data.LanguageProvider {
        public SCLanguageProvider_en_us(DataGenerator gen, String modid, String locale) {
            super(gen, modid, locale);
        }
    
        @Override
        protected void addTranslations() {
            add(RegistryItems.REVELATION_SCROLL.get(), "Try English: This is en_us");
            add(RegistryItems.MIND_CRYSTAL.get(), "Try Chinese: 这是中文");
        }
    }

    and there is the output

    {
      "item.saintchapter.mind_crystal": "Try Chinese: \u6769\u6B10\u69F8\u6D93\uE15F\u6783",
      "item.saintchapter.revelation_scroll": "Try English: This is en_us"
    }

    You can see the Chinese part is output as "\u6769\u6B10\u69F8\u6D93\uE15F\u6783"

    😭I am not good at English, I hope my expression is correct.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.