Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

kyazuki

Members
  • Joined

  • Last visited

Everything posted by kyazuki

  1. The values saved in playerdata file. However, the values aren't imported when login. How do I fix? Main: public static final ResourceLocation CAP_RESOURCE = new ResourceLocation(MODID, "capabilities"); public TestMod() { FMLJavaModLoadingContext.get().getModEventBus().addListener(TestMod::onFMLCommonSetup); } @SubscribeEvent public static void onFMLCommonSetup(FMLCommonSetupEvent event) { CapabilityManager.INSTANCE.register(IMyCapability.class, new MyCapabilityStorage(), () -> new MyCapability(2.0f)); } @SubscribeEvent public static void attachCapability(AttachCapabilitiesEvent<Entity> event) { if (!(event.getObject() instanceof PlayerEntity)) return; event.addCapability(CAP_RESOURCE, new MyCapabilityProvider()); } Provider: public class MyCapabilityProvider implements ICapabilitySerializable<INBT> { @CapabilityInject(IMyCapability.class) public static final Capability<IMyCapability> CAP = null; private LazyOptional<IMyCapability> instance = LazyOptional.of(CAP::getDefaultInstance); @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { return CAP.orEmpty(cap, instance); } @Override public INBT serializeNBT() { return (INBT) CAP.getStorage().writeNBT(CAP, instance.orElseThrow(() -> new IllegalArgumentException("at serialize")), null); } @Override public void deserializeNBT(INBT nbt) { CAP.getStorage().readNBT(CAP, instance.orElseThrow(() -> new IllegalArgumentException("at deserialize")), null, nbt); } } Storage: public class MyCapabilityStorage implements Capability.IStorage<IMyCapability> { @Override public INBT writeNBT(Capability<IMyCapability> capability, IMyCapability instance, Direction side) { CompoundNBT tag = new CompoundNBT(); tag.putFloat("cap1", instance.getCap1()); tag.putFloat("cap2", instance.getCap2()); tag.putFloat("cap3", instance.getCap3()); return tag; } @Override public void readNBT(Capability<IMyCapability> capability, IMyCapability instance, Direction side, INBT nbt) { CompoundNBT tag = (CompoundNBT) nbt; instance.setCap1(tag.getFloat("cap1")); instance.setCap2(tag.getFloat("cap2")); instance.setCap3(tag.getFloat("cap3")); } }
  2. It's first time using capability & packet system, so I think make a stupid mistake. Please tell me where I'm wrong? I want to sync float value in my capability with client. PacketHandler: public class PacketHandler { private static final String PROTOCOL_VERSION = "1.0"; public static final SimpleChannel CHANNEL = NetworkRegistry.ChannelBuilder .named(new ResourceLocation(DietMod.MODID, "main_channel")) .clientAcceptedVersions(PROTOCOL_VERSION::equals) .serverAcceptedVersions(PROTOCOL_VERSION::equals) .networkProtocolVersion(() -> PROTOCOL_VERSION) .simpleChannel(); public static void register() { CHANNEL.registerMessage(0, CapabilityPacket.class, CapabilityPacket::encode, CapabilityPacket::decode, CapabilityPacket::handle); } public static void sendTo(Object message, PlayerEntity player) { CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), message); } } Packet: public class CapabilityPacket { private final float scale; public CapabilityPacket(float scale) { this.scale = scale; } public static void encode(CapabilityPacket pkt, PacketBuffer buf) { buf.writeFloat(pkt.scale); } public static CapabilityPacket decode(PacketBuffer buf) { return new CapabilityPacket(buf.readFloat()); } public static void handle(CapabilityPacket pkt, Supplier<NetworkEvent.Context> contextSupplier) { NetworkEvent.Context context = contextSupplier.get(); context.enqueueWork(() -> { PlayerEntity player = context.getSender(); if (player == null) return; player.getCapability(ScaleProvider.SCALE_CAPABILITY).orElseThrow(() -> new IllegalArgumentException()).setScale(pkt.scale); <-- I think this is wrong. }); context.setPacketHandled(true); } } Main: public TestMod() { PacketHandler.register(); } @SubscribeEvent public static void syncCap(TickEvent.PlayerTickEvent event) { if (!event.player.world.isRemote()) { float scale = event.player.getCapability(ScaleProvider.SCALE_CAP).orElseThrow(() -> new IllegalArgumentException()).getScale(); PacketHandler.sendTo(new CapabilityPacket(scale), event.player); } }
  3. I forgot to add listner... public TestMod() { FMLJavaModLoadingContext.get().getModEventBus().addListener(TestMod::onFMLCommonSetup); } Thanks!
  4. Oh, I forget that. @SubscribeEvent public static void onFMLCommonSetup(FMLCommonSetupEvent event) { CapabilityManager.INSTANCE.register(IScale.class, new ScaleStorage(), () -> new Scale(2.0f)); } @SubscribeEvent public static void attachCapability(AttachCapabilitiesEvent<Entity> event) { if (!(event.getObject() instanceof PlayerEntity)) return; event.addCapability(SCALE_CAP, new ScaleProvider()); } I changed code, but it throws a new error. What's wrong?
  5. I want to add a float value to player. This code always throws IllegalArgumentException("at login") at onloginPlayer() in Main Class. I think attachCapability isn't called, but I don't know why it isn't. Please tell me if I'm making any other errors in this code. public class Scale implements IScale { private float scale; public Scale(float scale) { this.scale = scale; } @Override public void set(float scale) { this.scale = scale; } @Override public float get() { return this.scale; } } public class ScaleStorage implements Capability.IStorage<IScale> { @Override public INBT writeNBT(Capability<IScale> capability, IScale instance, Direction side) { return FloatNBT.valueOf(instance.get()); } @Override public void readNBT(Capability<IScale> capability, IScale instance, Direction side, INBT nbt) { instance.set(((FloatNBT) nbt).getFloat()); } } public class ScaleProvider implements ICapabilitySerializable<FloatNBT> { @CapabilityInject(IScale.class) public static final Capability<IScale> SCALE_CAP = null; private LazyOptional<IScale> instance = LazyOptional.of(SCALE_CAP::getDefaultInstance); @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { return SCALE_CAP.orEmpty(cap, instance); } @Override public FloatNBT serializeNBT() { return (FloatNBT) SCALE_CAP.getStorage().writeNBT(SCALE_CAP, instance.orElseThrow(() -> new IllegalArgumentException("at serialize")), null); } @Override public void deserializeNBT(FloatNBT nbt) { SCALE_CAP.getStorage().readNBT(SCALE_CAP, instance.orElseThrow(() -> new IllegalArgumentException("at deserialize")), null, nbt); } } Main Class: @SubscribeEvent public void onFMLCommonSetup(FMLCommonSetupEvent event) { CapabilityManager.INSTANCE.register(IScale.class, new ScaleStorage(), () -> new Scale(2.0f)); } @SubscribeEvent public void attachCapability(AttachCapabilitiesEvent<Entity> event) { if (!(event.getObject() instanceof PlayerEntity)) return; event.addCapability(SCALE_CAP, new ScaleProvider()); } @SubscribeEvent public static void onloginPlayer(PlayerEvent.PlayerLoggedInEvent event) { PlayerEntity player = event.getPlayer(); IScale p = player.getCapability(ScaleProvider.SCALE_CAP).orElseThrow(() -> new IllegalArgumentException("at login")); LOGGER.debug("check before: " + p.get()); p.set(1.0f); LOGGER.debug("check after: " + p.get()); LOGGER.debug(player.serializeNBT()); }
  6. I want to change player's eyeheight. I think it's impossible without replacing a vanilla method. So I used coremod, but my cord caused error. I think this cause is that 0.5 is double, but I don't know to define float value on JavaScript. How do I solve this? I have bad English, so I apologize if I say something strange. Vanilla Method: public float getStandingEyeHeight(Pose poseIn, EntitySize sizeIn) { switch(poseIn) { case SWIMMING: case FALL_FLYING: case SPIN_ATTACK: return 0.4F; case CROUCHING: return 1.27F; default: return 1.62F; } } Byte Code of Vanilla Method: public getStandingEyeHeight(Lnet/minecraft/entity/Pose;Lnet/minecraft/entity/EntitySize;)F L0 LINENUMBER 1771 L0 GETSTATIC net/minecraft/entity/player/PlayerEntity$1.$SwitchMap$net$minecraft$entity$Pose : [I ALOAD 1 INVOKEVIRTUAL net/minecraft/entity/Pose.ordinal ()I IALOAD TABLESWITCH 1: L1 2: L1 3: L1 4: L2 default: L3 L1 LINENUMBER 1775 L1 FRAME SAME LDC 0.4 FRETURN L2 LINENUMBER 1777 L2 FRAME SAME LDC 1.27 FRETURN L3 LINENUMBER 1779 L3 FRAME SAME LDC 1.62 FRETURN L4 LOCALVARIABLE this Lnet/minecraft/entity/player/PlayerEntity; L0 L4 0 LOCALVARIABLE p_213348_1_ Lnet/minecraft/entity/Pose; L0 L4 1 LOCALVARIABLE p_213348_2_ Lnet/minecraft/entity/EntitySize; L0 L4 2 MAXSTACK = 2 MAXLOCALS = 3 Transformer.js: var ASMAPI = Java.type('net.minecraftforge.coremod.api.ASMAPI'); var Opcodes = Java.type('org.objectweb.asm.Opcodes'); var mappedMethodName = ASMAPI.mapMethod("func_213348_b"); function initializeCoreMod() { return { 'coremodmethod': { 'target': { 'type': 'METHOD', 'class': 'net.minecraft.entity.player.PlayerEntity', 'methodName': mappedMethodName, 'methodDesc': '(Lnet/minecraft/entity/Pose;Lnet/minecraft/entity/EntitySize;)F' }, 'transformer': function(method) { var arrayLength = method.instructions.size(); for (var i = arrayLength - 1; i >= 0; --i) { var instruction = method.instructions.get(i); if (instruction.getOpcode() == Opcodes.LDC) { var LdcInsnNode = Java.type('org.objectweb.asm.tree.LdcInsnNode'); var newInstruction = new LdcInsnNode(0.5); method.instructions.insertBefore(instruction, newInstruction); method.instructions.remove(instruction); print("Transformed!"); break; } } return method; } } } } Error: [main/INFO] [STDERR/]: [java.lang.Throwable:printStackTrace:644]: Caused by: java.lang.VerifyError: Bad type on operand stack Exception Details: Location: net/minecraft/entity/player/PlayerEntity.getStandingEyeHeight(Lnet/minecraft/entity/Pose;Lnet/minecraft/entity/EntitySize;)F @51: freturn Reason: Type double_2nd (current frame, stack[1]) is not assignable to float Current Frame: bci: @51 flags: { } locals: { 'net/minecraft/entity/player/PlayerEntity', 'net/minecraft/entity/Pose', 'net/minecraft/entity/EntitySize' } stack: { double, double_2nd }
  7. This is it. @SubscribeEvent public static void onServerStart(FMLServerStartingEvent event) { SetDistanceCommand.register(event.getCommandDispatcher()); }
  8. My world type generates only 1 biome. So I use SingleBiomeProvider.
  9. Entity::eyeHeight is private...
  10. My mod makes player tall as the player walks. @SubscribeEvent public static void calc(TickEvent.PlayerTickEvent event) { if (!event.player.world.isRemote()) { walkDistance = event.player.distanceWalkedModified / 0.6f; scale = walkDistance * 0.001f + minScale; } } However, I can't access player's height. So I change render scale, hitbox and eye height. @SubscribeEvent public static void onRenderPlayerPre(RenderPlayerEvent.Pre event) { event.getMatrixStack().push(); event.getMatrixStack().scale(1.0f, scale, 1.0f); } @SubscribeEvent public static void onRenderPlayerPost(RenderPlayerEvent.Post event) { event.getMatrixStack().pop(); } @SubscribeEvent public static void sethitboxPlayer(TickEvent.PlayerTickEvent event) { float hitboxScale = event.player.getHeight() * scale; AxisAlignedBB playerBoundingBox = event.player.getBoundingBox(); event.player.setBoundingBox(new AxisAlignedBB(playerBoundingBox.minX, playerBoundingBox.minY, playerBoundingBox.minZ, playerBoundingBox.maxX, playerBoundingBox.minY + hitboxScale, playerBoundingBox.maxZ)); //Fire EyeHeightEvent } @SubscribeEvent public static void setEyePlayer(EntityEvent.EyeHeight event) { if (event.getEntity() instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) event.getEntity(); event.setNewHeight(event.getOldHeight() * scale); } }
  11. Sorry for my lack of explanation. I set the eye height by this cord. It works, but it fires only when the pose changes. Can't I fire Forge events manually in my mod? @SubscribeEvent public static void setEyePlayer(EntityEvent.EyeHeight event) { if (event.getEntity() instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) event.getEntity(); event.setNewHeight(event.getOldHeight() * scale); } }
  12. My mod changes player's height on every tick. So I use EntityEvent.EyeHeight#SetNewHeight method. I want to fire the event on every tick. This cord doesn't work. What's wrong? @SubscribeEvent public static void sethitboxPlayer(TickEvent.PlayerTickEvent event) { float hitboxScale = event.player.getHeight() * scale; AxisAlignedBB playerBoundingBox = event.player.getBoundingBox(); event.player.setBoundingBox(new AxisAlignedBB(playerBoundingBox.minX, playerBoundingBox.minY, playerBoundingBox.minZ, playerBoundingBox.maxX, playerBoundingBox.minY + hitboxScale, playerBoundingBox.maxZ)); Pose pose = event.player.getPose(); EntitySize size = event.player.getSize(pose); net.minecraftforge.event.entity.EntityEvent.EyeHeight evt = new net.minecraftforge.event.entity.EntityEvent.EyeHeight(event.player, pose, size, event.player.getEyeHeight(pose)); net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(evt); }
  13. I want to change player's height. So, I changed render scale and hitbox. However, view wasn't changed. Can I change it? I use EntityEvent.EyeHeight#SetNewHeight.
  14. I see. Then, can I change entity's render scale?
  15. Entity#size is private, so I can't access...
  16. I see. I can't find a method which change entity size. Which method should I call?
  17. I want to change mob's height. @SubscribeEvent public static void onSpawnMobs(LivingSpawnEvent event) { if (!event.getEntityLiving().getEntityWorld().isRemote) { LivingEntity entity = event.getEntityLiving(); entity.getSize(Pose.STANDING).scale(2.0f); } } This cord doesn't work. What's wrong?
  18. You're right, thanks!
  19. My mod add nether portal as item. NETHER_PORTAL = new BlockItem(Blocks.NETHER_PORTAL, new Item.Properties()), "nether_portal") However, the portal block always face north/south. I want to change portal's direction when players place. @SubscribeEvent public static void onPlaceNetherPortal(BlockEvent.EntityPlaceEvent event) { if (!event.getEntity().getEntityWorld().isRemote) { if (event.getPlacedBlock() == Blocks.NETHER_PORTAL.getDefaultState()) { if (event.getEntity() instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) event.getEntity(); float yaw = player.rotationYaw; if ((yaw > 45.0 && yaw <= 135.0) || (yaw > -135.0 && yaw <= -45.0)) { event.getPlacedBlock().rotate(Rotation.CLOCKWISE_90); } } } } This cord doesn't work... What should I do?
  20. Sorry, this is my class. @Mod(AloneReloaded.MODID) @Mod.EventBusSubscriber public class Testmod { public static final String MODID = "testmod"; public static final Logger LOGGER = LogManager.getLogger(MODID); public Testmod() { LOGGER.debug("Testmod Loaded!"); } @SubscribeEvent public static void onBreeding(BabyEntitySpawnEvent event) { LOGGER.debug("Child:" + event.getChild()); LOGGER.debug("Parents:" + event.getParentA() + ", " + event.getParentB()); } }

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.