Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Long distance render tnt
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 committed Jan 31, 2016
1 parent a7c6411 commit 00c8824
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/main/java/in/twizmwaz/cardinal/module/ModuleFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import in.twizmwaz.cardinal.module.modules.killReward.KillRewardBuilder;
import in.twizmwaz.cardinal.module.modules.killStreakCount.KillStreakBuilder;
import in.twizmwaz.cardinal.module.modules.kit.KitBuilder;
import in.twizmwaz.cardinal.module.modules.longTntRender.LongTntRenderBuilder;
import in.twizmwaz.cardinal.module.modules.mapNotification.MapNotificationBuilder;
import in.twizmwaz.cardinal.module.modules.match.MatchModuleBuilder;
import in.twizmwaz.cardinal.module.modules.matchTimer.MatchTimerBuilder;
Expand Down Expand Up @@ -171,7 +172,8 @@ private void addBuilders() {
ItemDropBuilder.class,
GuiKeepModuleBuilder.class,
RankModuleBuilder.class,
MultitradeBuilder.class
MultitradeBuilder.class,
LongTntRenderBuilder.class
));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package in.twizmwaz.cardinal.module.modules.longTntRender;

import com.mojang.authlib.GameProfile;
import in.twizmwaz.cardinal.module.TaskedModule;
import in.twizmwaz.cardinal.settings.Setting;
import in.twizmwaz.cardinal.settings.Settings;
import net.minecraft.server.v1_8_R3.DataWatcher;
import net.minecraft.server.v1_8_R3.IChatBaseComponent;
import net.minecraft.server.v1_8_R3.Packet;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityEquipment;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_8_R3.PacketPlayOutNamedEntitySpawn;
import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_8_R3.PacketPlayOutScoreboardTeam;
import net.minecraft.server.v1_8_R3.WorldSettings;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.inventory.ItemStack;

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

public class LongTntRender implements TaskedModule {

public HashMap<TNTPrimed, GameProfile> entityIDs = new HashMap<>();
public HashMap<TNTPrimed, Location> oldLocation = new HashMap<>();
public HashMap<TNTPrimed, List<Player>> viewers = new HashMap<>();
public List<Integer> usedIDs = new ArrayList<>();
public List<TNTPrimed> toAdd = new ArrayList<>();

public Setting setting = Settings.getSettingByName("TntRendering");

@Override
public void unload() {
HandlerList.unregisterAll(this);
}

@Override
public void run() {
for (int i = 0; i < toAdd.size(); i++) {
TNTPrimed tnt = toAdd.get(0);
broadcastPacket(teamPacket(tnt, true));
broadcastPacket(tabListPacket(tnt, true));
toAdd.remove(0);
}
Map<TNTPrimed, Integer> entityIDs2 = (Map<TNTPrimed, Integer>)entityIDs.clone();
for (Map.Entry<TNTPrimed, Integer> entry : entityIDs2.entrySet()) {
TNTPrimed tnt = entry.getKey();
Location old = oldLocation.get(tnt);
Location loc = tnt.getLocation();
if (tnt.isDead()) {
broadcastPacket(tabListPacket(tnt, false));
broadcastPacket(teamPacket(tnt, false));
broadcastPacket(removeFakePlayerPacket(tnt));
for (Player player : Bukkit.getOnlinePlayers()) {
player.sendBlockChange(old, old.getBlock().getType(), old.getBlock().getData());
}
usedIDs.remove(usedIDs.indexOf(Integer.parseInt(getProfileFor(tnt).getName())));
oldLocation.remove(tnt);
viewers.remove(tnt);
entityIDs.remove(tnt);
} else {
for (Player player : Bukkit.getOnlinePlayers()) {
if (loc.distance(player.getLocation()) >= 63.0f) {
if (setting.getValueByPlayer(player).getValue().equalsIgnoreCase("playerhead")) {
if (viewers.get(tnt).contains(player)) {
sendPacket(player, movePacket(tnt));
} else {
createFakePlayerPacket(player, tnt);
viewers.get(tnt).add(player);
}
} else if (setting.getValueByPlayer(player).getValue().equalsIgnoreCase("block")) {
if (viewers.get(tnt).contains(player)) {
player.sendBlockChange(old, old.getBlock().getType(), old.getBlock().getData());
player.sendBlockChange(loc, Material.TNT, (byte) 0);
} else {
player.sendBlockChange(loc, Material.TNT, (byte) 0);
viewers.get(tnt).add(player);
}
} else if (setting.getValueByPlayer(player).getValue().equalsIgnoreCase("none") && viewers.get(tnt).contains(player)) {
if (viewers.get(tnt).contains(player)) {
sendPacket(player, removeFakePlayerPacket(tnt));
player.sendBlockChange(old, old.getBlock().getType(), old.getBlock().getData());
viewers.get(tnt).remove(player);
}
}
} else {
if (viewers.get(tnt).contains(player)) {
sendPacket(player, removeFakePlayerPacket(tnt));
player.sendBlockChange(old, old.getBlock().getType(), old.getBlock().getData());
viewers.get(tnt).remove(player);
}
}
}
oldLocation.put(tnt, loc);
}
}
}

@EventHandler
public void onTntSpawn(ExplosionPrimeEvent event) {
if (event.getEntity() instanceof TNTPrimed) toAdd.add((TNTPrimed) event.getEntity());
}

@EventHandler
public void onTntExplode(EntityExplodeEvent event){
if (!(event.getEntity() instanceof TNTPrimed)) return;
Location actual = event.getLocation();
for (Player player : Bukkit.getOnlinePlayers()) {
if (actual.distance(player.getLocation()) >= 64.0f) player.playEffect(actual, Effect.EXPLOSION_HUGE, 0, 0, 0f, 0f, 0f, 1f, 256, 1);
}
}

public GameProfile getProfileFor(TNTPrimed tnt) {
if (entityIDs.isEmpty()) {
entityIDs.put(tnt, new GameProfile(UUID.randomUUID(), "" + 1000));
viewers.put(tnt, new ArrayList<Player>());
usedIDs.add(1000);
} else if (!entityIDs.containsKey(tnt)){
int i = Collections.max(usedIDs) + 1;
entityIDs.put(tnt, new GameProfile(UUID.randomUUID(), "" + i));
viewers.put(tnt, new ArrayList<Player>());
usedIDs.add(i);
}
return entityIDs.get(tnt);
}

public int getIdFor(TNTPrimed tnt) {
return Integer.MAX_VALUE - Integer.parseInt(getProfileFor(tnt).getName());
}

public DataWatcher createFakePlayerWatcher(Player player) {
DataWatcher data = new DataWatcher(((CraftPlayer)player).getHandle());
data.a(0, (byte) 0x20);
return data;
}
public Packet teamPacket(TNTPrimed tnt, boolean state) {
PacketPlayOutScoreboardTeam teamPacket = new PacketPlayOutScoreboardTeam();

teamPacket.a = "\000TabView" + 80; // team name
teamPacket.b = "\000TabView" + 80; // team display name
teamPacket.c = ""; // team prefix
teamPacket.d = ""; // team suffix
teamPacket.e = "never"; // name tag visibility
teamPacket.f = -1; // color
teamPacket.g = Collections.singletonList(getProfileFor(tnt).getName()); // list of player names (string list)
teamPacket.h = state ? 3 : 4; // action (0 to add team, 3 to add player, 4 to remove player)
teamPacket.i = 0; // allowFriendlyFire() + canSeeFriendlyInvisibles()
return teamPacket;
}

public Packet tabListPacket(TNTPrimed tnt, boolean state) {
PacketPlayOutPlayerInfo listPacket = new PacketPlayOutPlayerInfo();

listPacket.a = state ? PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER : PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER;
List<PacketPlayOutPlayerInfo.PlayerInfoData> dataList = Collections.singletonList(new PacketPlayOutPlayerInfo.PlayerInfoData(getProfileFor(tnt), 0, WorldSettings.EnumGamemode.SURVIVAL, IChatBaseComponent.ChatSerializer.a("{text:\"" + getProfileFor(tnt).getName() + "\"}")));
try {
Field b = listPacket.getClass().getDeclaredField("b");
b.setAccessible(true);
b.set(listPacket, dataList);
} catch (Exception e) {
e.printStackTrace();
}
return listPacket;
}

public void createFakePlayerPacket(Player player, TNTPrimed tnt) {
Location loc = tnt.getLocation();
PacketPlayOutNamedEntitySpawn spawnPacket = new PacketPlayOutNamedEntitySpawn();

spawnPacket.a = getIdFor(tnt);
spawnPacket.b = getProfileFor(tnt).getId();
spawnPacket.c = (int)(loc.getX() * 32); //x
spawnPacket.d = (int)((loc.getY() - 1.2) * 32); //y
spawnPacket.e = (int)(loc.getZ() * 32); //z
spawnPacket.f = 0; // yaw
spawnPacket.g = 0; // pitch
spawnPacket.h = 0; // item in hand
spawnPacket.i = createFakePlayerWatcher(player);// DataWatcher
spawnPacket.j = spawnPacket.i.c(); // List<WatchableObject>, from DataWatcher
DataWatcher.deepCopy(spawnPacket.j); // No idea what this is for, but the constructor for PacketPlayOutNamedEntitySpawn(EntityHuman) does it

sendPacket(player, spawnPacket);

PacketPlayOutEntityEquipment armorPacket = new PacketPlayOutEntityEquipment(getIdFor(tnt), 4, CraftItemStack.asNMSCopy(new ItemStack(Material.TNT)));

sendPacket(player, armorPacket);
}


public Packet movePacket(TNTPrimed tnt) {
Location loc = tnt.getLocation();

PacketPlayOutEntityTeleport movePacket = new PacketPlayOutEntityTeleport();

movePacket.a = getIdFor(tnt);
movePacket.b = (int) (loc.getX() * 32.0D);
movePacket.c = (int) ((loc.getY() - 1.2) * 32.0D);
movePacket.d = (int) (loc.getZ() * 32.0D);
movePacket.e = 0;
movePacket.f = 0;
movePacket.g = false;

return movePacket;
}

public Packet removeFakePlayerPacket(TNTPrimed tnt) {
return new PacketPlayOutEntityDestroy(getIdFor(tnt));
}

public void broadcastPacket(Packet packet) {
for (Player player : Bukkit.getOnlinePlayers()) {
sendPacket(player, packet);
}
}

public void sendPacket(Player player, Packet packet) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package in.twizmwaz.cardinal.module.modules.longTntRender;

import in.twizmwaz.cardinal.Cardinal;
import in.twizmwaz.cardinal.event.MatchStartEvent;
import in.twizmwaz.cardinal.module.Module;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import in.twizmwaz.cardinal.module.TaskedModule;
import in.twizmwaz.cardinal.settings.Setting;
import in.twizmwaz.cardinal.settings.Settings;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;

public class LongTntRenderBlock implements TaskedModule {
public LinkedHashMap<TNTPrimed, Location> oldLocation = new LinkedHashMap<>();
public List<TNTPrimed> toAdd = new ArrayList<>();
boolean running = true;

public Setting setting = Settings.getSettingByName("TntRendering");

public void unload() {
this.running = false;
HandlerList.unregisterAll(this);
}

@Override
public void run() {
for (int i = 0; i < toAdd.size(); i++) {
oldLocation.put(toAdd.get(0), toAdd.get(0).getLocation().add(0, 1, 0));
toAdd.remove(i);
}
HashMap<TNTPrimed, Location> copy = (HashMap<TNTPrimed, Location>)oldLocation.clone();
for (Map.Entry<TNTPrimed, Location> entry : copy.entrySet()) {
if (entry.getKey().isDead()) {
Location old = entry.getValue();
for (Player player : Bukkit.getOnlinePlayers()) {
player.sendBlockChange(old , old.getBlock().getType(), old.getBlock().getData());
}
oldLocation.remove(entry.getKey());
} else {
Location old = entry.getValue();
Location actual = entry.getKey().getLocation();
if (actual != old) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (setting.getValueByPlayer(player).getValue().equalsIgnoreCase("block") && actual.distance(player.getLocation()) >= 63.0f) {
player.sendBlockChange(old , old.getBlock().getType(), old.getBlock().getData());
player.sendBlockChange(actual , Material.TNT, (byte) 0);
} else {
player.sendBlockChange(old , old.getBlock().getType(), old.getBlock().getData());
}
}
}
oldLocation.put(entry.getKey(), actual);
}
}
}

@EventHandler
public void onTntSpawn(ExplosionPrimeEvent event) {
if ((event.getEntity() instanceof TNTPrimed)) {
toAdd.add((TNTPrimed)event.getEntity());
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package in.twizmwaz.cardinal.module.modules.longTntRender;

import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.Module;
import in.twizmwaz.cardinal.module.ModuleBuilder;
import in.twizmwaz.cardinal.module.ModuleCollection;

public class LongTntRenderBuilder implements ModuleBuilder {

@Override
public ModuleCollection<Module> load(Match match) {
ModuleCollection<Module> results = new ModuleCollection<>();
results.add(new LongTntRender());
//results.add(new LongTntRenderBlock());
return results;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;

public class Tnt implements Module {

Expand Down
Loading

0 comments on commit 00c8824

Please sign in to comment.