Posted November 19, 20168 yr Hey all! So I recently created an item that on eaten, will give potion effects and teleport the player around, however: - The random function only teleports them in a positive x & z ( Want to keep y positive ) - The 'For-Loop' runs too quickly So how would I slow this function down. here is the current state of the fuction: @Override protected void onFoodEaten(ItemStack stack, World worldIn, EntityPlayer player) { super.onFoodEaten(stack, worldIn, player); player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 100, 3)); player.addPotionEffect(new PotionEffect(MobEffects.BLINDNESS, 100, 3)); int playerX = player.getPosition().getX(); int playerY = player.getPosition().getY(); int playerZ = player.getPosition().getZ(); int max = 5; int min = 0; for(int i = 1; i < 5; i++) { player.setPosition(playerX + min + (int)(Math.random() * ((max - min) + 1)) , playerY + min + (int)(Math.random() * ((max - min) + 1)), playerZ + min + (int)(Math.random() * ((max - min) + 1))); } } and yes, I have tried: player.setPosition(playerX + rand.nextInt(5), playerY + rand.nextInt(5), playerZ + rand.nextInt(5)); However, it wants me to make the Random rand; to Random rand = null; Also, I dont think nextint uses negitive numbers. Not new to java >> New to modding.
November 19, 20168 yr you will have to give the player some sort of state (capability, or a combo of effects) that you can reference, then every player tick (or intervals of ticks) while the player is in the state apply your teleporting. And yes Random().nextInt() only takes postitives, however you can make the negative with another random playerX + New Random().nextInt(5) * (New Random.nextInt(2) == 0 ? 1 : -1) more or less the above will get a random of 0-4 then multiply by 1 or -1 if the random returns 0 or 1 making that 0-4 value negative or leaving it positive also it should be Random rand = new Random(); then call rand.nextInt(); EDIT: Also don't use my example of new Random().nextInt(); it's better to declare an instance like above "Random rand = new Random();" and call it, or get the RNG from the player most entities have a function called "getRNG()" which returns a Random so you can use player.getRNG().nextInt(); Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
November 19, 20168 yr Or...New Random().nextInt(10) - 5 (not that "New Random().nextInt(...)" will actually compile) Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
November 19, 20168 yr Author So how do ticks work in code? If i uses a int that increases and at 20 (one second in the game) will fire a function.. however, I dont know if code is updated in sink with ingame ticks ALSO: Some times it wont teleport me due to: [21:58:44] [server thread/WARN]: Player742 moved wrongly! Not new to java >> New to modding.
November 19, 20168 yr You could subscribe to a tick event, preferably a playertick event. and in there check for if(player had special potion effect && player.world.time % 20 == 0) do teleport logic; The game is in a constant loop, and each loop is more or less a tick, 20 MC ticks are roughly 1 second RL time. A tick event as it seems is run every tick. So instead of a for loop that will run x amount of times teleporting in a single game loop, you space it out every 20 ticks Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
November 19, 20168 yr Author Which event would you recommend or how would I find a event/create one that functions like I want it... New to events. However, i have the code for when the event is called. Not new to java >> New to modding.
November 19, 20168 yr Which event would you recommend or how would I find a event/create one that functions like I want it... New to events. However, i have the code for when the event is called. Hugo suggested PlayerTickEvent and there is plenty of documentation on the internet I specifically like cool alias'. http://www.minecraftforum.net/forums/mapping-and-modding/mapping-and-modding-tutorials/1571567-forge-1-6-4-1-8-eventhandler-and Jaeblar has good tutorials. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
November 20, 20168 yr not that I'm against Dracos Approach of rand.nextInt(10) - 5; as it looks like a very elegant solution done with less code and cpu But nextInt() goes from 0 (inclusive) and to the bound (exclusive basically bound - 1) so really the outcomes are (if the bound is 10): -5 0 -4 1 -3 2 -2 3 -1 4 0 5 1 6 2 7 3 8 4 9 And just a question regarding my method (as it appears as a horrible approach) how does it "Generate 0 more often" 0 * anything is going to be 0. But if the random gave you a 0, you got a 0. if you got a 1, it might be +pos or -neg same with any other number given. I wouldn't think it would return more 0's unless it's the fact that the bound is so low (0 to 4) that 0 is a higher possibility, but if the goal was to only move randomly 4 blocks left/right and 4 blocks forward/back my method would keep it as 4. where subtracting the number would allow the negative to have a -1 head start? Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
November 20, 20168 yr rand(9) - 4, then. Seriously, it's not that hard to make a linear distribution across any range. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
November 20, 20168 yr Author I have one problem though, it doesn't seem like my event is working. Here is the event: public class PlayerTickHandler { private Minecraft mc; // create a constructor that takes a Minecraft argument; now we have it whenever we need it public PlayerTickHandler(Minecraft mc) { this.mc = mc; } @SubscribeEvent public void onPlayerTick(PlayerTickEvent event) { if (event.phase == Phase.START) { if(mc.thePlayer.isPotionActive(ModEvent.eventEffectTeleportation) && mc.thePlayer.getEntityWorld().getWorldTime() % 20 == 0) { int playerX = mc.thePlayer.getPosition().getX(); int playerY = mc.thePlayer.getPosition().getY(); int playerZ = mc.thePlayer.getPosition().getZ(); Random rand = new Random(); mc.thePlayer.setPosition(playerX + rand.nextInt(10) - 5, playerY, playerZ + rand.nextInt(10) - 5); } } } } and here is the potion registry & the event registry: public class ModEvent { public static Potion eventEffectTeleportation; public static void init() { eventEffectTeleportation = new PotionEffectTeleportation(); GameRegistry.register(eventEffectTeleportation); } } public class ServerProxy implements CommonProxy { @Override public void preInit() { // Tile Registry: GameRegistry.registerTileEntity(TileEntityCrate.class, "plentifulmisc_crate"); // GUI Registry: NetworkRegistry.INSTANCE.registerGuiHandler(PlentifulMisc.instance, GuiRegistyHandler.getInstance()); GuiRegistyHandler.getInstance().registerGuiHandler(new GuiHandler(), GuiHandler.getGuiID()); } @Override public void init() { FMLCommonHandler.instance().bus().register(new PlayerTickHandler(Minecraft.getMinecraft())); } } Not new to java >> New to modding.
November 20, 20168 yr Your code in the ServerProxy never runs because you never call it, and that is good because you are calling client code in server elements. FMLCommonHandler.instance().bus().register(new PlayerTickHandler(Minecraft.getMinecraft())); You should be registering your EventHandler in common code, aka your main mod class. Why do you have a Minecraft instance when you are given a EntityPlayer instance from the event. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
November 20, 20168 yr Author Okay thanks, however im getting this error now: Caused by: java.lang.NullPointerException at com.lambda.PlentifulMisc.events.PlayerTickHandler.onPlayerTick(PlayerTickHandler.java:24) ~[PlayerTickHandler.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_PlayerTickHandler_onPlayerTick_PlayerTickEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:185) ~[EventBus.class:?] at net.minecraftforge.fml.common.FMLCommonHandler.onPlayerPreTick(FMLCommonHandler.java:360) ~[FMLCommonHandler.class:?] at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:215) ~[EntityPlayer.class:?] at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:341) ~[EntityPlayerMP.class:?] at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:174) ~[NetHandlerPlayServer.class:?] at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:216) ~[NetworkDispatcher$1.class:?] at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:308) ~[NetworkManager.class:?] at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:195) ~[NetworkSystem.class:?] ... 5 more Not new to java >> New to modding.
November 20, 20168 yr Okay thanks, however im getting this error now: Caused by: java.lang.NullPointerException at com.lambda.PlentifulMisc.events.PlayerTickHandler.onPlayerTick(PlayerTickHandler.java:24) ~[PlayerTickHandler.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_PlayerTickHandler_onPlayerTick_PlayerTickEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:185) ~[EventBus.class:?] at net.minecraftforge.fml.common.FMLCommonHandler.onPlayerPreTick(FMLCommonHandler.java:360) ~[FMLCommonHandler.class:?] at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:215) ~[EntityPlayer.class:?] at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:341) ~[EntityPlayerMP.class:?] at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:174) ~[NetHandlerPlayServer.class:?] at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:216) ~[NetworkDispatcher$1.class:?] at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:308) ~[NetworkManager.class:?] at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:195) ~[NetworkSystem.class:?] ... 5 more Caused by: java.lang.NullPointerException at com.lambda.PlentifulMisc.events.PlayerTickHandler.onPlayerTick(PlayerTickHandler.java:24) This is your problem, something is null on line 24. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
November 20, 20168 yr And just a question regarding my method (as it appears as a horrible approach) how does it "Generate 0 more often" 0 * anything is going to be 0. But if the random gave you a 0, you got a 0. if you got a 1, it might be +pos or -neg same with any other number given.You have two randoms: nextInt(3)nextInt(1) == 0 ? 1 : -1result 010 0-10 111 1-1-1 212 2-1-2 As you can see the 0 appears twice in the result column as opposed to every other number, hence it has a higher chance of appearing. actually since I'm using rand.nextInt(2) == 0 ? 1 : -1; which is a ternary so the random will return 0 or 1. If it is == 0 return 1, else -1 so even if I had rand.nextInt(50) == 0 ? 1 : -1; it would most likely be -1 more often as it could be != 0 as it has more options to pick from (0 to 49) But after I got up and left I figured Dracos is the best choice as my given example that I posted it'd be easier to just add 1, and it'd even it all out rand.nextInt(11) - 5; would give a range of -5 to +5 (including 0) or simply "rand.nextInt((range * 2) + 1) - range;" Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
November 20, 20168 yr actually since I'm using rand.nextInt(2) == 0 ? 1 : -1; which is a ternary so the random will return 0 or 1. If it is == 0 return 1, else -1 The fact that it's ternary doesn't matter. 0 * (rand.nextInt(2) == 0 ? 1 : -1) is still 0. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
November 20, 20168 yr Author Ah! So I know whats wrong, however trying to do a nullcheck/falsecheck seems to not work. here is the line thats crashing if(mc.thePlayer.isPotionActive(ModEvents.eventEffectTeleportation) && mc.thePlayer.getEntityWorld().getWorldTime() % 20 == 0) { And i just did an else statement and returned, however i get same error Not new to java >> New to modding.
November 20, 20168 yr Ah! So I know whats wrong, however trying to do a nullcheck/falsecheck seems to not work. here is the line thats crashing if(mc.thePlayer.isPotionActive(ModEvents.eventEffectTeleportation) && mc.thePlayer.getEntityWorld().getWorldTime() % 20 == 0) { And i just did an else statement and returned, however i get same error Does this ever get called? public static void init() { eventEffectTeleportation = new PotionEffectTeleportation(); GameRegistry.register(eventEffectTeleportation); } Plus ...and that is good because you are calling client code in server elements. FMLCommonHandler.instance().bus().register(new PlayerTickHandler(Minecraft.getMinecraft())); You should be registering your EventHandler in common code, aka your main mod class. Why do you have a Minecraft instance when you are given a EntityPlayer instance from the event. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
November 20, 20168 yr Author I dont think I've given an entityplayer, and does: FMLCommonHandler.instance().bus().register(new PlayerTickHandler(Minecraft.getMinecraft())); need to be in init() or preInit() Not new to java >> New to modding.
November 20, 20168 yr I dont think I've given an entityplayer, What? and does: FMLCommonHandler.instance().bus().register(new PlayerTickHandler(Minecraft.getMinecraft())); need to be in init() or preInit() What Minecraft version are you using? VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
November 20, 20168 yr Author 1.10 Does it matter if i use minecraft instance? or is it just more efficient the other way.. Not new to java >> New to modding.
November 20, 20168 yr 1.10 Does it matter if i use minecraft instance? or is it just more efficient the other way.. Events are registered with MinecraftForge.EVENT_BUS.register() now no matter which one, and yes it does matter if you use Minecraft instance because that is a client side class. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.