Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Nametag applies one tick later. Blank nametags erase names
Browse files Browse the repository at this point in the history
  • Loading branch information
RypoFalem committed Jan 11, 2017
1 parent 0e06fc1 commit 9cacc52
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>io.github.rypofalem.armorstandeditor</groupId>
<artifactId>armorstandeditor</artifactId>
<packaging>jar</packaging>
<version>1.11-18</version>
<version>1.11.2-18</version>
<name>armorstandeditor</name>
<url>http://maven.apache.org</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public void onEnable(){


private void updateConfig(String folder, String config) {
print(getDataFolder() + File.separator + folder + config);
if(!new File(getDataFolder() + File.separator + folder + config).exists()){
saveResource(folder + config, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.bukkit.event.player.*;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector;

//Manages PlayerEditors and Player Events related to editing armorstands
Expand All @@ -63,7 +64,7 @@ public class PlayerEditorManager implements Listener{
double fineMov;
boolean ignoreNextInteract = false;
TickCounter counter;


public PlayerEditorManager(ArmorStandEditorPlugin plugin){
this.plugin = plugin;
Expand Down Expand Up @@ -99,7 +100,7 @@ void onArmorStandInteract(PlayerInteractAtEntityEvent event){
Player player = event.getPlayer();
if(!(event.getRightClicked() instanceof ArmorStand)) return;
if(player.getInventory().getItemInMainHand() == null) return;
ArmorStand as = (ArmorStand)event.getRightClicked();
final ArmorStand as = (ArmorStand)event.getRightClicked();

if(!canEdit(player, as)) return;
if(plugin.isEditTool(player.getInventory().getItemInMainHand())){
Expand All @@ -111,27 +112,44 @@ void onArmorStandInteract(PlayerInteractAtEntityEvent event){

//Attempt rename
if(player.getInventory().getItemInMainHand().getType() == Material.NAME_TAG){
ItemStack nameTag = player.getInventory().getItemInMainHand();
if(nameTag.hasItemMeta() && nameTag.getItemMeta().hasDisplayName()){
as = (ArmorStand)event.getRightClicked();
String name = nameTag.getItemMeta().getDisplayName();
name = name.replace('&', ChatColor.COLOR_CHAR);
if((as.getCustomName() != null && !as.getCustomName().equals(name)) // armorstand has name and that name is not the same as the nametag
|| (as.getCustomName() == null && (!name.equals(""))) ){ // armorstand doesn't have name and nametag is not blank
event.setCancelled(true);
as.setCustomName(name);
as.setCustomNameVisible(true);

if((player.getGameMode() == GameMode.CREATIVE)) return;
ItemStack nameTag = player.getInventory().getItemInMainHand();
final String name;
if(nameTag.getItemMeta().hasDisplayName()){
name = nameTag.getItemMeta().getDisplayName().replace('&', ChatColor.COLOR_CHAR);
} else {
name = null;
}

if(name == null){
as.setCustomName(null);
as.setCustomNameVisible(false);
plugin.print("null");
} else if((as.getCustomName() != null && !as.getCustomName().equals(name)) // armorstand has name and that name is not the same as the nametag
|| (as.getCustomName() == null && (!name.equals(""))) ){ // armorstand doesn't have name and nametag is not blank
event.setCancelled(true);

if((player.getGameMode() != GameMode.CREATIVE)){
if(nameTag.getAmount() > 1){
nameTag.setAmount(nameTag.getAmount() - 1);
}else{
nameTag = new ItemStack(Material.AIR);
}
player.getInventory().setItemInMainHand(nameTag);
}

plugin.print("Preparing to change " + as.getUniqueId().toString());
//minecraft will set the name after this event even if the event is cancelled.
//change it 1 tick later to apply formatting without it being overwritten
Bukkit.getScheduler().runTaskLater(plugin, new Runnable(){
public void run(){
as.setCustomName(name);
plugin.print(as.getUniqueId().toString() + "\nChanged name to " + name);
as.setCustomNameVisible(true);
plugin.print("Set name visible");
}
}, 1);
}
}// end rename
}
}

@EventHandler (priority = EventPriority.LOW, ignoreCancelled=true)
Expand All @@ -157,22 +175,20 @@ ArrayList<ArmorStand> getTargets(Player player){
for(double i = 0; i<RANGE; i+= STEPSIZE){
List<Entity> nearby = (List<Entity>) player.getWorld().getNearbyEntities(eyeLaser, LASERRADIUS, LASERRADIUS, LASERRADIUS);
if(!nearby.isEmpty()){
boolean endLoop = false;
boolean endLaser = false;
for(Entity e : nearby){
if(e instanceof ArmorStand){
if(canEdit(player, (ArmorStand)e)){
armorStands.add((ArmorStand)e);
endLoop = true;
endLaser = true;
}
}
}
if(endLoop) break;

if(endLaser) break;
}
if(eyeLaser.getBlock().getType().isSolid()) break;
eyeLaser.add(STEP);
}

return armorStands;
}

Expand All @@ -187,7 +203,7 @@ boolean canEdit(Player player, ArmorStand as){
try{
plugin.getServer().getPluginManager().callEvent(event);
} catch(IllegalStateException ise){
ise.printStackTrace();
ise.printStackTrace();
ignoreNextInteract = false;
return false; //Something went wrong, don't allow edit just in case
}
Expand All @@ -212,7 +228,7 @@ void applyRightTool(Player player, ArmorStand as){

@EventHandler (priority = EventPriority.LOWEST, ignoreCancelled=false)
void onRightClickTool(PlayerInteractEvent e){
if( !(e.getAction() == Action.LEFT_CLICK_AIR
if( !(e.getAction() == Action.LEFT_CLICK_AIR
|| e.getAction() == Action.RIGHT_CLICK_AIR
|| e.getAction() == Action.LEFT_CLICK_BLOCK
|| e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
Expand Down Expand Up @@ -246,7 +262,7 @@ void onPlayerMenuSelect(InventoryClickEvent e){
e.setCancelled(true);
ItemStack item = e.getCurrentItem();
if(item!= null && item.hasItemMeta() && item.getItemMeta().hasLore()
&& !item.getItemMeta().getLore().isEmpty()
&& !item.getItemMeta().getLore().isEmpty()
&& item.getItemMeta().getLore().get(0).startsWith(Util.encodeHiddenLore("ase"))){
Player player = (Player) e.getWhoClicked();
String command = Util.decodeHiddenLore(item.getItemMeta().getLore().get(0));
Expand Down Expand Up @@ -298,11 +314,11 @@ void removePlayerEditor(UUID uuid){
public ASEHolder getPluginHolder() {
return pluginHolder;
}

public long getTime(){
return counter.ticks;
}

class TickCounter implements Runnable{
long ticks = 0; //I am optimistic
@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ArmorStandEditor
main: io.github.rypofalem.armorstandeditor.ArmorStandEditorPlugin
version: 1.11-0.1.17
version: 1.11.2-18
website: rypofalem.github.io
author: RypoFalem
description: Allows players to edit data of armorstands without any commands.
Expand Down

0 comments on commit 9cacc52

Please sign in to comment.