Files
src
build.gradle
settings.gradle
Code
13 975
src/main/java/com/kodari/traktor/TraktorPlugin.java
package com.kodari.traktor;
import com.cryptomorin.xseries.XEnchantment;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Arrays;
public final class TraktorPlugin extends JavaPlugin {
private NamespacedKey tractorItemKey;
private TractorManager tractorManager;
private RecipeManager recipeManager;
@Override
public void onEnable() {
saveDefaultConfig();
tractorItemKey = new NamespacedKey(this, "tractor_item");
tractorManager = new TractorManager(this);
recipeManager = new RecipeManager(this);
recipeManager.registerRecipe();
tractorManager.load();
TraktorListener listener = new TraktorListener(this);
getServer().getPluginManager().registerEvents(listener, this);
TraktorCommand command = new TraktorCommand(this);
getCommand("traktor").setExecutor(command);
getCommand("traktor").setTabCompleter(command);
getServer().getScheduler().runTaskTimer(this, tractorManager::harvestAll, 20L, 20L);
}
@Override
public void onDisable() {
if (tractorManager != null) {
tractorManager.save();
}
}
public TractorManager getTractorManager() {
return tractorManager;
}
public RecipeManager getRecipeManager() {
return recipeManager;
}
public String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}
public ItemStack createTractorItem() {
Material chest = MaterialUtil.material("CHEST");
ItemStack item = new ItemStack(chest);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(color("&6&lTRAKTOR"));
meta.setLore(Arrays.asList(
color("&8&lIndustrial farming machine"),
color("&7&lHarvests crops and replants them"),
color("&7&lautomatically from its tanks."),
"",
color("&e&lSHIFT + RIGHT CLICK &7<o pick it up"),
color("&8&lEmpty both tanks before pickup."),
"",
color("&6&lBuilt for the fields")
));
XEnchantment.matchXEnchantment("UNBREAKING")
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), 10, true));
meta.getPersistentDataContainer().set(tractorItemKey, PersistentDataType.BYTE, (byte) 1);
item.setItemMeta(meta);
return item;
}
public boolean isTractorItem(ItemStack item) {
if (item == null || !item.hasItemMeta()) {
return false;
}
Byte value = item.getItemMeta().getPersistentDataContainer().get(tractorItemKey, PersistentDataType.BYTE);
return value != null && value == 1;
}
}