Skip to content

Instantly share code, notes, and snippets.

@effective-light
Last active June 19, 2017 21:09
Show Gist options
  • Select an option

  • Save effective-light/2b2520b1d15b9fe4205c989c06296c37 to your computer and use it in GitHub Desktop.

Select an option

Save effective-light/2b2520b1d15b9fe4205c989c06296c37 to your computer and use it in GitHub Desktop.
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package packagename;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.Plugin;
public class ProjectileListener implements Listener
{
private Plugin plugin;
public ProjectileListener(Plugin plugin)
{
this.plugin = plugin;
}
@EventHandler
public void onProjectileLaunch(ProjectileLaunchEvent event)
{
if ( event.getEntity().getShooter() instanceof Player )
{
event.getEntity().setMetadata( "launchLocation",
new FixedMetadataValue( plugin, event.getEntity().getLocation() ) );
}
}
@EventHandler
public void onProjectileHit(ProjectileHitEvent event)
{
if ( event.getHitEntity() != null && event.getEntity().getShooter() instanceof Player )
{
Location launchLocation = (Location) event.getEntity()
.getMetadata( "launchLocation" ).get( 0 ).value();
long distance = Math.round( event.getHitEntity().getLocation().distance( launchLocation ) );
Player player = (Player) event.getEntity().getShooter();
BaseComponent[] components =
new ComponentBuilder( "You hit a " ).color( ChatColor.YELLOW ).bold( true )
.append( event.getHitEntity().getName() ).color( ChatColor.GOLD ).bold( true )
.append( " from " ).color( ChatColor.YELLOW ).bold( true )
.append( String.valueOf( distance ) ).color( ChatColor.RED ).bold( true )
.append( distance == 1 ? " block." : " blocks." ).color( ChatColor.YELLOW ).bold( true )
.create();
for ( BaseComponent component : components )
{
component.setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder( "Using a(n) " ).color( ChatColor.AQUA ).bold( true )
.append( event.getEntity().getName().toLowerCase() + "." ).color( ChatColor.DARK_AQUA )
.bold( true ).create() ) );
}
player.spigot().sendMessage( components );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment