Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
Initialize the project
  • Loading branch information
Watch54 authored Jan 12, 2020
1 parent 4d95839 commit 792f976
Show file tree
Hide file tree
Showing 11 changed files with 563 additions and 0 deletions.
4 changes: 4 additions & 0 deletions resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: HologramDisplays
author: Watch54
main: fr.watch54.displays.Plugin
version: 1.0.0
5 changes: 5 additions & 0 deletions src/fr/watch54/displays/Plugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fr.watch54.displays;

import org.bukkit.plugin.java.JavaPlugin;

public class Plugin extends JavaPlugin {}
65 changes: 65 additions & 0 deletions src/fr/watch54/displays/holograms/Hologram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package fr.watch54.displays.holograms;

import fr.watch54.displays.interfaces.Action;
import fr.watch54.displays.interfaces.Text;
import org.bukkit.Location;

import java.util.List;

public abstract class Hologram {

protected List<Text> textList;
protected Location location;
protected boolean spawned;

public Hologram(List<Text> textList, Location location){
this.textList = textList;
this.location = location;
this.spawned = false;

}

public List<Text> getTextList(){
return textList;

}

public void setTextList(List<Text> textList){
this.textList = textList;

}

public Location getLocation(){
return location;

}

public void setLocation(Location location){
this.location = location;

}

public boolean isSpawned(){
return spawned;

}

public void setSpawned(boolean spawned){
this.spawned = spawned;

}

public abstract void display();

public abstract void update();

public abstract void remove();

public abstract void interact(Action action);

public void teleport(Location location){
this.update();

}

}
155 changes: 155 additions & 0 deletions src/fr/watch54/displays/holograms/HologramClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package fr.watch54.displays.holograms;

import fr.watch54.displays.interfaces.Action;
import fr.watch54.displays.interfaces.Text;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import net.minecraft.server.v1_8_R3.EntityArmorStand;
import net.minecraft.server.v1_8_R3.PacketPlayInUseEntity;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy;
import net.minecraft.server.v1_8_R3.PacketPlayOutSpawnEntityLiving;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HologramClient extends Hologram {

private Player player;
private Map<EntityArmorStand, Text> entityArmorStandTextMap;

public HologramClient(Player player, List<Text> textList, Location location){
super(textList, location);
this.player = player;
this.entityArmorStandTextMap = new HashMap<>();

}

@Override
public void display(){

if(player == null) throw new NullPointerException("Player cannot be null!");
if(this.getTextList() == null) throw new NullPointerException("Texts cannot be null!");
if(this.getLocation() == null) throw new NullPointerException("Location cannot be null!");

Location locationClone = this.getLocation().clone();

CraftPlayer craftPlayer = (CraftPlayer) player;
CraftWorld craftWorld = (CraftWorld) locationClone.getWorld();

for(Text text : this.getTextList()){

EntityArmorStand armorStand = new EntityArmorStand(craftWorld.getHandle(), locationClone.getX(), locationClone.getY(), locationClone.getZ());
armorStand.setGravity(false);
armorStand.setInvisible(true);
armorStand.setCustomNameVisible(true);
armorStand.setCustomName(text.getText());
armorStand.fireTicks = Integer.MAX_VALUE;

entityArmorStandTextMap.put(armorStand, text);
locationClone.add(0, -0.3D, 0);

PacketPlayOutSpawnEntityLiving entityLiving = new PacketPlayOutSpawnEntityLiving(armorStand);
craftPlayer.getHandle().playerConnection.sendPacket(entityLiving);

this.setSpawned(true);

}

}

@Override
public void update(){
this.remove();
this.display();

}

@Override
public void remove(){

CraftPlayer craftPlayer = (CraftPlayer) player;

List<EntityArmorStand> armorStandClone = new ArrayList<>(entityArmorStandTextMap.keySet());
for(EntityArmorStand armorStand : armorStandClone){

PacketPlayOutEntityDestroy entityDestroy = new PacketPlayOutEntityDestroy(armorStand.getId());
craftPlayer.getHandle().playerConnection.sendPacket(entityDestroy);
entityArmorStandTextMap.clear();

this.setSpawned(false);

}
}

@Override
public void interact(Action action){

final int[] id = {0};
ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler(){

@Override
public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception {

if(packet instanceof PacketPlayInUseEntity){

PacketPlayInUseEntity useEntity = (PacketPlayInUseEntity) packet;

if (useEntity.a() == PacketPlayInUseEntity.EnumEntityUseAction.ATTACK) return;

Field entityIDField = useEntity.getClass().getDeclaredField("a");
entityIDField.setAccessible(true);

for(EntityArmorStand entityArmorStand : entityArmorStandTextMap.keySet()){

if(entityIDField.get(useEntity).equals(entityArmorStand.getId())){
action.execute(player);
id[0] = entityArmorStand.getId();
break;

}

}

}

super.channelRead(channelHandlerContext, packet);

}

};

ChannelPipeline channelPipeline = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel.pipeline();
channelPipeline.addBefore("packet_handler", player.getName() + "/" + id[0], channelDuplexHandler);

}

@Override
public void teleport(Location location){

this.setLocation(location);
Location locationClone = location.clone();

entityArmorStandTextMap.keySet().forEach(armorStand -> {
armorStand.setPosition(locationClone.getX(), location.getY(), location.getZ());
locationClone.add(0, -0.3D, 0);

});

}

@Override
public void setTextList(List<Text> textList){
this.textList = textList;
this.update();

}

}
99 changes: 99 additions & 0 deletions src/fr/watch54/displays/holograms/HologramServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package fr.watch54.displays.holograms;

import fr.watch54.displays.interfaces.Action;
import fr.watch54.displays.interfaces.Text;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HologramServer extends Hologram {

private Map<ArmorStand, Text> armorStandTextMap;
private Action action;

public HologramServer(List<Text> textList, Location location){
super(textList, location);
this.armorStandTextMap = new HashMap<>();

}

@Override
public void display(){

if(this.getTextList() == null) throw new NullPointerException("Texts cannot be null!");
if(this.getLocation() == null) throw new NullPointerException("Location cannot be null!");

Location locationClone = this.getLocation().clone();
World world = locationClone.getWorld();

for(Text text : this.getTextList()){

ArmorStand armorStand = (ArmorStand) world.spawnEntity(locationClone, EntityType.ARMOR_STAND);
armorStand.setGravity(false);
armorStand.setVisible(false);
armorStand.setCustomNameVisible(true);
armorStand.setCustomName(text.getText());

armorStandTextMap.put(armorStand, text);
locationClone.add(0, -0.3D, 0);

this.setSpawned(true);

}

}

@Override
public void update(){
armorStandTextMap.keySet().forEach(armorStand -> armorStand.setCustomName(armorStandTextMap.get(armorStand).getText()));

}

@Override
public void remove(){
armorStandTextMap.keySet().forEach(Entity::remove);
armorStandTextMap.clear();
this.setSpawned(false);

}

@Override
public void interact(Action action){
this.action = action;

}

@Override
public void teleport(Location location){

this.setLocation(location);
Location locationClone = location.clone();

armorStandTextMap.keySet().forEach(armorStand -> {
armorStand.teleport(locationClone);
locationClone.add(0, -0.3D, 0);

});

}

@Override
public void setTextList(List<Text> textList){
this.textList = textList;
this.remove();
this.display();

}

public Action getAction(){
return action;

}

}
9 changes: 9 additions & 0 deletions src/fr/watch54/displays/interfaces/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.watch54.displays.interfaces;

import org.bukkit.entity.Player;

public interface Action {

void execute(Player player);

}
7 changes: 7 additions & 0 deletions src/fr/watch54/displays/interfaces/Text.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.watch54.displays.interfaces;

public interface Text {

String getText();

}
15 changes: 15 additions & 0 deletions src/fr/watch54/displays/listeners/ChunkUnloadListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.watch54.displays.listeners;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkUnloadEvent;

public class ChunkUnloadListener implements Listener {

@EventHandler
public void onChunkUnload(ChunkUnloadEvent event){
event.setCancelled(true);

}

}
Loading

0 comments on commit 792f976

Please sign in to comment.