Created
August 16, 2018 13:29
-
-
Save Andrewcpu/444d88047da6aa2c574557faa538ec00 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package io.minelights.lights; | |
public enum ColorMode { | |
MIX((byte)0),OVERRIDE((byte)1); | |
public final byte value; | |
ColorMode(byte b){ | |
this.value = b; | |
} | |
} |
This file contains hidden or 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
package io.minelights.lights; | |
public enum LightEvent { | |
RELEASE((byte)-128), | |
LIGHTNING((byte)-127), | |
EXPLOSION((byte)-126); | |
public final byte point; | |
LightEvent(byte b){ | |
this.point = b; | |
} | |
} |
This file contains hidden or 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
package io.minelights; | |
import io.minelights.lights.ColorMode; | |
import io.minelights.lights.LightEvent; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.buffer.ByteBufUtil; | |
import net.minecraft.server.v1_12_R1.NBTCompressedStreamTools; | |
import net.minecraft.server.v1_12_R1.NBTTagCompound; | |
import net.minecraft.server.v1_12_R1.PacketDataSerializer; | |
import org.bukkit.Bukkit; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.player.AsyncPlayerChatEvent; | |
import org.bukkit.event.player.PlayerJoinEvent; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.plugin.messaging.PluginMessageListener; | |
import java.io.*; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.concurrent.Executors; | |
public class Main extends JavaPlugin implements PluginMessageListener,Listener { | |
private NBTTagCompound currentAmbientLighting = null; | |
public void onEnable(){ | |
getServer().getMessenger().registerIncomingPluginChannel(this, "MINELIGHTS", this); | |
getServer().getMessenger().registerOutgoingPluginChannel(this, "MINELIGHTS"); | |
getServer().getPluginManager().registerEvents(this,this); | |
Bukkit.getScheduler().scheduleSyncRepeatingTask(this,()->{ | |
if(currentAmbientLighting != null){ | |
for(Player player : Bukkit.getOnlinePlayers()){ | |
player.sendPluginMessage(this,"MINELIGHTS",convertNBTToBytes(currentAmbientLighting)); | |
} | |
} | |
},100,100); | |
} | |
public NBTTagCompound createLightEvent(LightEvent event){ | |
NBTTagCompound compound = new NBTTagCompound(); | |
compound.setByte("event",event.point); | |
return compound; | |
} | |
public byte[] convertNBTToBytes(NBTTagCompound tag){ | |
ByteArrayOutputStream data = new ByteArrayOutputStream(); | |
byte[] b = {0}; | |
data.write(b,0,1); | |
try { | |
NBTCompressedStreamTools.a(tag,data); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
byte[] result = data.toByteArray(); | |
return result; | |
} | |
public NBTTagCompound setAmbient(byte r, byte g, byte b, ColorMode colorMode){ | |
NBTTagCompound external = new NBTTagCompound(); | |
NBTTagCompound ambient = new NBTTagCompound(); | |
byte[] byteArray = {r,g,b}; | |
ambient.setByteArray("color",byteArray); | |
ambient.setByte("color", colorMode.value); | |
external.setByteArray("message",convertNBTToBytes(ambient)); | |
this.currentAmbientLighting = external; | |
return external; | |
} | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | |
if(command.getName().equalsIgnoreCase("light")){ | |
if(args.length == 4){ | |
int r = Integer.parseInt(args[0]); | |
int g = Integer.parseInt(args[1]); | |
int b = Integer.parseInt(args[2]); | |
ColorMode colorMode = ColorMode.valueOf(args[3]); | |
setAmbient((byte)r,(byte)g,(byte)b,colorMode); | |
sender.sendMessage("Setting ambient lighting to: {" + r + ", "+ g + ", " + b + "} with color mode: " + colorMode.toString()); | |
return true; | |
} | |
else if(args.length == 1 && args[0].equalsIgnoreCase("release")){ | |
for(Player player : Bukkit.getOnlinePlayers()){ | |
player.sendPluginMessage(this,"MINELIGHTS",convertNBTToBytes(createLightEvent(LightEvent.RELEASE))); | |
} | |
sender.sendMessage("Releasing client lights."); | |
} | |
else{ | |
sender.sendMessage("Incorrect usage."); | |
return true; | |
} | |
} | |
return true; | |
} | |
@Override | |
public void onPluginMessageReceived(String s, Player player, byte[] bytes) { | |
if(!s.equalsIgnoreCase("MINELIGHTS")) return; | |
InputStream inputStream = new ByteArrayInputStream(bytes); | |
try { | |
NBTTagCompound tag = NBTCompressedStreamTools.a(inputStream); | |
Bukkit.broadcastMessage("[" + player.getName() + "] Has sent the following message: " + tag.getString("message")); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@EventHandler | |
public void asyncPlayerChat(AsyncPlayerChatEvent event){ | |
if(event.getPlayer().getName().equalsIgnoreCase("Andrewcpu")){ | |
try { | |
NBTTagCompound tag = new NBTTagCompound(); | |
tag.setString("message", event.getMessage()); | |
ByteArrayOutputStream data = new ByteArrayOutputStream(); | |
NBTCompressedStreamTools.a(tag,data); | |
byte[] result = data.toByteArray(); | |
onPluginMessageReceived("MINELIGHTS",event.getPlayer(),result); | |
// player.sendPluginMessage(this,"MINELIGHTS",result); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@EventHandler | |
public void onJoin(PlayerJoinEvent event){ | |
try { | |
Player player = event.getPlayer(); | |
NBTTagCompound tag = new NBTTagCompound(); | |
tag.setString("message", "Greetings, " + event.getPlayer().getName() + ". This probably works..."); | |
byte[] result = convertNBTToBytes(tag); | |
player.sendPluginMessage(this,"MINELIGHTS",result); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or 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
name: MineLights | |
main: io.minelights.Main | |
version: 1.0 | |
commands: | |
light: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment