Skip to content

Commit 6e1fd51

Browse files
authored
uploaded SourceCode
1 parent 349a1ac commit 6e1fd51

File tree

10 files changed

+423
-0
lines changed

10 files changed

+423
-0
lines changed

src/plugin.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: ViaRewindPotions
2+
version: 0.0.1
3+
description: Translates thrown potions for ProtocolSupport and ViaRewind
4+
depend: [ProtocolLib]
5+
softdepend:
6+
- "ProtocolSupport"
7+
- "ViaRewind"
8+
- "ViaRewind-Legacy-Support"
9+
main: twolovers.viarewindpotions.Main
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package twolovers.viarewindpotions;
2+
3+
import com.comphenix.protocol.ProtocolLibrary;
4+
import com.comphenix.protocol.ProtocolManager;
5+
import twolovers.viarewindpotions.listeners.AreaEffectCloudListener;
6+
import twolovers.viarewindpotions.listeners.SpawnEntityListener;
7+
import twolovers.viarewindpotions.adapters.WorldEventAdapter;
8+
import twolovers.viarewindpotions.utils.ConfigurationUtil;
9+
import twolovers.viarewindpotions.utils.VersionUtil;
10+
import org.bukkit.plugin.PluginManager;
11+
import org.bukkit.plugin.java.JavaPlugin;
12+
13+
public class Main extends JavaPlugin {
14+
public void onEnable() {
15+
final ConfigurationUtil configurationUtil = new ConfigurationUtil(this);
16+
final VersionUtil versionUtil = new VersionUtil(this);
17+
final PluginManager pluginManager = getServer().getPluginManager();
18+
final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
19+
20+
if (pluginManager.getPlugin("ViaRewind-Legacy-Support") == null || !configurationUtil.getConfiguration("%datafolder%/ViaRewind-Legacy-Support/config.yml").getBoolean("area-effect-cloud-particles"))
21+
pluginManager.registerEvents(new AreaEffectCloudListener(this, versionUtil), this);
22+
23+
protocolManager.addPacketListener(new SpawnEntityListener(this, versionUtil));
24+
protocolManager.addPacketListener(new WorldEventAdapter(this, versionUtil));
25+
}
26+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package twolovers.viarewindpotions.adapters;
2+
3+
import com.comphenix.protocol.PacketType;
4+
import com.comphenix.protocol.ProtocolLibrary;
5+
import com.comphenix.protocol.events.PacketAdapter;
6+
import com.comphenix.protocol.events.PacketContainer;
7+
import com.comphenix.protocol.events.PacketEvent;
8+
import com.comphenix.protocol.reflect.StructureModifier;
9+
import twolovers.viarewindpotions.utils.SplashTranslator;
10+
import twolovers.viarewindpotions.utils.TranslationData;
11+
import twolovers.viarewindpotions.utils.VersionUtil;
12+
import org.bukkit.entity.Player;
13+
import org.bukkit.plugin.Plugin;
14+
15+
public class WorldEventAdapter extends PacketAdapter {
16+
private final VersionUtil versionUtil;
17+
18+
public WorldEventAdapter(final Plugin plugin, final VersionUtil versionUtil) {
19+
super(plugin, PacketType.Play.Server.WORLD_EVENT);
20+
21+
this.versionUtil = versionUtil;
22+
}
23+
24+
@Override
25+
public void onPacketReceiving(PacketEvent event) {
26+
}
27+
28+
@Override
29+
public void onPacketSending(PacketEvent event) {
30+
final PacketContainer packet = event.getPacket();
31+
final int effectId = packet.getIntegers().read(0);
32+
33+
if (effectId == 2002 || effectId == 2007) {
34+
final Player player = event.getPlayer();
35+
final int version = versionUtil.getVersion(player);
36+
final PacketContainer edited = packet.deepClone();
37+
final StructureModifier<Integer> editedIntegers = edited.getIntegers();
38+
39+
if (version <= 210)
40+
editedIntegers.write(0, 2002);
41+
42+
for (final SplashTranslator translator : SplashTranslator.values()) {
43+
if (editedIntegers.read(1) == translator.getRGB()) {
44+
for (final TranslationData data : translator.getDatas()) {
45+
if (data.getLowestVersion() <= version && data.getHighestVersion() >= version)
46+
editedIntegers.write(1, data.getRemap());
47+
}
48+
}
49+
}
50+
51+
try {
52+
ProtocolLibrary.getProtocolManager().sendServerPacket(player, edited, false);
53+
} catch (Exception e) {
54+
e.printStackTrace();
55+
}
56+
57+
event.setCancelled(true);
58+
}
59+
}
60+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package twolovers.viarewindpotions.listeners;
2+
3+
import twolovers.viarewindpotions.utils.VersionUtil;
4+
import org.bukkit.Location;
5+
import org.bukkit.entity.AreaEffectCloud;
6+
import org.bukkit.entity.Player;
7+
import org.bukkit.event.EventHandler;
8+
import org.bukkit.event.Listener;
9+
import org.bukkit.event.entity.LingeringPotionSplashEvent;
10+
import org.bukkit.plugin.Plugin;
11+
12+
import java.util.List;
13+
import java.util.concurrent.CopyOnWriteArrayList;
14+
15+
public class AreaEffectCloudListener implements Listener {
16+
final private List<AreaEffectCloud> effectClouds;
17+
18+
public AreaEffectCloudListener(final Plugin plugin, final VersionUtil versionUtil) {
19+
effectClouds = new CopyOnWriteArrayList<>();
20+
21+
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
22+
for (final AreaEffectCloud cloud : effectClouds) {
23+
if (cloud == null || cloud.isDead() || !cloud.isValid())
24+
effectClouds.remove(cloud);
25+
else {
26+
final Location location = cloud.getLocation();
27+
final float radius = cloud.getRadius();
28+
final float area = (float) Math.PI * radius * radius;
29+
30+
final int color = cloud.getColor().asRGB();
31+
final int red = color >> 16 & 255;
32+
final int green = color >> 8 & 255;
33+
final int blue = color & 255;
34+
35+
for (int i = 0; i < area; i++) {
36+
float f1 = (float) Math.random() * 6.2831855F;
37+
float f2 = (float) Math.sqrt(Math.random()) * radius;
38+
float f3 = (float) Math.cos(f1) * f2;
39+
float f4 = (float) Math.sin(f1) * f2;
40+
41+
for (final Player player : cloud.getWorld().getPlayers())
42+
if (versionUtil.getVersion(player) <= 106)
43+
player.spawnParticle(cloud.getParticle(), location.getX() + f3, location.getY(), location.getZ() + f4, 0, red / 255f, green / 255f, blue / 255f);
44+
}
45+
}
46+
}
47+
}, 1L, 1L);
48+
}
49+
50+
@EventHandler
51+
public void onLingeringPotionSplash(final LingeringPotionSplashEvent event) {
52+
effectClouds.add(event.getAreaEffectCloud());
53+
}
54+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package twolovers.viarewindpotions.listeners;
2+
3+
import com.comphenix.protocol.PacketType;
4+
import com.comphenix.protocol.ProtocolLibrary;
5+
import com.comphenix.protocol.events.PacketAdapter;
6+
import com.comphenix.protocol.events.PacketContainer;
7+
import com.comphenix.protocol.events.PacketEvent;
8+
import twolovers.viarewindpotions.utils.PotionTranslator;
9+
import twolovers.viarewindpotions.utils.TranslationData;
10+
import twolovers.viarewindpotions.utils.VersionUtil;
11+
import org.bukkit.entity.Entity;
12+
import org.bukkit.entity.Player;
13+
import org.bukkit.entity.ThrownPotion;
14+
import org.bukkit.plugin.Plugin;
15+
import org.bukkit.potion.PotionEffect;
16+
17+
public class SpawnEntityListener extends PacketAdapter {
18+
private final VersionUtil versionUtil;
19+
20+
public SpawnEntityListener(final Plugin plugin, final VersionUtil versionUtil) {
21+
super(plugin, PacketType.Play.Server.SPAWN_ENTITY);
22+
23+
this.versionUtil = versionUtil;
24+
}
25+
26+
@Override
27+
public void onPacketSending(final PacketEvent event) {
28+
final PacketContainer packet = event.getPacket();
29+
final Entity entity = packet.getEntityModifier(event).read(0);
30+
31+
if (packet.getIntegers().read(6) == 73 && entity instanceof ThrownPotion) {
32+
final Player player = event.getPlayer();
33+
final int version = versionUtil.getVersion(player);
34+
final PacketContainer edited = packet.deepClone();
35+
final ThrownPotion potion = (ThrownPotion) entity;
36+
37+
for (final PotionEffect effect : potion.getEffects()) {
38+
for (final PotionTranslator translator : PotionTranslator.values()) {
39+
if (effect.getType().equals(translator.getPotionEffectType())) {
40+
for (TranslationData data : translator.getDatas())
41+
if (data.getLowestVersion() <= version && data.getHighestVersion() >= version)
42+
edited.getIntegers().write(7, data.getRemap());
43+
}
44+
}
45+
}
46+
47+
try {
48+
ProtocolLibrary.getProtocolManager().sendServerPacket(player, edited, false);
49+
} catch (Exception e) {
50+
e.printStackTrace();
51+
}
52+
53+
event.setCancelled(true);
54+
}
55+
}
56+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package twolovers.viarewindpotions.utils;
2+
3+
import org.bukkit.configuration.file.YamlConfiguration;
4+
import org.bukkit.plugin.Plugin;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.nio.file.Files;
10+
11+
public class ConfigurationUtil {
12+
final private Plugin plugin;
13+
14+
public ConfigurationUtil(final Plugin plugin) {
15+
this.plugin = plugin;
16+
}
17+
18+
public YamlConfiguration getConfiguration(String filePath) {
19+
final File dataFolder = plugin.getDataFolder();
20+
final File file = new File(filePath.replace("%datafolder%", dataFolder.toPath().toString()));
21+
22+
if (file.exists())
23+
return YamlConfiguration.loadConfiguration(file);
24+
else return new YamlConfiguration();
25+
}
26+
27+
public void createConfiguration(String file) {
28+
try {
29+
final File dataFolder = plugin.getDataFolder();
30+
31+
file = file.replace("%datafolder%", dataFolder.toPath().toString());
32+
33+
final File configFile = new File(file);
34+
35+
if (!configFile.exists()) {
36+
final String[] files = file.split("/");
37+
final InputStream inputStream = plugin.getClass().getClassLoader().getResourceAsStream(files[files.length - 1]);
38+
final File parentFile = configFile.getParentFile();
39+
40+
if (parentFile != null) parentFile.mkdirs();
41+
42+
if (inputStream != null) {
43+
Files.copy(inputStream, configFile.toPath());
44+
System.out.print(("[%pluginname%] File " + configFile + " has been created!").replace("%pluginname%", plugin.getDescription().getName()));
45+
} else configFile.createNewFile();
46+
}
47+
} catch (final IOException e) {
48+
System.out.print(("[%pluginname%] Unable to create configuration file!").replace("%pluginname%", plugin.getDescription().getName()));
49+
}
50+
}
51+
52+
public void saveConfiguration(final YamlConfiguration yamlConfiguration, final String file) {
53+
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
54+
try {
55+
final File dataFolder = plugin.getDataFolder();
56+
57+
yamlConfiguration.save(file.replace("%datafolder%", dataFolder.toPath().toString()));
58+
} catch (final IOException e) {
59+
System.out.print(("[%pluginname%] Unable to save configuration file!").replace("%pluginname%", plugin.getDescription().getName()));
60+
}
61+
});
62+
}
63+
64+
public void deleteConfiguration(final String file) {
65+
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
66+
final File file1 = new File(file);
67+
68+
if (file1.exists()) file1.delete();
69+
});
70+
}
71+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package twolovers.viarewindpotions.utils;
2+
3+
import org.bukkit.potion.PotionEffectType;
4+
5+
6+
public enum PotionTranslator {
7+
NIGHT_VISION(PotionEffectType.NIGHT_VISION, new TranslationData(16422, 0, 106)),
8+
INVISIBILITY(PotionEffectType.INVISIBILITY, new TranslationData(16430, 0, 106)),
9+
JUMP_BOOST(PotionEffectType.JUMP, new TranslationData(16388, 0, 46), new TranslationData(16388, 48, 106), new TranslationData(16395, 47, 47)),
10+
FIRE_RESISTANCE(PotionEffectType.FIRE_RESISTANCE, new TranslationData(16419, 0, 106)),
11+
SPEED(PotionEffectType.SPEED, new TranslationData(16386, 0, 106)),
12+
SLOWNESS(PotionEffectType.SLOW, new TranslationData(16426, 0, 106)),
13+
TURTLE_MASTER(PotionEffectType.DAMAGE_RESISTANCE, new TranslationData(16424, 0, 106)),
14+
WATER_BREATHING(PotionEffectType.WATER_BREATHING, new TranslationData(16429, 0, 106)),
15+
INSTANT_HEALTH(PotionEffectType.HEAL, new TranslationData(16453, 0, 106)),
16+
INSTANT_DAMAGE(PotionEffectType.HARM, new TranslationData(16460, 0, 106)),
17+
POISON(PotionEffectType.POISON, new TranslationData(16388, 0, 106)),
18+
REGENERATION(PotionEffectType.REGENERATION, new TranslationData(16385, 0, 106)),
19+
STRENGTH(PotionEffectType.INCREASE_DAMAGE, new TranslationData(16393, 0, 106)),
20+
WEAKNESS(PotionEffectType.WEAKNESS, new TranslationData(16424, 0, 106)),
21+
LUCK(PotionEffectType.LUCK, new TranslationData(16388, 0, 106));
22+
23+
private PotionEffectType effect;
24+
private TranslationData[] datas;
25+
26+
PotionTranslator(PotionEffectType effect, TranslationData... datas) {
27+
this.effect = effect;
28+
this.datas = datas;
29+
}
30+
31+
public PotionEffectType getPotionEffectType() {
32+
return effect;
33+
}
34+
35+
public TranslationData[] getDatas() {
36+
return datas;
37+
}
38+
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package twolovers.viarewindpotions.utils;
2+
3+
public enum SplashTranslator {
4+
NIGHT_VISION(2039713, new TranslationData(8356774, 0, 106), new TranslationData(5, 107, 210)),
5+
INVISIBILITY(8356754, new TranslationData(8356778, 0, 106), new TranslationData(7, 107, 210)),
6+
JUMP_BOOST(2293580, new TranslationData(8356772, 0, 46), new TranslationData(8356772, 48, 106), new TranslationData(8356779, 47, 47), new TranslationData(9, 107, 210)),
7+
FIRE_RESISTANCE(14981690, new TranslationData(9643043, 0, 106), new TranslationData(12, 107, 210)),
8+
SPEED(8171462, new TranslationData(8356754, 0, 106), new TranslationData(14, 107, 210)),
9+
SLOWNESS(5926017, new TranslationData(6655210, 0, 106), new TranslationData(17, 107, 210)),
10+
TURTLE_MASTER(7691106, new TranslationData(4738376, 0, 106), new TranslationData(34, 107, 210)),
11+
WATER_BREATHING(3035801, new TranslationData(3035767, 0, 106), new TranslationData(19, 107, 210)),
12+
INSTANT_HEALTH(16262179, new TranslationData(8356757, 0, 106), new TranslationData(21, 107, 210)),
13+
INSTANT_DAMAGE(4393481, new TranslationData(2293580, 0, 106), new TranslationData(23, 107, 210)),
14+
POISON(3381504, new TranslationData(8356772, 0, 106), new TranslationData(25, 107, 210)),
15+
REGENERATION(13458603, new TranslationData(2039713, 0, 106), new TranslationData(28, 107, 210)),
16+
STRENGTH(9643043, new TranslationData(3035801, 0, 106), new TranslationData(31, 107, 210)),
17+
WEAKNESS(4738376, new TranslationData(34, 107, 210)),
18+
LUCK(3381504, new TranslationData(8356772, 0, 106), new TranslationData(36, 107, 210)),
19+
SLOW_FALLING(16773073, new TranslationData(8356754, 0, 106), new TranslationData(14, 107, 210));
20+
21+
private int rgb;
22+
private TranslationData[] datas;
23+
24+
SplashTranslator(int rgb, TranslationData... datas) {
25+
this.rgb = rgb;
26+
this.datas = datas;
27+
}
28+
29+
public int getRGB() {
30+
return rgb;
31+
}
32+
33+
public TranslationData[] getDatas() {
34+
return datas;
35+
}
36+
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package twolovers.viarewindpotions.utils;
2+
3+
public class TranslationData {
4+
private int remap, lowestVersion, highestVersion;
5+
6+
public TranslationData(int remap, int lowestVersion, int highestVersion) {
7+
this.remap = remap;
8+
this.lowestVersion = lowestVersion;
9+
this.highestVersion = highestVersion;
10+
}
11+
12+
public int getRemap() {
13+
return remap;
14+
}
15+
16+
public int getLowestVersion() {
17+
return lowestVersion;
18+
}
19+
20+
public int getHighestVersion() {
21+
return highestVersion;
22+
}
23+
24+
}

0 commit comments

Comments
 (0)