private static void copyFileUsingFileStreams(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}
File file= new File("plugins/PLUGIN_ORDNER", "messages.yml");
private static void copyFileUsingFileStreams(File src\messages.yml, file)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(src\messages.yml);
output = new FileOutputStream(file);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}
FileOutputStream writer = new FileOutputStream(new File("verzeichnis//messages.yml"));
InputStream out = Mainclass.class.getResourceAsStream("/messages.yml");
byte[] linebuffer = new byte[1024];
int lineLength;
while((lineLength = out.read(linebuffer)) > 0)
{
writer.write(linebuffer, 0, lineLength);
}
out.close();
writer.close();
Ja, das meine ichEr meint wie man eine Datei aus den Ressourcen der .jar rüberkopiert denke ich mal.
package de.mrpyro13.bf_stats;
import java.io.File;
import java.io.IOException;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
public class Listener_Kills_Death implements Listener {
//MAIN IMPORT - Listener
@SuppressWarnings("unused")
private Main inst;
public Listener_Kills_Death(Main plugin) {
this.inst = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
//FILE IMPORT - messages
File messages= new File("plugins/Battelfield_Statistik_Plugin", "messages.yml");
FileConfiguration msg_cfg = YamlConfiguration.loadConfiguration(messages);
//FILE IMPORT - data
File data = new File("plugins/Battelfield_Statistik_Plugin", "data.yml");
FileConfiguration data_cfg = YamlConfiguration.loadConfiguration(data);
@EventHandler
public void onKill(PlayerDeathEvent e) {
if (e.getEntity().getKiller() instanceof Player) {
Player death = e.getEntity();
Player killer = e.getEntity().getKiller();
int verluste = data_cfg.getInt(death.getName() + ".deaths");
int kills = data_cfg.getInt(killer.getName() + ".kills");
verluste++;
kills++;
data_cfg.set(death.getName() + ".deaths", verluste);
data_cfg.set(killer.getName() + ".kills", kills);
try {
data_cfg.save(data);
} catch (IOException e1) {
e1.printStackTrace();
}
String death_message = msg_cfg.getString("messages.system.death_message");
death_message = death_message.replace("{Killer}", killer.getName());
death_message = death_message.replace("{Death}", death.getName());
if (killer.getInventory().getItemInHand().hasItemMeta()) {
death_message = death_message.replace("{Item}", killer.getInventory().getItemInHand().getItemMeta().getDisplayName());
Bukkit.broadcastMessage(death_message);
e.setDeathMessage(null);
} else {
death_message = death_message.replace("{Item}", killer.getInventory().getItemInHand().getType().name());
Bukkit.broadcastMessage(death_message);
e.setDeathMessage(null);
}
String prefix = msg_cfg.getString("messages.global.prefix");
String add_death = msg_cfg.getString("messages.system.add_death");
death.sendMessage(prefix + " " + add_death);
death.playSound(death.getLocation(), Sound.GHAST_SCREAM, 100, 100);
String add_kill = msg_cfg.getString("messages.system.add_kill");
killer.sendMessage(prefix + " " + add_kill);
killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 100, 100);
}
}
//Static Voids
}
package de.mrpyro13.bf_stats;
import java.io.File;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
public class Command_Kills implements CommandExecutor {
//MAIN IMPORT - CommandExecutor
@SuppressWarnings("unused")
private Main inst;
public Command_Kills(Main plugin){
this.inst = plugin;
}
//FILE IMPORT - messages
File messages= new File("plugins/Battelfield_Statistik_Plugin", "messages.yml");
FileConfiguration msg_cfg = YamlConfiguration.loadConfiguration(messages);
//FILE IMPORT - data
File data = new File("plugins/Battelfield_Statistik_Plugin", "data.yml");
FileConfiguration data_cfg = YamlConfiguration.loadConfiguration(data);
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
//SCRIPT ANFANG
if (sender instanceof Player) {
Player p = (Player) sender;
if (p.hasPermission("Battelfield.kills")) {
int kills = data_cfg.getInt(p.getName() + ".kills");
String your_kills = msg_cfg.getString("messages.system.your_kills");
your_kills = your_kills.replace("{Kills}", String.valueOf(kills));
String prefix = msg_cfg.getString("messages.global.prefix");
p.sendMessage(prefix + " " + your_kills);
return true;
} else {
String no_perms = msg_cfg.getString("messages.error.no_perms");
String error_prefix = msg_cfg.getString("messages.error.error_prefix");
p.sendMessage(error_prefix + " " + no_perms);
return true;
}
} else {
String only_for_players = msg_cfg.getString("messages.error.only_for_players");
String error_prefix = msg_cfg.getString("messages.error.error_prefix");
sender.sendMessage(error_prefix + " " + only_for_players);
return true;
}
//SCRIPT ENDE
}
//Static Voids
}
Es wäre sinnvoll, die FileConfiguration der Dateien 1x zu laden, statt die Datei für jede Klasse neu zu laden. Kein Wunder, dass das nicht so klappt, wie es soll. Außerdem musst du nicht immer die Datei speichern, wenn du was geändert hast. Es reicht, wenn man das beim herunterfahren des Servers (evtl. auch in einem bestimmten Intervall, falls der Server nicht richtig heruntergefahren wird), die Daten in die Datei geschrieben werden.
//FILE IMPORT - messages
File messages= new File("plugins/Battelfield_Statistik_Plugin", "messages.yml");
FileConfiguration msg_cfg = YamlConfiguration.loadConfiguration(messages);
//MAIN IMPORT - Listener
private Main inst;
public Listener_Level(Main plugin) {
this.inst = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}