Skip to content

Commit

Permalink
Merge branch 'oraxen:master' into dev/update
Browse files Browse the repository at this point in the history
  • Loading branch information
SrBedrock authored Aug 26, 2024
2 parents cfe3723 + 0fcdb99 commit c985f98
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 74 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ allprojects {
implementation("io.th0rgal:protectionlib:1.6.0")
implementation("com.github.stefvanschie.inventoryframework:IF:0.10.12")
implementation("com.jeff-media:custom-block-data:2.2.2")
implementation("com.jeff_media:MorePersistentDataTypes:2.4.0")
implementation("com.jeff-media:MorePersistentDataTypes:2.4.0")
implementation("com.jeff-media:persistent-data-serializer:1.0")
implementation("org.jetbrains:annotations:24.1.0") { isTransitive = false }
implementation("dev.triumphteam:triumph-gui:3.1.10") { exclude("net.kyori") }
Expand Down Expand Up @@ -147,7 +147,7 @@ tasks {
downloadPlugins {
url("https://ci.dmulloy2.net/job/ProtocolLib/lastSuccessfulBuild/artifact/build/libs/ProtocolLib.jar")
}
minecraftVersion("1.20.4")
minecraftVersion("1.21.1")
}

shadowJar {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/io/th0rgal/oraxen/font/FontEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public void onSignGlyph(final SignChangeEvent event) {
public void onPlayerRename(final InventoryClickEvent event) {
if (!(event.getClickedInventory() instanceof AnvilInventory clickedInv)) return;
if (!Settings.FORMAT_ANVIL.toBool() || manager.useNmsGlyphs() || event.getSlot() != 2) return;
if (VersionUtil.atOrAbove("1.20.5")) return;

Player player = (Player) event.getWhoClicked();
String displayName = clickedInv.getRenameText();
Expand Down
12 changes: 4 additions & 8 deletions core/src/main/java/io/th0rgal/oraxen/items/ItemParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.th0rgal.oraxen.utils.logs.Logs;
import net.kyori.adventure.key.Key;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand Down Expand Up @@ -171,16 +172,11 @@ private void parseDataComponents(ItemBuilder item) {
@SuppressWarnings({"UnstableApiUsage", "unchecked"})
private void parseToolComponent(ItemBuilder item, @NotNull ConfigurationSection toolSection) {
ToolComponent toolComponent = new ItemStack(Material.PAPER).getItemMeta().getTool();
toolComponent.setDamagePerBlock(Math.min(toolSection.getInt("damage_per_block", 1), 0));
toolComponent.setDefaultMiningSpeed(Math.min((float) toolSection.getDouble("default_mining_speed", 1.0), 0f));
toolComponent.setDamagePerBlock(Math.max(toolSection.getInt("damage_per_block", 1), 0));
toolComponent.setDefaultMiningSpeed(Math.max((float) toolSection.getDouble("default_mining_speed", 1.0), 0f));

for (Map<?, ?> ruleEntry : toolSection.getMapList("rules")) {
float speed;
try {
speed = Float.parseFloat(String.valueOf(ruleEntry.get("speed")));
} catch (Exception e) {
speed = 1f;
}
float speed = NumberUtils.toFloat(String.valueOf(ruleEntry.get("speed")), 1f);
boolean correctForDrops = Boolean.parseBoolean(String.valueOf(ruleEntry.get("correct_for_drops")));
Set<Material> materials = new HashSet<>();
Set<Tag<Material>> tags = new HashSet<>();
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/java/io/th0rgal/oraxen/items/ItemUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ public static ItemStack updateItem(ItemStack oldItem) {
}

// On 1.20.5+ we use ItemName which is different from userchanged displaynames
// Thus removing the need for this logic
if (!VersionUtil.atOrAbove("1.20.5")) {

String oldDisplayName = oldMeta.hasDisplayName() ? AdventureUtils.parseLegacy(VersionUtil.isPaperServer() ? AdventureUtils.MINI_MESSAGE.serialize(oldMeta.displayName()) : AdventureUtils.parseLegacy(oldMeta.getDisplayName())) : null;
Expand All @@ -266,6 +265,14 @@ public static ItemStack updateItem(ItemStack oldItem) {
: newMeta.getDisplayName()
: null;
if (originalName != null) itemPdc.set(ORIGINAL_NAME_KEY, DataType.STRING, originalName);
} else { // Set the displayName/customName if it exists on an item before
if (newMeta.hasDisplayName() && !newMeta.getDisplayName().isEmpty()) {
if (VersionUtil.isPaperServer()) itemMeta.displayName(newMeta.displayName());
else itemMeta.setDisplayName(newMeta.getDisplayName());
} else {
if (VersionUtil.isPaperServer()) itemMeta.displayName(oldMeta.displayName());
else itemMeta.setDisplayName(oldMeta.getDisplayName());
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,19 @@ public Entity place(Location location, ItemStack originalItem, Float yaw, BlockF
}

private Location correctedSpawnLocation(Location baseLocation, BlockFace facing) {
Location correctedLocation = BlockHelpers.toCenterBlockLocation(baseLocation);
boolean isWall = hasLimitedPlacing() && limitedPlacing.isWall();
boolean isRoof = hasLimitedPlacing() && limitedPlacing.isRoof();
boolean isFixed = hasDisplayEntityProperties() && displayEntityProperties.getDisplayTransform() == ItemDisplay.ItemDisplayTransform.FIXED;
Location correctedLocation = isFixed && facing == BlockFace.UP ? BlockHelpers.toCenterBlockLocation(baseLocation) : BlockHelpers.toCenterLocation(baseLocation);

if (furnitureType != FurnitureType.DISPLAY_ENTITY || !hasDisplayEntityProperties()) return correctedLocation;
if (displayEntityProperties.getDisplayTransform() != ItemDisplay.ItemDisplayTransform.NONE && !isWall && !isRoof) return correctedLocation;
float scale = displayEntityProperties.hasScale() ? displayEntityProperties.getScale().y() : 1;
// Since roof-furniture need to be more or less flipped, we have to add 0.5 (0.49 or it is "inside" the block above) to the Y coordinate
if (isFixed && isWall && facing.getModY() == 0) correctedLocation.add(-facing.getModX() * (0.49 * scale), 0, -facing.getModZ() * (0.49 * scale));

float hitboxOffset = (hasHitbox() ? hitbox.height : 1) - 1;
double yCorrection = (facing != BlockFace.UP ? (0.5 * scale) : 0);
yCorrection += ((isRoof && facing == BlockFace.DOWN) ? isFixed ? 0.49 : -1 * hitboxOffset : 0);
double yCorrection = ((isRoof && facing == BlockFace.DOWN) ? isFixed ? 0.49 : -1 * hitboxOffset : 0);

return correctedLocation.add(0, yCorrection, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,25 @@ public void onBreakingCustomBlock(final BlockBreakEvent event) {
OraxenBlocks.remove(event.getBlock().getLocation(), event.getPlayer());
}

@EventHandler
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityExplosion(EntityExplodeEvent event) {
ExplosionResult result = event.getExplosionResult();
if (result != ExplosionResult.DESTROY && result != ExplosionResult.DESTROY_WITH_DECAY) return;
if (VersionUtil.atOrAbove("1.21")) {
ExplosionResult result = event.getExplosionResult();
if (result != ExplosionResult.DESTROY && result != ExplosionResult.DESTROY_WITH_DECAY) return;
}
for (Block block : new HashSet<>(event.blockList())) {
if (!OraxenBlocks.isOraxenNoteBlock(block)) continue;
OraxenBlocks.remove(block.getLocation(), null);
event.blockList().remove(block);
}
}

@EventHandler
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockExplosion(BlockExplodeEvent event) {
ExplosionResult result = event.getExplosionResult();
if (result != ExplosionResult.DESTROY && result != ExplosionResult.DESTROY_WITH_DECAY) return;
if (VersionUtil.atOrAbove("1.21")) {
ExplosionResult result = event.getExplosionResult();
if (result != ExplosionResult.DESTROY && result != ExplosionResult.DESTROY_WITH_DECAY) return;
}
for (Block block : new HashSet<>(event.blockList())) {
if (!OraxenBlocks.isOraxenNoteBlock(block)) continue;
OraxenBlocks.remove(block.getLocation(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ public void generate() {
// needs to be ordered, forEach cannot be used
File[] files = packFolder.listFiles();
if (files != null) for (final File folder : files) {
if (folder.isDirectory() && folder.getName().equalsIgnoreCase("assets"))
getAllFiles(folder, output, "");
else if (folder.isDirectory())
getAllFiles(folder, output, "assets/minecraft");
if (!folder.isDirectory()) continue;
getAllFiles(folder, output, folder.getName().matches("models|textures|lang|font|sounds") ? "assets/minecraft" : "");
}

// Convert the global.json within the lang-folder to all languages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.th0rgal.oraxen.OraxenPlugin;
import io.th0rgal.oraxen.config.Message;
import io.th0rgal.oraxen.config.ResourcesManager;
import io.th0rgal.oraxen.config.Settings;
import io.th0rgal.oraxen.recipes.listeners.RecipesBuilderEvents;
import io.th0rgal.oraxen.recipes.listeners.RecipesEventsManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.th0rgal.oraxen.OraxenPlugin;
import io.th0rgal.oraxen.api.OraxenItems;
import io.th0rgal.oraxen.config.Settings;
import io.th0rgal.oraxen.mechanics.provided.misc.misc.MiscMechanic;
import io.th0rgal.oraxen.mechanics.provided.misc.misc.MiscMechanicFactory;
import io.th0rgal.oraxen.recipes.CustomRecipe;
import io.th0rgal.oraxen.utils.InventoryUtils;
import io.th0rgal.oraxen.utils.VersionUtil;
Expand Down Expand Up @@ -71,6 +73,14 @@ public void onCrafted(PrepareItemCraftEvent event) {
boolean containsOraxenItem = Arrays.stream(event.getInventory().getMatrix()).anyMatch(OraxenItems::exists);
if (!containsOraxenItem || recipe == null) return;

if (Arrays.stream(event.getInventory().getMatrix()).anyMatch(item -> {
MiscMechanic mechanic = MiscMechanicFactory.get().getMechanic(item);
return mechanic != null && !mechanic.isAllowedInVanillaRecipes();
})) {
event.getInventory().setResult(null);
return;
}

if (customRecipe == null || whitelistedCraftRecipes.stream().anyMatch(customRecipe::equals) || customRecipe.isValidDyeRecipe()) return;

event.getInventory().setResult(customRecipe.getResult());
Expand Down
80 changes: 32 additions & 48 deletions core/src/main/java/io/th0rgal/oraxen/utils/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,44 @@
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryEvent;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
import org.bukkit.inventory.InventoryView;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class InventoryUtils {

private static final Map<String, Method> methodCache = new HashMap<>();
private static Method getTitleMethod;
private static Method titleMethod;
private static Method topInventoryMethod;
private static Method playerFromViewMethod;

static {
try {
getTitleMethod = InventoryView.class.getDeclaredMethod("getTitle");
} catch (Exception e) {

}
try {
titleMethod = InventoryView.class.getDeclaredMethod("title");
} catch (Exception e) {

}
try {
topInventoryMethod = InventoryView.class.getDeclaredMethod("getTopInventory");
} catch (Exception e) {

}
try {
playerFromViewMethod = InventoryView.class.getDeclaredMethod("getPlayer");
} catch (Exception e) {

}
}

public static Component titleFromView(InventoryEvent event) {
if (VersionUtil.atOrAbove("1.21")) return event.getView().title();
Object view = event.getView();
try {
return (Component) methodCache.computeIfAbsent("title", (title) -> {
try {
Method method = view.getClass().getMethod("title");
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
if (Settings.DEBUG.toBool()) e.printStackTrace();
return null;
}
}).invoke(view);
return (Component) titleMethod.invoke(event.getView());
} catch (Exception e) {
if (Settings.DEBUG.toBool()) e.printStackTrace();
return Component.empty();
Expand All @@ -37,18 +51,8 @@ public static Component titleFromView(InventoryEvent event) {

public static Player playerFromView(InventoryEvent event) {
if (VersionUtil.atOrAbove("1.21")) return (Player) event.getView().getPlayer();
Object view = event.getView();
try {
return (Player) methodCache.computeIfAbsent("getPlayer", (player) -> {
try {
Method method = view.getClass().getMethod("getPlayer");
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
if (Settings.DEBUG.toBool()) e.printStackTrace();
return null;
}
}).invoke(view);
return (Player) playerFromViewMethod.invoke(event.getView());
} catch (Exception e) {
if (Settings.DEBUG.toBool()) e.printStackTrace();
return null;
Expand All @@ -57,18 +61,8 @@ public static Player playerFromView(InventoryEvent event) {

public static String getTitleFromView(InventoryEvent event) {
if (VersionUtil.atOrAbove("1.21")) return event.getView().getTitle();
@NotNull Object view = event.getView();
try {
return (String) methodCache.computeIfAbsent("getTitle", (title) -> {
try {
Method method = view.getClass().getMethod("getTitle");
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
if (Settings.DEBUG.toBool()) e.printStackTrace();
return null;
}
}).invoke(view);
return (String) getTitleMethod.invoke(event.getView());
} catch (Exception e) {
if (Settings.DEBUG.toBool()) e.printStackTrace();
return "";
Expand All @@ -77,18 +71,8 @@ public static String getTitleFromView(InventoryEvent event) {

public static Inventory topInventoryForPlayer(Player player) {
if (VersionUtil.atOrAbove("1.21")) return player.getOpenInventory().getTopInventory();
Object view = player.getOpenInventory();
try {
return (Inventory) methodCache.computeIfAbsent("getTopInventory", (topInv) -> {
try {
Method method = view.getClass().getMethod("getTopInventory");
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
if (Settings.DEBUG.toBool()) e.printStackTrace();
return null;
}
}).invoke(view);
return (Inventory) topInventoryMethod.invoke(player);
} catch (Exception e) {
if (Settings.DEBUG.toBool()) e.printStackTrace();
return player.getInventory();
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pluginVersion=1.180.0
pluginVersion=1.181.0
idofrontVersion=0.21.12

0 comments on commit c985f98

Please sign in to comment.