Skip to content

Commit

Permalink
Merge pull request #3 from ShaneBeeStudios/dev/update
Browse files Browse the repository at this point in the history
Dev/update
  • Loading branch information
ShaneBeee authored Dec 27, 2024
2 parents dac0fbb + 7e7ee47 commit 4d7716f
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 84 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ name: Java CI with Gradle

on:
push:
branches: ['**']
branches: [ '**' ]
pull_request:
branches: ['**']
branches: [ '**' ]

permissions:
contents: read
Expand All @@ -23,17 +23,17 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
- name: Set up JDK 21
uses: actions/setup-java@v4.2.1
with:
java-version: '17'
java-version: '21'
distribution: 'temurin'
- name: Build with Gradle
uses: gradle/gradle-build-action@bd5760595778326ba7f1441bcf7e88b49de61a25 # v2.6.0
uses: gradle/actions/setup-gradle@v3
with:
arguments: build
- name: Upload a Build Artifact
uses: actions/upload-artifact@v3.1.2
uses: actions/upload-artifact@master
with:
# Artifact name
name: VirtualFurnace-Artifact
Expand Down
11 changes: 7 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ plugins {
}

compileJava {
sourceCompatibility = '17'
targetCompatibility = '17'
options.encoding = 'UTF-8'
}

// VirtualFurnace version
version = '1.0.0'
version = '1.1.0'
def oldestJava = 17

repositories {
mavenCentral()
Expand All @@ -28,12 +27,16 @@ repositories {

dependencies {
// Paper
compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT")

// bStats
implementation(group: 'org.bstats', name: 'bstats-bukkit', version: '3.0.2')
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs += ['-source', '' + oldestJava, '-target', '' + oldestJava]
}

build {
tasks.withType(JavaCompile).tap {
configureEach {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/shanebeestudios/vf/VirtualFurnace.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.shanebeestudios.vf.api.VirtualFurnaceAPI;
import com.shanebeestudios.vf.api.recipe.Fuel;
import com.shanebeestudios.vf.api.recipe.FurnaceRecipe;
import com.shanebeestudios.vf.api.task.FurnaceTick;
import com.shanebeestudios.vf.api.util.Util;
import com.shanebeestudios.vf.command.FurnaceCommand;
import org.bukkit.Bukkit;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/shanebeestudios/vf/api/FurnaceListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.shanebeestudios.vf.api.chunk.VirtualChunk;
import com.shanebeestudios.vf.api.event.machine.FurnaceExtractEvent;
import com.shanebeestudios.vf.api.machine.Furnace;
import com.shanebeestudios.vf.api.machine.Machine;
import com.shanebeestudios.vf.api.recipe.Fuel;
import com.shanebeestudios.vf.api.tile.Tile;
import org.bukkit.Chunk;
Expand Down Expand Up @@ -40,10 +41,10 @@ private void onClickFurnace(PlayerInteractEvent event) {
ItemStack hand = event.getItem();
Player player = event.getPlayer();
if (hand != null) {
Furnace furnace = this.furnaceManager.getFurnaceFromItemStack(hand);
if (furnace != null) {
Machine machine = this.furnaceManager.getMachineFromItemStack(hand);
if (machine != null) {
event.setCancelled(true);
furnace.openInventory(player);
machine.openInventory(player);
return;
}
}
Expand Down
136 changes: 99 additions & 37 deletions src/main/java/com/shanebeestudios/vf/api/FurnaceManager.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.shanebeestudios.vf.api;

import com.shanebeestudios.vf.api.machine.Furnace;
import com.shanebeestudios.vf.api.machine.Machine;
import com.shanebeestudios.vf.api.property.FurnaceProperties;
import com.shanebeestudios.vf.api.util.Util;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -18,51 +20,76 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;

/**
* Manager for {@link Furnace Furnaces}
* Manager for {@link Machine Machines}
* <p>You can get an instance of this class from <b>{@link VirtualFurnaceAPI#getFurnaceManager()}</b></p>
*/
@SuppressWarnings({"unused", "UnusedReturnValue"})
public class FurnaceManager {

private static final boolean HAS_GLINT = Util.methodExists(ItemMeta.class, "getEnchantmentGlintOverride");
@SuppressWarnings({"deprecation", "DataFlowIssue"})
private static final @NotNull Enchantment SHARPNESS = Registry.ENCHANTMENT.get(NamespacedKey.minecraft("sharpness"));
@SuppressWarnings({"deprecation", "DataFlowIssue"})
private static final @NotNull Enchantment INFINITY = Registry.ENCHANTMENT.get(NamespacedKey.minecraft("infinity"));

private final VirtualFurnaceAPI virtualFurnaceAPI;
private File furnaceFile;
private FileConfiguration furnaceConfig;
private final Map<UUID, Furnace> furnaceMap;
private File machineFile;
private FileConfiguration machineConfig;
private final Map<UUID, Machine> machineMap;
private final NamespacedKey key;

FurnaceManager(VirtualFurnaceAPI virtualFurnaceAPI) {
this.virtualFurnaceAPI = virtualFurnaceAPI;
this.furnaceMap = new HashMap<>();
this.machineMap = new HashMap<>();
this.key = new NamespacedKey(virtualFurnaceAPI.getJavaPlugin(), "furnaceID");
loadFurnaceConfig();
}

/**
* Get a collection of all {@link Furnace}s
* Get a collection of all {@link Furnace Furnaces}
*
* @return Collection of all furnaces
* @deprecated Use {@link #getAllMachines()} instead
*/
@Deprecated(since = "1.1.0")
public Collection<Furnace> getAllFurnaces() {
return Collections.unmodifiableCollection(this.furnaceMap.values());
List<Furnace> furnaces = new ArrayList<>();
for (Machine value : this.machineMap.values()) {
if (value instanceof Furnace furnace) {
furnaces.add(furnace);
}
}
return furnaces;
}

/**
* Get a {@link Furnace} by ID
* Get a collection of all {@link Machine Machines}
*
* @param uuid ID of furnace to grab
* @return Furnace from ID (null if a furnace with this ID does not exist)
* @return Collection of all machines
*/
public Furnace getByID(@NotNull UUID uuid) {
return this.furnaceMap.get(uuid);
public Collection<Machine> getAllMachines() {
return Collections.unmodifiableCollection(this.machineMap.values());
}

/**
* Get a {@link Machine} by ID
*
* @param uuid ID of machine to grab
* @return Machine from ID (null if a machine with this ID does not exist)
*/
public Machine getByID(@NotNull UUID uuid) {
return this.machineMap.get(uuid);
}

/**
Expand Down Expand Up @@ -116,7 +143,7 @@ public Furnace createFurnace(@NotNull String name, @NotNull FurnaceProperties fu
if (function != null) {
function.accept(furnace);
}
this.furnaceMap.put(furnace.getUniqueID(), furnace);
this.machineMap.put(furnace.getUniqueID(), furnace);
saveFurnace(furnace, true);
return furnace;
}
Expand Down Expand Up @@ -229,12 +256,16 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnacePro
ItemMeta meta = item.getItemMeta();
assert meta != null;
if (glowing) {
if (item.getType() == Material.ARROW) {
meta.addEnchant(Enchantment.DAMAGE_ALL, 1, true);
if (HAS_GLINT) {
meta.setEnchantmentGlintOverride(true);
} else {
meta.addEnchant(Enchantment.ARROW_INFINITE, 1, true);
if (item.getType() == Material.ARROW) {
meta.addEnchant(SHARPNESS, 1, true);
} else {
meta.addEnchant(INFINITY, 1, true);
}
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
Furnace furnace;
if (function == null) {
Expand All @@ -252,8 +283,27 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnacePro
*
* @param itemStack ItemStack to grab furnace from
* @return Furnace if the ItemStack has one assigned to it else null
* @deprecated Use {@link #getMachineFromItemStack(ItemStack)} instead
*/
@Deprecated(since = "1.1.0")
public Furnace getFurnaceFromItemStack(@NotNull ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (meta != null && meta.getPersistentDataContainer().has(this.key, PersistentDataType.STRING)) {
String u = meta.getPersistentDataContainer().get(this.key, PersistentDataType.STRING);
if (u == null) return null;
Machine machine = getByID(UUID.fromString(u));
if (machine instanceof Furnace furnace) return furnace;
}
return null;
}

/**
* Get a {@link Machine} from an {@link ItemStack}
*
* @param itemStack ItemStack to grab machine from
* @return Machine if the ItemStack has one assigned to it else null
*/
public Machine getMachineFromItemStack(@NotNull ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (meta != null && meta.getPersistentDataContainer().has(this.key, PersistentDataType.STRING)) {
String u = meta.getPersistentDataContainer().get(this.key, PersistentDataType.STRING);
Expand All @@ -264,64 +314,76 @@ public Furnace getFurnaceFromItemStack(@NotNull ItemStack itemStack) {
}

private void loadFurnaceConfig() {
if (this.furnaceFile == null) {
this.furnaceFile = new File(this.virtualFurnaceAPI.getJavaPlugin().getDataFolder(), "furnaces.yml");
if (this.machineFile == null) {
this.machineFile = new File(this.virtualFurnaceAPI.getJavaPlugin().getDataFolder(), "furnaces.yml");
}
if (!furnaceFile.exists()) {
if (!machineFile.exists()) {
this.virtualFurnaceAPI.getJavaPlugin().saveResource("furnaces.yml", false);
}
this.furnaceConfig = YamlConfiguration.loadConfiguration(this.furnaceFile);
this.machineConfig = YamlConfiguration.loadConfiguration(this.machineFile);
loadFurnaces();
}

void loadFurnaces() {
ConfigurationSection section = this.furnaceConfig.getConfigurationSection("furnaces");
ConfigurationSection section = this.machineConfig.getConfigurationSection("furnaces");
if (section != null) {
for (String string : section.getKeys(true)) {
if (section.get(string) instanceof Furnace furnace) {
this.furnaceMap.put(UUID.fromString(string), (Furnace) section.get(string));
this.machineMap.put(UUID.fromString(string), furnace);
}
}
}
Util.log("Loaded: &b" + this.furnaceMap.size() + "&7 furnaces");
Util.log("Loaded: &b" + this.machineMap.size() + "&7 furnaces");
}

/**
* Save a furnace to YAML storage
* Save a machine to YAML storage
* <p><b>NOTE:</b> If choosing not to save to file, this change will not take effect
* in the YAML file, this may be useful for saving a large batch and saving file at the
* end of the batch change, use {@link #saveConfig()} to save all changes to file</p>
*
* @param furnace Furnace to save
* @param machine Machine to save
* @param saveToFile Whether to save to file
*/
public void saveFurnace(@NotNull Furnace furnace, boolean saveToFile) {
this.furnaceConfig.set("furnaces." + furnace.getUniqueID(), furnace);
public void saveFurnace(@NotNull Machine machine, boolean saveToFile) {
this.machineConfig.set("furnaces." + machine.getUniqueID(), machine);
if (saveToFile)
saveConfig();
}

/**
* Remove a furnace from YAML storage
* Remove a machine from YAML storage
* <p><b>NOTE:</b> If choosing not to save to file, this change will not take effect
* in the YAML file, this may be useful it removing a large batch and saving file at the
* in the YAML file, this may be useful if removing a large batch and saving file at the
* end of the batch change, use {@link #saveConfig()} to save all changes to file</p>
*
* @param furnace Furnace to remove
* @param machine Machine to remove
* @param saveToFile Whether to save changes to file
*/
public void removeFurnaceFromConfig(@NotNull Furnace furnace, boolean saveToFile) {
this.furnaceConfig.set("furnaces." + furnace.getUniqueID(), null);
public void removeFurnaceFromConfig(@NotNull Machine machine, boolean saveToFile) {
this.machineConfig.set("furnaces." + machine.getUniqueID(), null);
if (saveToFile)
saveConfig();
}

/**
* Remove a {@link Machine}
* <p>This will remove from memory and from file (if saving is true)
*
* @param machine Machine to remove
* @param saveToFile Whether to save to file
*/
public void removeMachine(@NotNull Machine machine, boolean saveToFile) {
this.machineMap.remove(machine.getUniqueID());
removeFurnaceFromConfig(machine, saveToFile);
}

/**
* Save all furnaces to file
*/
public void saveAll() {
for (Furnace furnace : this.furnaceMap.values()) {
saveFurnace(furnace, false);
for (Machine machine : this.machineMap.values()) {
saveFurnace(machine, false);
}
saveConfig();
}
Expand All @@ -332,7 +394,7 @@ public void saveAll() {
@SuppressWarnings("CallToPrintStackTrace")
public void saveConfig() {
try {
furnaceConfig.save(furnaceFile);
machineConfig.save(machineFile);
} catch (ConcurrentModificationException ignore) {
// TODO figure out a proper way to handle this exception and figure out why its happening
} catch (IOException e) {
Expand All @@ -342,7 +404,7 @@ public void saveConfig() {

void shutdown() {
saveAll();
furnaceMap.clear();
machineMap.clear();
}

}
Loading

0 comments on commit 4d7716f

Please sign in to comment.