Jump to content

Zemelua

Members
  • Posts

    190
  • Joined

  • Last visited

Everything posted by Zemelua

  1. @Override public void performEffect(LivingEntity entityLivingBaseIn, int amplifier) { if (entityLivingBaseIn instanceof PlayerEntity) { System.out.print("test"); LazyOptional<PlayerMentalInterface> cap = ((PlayerEntity)entityLivingBaseIn).getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); cap.ifPresent(f -> { int _currentMental = f.getMental() + 3; f.setMental(_currentMental); DepressionModPacketHandler.sendToClient(new PlayerMentalCapabilityMessage(_currentMental), (PlayerEntity) entityLivingBaseIn); }); } } I've improved the code, but it crashed when I drank a potion. I don't know the cause, so can you tell me? The crash report is below. ---- Minecraft Crash Report ---- // Shall we play a game? Time: 20/10/26 3:40 Description: Ticking player java.lang.ClassCastException: net.minecraftforge.common.util.LazyOptional cannot be cast to depression_mod.capability.mentalpoint.PlayerMentalInterface at depression_mod.network.PlayerMentalCapabilityMessage.<init>(PlayerMentalCapabilityMessage.java:19) ~[?:?] {re:classloading} at depression_mod.potion.ModEffect.lambda$0(ModEffect.java:30) ~[?:?] {re:classloading} at net.minecraftforge.common.util.LazyOptional.ifPresent(LazyOptional.java:161) ~[?:?] {re:classloading} at depression_mod.potion.ModEffect.performEffect(ModEffect.java:27) ~[?:?] {re:classloading} at depression_mod.potion.ModEffect.affectEntity(ModEffect.java:37) ~[?:?] {re:classloading} at net.minecraft.item.PotionItem.onItemUseFinish(PotionItem.java:47) ~[?:?] {re:classloading} at net.minecraft.item.ItemStack.onItemUseFinish(ItemStack.java:213) ~[?:?] {re:classloading} at net.minecraft.entity.LivingEntity.onItemUseFinish(LivingEntity.java:2876) ~[?:?] {re:classloading} at net.minecraft.entity.player.ServerPlayerEntity.onItemUseFinish(ServerPlayerEntity.java:1027) ~[?:?] {re:classloading,pl:accesstransformer:B} at net.minecraft.entity.LivingEntity.updateActiveHand(LivingEntity.java:2754) ~[?:?] {re:classloading} at net.minecraft.entity.LivingEntity.tick(LivingEntity.java:2185) ~[?:?] {re:classloading} at net.minecraft.entity.player.PlayerEntity.tick(PlayerEntity.java:237) ~[?:?] {re:classloading,pl:accesstransformer:B} at net.minecraft.entity.player.ServerPlayerEntity.playerTick(ServerPlayerEntity.java:379) ~[?:?] {re:classloading,pl:accesstransformer:B} at net.minecraft.network.play.ServerPlayNetHandler.tick(ServerPlayNetHandler.java:188) ~[?:?] {re:classloading} at net.minecraft.network.NetworkManager.tick(NetworkManager.java:250) ~[?:?] {re:classloading} at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:152) ~[?:?] {re:classloading} at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:903) ~[?:?] {re:classloading,pl:accesstransformer:B} at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:821) ~[?:?] {re:classloading,pl:accesstransformer:B} at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120) ~[?:?] {re:classloading,pl:runtimedistcleaner:A} at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:662) [?:?] {re:classloading,pl:accesstransformer:B} at java.lang.Thread.run(Thread.java:748) [?:1.8.0_261] {} A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Server thread Stacktrace: at depression_mod.network.PlayerMentalCapabilityMessage.<init>(PlayerMentalCapabilityMessage.java:19) at depression_mod.potion.ModEffect.lambda$0(ModEffect.java:30) at net.minecraftforge.common.util.LazyOptional.ifPresent(LazyOptional.java:161) at depression_mod.potion.ModEffect.performEffect(ModEffect.java:27) at depression_mod.potion.ModEffect.affectEntity(ModEffect.java:37) at net.minecraft.item.PotionItem.onItemUseFinish(PotionItem.java:47) at net.minecraft.item.ItemStack.onItemUseFinish(ItemStack.java:213) at net.minecraft.entity.LivingEntity.onItemUseFinish(LivingEntity.java:2876) at net.minecraft.entity.player.ServerPlayerEntity.onItemUseFinish(ServerPlayerEntity.java:1027) at net.minecraft.entity.LivingEntity.updateActiveHand(LivingEntity.java:2754) at net.minecraft.entity.LivingEntity.tick(LivingEntity.java:2185) at net.minecraft.entity.player.PlayerEntity.tick(PlayerEntity.java:237) -- Player being ticked -- Details: Entity Type: minecraft:player (net.minecraft.entity.player.ServerPlayerEntity) Entity ID: 135 Entity Name: Dev Entity's Exact location: -140.31, 70.20, -6.46 Entity's Block location: World: (-141,70,-7), Chunk: (at 3,4,9 in -9,-1; contains blocks -144,0,-16 to -129,255,-1), Region: (-1,-1; contains chunks -32,-32 to -1,-1, blocks -512,0,-512 to -1,255,-1) Entity's Momentum: 0.00, 0.01, 0.00 Entity's Passengers: [] Entity's Vehicle: ~~ERROR~~ NullPointerException: null Stacktrace: at net.minecraft.entity.player.ServerPlayerEntity.playerTick(ServerPlayerEntity.java:379) at net.minecraft.network.play.ServerPlayNetHandler.tick(ServerPlayNetHandler.java:188) at net.minecraft.network.NetworkManager.tick(NetworkManager.java:250) -- Ticking connection -- Details: Connection: net.minecraft.network.NetworkManager@3bcb3751 Stacktrace: at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:152) at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:903) at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:821) at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:662) at java.lang.Thread.run(Thread.java:748) -- System Details -- Details: Minecraft Version: 1.15.2 Minecraft Version ID: 1.15.2 Operating System: Windows 10 (amd64) version 10.0 Java Version: 1.8.0_261, Oracle Corporation Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation Memory: 532880904 bytes (508 MB) / 1263534080 bytes (1205 MB) up to 1873805312 bytes (1787 MB) CPUs: 12 JVM Flags: 1 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump ModLauncher: 5.1.2+70+master.2845bb9 ModLauncher launch target: fmluserdevclient ModLauncher naming: mcp ModLauncher services: /eventbus-2.2.0-service.jar eventbus PLUGINSERVICE /forge-1.15.2-31.2.41_mapped_snapshot_20200514-1.15.1-launcher.jar object_holder_definalize PLUGINSERVICE /forge-1.15.2-31.2.41_mapped_snapshot_20200514-1.15.1-launcher.jar runtime_enum_extender PLUGINSERVICE /accesstransformers-2.1.3-shadowed.jar accesstransformer PLUGINSERVICE /forge-1.15.2-31.2.41_mapped_snapshot_20200514-1.15.1-launcher.jar capability_inject_definalize PLUGINSERVICE /forge-1.15.2-31.2.41_mapped_snapshot_20200514-1.15.1-launcher.jar runtimedistcleaner PLUGINSERVICE /forge-1.15.2-31.2.41_mapped_snapshot_20200514-1.15.1-launcher.jar fml TRANSFORMATIONSERVICE FML: 31.2 Forge: net.minecraftforge:31.2.41 FML Language Providers: [email protected] minecraft@1 Mod List: client-extra.jar Minecraft {[email protected] DONE} forge-1.15.2-31.2.41_mapped_snapshot_20200514-1.15.1-recomp.jar Forge {[email protected] DONE} main Depression Mod {depression_mod@NONE DONE} Player Count: 1 / 8; [ServerPlayerEntity['Dev'/135, l='New World', x=-140.31, y=70.20, z=-6.46]] Data Packs: vanilla, mod:forge (incompatible), mod:depression_mod Type: Integrated Server (map_client.txt) Is Modded: Definitely; Client brand changed to 'forge'
  2. I know how to send a packet, but I don't know how to do it "when the data is updated".
  3. Can you tell me how to sync to the client every time the data is updated? I looked it up, but I don't know how.
  4. I'm sorry. I forgot to answer. The data is displayed as a HUD in the client / gui / MentalOverlayHandler. (This has already been successful.) The current ultimate goal is to increase the value of capacity by drinking potions, and reflect that in the HUD. The code doesn't reflect the potion and effect code yet, but it's here. https://github.com/Zemelua/Depression-Mod
  5. I'm sorry. Since I am learning java in parallel with modding (such as learning java code that got stuck in modding at that time), I am aware that there is a part that is not learned enough. This time, I asked a question because I misunderstood that the cause was not in java. I'm sorry. @Override public void performEffect(LivingEntity entityLivingBaseIn, int amplifier) { if (entityLivingBaseIn instanceof PlayerEntity) { System.out.print("test"); LazyOptional<PlayerMentalInterface> cap = ((PlayerEntity)entityLivingBaseIn).getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); cap.ifPresent(f -> f.setMental(f.getMental() + 3) ); Well, I've fixed the code and I think this is the correct java code, but it doesn't work. Is this also a java issue?
  6. public class ModEffect extends InstantEffect { public ModEffect(EffectType typeIn, int liquidColorIn) { super(typeIn, liquidColorIn); } @Override public void performEffect(LivingEntity entityLivingBaseIn, int amplifier) { if (entityLivingBaseIn instanceof PlayerEntity) { LazyOptional<PlayerMentalInterface> cap = ((PlayerEntity)entityLivingBaseIn).getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); int currentMental = cap.ifPresent(f -> f.getMental()); //<-here int _currentMental = currentMental + 3; //if (_currentMental > 20) { // _currentMental = 20; //} cap.ifPresent(f -> f.setMental(_currentMental)); } } @Override public void affectEntity(@Nullable Entity source, @Nullable Entity indirectSource, LivingEntity entityLivingBaseIn, int amplifier, double health) { this.performEffect(entityLivingBaseIn, amplifier); } } I've improved the code, but I don't know how to use ifPresent with getMental (). The code can be found at https://github.com/Zemelua/Depression-Mod . I haven't updated it yet, but I have a complete implementation of capability.
  7. public class ModEffect extends InstantEffect { protected ModEffect(EffectType typeIn, int liquidColorIn) { super(typeIn, liquidColorIn); } @Override public void affectEntity(@Nullable Entity source, @Nullable Entity indirectSource, LivingEntity entityLivingBaseIn, int amplifier, double health) { if (this == DepressionModRegistry.INSTANT_ANTIDEPRESSANT.get()) { this.performEffect(entityLivingBaseIn, amplifier); } } @Override public void performEffect(LivingEntity entityLivingBaseIn, int amplifier) { if (this == DepressionModRegistry.INSTANT_ANTIDEPRESSANT.get()) { LazyOptional<PlayerMentalInterface> cap = ((PlayerEntity)entityLivingBaseIn).getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); int currentMental = 0; try { currentMental = cap.orElseThrow(IllegalAccessException::new).getMental(); } catch (IllegalAccessException e) { e.printStackTrace(); } int _currentMental = currentMental + 3; if (_currentMental > 20) { _currentMental = 20; } try { cap.orElseThrow(IllegalAccessException::new).setMental(_currentMental); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } I extended InstantEffect and override affectEntity, but it's still not working.
  8. public class ModEffect extends Effect { protected ModEffect(EffectType typeIn, int liquidColorIn) { super(typeIn, liquidColorIn); } @Override public void performEffect(LivingEntity entityLivingBaseIn, int amplifier) { if (this == DepressionModRegistry.INSTANT_ANTIDEPRESSANT.get()) { LazyOptional<PlayerMentalInterface> cap = ((PlayerEntity)entityLivingBaseIn).getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); int currentMental = 0; try { currentMental = cap.orElseThrow(IllegalAccessException::new).getMental(); } catch (IllegalAccessException e) { e.printStackTrace(); } int _currentMental = currentMental + 3; if (_currentMental > 20) { _currentMental = 20; } try { cap.orElseThrow(IllegalAccessException::new).setMental(_currentMental); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } public class DepressionModRegistry { //EffectRegistry public static final DeferredRegister<Effect> EFFECTS = DeferredRegister.create(ForgeRegistries.POTIONS, DepressionMod.MOD_ID); public static final RegistryObject<Effect> INSTANT_ANTIDEPRESSANT = EFFECTS.register("instant_antidepressant", () -> new InstantEffect(EffectType.BENEFICIAL, 123456)); //PotionRegistry public static final DeferredRegister<Potion> POTIONS = DeferredRegister.create(ForgeRegistries.POTION_TYPES, DepressionMod.MOD_ID); public static final RegistryObject<Potion> BRON = POTIONS.register("bron", () -> new Potion(new EffectInstance(Effects.LEVITATION, 120), new EffectInstance(Effects.JUMP_BOOST, 3600, 2), new EffectInstance(Effects.SLOW_FALLING, 3600), new EffectInstance(INSTANT_ANTIDEPRESSANT.get(), 1))); //RecipeRegistry public static void addRecipes() { BrewingRecipeRegistry.addRecipe(new PotionRecipe(Lazy.concurrentOf(() -> Potions.AWKWARD), Ingredient.fromItems(Items.SUNFLOWER), Lazy.concurrentOf(() -> BRON.get()))); } } In the code above, I've succeeded in implementing potions and effects, but I can't get the effect's effect to work. Specifically, it increases the capacity value by a certain amount, similar to instant health. It has also been confirmed that the capabilities are properly implemented. It just seems to me that performEffect isn't working.
  9. Since I am using a translator, my English may be unnatural. I'm trying a certain block now. It uses onBlockActivated to fix the pose and coordinates of the player who right-clicked on the block. I thought the vanilla bed code might be helpful, but I couldn't find it. It would be helpful if someone could tell me.
  10. Thank you. That way I was able to solve it.
  11. Since I am using a translator, my English may be unnatural. sorry. This may be a rudimentary and ridiculous mistake, but the texture and model of the block will appear as black and purple blocks. Also, the name of the block item is displayed as Air. public class TestBlock extends Block{ public TestBlock() { super(Properties.create(Material.WEB, MaterialColor.WOOL).hardnessAndResistance(2.0F, 3.0F).sound(SoundType.CLOTH)); setRegistryName("test_block"); } } public class TestBlockItem extends BlockItem{ public TestBlockItem() { super(new TestBlock(), new Item.Properties().group(DepressionMod.DEPRESSIONMOD_TAB)); this.setRegistryName(new TestBlock().getRegistryName()); } } @ObjectHolder(DepressionMod.MOD_ID) public class DepressionModRegistry { @Mod.EventBusSubscriber(modid = DepressionMod.MOD_ID, bus = Bus.MOD) public static class Register { //Blocks public static final Block TEST_BLOCK = new TestBlock(); //BlockItems public static final BlockItem _TEST_BLOCK = new TestBlockItem(); //TileEntities //public static final TileEntityType<RedstoneCapacitorTileEntity> CHARGED_CAPACITY_TILEENTITY = TileEntityType.Builder.create(RedstoneCapacitorTileEntity::new, REDSTONE_CAPACITOR_BLOCK).build(null); //ItemRegistry /* @SubscribeEvent public static void registerItems(RegistryEvent.Register<Item> event) { final Item[] items = { new SilverIngotItem() }; event.getRegistry().registerAll(items); } */ //BlockRegistry @SubscribeEvent public static void registarBlocks(RegistryEvent.Register<Block> event) { final Block[] blocks = { TEST_BLOCK }; event.getRegistry().registerAll(blocks); } //BlockItemRegistry @SubscribeEvent public static void registarBlockItems(RegistryEvent.Register<Item> event) { final BlockItem[] blockitems = { _TEST_BLOCK }; event.getRegistry().registerAll(blockitems); } } }
  12. Thanks to everyone, I was able to achieve what I wanted to do. Thank you very much. I'm not good at English, so it's often difficult to understand even after reading the tutorial, but actually speaking in text here makes it somewhat easier to understand. public class DepressionModPacketHandler { private static final String PROTOCOL_VERSION = "1"; //プロトコルのバージョンを宣言 public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( //INSTANCEに=の右側以下をぶち込む new ResourceLocation(DepressionMod.MOD_ID, "main"), () -> PROTOCOL_VERSION, //プロトコルのバージョンをぶち込む PROTOCOL_VERSION::equals, //クライアントのバージョンとPROTOCOL_VERSIONを比較? PROTOCOL_VERSION::equals); //サーバーのバージョンとPROTOCOL_VERSIONを比較? //パケットの登録 public static void packetRegister() { INSTANCE.registerMessage( 0, //パケットの識別子 PlayerMentalCapabilityMessage.class, //実際のパケットクラス PlayerMentalCapabilityMessage::encode, //エンコード PlayerMentalCapabilityMessage::decode, //デコード PlayerMentalCapabilityMessage::handle); //メッセージ自体の処理 } public static void sendToClient(Object message, PlayerEntity player) { INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), message); } } public class PlayerMentalCapabilityMessage { ClientPlayerEntity player = Minecraft.getInstance().player; //getInstance().playerをplayerにぶち込む LazyOptional<PlayerMentalInterface> playerMentalInterface = player.getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); //playerからキャパビリティを取得してplayerMentalInterfaceにぶち込む int currentMental = ((PlayerMentalInterface) playerMentalInterface).getMental(); //playerMentalInterfaceからmentalの値を取得してcurrentMentalにぶち込む public PlayerMentalCapabilityMessage(int currentMental) { this.currentMental = currentMental; } public static void encode(PlayerMentalCapabilityMessage message, PacketBuffer buf) { buf.writeInt(message.currentMental); } public static PlayerMentalCapabilityMessage decode(PacketBuffer buf) { int currentMental = buf.readInt(); return new PlayerMentalCapabilityMessage(currentMental); } public static void handle(PlayerMentalCapabilityMessage message, Supplier<NetworkEvent.Context> ctx) { ctx.get().enqueueWork(() -> { DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> Handle.handleClient(message.currentMental)); }); ctx.get().setPacketHandled(true); } public static class Handle { public static DistExecutor.SafeRunnable handleClient(int currentMental) { return new DistExecutor.SafeRunnable() { @Override public void run() { LazyOptional<PlayerMentalInterface> cap = Minecraft.getInstance().player.getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); cap.orElseThrow(IllegalArgumentException::new).setMental(currentMental); } }; } } } public class MentalOverlayHander { public static final ResourceLocation MENTALICON = new ResourceLocation("depressionmod:textures/gui/overlay.png"); private final Minecraft mc = Minecraft.getInstance(); @SubscribeEvent public void onPreRenderOverlay(RenderGameOverlayEvent.Post event) { if (event.getType() == ElementType.ALL && mc.playerController.gameIsSurvivalOrAdventure()) { int scaledWidth = this.mc.getMainWindow().getScaledWidth(); int scaledHeight = this.mc.getMainWindow().getScaledHeight(); int left_height = ForgeIngameGui.left_height; int left = scaledWidth / 2 - 91; int top = scaledHeight - left_height; LazyOptional<PlayerMentalInterface> cap = Minecraft.getInstance().player.getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); int level =cap.orElseThrow(IllegalArgumentException::new).getMental(); //int level = currentMental; //((PlayerMentalInterface) PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY).getMental(); RenderSystem.enableBlend(); RenderSystem.enableAlphaTest(); RenderSystem.color4f(1F, 1F, 1F, 1F); mc.getTextureManager().bindTexture(MENTALICON); for (int i = 0; i < 10; ++i) { //iが10未満のとき繰り返す。iは繰り返しごとに1増える。 int idx = i * 2 + 1; if (idx < level) { //iがMPより小さいとき普通の薬を描画。 //mc.getTextureManager().bindTexture(MENTALICON); mc.ingameGUI.blit(left, top, 0, 0, 9, 9); } else if (idx == level) { //iがMPと同じとき半分の薬を描画。 //mc.getTextureManager().bindTexture(MENTALICON); mc.ingameGUI.blit(left, top, 9, 0, 9, 9); } else if (idx > level) { //iがMPより大きいとき黒の薬を描画。 //mc.getTextureManager().bindTexture(MENTALICON); mc.ingameGUI.blit(left, top, 18, 0, 9, 9); } left += 8; //アイコンを描画する位置を右に一つ分ずらす } RenderSystem.disableBlend(); mc.getTextureManager().bindTexture(AbstractGui.GUI_ICONS_LOCATION); } } }
  13. public static void handle(PlayerMentalCapabilityMessage message, Supplier<NetworkEvent.Context> ctx) { ctx.get().enqueueWork(() -> { DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> Handle.handleClient(message.currentMental)); }); ctx.get().setPacketHandled(true); } public static class Handle { public static DistExecutor.SafeRunnable handleClient(int currentMental) { return new DistExecutor.SafeRunnable() { @Override public void run() { LazyOptional<PlayerMentalInterface> cap = Minecraft.getInstance().player.getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); cap.orElseThrow(IllegalArgumentException::new).setMental(currentMental); } }; } } } Is this right?
  14. public class PlayerMentalCapabilityMessage { ClientPlayerEntity player = Minecraft.getInstance().player; //getInstance().playerをplayerにぶち込む LazyOptional<PlayerMentalInterface> playerMentalInterface = player.getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); //playerからキャパビリティを取得してplayerMentalInterfaceにぶち込む int currentMental = ((PlayerMentalInterface) playerMentalInterface).getMental(); //playerMentalInterfaceからmentalの値を取得してcurrentMentalにぶち込む public PlayerMentalCapabilityMessage(int currentMental) { this.currentMental = currentMental; } public static void encode(PlayerMentalCapabilityMessage message, PacketBuffer buf) { buf.writeInt(message.currentMental); } public static PlayerMentalCapabilityMessage decode(PacketBuffer buf) { int currentMental = buf.readInt(); return new PlayerMentalCapabilityMessage(currentMental); } public static void handle(PlayerMentalCapabilityMessage message, Supplier<NetworkEvent.Context> ctx) { ctx.get().enqueueWork(() -> { DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> Handle.handleClient(message.currentMental)); }); ctx.get().setPacketHandled(true); } public static class Handle { public static DistExecutor.SafeRunnable handleClient(int currentMental) { return new DistExecutor.SafeRunnable() { @Override public void run() { LazyOptional<PlayerMentalInterface> cap = Minecraft.getInstance().player.getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); ((PlayerMentalInterface) cap).setMental(currentMental); //MinecraftForge.EVENT_BUS.post(new DietModEvents.UpdatePlayerSizeEvent((PlayerEntity) player)); /*Entity player = Minecraft.getInstance().world.getEntityByID(playerEntityID); if (!(player instanceof PlayerEntity)) return; player.getCapability(ScaleProvider.SCALE_CAP).orElseThrow(IllegalArgumentException::new).setScale(scale); MinecraftForge.EVENT_BUS.post(new DietModEvents.UpdatePlayerSizeEvent((PlayerEntity) player)); */ } }; } } } I wrote the code with reference to https://forums.minecraftforge.net/topic/89515-solved1161client-works-but-server-crashes-at-packet-handler, but what role does safeRunWhenOn have? I don't really understand it, so please let me know.
  15. I'm sorry, I still don't have enough knowledge and understanding about Dist Executor. I tried searching, but I created a clientproxy class and what happens when the data is changed there (in this case, MyCap cap = Minecraft.getInstance (). player.getCapability (....); cap.setMental (valuefromserver); Is it correct to recognize that it executes this code? nothing. I may have found the appropriate sample code. With that as a reference, I will think for a moment.
  16. public static void handle(PlayerMentalCapabilityMessage msg, Supplier<NetworkEvent.Context> ctx) { ctx.get().enqueueWork(() -> { }); ctx.get().setPacketHandled(true); } Is this all right? public static void handle(MyMessage msg, Supplier<NetworkEvent.Context> ctx) { ctx.get().enqueueWork(() -> { // Work that needs to be threadsafe (most work) EntityPlayerMP sender = ctx.get().getSender(); // the client that sent this packet // do stuff }); ctx.get().setPacketHandled(true); } This is what the forge docs say, but if I'm right, EntityPlayerMP sender = ctx.get (). GetSender (); is for sending from client to server. I don't know how to send from server to client instead. public class MentalOverlayHander { public static final ResourceLocation MENTALICON = new ResourceLocation("depressionmod:textures/gui/overlay.png"); private final Minecraft mc = Minecraft.getInstance(); public int _currentMental; @SubscribeEvent public void onPreRenderOverlay(RenderGameOverlayEvent.Post event) { if (event.getType() == ElementType.ALL && mc.playerController.gameIsSurvivalOrAdventure()) { int scaledWidth = this.mc.getMainWindow().getScaledWidth(); int scaledHeight = this.mc.getMainWindow().getScaledHeight(); int left_height = ForgeIngameGui.left_height; int left = scaledWidth / 2 - 91; int top = scaledHeight - left_height; int level = _currentMental; RenderSystem.enableBlend(); RenderSystem.enableAlphaTest(); RenderSystem.color4f(1F, 1F, 1F, 1F); mc.getTextureManager().bindTexture(MENTALICON); for (int i = 0; i < 10; ++i) { //iが10未満のとき繰り返す。iは繰り返しごとに1増える。 int idx = i * 2 + 1; if (idx < level) { //iがMPより小さいとき普通の薬を描画。 //mc.getTextureManager().bindTexture(MENTALICON); mc.ingameGUI.blit(left, top, 0, 0, 9, 9); } else if (idx == level) { //iがMPと同じとき半分の薬を描画。 //mc.getTextureManager().bindTexture(MENTALICON); mc.ingameGUI.blit(left, top, 9, 0, 9, 9); } else if (idx > level) { //iがMPより大きいとき黒の薬を描画。 //mc.getTextureManager().bindTexture(MENTALICON); mc.ingameGUI.blit(left, top, 18, 0, 9, 9); } left += 8; //アイコンを描画する位置を右に一つ分ずらす } RenderSystem.disableBlend(); mc.getTextureManager().bindTexture(AbstractGui.GUI_ICONS_LOCATION); } } } I want to put the currentMental value of PlayerMentalCapabilityMessage in _currentMental of MentalOverlayHander.
  17. public class PlayerMentalCapabilityMessage { ClientPlayerEntity player = Minecraft.getInstance().player; LazyOptional<PlayerMentalInterface> playermentalinterface = player.getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); int currentMental = ((PlayerMentalInterface) playermentalinterface).getMental(); public PlayerMentalCapabilityMessage(int currentMental) { this.currentMental = currentMental; } public static void encode(PlayerMentalCapabilityMessage message, PacketBuffer buf) { buf.writeInt(message.currentMental); } public static PlayerMentalCapabilityMessage decode(PacketBuffer buf) { int currentMental = buf.readInt(); return new PlayerMentalCapabilityMessage(currentMental); } public static void handle(PlayerMentalCapabilityMessage msg, Supplier<NetworkEvent.Context> ctx) { ctx.get().enqueueWork(() -> { }); } } Is it like this? I'm still not sure about Context.setPacketHandled () so please tell me more.
  18. public class PlayerMentalCapabilityMessage { private final int currentMental; public PlayerMentalCapabilityMessage(int currentMental) { this.currentMental = currentMental; } public static void encode(PlayerMentalCapabilityMessage message, PacketBuffer buf) { buf.writeInt(message.currentMental); } public static PlayerMentalCapabilityMessage decode(PacketBuffer buf) { int currentMental = buf.readInt(); return new PlayerMentalCapabilityMessage(currentMental); } public static void handle(PlayerMentalCapabilityMessage msg, Supplier<NetworkEvent.Context> ctx) { //Player player = Minecraft.pla ctx.get().enqueueWork(() -> { PlayerMental playerMental = (PlayerMental) PlayerEntity.getCapability(PlayerMentalCapabilityRegistry.PLAYER_MENTAL_CAPABILITY); }); } } I wrote the code with reference to the existing mod, but I still don't know what to do with the contents of the handle. I'm sorry to have taken the trouble, but if possible, I would like a sample code and its explanation.
  19. I'm sorry, nothing.
  20. INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), new PlayerMentalCapabilityMessage()); When I searched for the code of the existing mod, I found the above sentence. Even if I change this ServerPlayerEntity to PlayerEntity, an error is displayed.
  21. Where is the original code for the player object?
  22. In other words, I understand that with () should contain mental, which is the value I want to reflect in the HUD, but I don't know how to get the value of that capability.
  23. I've read the docs and coded with reference to the existing mod code, but there are many things I don't understand. What should I put in with () of sendToClient of DepressionModPacketHandler? Also, I don't know what to do with encode, decode, and handle of PlayerMentalCapabilityMessage. public class DepressionModPacketHandler { private static final String PROTOCOL_VERSION = "1"; //プロトコルのバージョンを宣言 public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( //INSTANCEに=の右側以下をぶち込む new ResourceLocation(DepressionMod.MOD_ID, "main"), () -> PROTOCOL_VERSION, //プロトコルのバージョンをぶち込む PROTOCOL_VERSION::equals, //クライアントのバージョンとPROTOCOL_VERSIONを比較? PROTOCOL_VERSION::equals); //サーバーのバージョンとPROTOCOL_VERSIONを比較? //パケットの登録 public static void packetRegister() { INSTANCE.registerMessage( 0, //パケットの識別子 PlayerMentalCapabilityMessage.class, //実際のパケットクラス PlayerMentalCapabilityMessage::encode, //エンコード PlayerMentalCapabilityMessage::decode, //デコード PlayerMentalCapabilityMessage::handle); //メッセージ自体の処理 } public static void sendToClient(Object message, PlayerEntity player) { INSTANCE.send(PacketDistributor.PLAYER.with(null), new PlayerMentalCapabilityMessage()); } } package depressionmod.network; import java.util.function.Supplier; import net.minecraft.network.PacketBuffer; import net.minecraftforge.fml.network.NetworkEvent; public class PlayerMentalCapabilityMessage { public static void encode(PlayerMentalCapabilityMessage message, PacketBuffer buf) { } public static PlayerMentalCapabilityMessage decode(PacketBuffer buf) { return null; } public static void handle(PlayerMentalCapabilityMessage msg, Supplier<NetworkEvent.Context> ctx) { } }
  24. Since I use a translator, my English may be unnatural. Sorry. https://github.com/Zemelua/Depression-Mod This is my code. In MentalOverlayHander, I want to get the mental value of the capability to int level, but I don't know how.
  25. Thank you very much. I solved it by setting the size of overlay.png to 256x256.
×
×
  • Create New...

Important Information

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