fix code examples

This commit is contained in:
Andre_601 2021-07-22 12:56:56 +02:00 committed by GitHub
parent e16287405b
commit fa717c34ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,17 +41,17 @@ import me.clip.placeholderapi.expansion.PlaceholderExpansion;
public class SomeExpansion extends PlaceholderExpansion { public class SomeExpansion extends PlaceholderExpansion {
@Override @Override
public String getAuthor(){ public String getAuthor() {
return "someauthor"; return "someauthor";
} }
@Override @Override
public String getIdentifier(){ public String getIdentifier() {
return "example"; return "example";
} }
@Override @Override
public String getVersion(){ public String getVersion() {
return "1.0.0"; return "1.0.0";
} }
} }
@ -105,30 +105,33 @@ import me.clip.placeholderapi.expansion.PlaceholderExpansion;
public class SomeExpansion extends PlaceholderExpansion { public class SomeExpansion extends PlaceholderExpansion {
@Override @Override
public String getAuthor(){ public String getAuthor() {
return "someauthor"; return "someauthor";
} }
@Override @Override
public String getIdentifier(){ public String getIdentifier() {
return "example"; return "example";
} }
@Override @Override
public String getVersion(){ public String getVersion() {
return "1.0.0"; return "1.0.0";
} }
@Override @Override
public String onRequest(OfflinePlayer player, String params){ public String onRequest(OfflinePlayer player, String params) {
if(params.equalsIgnoreCase("name")){ if(params.equalsIgnoreCase("name")) {
return player == null ? null : player.getName(); // "name" requires the player to be valid return player == null ? null : player.getName(); // "name" requires the player to be valid
}
if(params.equalsIgnoreCase("placeholder1")){
if(params.equalsIgnoreCase("placeholder1")) {
return "Placeholder Text 1"; return "Placeholder Text 1";
}
if(params.equalsIgnoreCase("placeholder2")){
if(params.equalsIgnoreCase("placeholder2")) {
return "Placeholder Text 2"; return "Placeholder Text 2";
}
return null; // Placeholder is unknown by the Expansion return null; // Placeholder is unknown by the Expansion
} }
@ -149,7 +152,7 @@ One way to bypass this is to override the `canRegister()` method with the follow
SomePlugin plugin; // This would be the plugin your expansion depends on SomePlugin plugin; // This would be the plugin your expansion depends on
@Override @Override
public boolean canregister(){ public boolean canregister() {
// This sets plugin to the SomePlugin instance you get through the PluginManager // This sets plugin to the SomePlugin instance you get through the PluginManager
return (plugin = (SomePlugin) Bukkit.getPluginManager().getPlugin(getRequiredPlugin())) != null; return (plugin = (SomePlugin) Bukkit.getPluginManager().getPlugin(getRequiredPlugin())) != null;
} }
@ -172,37 +175,39 @@ public class SomeExpansion extends PlaceholderExpansion {
SomePlugin plugin; // This instance is assigned in canRegister() SomePlugin plugin; // This instance is assigned in canRegister()
@Override @Override
public String getAuthor(){ public String getAuthor() {
return "someauthor"; return "someauthor";
} }
@Override @Override
public String getIdentifier(){ public String getIdentifier() {
return "example"; return "example";
} }
@Override @Override
public String getVersion(){ public String getVersion() {
return "1.0.0"; return "1.0.0";
} }
@Override @Override
public String getRequiredPlugin(){ public String getRequiredPlugin() {
return "SomePlugin"; return "SomePlugin";
} }
@Override @Override
public boolean canRegister(){ public boolean canRegister() {
return (plugin = (SomePlugin) Bukkit.getPluginManager().getPlugin(getRequiredPlugin())) != null; return (plugin = (SomePlugin) Bukkit.getPluginManager().getPlugin(getRequiredPlugin())) != null;
} }
@Override @Override
public String onRequest(OfflinePlayer player, String params){ public String onRequest(OfflinePlayer player, String params) {
if(params.equalsIgnoreCase("placeholder1")){ if(params.equalsIgnoreCase("placeholder1")){
return plugin.getConfig().getString("placeholders.placeholder1", "default1"); return plugin.getConfig().getString("placeholders.placeholder1", "default1");
}
if(params.equalsIgnoreCase("placeholder2")){ if(params.equalsIgnoreCase("placeholder2")){
return plugin.getConfig().getString("placeholders.placeholder2", "default2"); return plugin.getConfig().getString("placeholders.placeholder2", "default2");
}
return null; // Placeholder is unknown by the expansion return null; // Placeholder is unknown by the expansion
} }
@ -223,7 +228,7 @@ Here is a small code example of how dependency injection may look:
public class SomeExpansion extends PlaceholderExpansion { public class SomeExpansion extends PlaceholderExpansion {
final SomePlugin plugin; // The instance is created in the constructor and won't be modified, so it can be final final SomePlugin plugin; // The instance is created in the constructor and won't be modified, so it can be final
public SomeExpansion(SomePlugin plugin){ public SomeExpansion(SomePlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
} }
@ -244,37 +249,39 @@ public class SomeExpansion extends PlaceholderExpansion {
private SomePlugin plugin; private SomePlugin plugin;
public SomeExpansion(SomePlugin plugin){ public SomeExpansion(SomePlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@Override @Override
public String getAuthor(){ public String getAuthor() {
return "someauthor"; return "someauthor";
} }
@Override @Override
public String getIdentifier(){ public String getIdentifier() {
return "example"; return "example";
} }
@Override @Override
public String getVersion(){ public String getVersion() {
return "1.0.0"; return "1.0.0";
} }
@Override @Override
public boolean persist(){ public boolean persist() {
return true; // This is required or else PlaceholderAPI will unregister the Expansion on reload return true; // This is required or else PlaceholderAPI will unregister the Expansion on reload
} }
@Override @Override
public String onRequest(OfflinePlayer player, String params){ public String onRequest(OfflinePlayer player, String params) {
if(params.equalsIgnoreCase("placeholder1")){ if(params.equalsIgnoreCase("placeholder1")){
return plugin.getConfig().getString("placeholders.placeholder1", "default1"); return plugin.getConfig().getString("placeholders.placeholder1", "default1");
}
if(params.equalsIgnoreCase("placeholder2")){
if(params.equalsIgnoreCase("placeholder2")) {
return plugin.getConfig().getString("placeholders.placeholder2", "default2"); return plugin.getConfig().getString("placeholders.placeholder2", "default2");
}
return null; // Placeholder is unknown by the Expansion return null; // Placeholder is unknown by the Expansion
} }
@ -291,12 +298,12 @@ package at.helpch.placeholderapi.example
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class SomePlugin extends JavaPlugin{ public class SomePlugin extends JavaPlugin {
@Override @Override
public void onEnable(){ public void onEnable() {
// Small check to make sure that PlaceholderAPI is installed // Small check to make sure that PlaceholderAPI is installed
if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null){ if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
new SomeExpansion(this).register(); new SomeExpansion(this).register();
} }
} }
@ -328,40 +335,41 @@ public class SomeExpansion extends PlaceholderExpansion implements Relational {
SomePlugin plugin; SomePlugin plugin;
public SomeExpansion(SomePlugin plugin){ public SomeExpansion(SomePlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@Override @Override
public String getAuthor(){ public String getAuthor() {
return "someauthor"; return "someauthor";
} }
@Override @Override
public String getIdentifier(){ public String getIdentifier() {
return "example"; return "example";
} }
@Override @Override
public String getVersion(){ public String getVersion() {
return "1.0.0"; return "1.0.0";
} }
@Override @Override
public boolean persist(){ public boolean persist() {
return true; // This is required or else PlaceholderAPI will unregister the Expansion on reload return true; // This is required or else PlaceholderAPI will unregister the Expansion on reload
} }
@Override @Override
public String onPlaceholderRequest(Player one, Player two, String identifier){ public String onPlaceholderRequest(Player one, Player two, String identifier) {
if(one == null || two == null) if(one == null || two == null)
return null; // We require both Players to be online return null; // We require both Players to be online
if(params.equalsIgnoreCase("friend")){ if(params.equalsIgnoreCase("friend")) {
if(plugin.areFriends(one, two)) if(plugin.areFriends(one, two)) {
return ChatColor.GREEN + one.getName() + " and " + two.getName() + " are friends!"; return ChatColor.GREEN + one.getName() + " and " + two.getName() + " are friends!";
else } else {
return ChatColor.GREEN + one.getName() + " and " + two.getName() + " are not friends!"; return ChatColor.GREEN + one.getName() + " and " + two.getName() + " are not friends!";
}
} }
return null; // Placeholder is unknown by the Expansion return null; // Placeholder is unknown by the Expansion