forked from TotalFreedomMC/TotalFreedomMod
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Change trailer from blocks to particles.
- Loading branch information
Showing
6 changed files
with
182 additions
and
121 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
commons/src/main/java/me/totalfreedom/totalfreedommod/api/Interpolator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package me.totalfreedom.totalfreedommod.api; | ||
|
|
||
| @FunctionalInterface | ||
| public interface Interpolator | ||
| { | ||
| double[] interpolate(double from, double to, int max); | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
commons/src/main/java/me/totalfreedom/totalfreedommod/util/Interpolation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package me.totalfreedom.totalfreedommod.util; | ||
|
|
||
| import me.totalfreedom.totalfreedommod.api.Interpolator; | ||
| import org.bukkit.Color; | ||
|
|
||
| import java.util.LinkedHashSet; | ||
|
|
||
| public class Interpolation | ||
| { | ||
| public LinkedHashSet<Color> rainbow(int length) | ||
| { | ||
| LinkedHashSet<Color> base = new LinkedHashSet<>(); | ||
| LinkedHashSet<Color> redToOrange = hsvGradient(length, Color.RED, Color.ORANGE, this::linear); | ||
| LinkedHashSet<Color> orangeToYellow = hsvGradient(length, Color.ORANGE, Color.YELLOW, this::linear); | ||
| LinkedHashSet<Color> yellowToGreen = hsvGradient(length, Color.YELLOW, Color.GREEN, this::linear); | ||
| LinkedHashSet<Color> greenToBlue = hsvGradient(length, Color.GREEN, Color.BLUE, this::linear); | ||
| LinkedHashSet<Color> blueToPurple = hsvGradient(length, Color.BLUE, Color.PURPLE, this::linear); | ||
| LinkedHashSet<Color> purpleToRed = hsvGradient(length, Color.PURPLE, Color.RED, this::linear); | ||
| base.addAll(redToOrange); | ||
| base.addAll(orangeToYellow); | ||
| base.addAll(yellowToGreen); | ||
| base.addAll(greenToBlue); | ||
| base.addAll(blueToPurple); | ||
| base.addAll(purpleToRed); | ||
| return base; | ||
| } | ||
|
|
||
| private double[] linear(double from, double to, int max) | ||
| { | ||
| final double[] res = new double[max]; | ||
| for (int i = 0; i < max; i++) | ||
| { | ||
| res[i] = from + i * ((to - from) / (max - 1)); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| private LinkedHashSet<Color> hsvGradient(int length, Color from, Color to, Interpolator interpolator) | ||
| { | ||
| // returns a float-array where hsv[0] = hue, hsv[1] = saturation, hsv[2] = value/brightness | ||
| final float[] hsvFrom = java.awt.Color.RGBtoHSB(from.getRed(), from.getGreen(), from.getBlue(), null); | ||
| final float[] hsvTo = java.awt.Color.RGBtoHSB(to.getRed(), to.getGreen(), to.getBlue(), null); | ||
|
|
||
| final double[] h = interpolator.interpolate(hsvFrom[0], hsvTo[0], length); | ||
| final double[] s = interpolator.interpolate(hsvFrom[1], hsvTo[1], length); | ||
| final double[] v = interpolator.interpolate(hsvFrom[2], hsvTo[2], length); | ||
|
|
||
| final LinkedHashSet<Color> gradient = new LinkedHashSet<>(); | ||
|
|
||
| for (int i = 0; i < length; i++) | ||
| { | ||
| final int rgb = java.awt.Color.HSBtoRGB((float) h[i], (float) s[i], (float) v[i]); | ||
| final Color color = Color.fromRGB(rgb); | ||
| gradient.add(color); | ||
| } | ||
| return gradient; | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
commons/src/main/java/me/totalfreedom/totalfreedommod/util/ParticleDisplay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package me.totalfreedom.totalfreedommod.util; | ||
|
|
||
| import com.destroystokyo.paper.ParticleBuilder; | ||
| import org.bukkit.Color; | ||
| import org.bukkit.Location; | ||
| import org.bukkit.Particle; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.util.Vector; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Iterator; | ||
|
|
||
| public class ParticleDisplay | ||
| { | ||
| private final Interpolation interpolation; | ||
| private Iterator<Color> colorIterator; | ||
|
|
||
| public ParticleDisplay() | ||
| { | ||
| this.interpolation = new Interpolation(); | ||
| this.colorIterator = interpolation.rainbow(43).iterator(); | ||
| } | ||
|
|
||
| public void spawnNext(Player player) | ||
| { | ||
| Location location = getBehind(player); | ||
| Color color = getIterator().next(); | ||
| Particle.DustOptions options = new Particle.DustOptions(color, 3); | ||
| ParticleBuilder builder = new ParticleBuilder(Particle.REDSTONE); | ||
| builder.data(options) | ||
| .receivers(30, 15, true) | ||
| .offset(0.5, 0.5, 0.5) | ||
| .location(location) | ||
| .spawn(); | ||
| } | ||
|
|
||
| private Location getBehind(Player player) | ||
| { | ||
| @NotNull Vector inverse = player.getLocation() | ||
| .clone() | ||
| .getDirection() | ||
| .normalize() | ||
| .multiply(-1); | ||
|
|
||
| return player.getLocation().add(inverse); | ||
| } | ||
|
|
||
| private Iterator<Color> getIterator() | ||
| { | ||
| if (!this.colorIterator.hasNext()) | ||
| { | ||
| this.colorIterator = interpolation.rainbow(43).iterator(); | ||
| } | ||
|
|
||
| return this.colorIterator; | ||
| } | ||
| } |
Oops, something went wrong.