Skip to content

Commit 2896f9b

Browse files
committed
Merge branch 'master' of https://github.com/oraxen/oraxen
2 parents 2d2a51c + f7aef81 commit 2896f9b

File tree

25 files changed

+923
-480
lines changed

25 files changed

+923
-480
lines changed

core/src/main/java/io/th0rgal/oraxen/config/Message.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public enum Message {
4343
PLUGIN_UNLOADED("logs.unloaded"),
4444
NO_ARMOR_ITEM("logs.no_armor_item"),
4545
DUPLICATE_ARMOR_COLOR("logs.duplicate_armor_color"),
46+
DATAPACK_GENERATED("logs.datapack_generated"),
4647

4748
// command
4849
COMMAND_HELP("command.help"),
@@ -83,7 +84,6 @@ public enum Message {
8384
this.path = path;
8485
}
8586

86-
8787
public String getPath() {
8888
return path;
8989
}
@@ -94,12 +94,13 @@ public String toString() {
9494
}
9595

9696
public void send(final CommandSender sender, final TagResolver... placeholders) {
97-
if (sender == null) return;
97+
if (sender == null)
98+
return;
9899
String lang = OraxenPlugin.get().getConfigsManager().getLanguage().getString(path);
99-
if (lang == null || lang.isEmpty()) return;
100+
if (lang == null || lang.isEmpty())
101+
return;
100102
OraxenPlugin.get().getAudience().sender(sender).sendMessage(
101-
AdventureUtils.MINI_MESSAGE.deserialize(lang, TagResolver.resolver(placeholders))
102-
);
103+
AdventureUtils.MINI_MESSAGE.deserialize(lang, TagResolver.resolver(placeholders)));
103104
}
104105

105106
@NotNull

core/src/main/java/io/th0rgal/oraxen/items/ItemParser.java

Lines changed: 184 additions & 111 deletions
Large diffs are not rendered by default.

core/src/main/java/io/th0rgal/oraxen/mechanics/provided/gameplay/furniture/jukebox/JukeboxBlock.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,26 @@ public class JukeboxBlock {
1919
private final String permission;
2020
private final double volume;
2121
private final double pitch;
22+
public final String active_stage;
2223

2324
public JukeboxBlock(MechanicFactory factory, ConfigurationSection section) {
2425
this.factory = factory;
2526
this.volume = section.getDouble("volume", 1);
2627
this.pitch = section.getDouble("pitch", 1);
2728
this.permission = section.getString("permission");
29+
this.active_stage = section.getString("active_stage");
2830
}
2931

3032
public String getPlayingSong(Entity baseEntity) {
3133
ItemStack disc = baseEntity.getPersistentDataContainer().get(MUSIC_DISC_KEY, DataType.ITEM_STACK);
32-
if (disc == null) return null;
34+
if (disc == null)
35+
return null;
3336
MusicDiscMechanic mechanic = (MusicDiscMechanic) factory.getMechanic(OraxenItems.getIdByItem(disc));
3437
return (mechanic != null && !mechanic.hasNoSong()) ? mechanic.getSong()
35-
: disc.getType().isRecord() ? disc.getType().toString().toLowerCase(Locale.ROOT).replace("music_disc_", "minecraft:music_disc.") : null;
38+
: disc.getType().isRecord()
39+
? disc.getType().toString().toLowerCase(Locale.ROOT).replace("music_disc_",
40+
"minecraft:music_disc.")
41+
: null;
3642
}
3743

3844
public String getPermission() {

core/src/main/java/io/th0rgal/oraxen/mechanics/provided/gameplay/furniture/jukebox/JukeboxListener.java

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ public void onInsertDisc(OraxenFurnitureInteractEvent event) {
4040
Player player = event.getPlayer();
4141
ItemStack itemStack = player.getInventory().getItemInMainHand();
4242

43-
if (event.getHand() != EquipmentSlot.HAND) return;
43+
if (event.getHand() != EquipmentSlot.HAND)
44+
return;
4445

4546
boolean played = insertAndPlayDisc(baseEntity, itemStack, player);
46-
if (!played) return;
47+
48+
if (!played)
49+
return;
4750
player.swingMainHand();
4851

4952
String displayName = null;
@@ -58,7 +61,8 @@ public void onInsertDisc(OraxenFurnitureInteractEvent event) {
5861
}
5962

6063
if (displayName != null) {
61-
Component message = AdventureUtils.MINI_MESSAGE.deserialize(Message.MECHANICS_JUKEBOX_NOW_PLAYING.toString(), AdventureUtils.tagResolver("disc", displayName));
64+
Component message = AdventureUtils.MINI_MESSAGE.deserialize(
65+
Message.MECHANICS_JUKEBOX_NOW_PLAYING.toString(), AdventureUtils.tagResolver("disc", displayName));
6266
OraxenPlugin.get().getAudience().player(player).sendActionBar(message);
6367
}
6468

@@ -67,7 +71,8 @@ public void onInsertDisc(OraxenFurnitureInteractEvent event) {
6771

6872
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
6973
public void onEjectDisc(OraxenFurnitureInteractEvent event) {
70-
if (!ejectAndStopDisc(event.getBaseEntity(), event.getPlayer())) return;
74+
if (!ejectAndStopDisc(event.getBaseEntity(), event.getPlayer()))
75+
return;
7176
event.getPlayer().swingMainHand();
7277
event.setCancelled(true);
7378
}
@@ -82,16 +87,34 @@ private boolean insertAndPlayDisc(Entity baseEntity, ItemStack disc, @Nullable P
8287
FurnitureMechanic furnitureMechanic = OraxenFurniture.getFurnitureMechanic(baseEntity);
8388
Location loc = BlockHelpers.toCenterLocation(baseEntity.getLocation());
8489

85-
if (furnitureMechanic == null || !furnitureMechanic.isJukebox()) return false;
86-
if (pdc.has(MUSIC_DISC_KEY, DataType.ITEM_STACK) || !ItemUtils.isMusicDisc(disc)) return false;
90+
if (furnitureMechanic == null || !furnitureMechanic.isJukebox())
91+
return false;
92+
93+
if (!ItemUtils.isMusicDisc(disc))
94+
return false;
95+
96+
if (pdc.has(MUSIC_DISC_KEY, DataType.ITEM_STACK))
97+
return false;
98+
8799
JukeboxBlock jukebox = furnitureMechanic.getJukebox();
88-
if (!jukebox.hasPermission(player)) return false;
100+
if (!jukebox.hasPermission(player))
101+
return false;
102+
89103
ItemStack insertedDisc = disc.clone();
90104
insertedDisc.setAmount(1);
91105
if (player != null && player.getGameMode() != GameMode.CREATIVE)
92106
disc.setAmount(disc.getAmount() - insertedDisc.getAmount());
107+
Key songKey = getSongFromDisc(insertedDisc);
93108
pdc.set(MUSIC_DISC_KEY, DataType.ITEM_STACK, insertedDisc);
94-
baseEntity.getWorld().playSound(loc, jukebox.getPlayingSong(baseEntity), SoundCategory.RECORDS, jukebox.getVolume(), jukebox.getPitch());
109+
String soundId = OraxenPlugin.get().getSoundManager().songKeyToSoundId(songKey);
110+
111+
baseEntity.getWorld().playSound(loc, soundId, SoundCategory.RECORDS,
112+
jukebox.getVolume(), jukebox.getPitch());
113+
114+
if (jukebox.active_stage != null) {
115+
FurnitureMechanic.setFurnitureItem(baseEntity, OraxenItems.getItemById(jukebox.active_stage).build());
116+
}
117+
95118
return true;
96119
}
97120

@@ -101,33 +124,44 @@ private boolean ejectAndStopDisc(Entity baseEntity, @Nullable Player player) {
101124
FurnitureMechanic furnitureMechanic = OraxenFurniture.getFurnitureMechanic(baseEntity);
102125
Location loc = BlockHelpers.toCenterLocation(baseEntity.getLocation());
103126

104-
if (furnitureMechanic == null || !furnitureMechanic.isJukebox()) return false;
105-
if (!pdc.has(MUSIC_DISC_KEY, DataType.ITEM_STACK) || !ItemUtils.isMusicDisc(item)) return false;
127+
if (furnitureMechanic == null || !furnitureMechanic.isJukebox())
128+
return false;
129+
if (!pdc.has(MUSIC_DISC_KEY, DataType.ITEM_STACK) || !ItemUtils.isMusicDisc(item))
130+
return false;
106131

107132
JukeboxBlock jukebox = furnitureMechanic.getJukebox();
108-
if (!jukebox.hasPermission(player)) return false;
133+
if (!jukebox.hasPermission(player))
134+
return false;
109135

110136
baseEntity.getWorld().getNearbyEntities(loc, 32, 32, 32).stream()
111137
.filter(entity -> entity instanceof Player)
112138
.map(entity -> (Player) entity)
113139
.forEach(p -> {
114140
Key songKey = getSongFromDisc(item);
115-
if (songKey == null) return;
116-
OraxenPlugin.get().getAudience().player(p).stopSound(Sound.sound(songKey, Sound.Source.RECORD, jukebox.getVolume(), jukebox.getPitch()));
141+
if (songKey == null)
142+
return;
143+
OraxenPlugin.get().getAudience().player(p).stopSound(
144+
Sound.sound(songKey, Sound.Source.RECORD, jukebox.getVolume(), jukebox.getPitch()));
117145
});
118146
baseEntity.getWorld().dropItemNaturally(loc, item);
147+
148+
// if the active stage was not null we need to reset it because it changed
149+
if (jukebox.active_stage != null) {
150+
FurnitureMechanic.setFurnitureItem(baseEntity,
151+
OraxenItems.getItemById(furnitureMechanic.getItemID()).build());
152+
}
153+
119154
pdc.remove(MUSIC_DISC_KEY);
120155
return true;
121156
}
122157

123158
@Nullable
124159
private Key getSongFromDisc(ItemStack disc) {
125-
if (VersionUtil.atOrAbove("1.21")) {
126-
return disc.hasItemMeta() && disc.getItemMeta().hasJukeboxPlayable()
127-
? disc.getItemMeta().getJukeboxPlayable().getSongKey().key()
128-
: null;
160+
if (VersionUtil.atOrAbove("1.21") && disc.hasItemMeta() && disc.getItemMeta().hasJukeboxPlayable()) {
161+
return disc.getItemMeta().getJukeboxPlayable().getSong().key();
129162
} else {
130-
return Key.key("minecraft", "music_disc." + disc.getType().toString().toLowerCase(Locale.ROOT).split("music_disc_")[1]);
163+
return Key.key("minecraft",
164+
"music_disc." + disc.getType().toString().toLowerCase(Locale.ROOT).split("music_disc_")[1]);
131165
}
132166
}
133167
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package io.th0rgal.oraxen.pack.generation;
2+
3+
import io.th0rgal.oraxen.utils.VirtualFile;
4+
import io.th0rgal.oraxen.utils.VersionUtil;
5+
6+
import org.apache.commons.io.FileUtils;
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.World;
9+
import org.bukkit.packs.DataPack;
10+
11+
import com.google.gson.JsonObject;
12+
13+
import net.kyori.adventure.key.Key;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
import java.nio.charset.StandardCharsets;
18+
import java.util.List;
19+
20+
public abstract class OraxenDatapack {
21+
protected static final World defaultWorld = Bukkit.getWorlds().get(0);
22+
protected final File datapackFolder;
23+
protected final JsonObject datapackMeta = new JsonObject();
24+
protected final boolean isFirstInstall;
25+
protected final boolean datapackEnabled;
26+
protected final String name;
27+
28+
protected OraxenDatapack(String name, String description, int packFormat) {
29+
this.datapackFolder = defaultWorld.getWorldFolder().toPath()
30+
.resolve("datapacks/" + name).toFile();
31+
32+
JsonObject data = new JsonObject();
33+
data.addProperty("description", description);
34+
data.addProperty("pack_format", packFormat);
35+
datapackMeta.add("pack", data);
36+
37+
this.name = name;
38+
this.isFirstInstall = isFirstInstall();
39+
this.datapackEnabled = isDatapackEnabled();
40+
}
41+
42+
protected void writeMCMeta() {
43+
try {
44+
File packMeta = datapackFolder.toPath().resolve("pack.mcmeta").toFile();
45+
packMeta.createNewFile();
46+
FileUtils.writeStringToFile(packMeta, datapackMeta.toString(), StandardCharsets.UTF_8);
47+
} catch (IOException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
52+
public void clearOldDataPack() {
53+
try {
54+
FileUtils.deleteDirectory(datapackFolder);
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
60+
protected abstract Key getDatapackKey();
61+
62+
public abstract void generateAssets(List<VirtualFile> output);
63+
64+
protected boolean isFirstInstall() {
65+
return Bukkit.getDataPackManager().getDataPacks().stream()
66+
.filter(d -> d.getKey() != null)
67+
.noneMatch(d -> getDatapackKey().equals(Key.key(d.getKey().toString())));
68+
}
69+
70+
protected boolean isDatapackEnabled() {
71+
for (DataPack dataPack : Bukkit.getDataPackManager().getEnabledDataPacks(defaultWorld)) {
72+
if (dataPack.getKey() == null)
73+
continue;
74+
if (dataPack.getKey().equals(getDatapackKey()))
75+
return true;
76+
}
77+
for (DataPack dataPack : Bukkit.getDataPackManager().getDisabledDataPacks(defaultWorld)) {
78+
if (dataPack.getKey() == null)
79+
continue;
80+
if (dataPack.getKey().equals(getDatapackKey()))
81+
return true;
82+
}
83+
return false;
84+
}
85+
86+
protected void enableDatapack(boolean enabled) {
87+
if (VersionUtil.isPaperServer()) {
88+
Bukkit.getDatapackManager().getPacks().stream()
89+
.filter(d -> d.getName() == this.name)
90+
.findFirst()
91+
.ifPresent(d -> d.setEnabled(enabled));
92+
}
93+
}
94+
95+
public boolean isEnabled() {
96+
return datapackEnabled;
97+
}
98+
99+
public boolean isFirstTime() {
100+
return isFirstInstall;
101+
}
102+
}

core/src/main/java/io/th0rgal/oraxen/pack/generation/ResourcePack.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.th0rgal.oraxen.items.OraxenMeta;
1616
import io.th0rgal.oraxen.pack.upload.UploadManager;
1717
import io.th0rgal.oraxen.sound.CustomSound;
18+
import io.th0rgal.oraxen.sound.JukeboxDatapack;
1819
import io.th0rgal.oraxen.sound.SoundManager;
1920
import io.th0rgal.oraxen.utils.*;
2021
import io.th0rgal.oraxen.utils.customarmor.ComponentArmorModels;
@@ -483,7 +484,10 @@ private void generateSound(List<VirtualFile> output) {
483484
output.remove(soundFile);
484485
}
485486

486-
for (CustomSound sound : handleCustomSoundEntries(soundManager.getCustomSounds())) {
487+
Collection<CustomSound> customSounds = handleCustomSoundEntries(soundManager.getCustomSounds());
488+
489+
// Add all sounds to the sounds.json
490+
for (CustomSound sound : customSounds) {
487491
outputJson.add(sound.getName(), sound.toJson());
488492
}
489493

@@ -494,6 +498,16 @@ private void generateSound(List<VirtualFile> output) {
494498
} catch (IOException e) {
495499
e.printStackTrace();
496500
}
501+
502+
// Initialize JukeboxDatapack with jukebox sounds after processing all sounds
503+
Collection<CustomSound> jukeboxSounds = customSounds.stream()
504+
.filter(CustomSound::isJukeboxSound)
505+
.toList();
506+
if (!jukeboxSounds.isEmpty()) {
507+
JukeboxDatapack jukeboxDatapack = new JukeboxDatapack(jukeboxSounds);
508+
jukeboxDatapack.clearOldDataPack();
509+
jukeboxDatapack.generateAssets(output);
510+
}
497511
}
498512

499513
private Collection<CustomSound> handleCustomSoundEntries(Collection<CustomSound> sounds) {
@@ -622,16 +636,18 @@ private String getZipFilePath(String path, String newFolder) throws IOException
622636

623637
private void handleCustomArmor(List<VirtualFile> output) {
624638
CustomArmorType customArmorType = CustomArmorType.getSetting();
625-
// Clear out old datapacks before generating new ones, in case type changed or
626-
// otherwise
627-
TrimArmorDatapack.clearOldDataPacks();
628639

629640
switch (customArmorType) {
630641
case COMPONENT -> componentArmorModels.generatePackFiles(output);
631-
case TRIMS -> trimArmorDatapack.generateTrimAssets(output);
642+
case TRIMS -> {
643+
if (trimArmorDatapack == null)
644+
trimArmorDatapack = new TrimArmorDatapack();
645+
trimArmorDatapack.clearOldDataPack();
646+
trimArmorDatapack.generateAssets(output);
647+
}
632648
case SHADER -> {
633649
if (Settings.CUSTOM_ARMOR_SHADER_GENERATE_CUSTOM_TEXTURES.toBool()
634-
&& shaderArmorTextures.hasCustomArmors())
650+
&& shaderArmorTextures.hasCustomArmors()) {
635651
try {
636652
String armorPath = "assets/minecraft/textures/models/armor";
637653
output.add(
@@ -644,12 +660,10 @@ private void handleCustomArmor(List<VirtualFile> output) {
644660
} catch (IOException e) {
645661
e.printStackTrace();
646662
}
663+
}
647664
}
648-
}
649-
if (VersionUtil.isPaperServer()) {
650-
Bukkit.getDatapackManager().getPacks().stream()
651-
.filter(d -> d.getName().equals(TrimArmorDatapack.datapackKey.value()))
652-
.findFirst().ifPresent(d -> d.setEnabled(CustomArmorType.getSetting() == CustomArmorType.TRIMS));
665+
default -> {
666+
} // Handle NONE
653667
}
654668
}
655669

0 commit comments

Comments
 (0)