Skip to content

Commit

Permalink
Performance fixes
Browse files Browse the repository at this point in the history
Closes #310
Lorenzo0111 committed Aug 20, 2024
1 parent 879b6bf commit 0e756b1
Showing 4 changed files with 95 additions and 77 deletions.
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ plugins {
}

group = "me.zombie_striker"
version = "2.3.9"
version = "2.3.10-SNAPSHOT"
description = "QualityArmoryVehicles"

java.sourceCompatibility = JavaVersion.VERSION_1_8
@@ -32,19 +32,19 @@ repositories {

dependencies {
// Libraries
implementation("com.github.cryptomorin:XSeries:11.0.0")
implementation("com.github.cryptomorin:XSeries:11.2.1")
implementation("net.jodah:expiringmap:0.5.11")
implementation("org.codemc.worldguardwrapper:worldguardwrapper:1.2.0-SNAPSHOT")
implementation("org.codemc.worldguardwrapper:worldguardwrapper:1.2.1-SNAPSHOT")
implementation("dev.triumphteam:triumph-gui:3.1.10")
compileOnly("org.jetbrains:annotations:24.1.0")

// API
compileOnly("net.kyori:adventure-api:4.17.0")
compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.20.6-R0.1-SNAPSHOT")

// Compatibilities
compileOnly("com.comphenix.protocol:ProtocolLib:5.0.0")
compileOnly("me.zombie_striker:QualityArmory:2.0.13")
compileOnly("me.zombie_striker:QualityArmory:2.0.14.1")
compileOnly("com.github.TownyAdvanced:Towny:0.100.2.0")
compileOnly("net.milkbowl.vault:VaultAPI:1.7")
compileOnly("com.ticxo.modelengine:api:R3.2.0")
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
158 changes: 88 additions & 70 deletions src/main/java/me/zombie_striker/qav/util/BlockCollisionUtil.java
Original file line number Diff line number Diff line change
@@ -1,83 +1,101 @@
package me.zombie_striker.qav.util;

import com.cryptomorin.xseries.reflection.XReflection;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

public class BlockCollisionUtil {
private static final ExpiringMap<Location,Material> CACHE;
private static final HashMap<Material,Double> customBlockHeights = new HashMap<>();

static{
CACHE = ExpiringMap.builder()
.expiration(5, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();

for(Material m : Material.values()){
if(m.name().endsWith("_WALL"))
customBlockHeights.put(m,1.5);
if(m.name().endsWith("_FENCE_GATE")||m.name().endsWith("_FENCE"))
customBlockHeights.put(m,1.5);
if(m.name().endsWith("_BED"))
customBlockHeights.put(m,0.5);
if(m.name().endsWith("_SLAB")||m.name().endsWith("_FENCE"))
customBlockHeights.put(m,0.5);
if(m.name().endsWith("DAYLIGHT_DETECTOR"))
customBlockHeights.put(m,0.4);
if(m.name().endsWith("CARPET"))
customBlockHeights.put(m,0.1);
if(m.name().endsWith("TRAPDOOR"))
customBlockHeights.put(m,0.2);
if(m.name().endsWith("RAIL"))
customBlockHeights.put(m,0.0);
}
}

public static double getHeight(Block b){
Material type = getMaterial(b.getLocation());
if (type == null) return 0;
if (type.name().contains("SLAB") || type.name().contains("STEP")) {
if (b.getData() == 0)
return 0.5;
if (b.getData() == 1)
return 1;
}
if(customBlockHeights.containsKey(type))
return customBlockHeights.get(type);
return type.isSolid()?1:0;
}

public static boolean isSolidAt(Location loc){
Block b = loc.getBlock();
if(b.getLocation().getY()+getHeight(b)>loc.getY())
return true;
Block temp = b.getRelative(0,-1,0);
return temp.getLocation().getY() + getHeight(temp) > loc.getY();
}


public static Material getMaterial(Location location) {
if (CACHE.containsKey(location)) {
return CACHE.get(location);
}

Material material = location.getBlock().getType();
CACHE.put(location,material);
return material;
}

public static boolean isSolid(Location loc) {
return isSolid(getMaterial(loc));
}

public static boolean isSolid(Material material) {
return material.isSolid();
}
private static final ExpiringMap<Location, Material> CACHE;
private static final HashMap<Material, Double> customBlockHeights = new HashMap<>();

static {
CACHE = ExpiringMap.builder()
.expiration(5, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();

for (Material m : Material.values()) {
if (m.name().endsWith("_WALL"))
customBlockHeights.put(m, 1.5);
if (m.name().endsWith("_FENCE_GATE") || m.name().endsWith("_FENCE"))
customBlockHeights.put(m, 1.5);
if (m.name().endsWith("_BED"))
customBlockHeights.put(m, 0.5);
if (m.name().endsWith("_SLAB") || m.name().endsWith("_FENCE"))
customBlockHeights.put(m, 0.5);
if (m.name().endsWith("DAYLIGHT_DETECTOR"))
customBlockHeights.put(m, 0.4);
if (m.name().endsWith("CARPET"))
customBlockHeights.put(m, 0.1);
if (m.name().endsWith("TRAPDOOR"))
customBlockHeights.put(m, 0.2);
if (m.name().endsWith("RAIL"))
customBlockHeights.put(m, 0.0);
}
}

public static double getHeight(Block b) {
Material type = getMaterial(b.getLocation());
if (type == null) return 0;
if (type.name().contains("SLAB") || type.name().contains("STEP")) {
boolean modernCheck = XReflection.supports(13);

if (modernCheck) {
try {
if (b.getBlockData().getAsString().toLowerCase().contains("type=double")) {
return 1;
}

return 0.5;
} catch (Exception ignored) {
modernCheck = false;
}
}

if (!modernCheck) {
if (b.getData() == 0)
return 0.5;
if (b.getData() == 1)
return 1;
}
}

if (customBlockHeights.containsKey(type))
return customBlockHeights.get(type);

return type.isSolid() ? 1 : 0;
}

public static boolean isSolidAt(Location loc) {
Block b = loc.getBlock();
if (b.getLocation().getY() + getHeight(b) > loc.getY())
return true;
Block temp = b.getRelative(0, -1, 0);
return temp.getLocation().getY() + getHeight(temp) > loc.getY();
}


public static Material getMaterial(Location location) {
if (CACHE.containsKey(location)) {
return CACHE.get(location);
}

Material material = location.getBlock().getType();
CACHE.put(location, material);
return material;
}

public static boolean isSolid(Location loc) {
return isSolid(getMaterial(loc));
}

public static boolean isSolid(Material material) {
return material.isSolid();
}
}

0 comments on commit 0e756b1

Please sign in to comment.