Skip to content

Instantly share code, notes, and snippets.

@sgdc3
Created August 14, 2016 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgdc3/99b01e1455ea94701c21ccf603728893 to your computer and use it in GitHub Desktop.
Save sgdc3/99b01e1455ea94701c21ccf603728893 to your computer and use it in GitHub Desktop.
NoVillagerTrading, requested by foxi69
package com.github.sgdc3.novillagerstrading;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.plugin.java.JavaPlugin;
public final class NoVillagerTrading extends JavaPlugin implements Listener {
private FileConfiguration config;
@Override
public void onEnable() {
saveDefaultConfig();
config = getConfig();
boolean changes = false;
for(Villager.Profession profession : Villager.Profession.values()) {
String name = profession.name().toLowerCase();
if(config.getString("types."+name) == null) {
config.set("types."+name, name);
changes = true;
}
}
if(config.getString("message") == null) {
config.set("message", "&eThe profession of the clicked villager is &a%&e!");
changes = true;
}
if(changes) {
saveConfig();
}
this.getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onEntityInteract(PlayerInteractEntityEvent event) {
Player player = event.getPlayer();
if(player.hasPermission("novillagertrading.bypass")) {
return;
}
Entity entity = event.getRightClicked();
if(!(entity instanceof Villager)) {
return;
}
Villager.Profession profession = ((Villager)entity).getProfession();
String professionName = getConfig().getString("types." + profession.name().toLowerCase());
String message = ChatColor.translateAlternateColorCodes('&', config.getString("message").replace("%", professionName));
player.sendMessage(message);
event.setCancelled(true);
}
}
name: NoVillagerTrading
version: ${project.version}
main: com.github.sgdc3.novillagertrading.NoVillagerTrading
authors: [sgdc3]
description: "Prevent players from trading with villagers and shows the villager's profession."
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.sgdc3</groupId>
<artifactId>novillagertrading</artifactId>
<version>1.0</version>
<name>NoVillagerTrading</name>
<description>Prevent players from trading with villagers.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<version>3.5.1</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.10.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment