# Interesting block of code for those looking to make any spells/effects for a plugin.

Discussion in 'Resources' started by DrBoweNur, Nov 6, 2011.

Not open for further replies.
1. Offline

### DrBowe

So I know that people hardly come into the resource forum, but I'd like to share a block of code that I recently came up with last night in order to produce some interesting spell effects. Maybe you guys will find it useful, maybe you won't:

http://pastie.org/2821142

For those who are curious as to what it does, it essentially uses the Parametric Equation for a line in 3D space in order to shoot effects from one location to another.
When t = 0, the location = that of the player
When t = 1, the location = that of the target

With this particular (and somewhat messy) block of code, I managed to accomplish a stream of smoke from the player followed by an explosion when it reached the destination point (AKA: When t > .95)

Demonstration Below (Skip to 30 for actual stuff):

Just thought some people might enjoy it if they're going for some cool spell effects in any RPG-esque plugin. This sample code can be manipulated (and has been by me) to do anything from falling meteor showers, to AoE spells that project smoke projectiles to anyone that gets near the player.

I do apologize for messy and questionable code (Such as looping through i for both explosions and smokes, when it only affects smokes), but the important thing here is an example of a 3D line in space for anyone looking for Grade-A effects (Grade-A for Minecraft, at least)

EDIT:

Here's a much cleaner, easier to use version that can have variable projectile speeds:

SmokeStreamEffect: http://pastie.org/2823201
SmokeData: http://pastie.org/2822906

Important things to note:
• turningPoint is the value of t (0 start - 1 end) at which you want to have your stream do something different, as is defined in run()
• You must set the id of the task back into your class instance, or else it will not be able to cancel itself after completion
• Keep smokeThickness below 9. Preferrably below even that.
• There are two classes: SmokeStreamEffect and SmokeData -- You need them both

Here's a link to a more configurable method, as opposed to a chunk of code ripped out of my experiment's source:
http://pastie.org/2821142

EDIT:
Moved to replace the mess that was in OP

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 21, 2016
#1
civ77 and Mitsugaru like this.
2. Offline

### Sleaker

seems like it would start to slow down the server if you were manipulating loops like this a lot. noticed any drawbacks at all?

#2
3. Offline

### DrBowe

I haven't done much testing, but unless you have an infinite-range spell, the loop remains rather small.
This:
Code:java
` t+= 1/loc1.distance(loc2) `

Helps to keep the loop as small as possible whilst keeping the visuals intact, and it could even be shortened (For example, playing the smoke every-other block as opposed to every single one).
Obviously the thickness of the smoke is a factor as well. A thickness of 5 is about as high as I would go, personally.

Like I said though, it's more of a 'proof of concept' than a be all, end all solution. I doubt it would make a noticable difference on the server, but the particle effects could potentially jamp up the FPS of players with slower computers (provided there were several people shooting smoky-spells everywhere)

EDIT:
Just realized it could be sped up by making the distance into a variable. Can't believe I had it calculating that on every iteration with such an expensive calculation.

/me facepalms

#3
4. Offline

### thehutch

Awesome might try and add this to the plugin i am currently working on also how would you slow this down? like make the rocket go slower? or would you just increase the for loop to like 99999999 and nearly crash the server doing so?

#4
5. Offline

### DrBowe

@thehutch
No, God no. That is not how you would approach that at all. That -would- result in crashing the server, as the loop size doesn't affect how fast it travels at all.

On the subject of slowing down this 'fake' projectile, however, I have been working on using the scheduler to fire it at a slower rate. If I find something that works, I'll be sure to post it back into this thread.

#5
6. Offline

### thehutch

Okay well i am using the scheduler now to do a timed delay so it happens after a certain amount of time also how do you get the HashSet<Byte> for the players target block i would like to see your utils.getTargetedBlock method please

#6
7. Offline

#7
8. Offline

### DirtyStarfish

I really like the smoke effect, thats a good idea.

#8
9. Offline

### DrBowe

I've been toying around with smoke and other various 'visuals' of Minecraft for a week or so. Finally came up with the idea to use Parametric equations and got a nice result (Not sure about the efficiency of it, as @Sleaker mentioned)

In any case, hopefully this will help some of the RPG plugins get a bit more panache (again, provided it doesn't cause unnecessary weight)

#9
10. Offline

### DirtyStarfish

I've used some of this code before when trying to make a magic plugin. I stopped in the end because I couldn't think of enough things to add to it, and university kinda got in the way. Never thought to try and create a "fake projectile".
Surely if you kept the 'range' at a reasonable length, limiting the size of the loop, it wouldn't cause any problems?

#10
11. Offline

### DrBowe

That's my take on it. With a max range of 20 blocks (Pretty far), and a smoke thickness of 2, it would loop through 40 times every time the spell is cast.

But I feel like that's a very minuscule weight on the server, in the grand scheme of things. Could be wrong.

#11
12. Offline

### thehutch

Couldnt you do a repeatedtask on that then using the schedular so its only like 5 times a second you wouldnt notice much tbh.

#12
13. Offline

### DrBowe

You would use a repeated task, yes. But it's not that simple.

I'm 90% done to a working version though, so give me a bit more time.

@thehutch
@Sleaker
@DirtyStarfish

Runnable version of this effect, with more efficiency changes. Allows the effect to be slowed down depending on the period that you schedule it with. You can alter the run() to give it a desired effect.

Important things to note:
• turningPoint is the value of t (0 start - 1 end) at which you want to have your stream do something different, as is defined in run()
• You must set the id of the task back into your class instance, or else it will not be able to cancel itself after completion
• Keep smokeThickness below 9. Preferrably below even that.
• There are two classes: SmokeStreamEffect and SmokeData -- You need them both
SmokeStreamEffect: http://pastie.org/2822901
SmokeData: http://pastie.org/2822906

Let me know what you think (if you use it / experiment with it)

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 21, 2016
#13
thehutch likes this.
14. Offline

### Forge_User_50718435

There are some things that need to be changed by the person using the code, like:
Code:
`DeadMines.scheduler.cancelTask(id);`

15. Offline

### DrBowe

Ah, knew I left a few things in there
Regardless, your IDE won't let you get far without 'em being changed.

I'll be sure to re-post editted versions after dinner.

@tips48 Done.

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 21, 2016
#15
tips48 likes this.
16. Offline

### Sleaker

hmm might use something like this for some of my skills in HeroCraft - or a modification on it.

#16
17. Offline

### DrBowe

Modify it all you want, that's why I put it out here in the open
Just retain some credit in the comments, if you don't mind.

#17
18. Offline

### Forge_User_92948280

This looks nice! Will use this (or part of) for my plugin!

*Also thinking of a smoke trail going up ending up with an explosion* HAPPY NEWYEAR!

19. Offline

### Darkspear

Ah, I remembered this from when you gave me the link ages ago on #spoutdev I get to put this to use in my magic wand plugin I'm working on! Thanks!

#19
20. Offline

### andrewpo

Would it be possible to do a similar thing with the new potion effect animations?

e.g. the particles when you throw a splash potion

#20
21. Offline

### DrBowe

andrewpo
Seeing as potion effects need an entity to be attached to, I'm not entirely sure. If you could somehow fake an entity and just teleport it to every location that is calculated by the formula, it might be possible. Haven't experimented with it too much, been too busy.

#21
22. Offline

23. Offline

#23
24. Offline

### Forge_User_92948280

No problems here...

25. Offline

### civ77

nvm they're working again, it must have been a problem on my end.

#25