96 lines
4.8 KiB
Java
96 lines
4.8 KiB
Java
package com.dunerealms.gui;
|
|
|
|
import java.io.File;
|
|
import java.io.InputStreamReader;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.HashMap;
|
|
import java.util.HexFormat;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import org.bukkit.Bukkit;
|
|
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.player.PlayerChangedWorldEvent;
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
|
import org.bukkit.event.player.PlayerResourcePackStatusEvent;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
/** One replaceable vanilla-material layer. Never clears GUI or Dune artwork packs. */
|
|
final class PlanetTexturePacks implements Listener {
|
|
static final UUID PACK_ID=PlanetMaterialPolicy.PACK_ID;
|
|
private final JavaPlugin plugin;
|
|
private final Map<String,Pack> packs=new HashMap<>();
|
|
private final Map<String,String> worlds=new HashMap<>();
|
|
private final Map<UUID,String> offered=new HashMap<>();
|
|
private final boolean enabled;
|
|
record Pack(String url,String hash,String name){}
|
|
|
|
PlanetTexturePacks(JavaPlugin plugin){
|
|
this.plugin=plugin;
|
|
File file=new File(plugin.getDataFolder(),"planet-textures.yml");
|
|
if(!file.exists())plugin.saveResource("planet-textures.yml",false);
|
|
var config=YamlConfiguration.loadConfiguration(file);
|
|
var bundled=YamlConfiguration.loadConfiguration(new InputStreamReader(
|
|
java.util.Objects.requireNonNull(plugin.getResource("planet-textures.yml")),StandardCharsets.UTF_8));
|
|
boolean valid=true;
|
|
try{
|
|
var section=config.getConfigurationSection("packs");
|
|
if(section==null)throw new IllegalArgumentException("Missing packs");
|
|
for(String key:section.getKeys(false)){
|
|
String url=section.getString(key+".url",""),hash=section.getString(key+".sha1",""),name=section.getString(key+".name",key);
|
|
PlanetMaterialPolicy.validate(key,url,hash);
|
|
// Upgrade only exactly identified old release pins, retaining custom profiles,
|
|
// custom world aliases and the administrator's enabled flag.
|
|
String prefix="packs."+key+".";
|
|
if(bundled.getStringList(prefix+"previous-pins").contains(url+"|"+hash)){
|
|
url=bundled.getString(prefix+"url","");hash=bundled.getString(prefix+"sha1","");
|
|
PlanetMaterialPolicy.validate(key,url,hash);
|
|
}
|
|
packs.put(key,new Pack(url,hash,name));
|
|
for(String alias:section.getStringList(key+".worlds")){
|
|
String previous=worlds.put(alias.toLowerCase(Locale.ROOT),key);
|
|
if(previous!=null&&!previous.equals(key))throw new IllegalArgumentException("Duplicate world alias");
|
|
}
|
|
}
|
|
// Exact per-server names can be configured without changing the planet catalogue.
|
|
var aliases=config.getConfigurationSection("worlds");
|
|
if(aliases!=null)for(String name:aliases.getKeys(false)){
|
|
String key=aliases.getString(name,"");if(!packs.containsKey(key))throw new IllegalArgumentException("Unknown planet alias");
|
|
worlds.put(name.toLowerCase(Locale.ROOT),key);
|
|
}
|
|
}catch(IllegalArgumentException invalid){valid=false;plugin.getLogger().warning("Planet material packs disabled: invalid planet-textures.yml");}
|
|
enabled=valid&&config.getBoolean("enabled",false);
|
|
Bukkit.getPluginManager().registerEvents(this,plugin);
|
|
Bukkit.getScheduler().runTaskLater(plugin,()->Bukkit.getOnlinePlayers().forEach(this::offer),40L);
|
|
}
|
|
|
|
private void offer(Player player){
|
|
if(!player.isOnline()||BedrockForms.isBedrock(player))return;
|
|
String profile=enabled?PlanetMaterialPolicy.profile(player.getWorld().getName(),worlds):null;
|
|
String desired=profile==null?"none":profile;
|
|
if(desired.equals(offered.get(player.getUniqueId())))return;
|
|
player.removeResourcePack(PACK_ID);
|
|
if(profile!=null){
|
|
Pack pack=packs.get(profile);
|
|
player.addResourcePack(PACK_ID,pack.url(),HexFormat.of().parseHex(pack.hash()),"DuneRealms · "+pack.name()+" materials",false);
|
|
}
|
|
offered.put(player.getUniqueId(),desired);
|
|
}
|
|
@EventHandler public void join(PlayerJoinEvent event){
|
|
Player player=event.getPlayer();
|
|
Bukkit.getScheduler().runTaskLater(plugin,()->offer(player),40L);
|
|
}
|
|
@EventHandler public void world(PlayerChangedWorldEvent event){offer(event.getPlayer());}
|
|
@EventHandler public void quit(PlayerQuitEvent event){offered.remove(event.getPlayer().getUniqueId());}
|
|
@EventHandler public void status(PlayerResourcePackStatusEvent event){
|
|
if(!PACK_ID.equals(event.getID()))return;
|
|
if(event.getStatus()==PlayerResourcePackStatusEvent.Status.FAILED_DOWNLOAD||event.getStatus()==PlayerResourcePackStatusEvent.Status.FAILED_RELOAD||event.getStatus()==PlayerResourcePackStatusEvent.Status.INVALID_URL){
|
|
offered.remove(event.getPlayer().getUniqueId());
|
|
plugin.getLogger().warning("Planet material pack failed for "+event.getPlayer().getUniqueId()+": "+event.getStatus());
|
|
}
|
|
}
|
|
}
|

Staff