Skip to content

Commit

Permalink
Merge pull request #534 from VolmitSoftware/Development
Browse files Browse the repository at this point in the history
1.16.4
  • Loading branch information
AvianAeternum authored Feb 6, 2025
2 parents dc4ea60 + 588c9d4 commit 2ecfccc
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.volmit.adapt.api.skill.Skill;
import com.volmit.adapt.api.world.AdaptPlayer;
import com.volmit.adapt.util.J;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -34,21 +36,23 @@ AdvancementTab createAdvancementTab(String namespace) {

public void grant(AdaptPlayer player, String key, boolean toast) {
player.getData().ensureGranted(key);
if (!AdaptConfig.get().isAdvancements() || !enabled.get()) return;
Player p = player.getPlayer();
if (!AdaptConfig.get().isAdvancements() || !enabled.get() || p == null || !p.isOnline()) return;
Advancement advancement = advancements.get(key);
try {
J.s(() -> advancement.grant(player.getPlayer(), true), 5);
J.s(() -> {
if (!p.isOnline()) return;
advancement.grant(player.getPlayer(), true);
}, 5);
} catch (Exception e) {
Adapt.error("Failed to grant advancement " + key);
}

if (toast) {
if (player.getPlayer() != null) {
try {
advancement.displayToastToPlayer(player.getPlayer());
} catch (Exception e) {
Adapt.error("Failed to grant advancement " + key + " Reattaching!");
}
try {
advancement.displayToastToPlayer(p);
} catch (Exception e) {
Adapt.error("Failed to grant advancement " + key + " Reattaching!");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,48 +76,66 @@ public void on(PlayerQuitEvent e) {
totalMap.remove(p);
}

@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void on(BlockPlaceEvent e) {
if (e.isCancelled()) {
return;
}
Player p = e.getPlayer();
SoundPlayer sp = SoundPlayer.of(p);
if (!hasAdaptation(p) || !p.isSneaking())
return;

if (hasAdaptation(p) && !totalMap.isEmpty() && totalMap.get(p) != null && totalMap.get(p).size() > 0) {
ItemStack is = p.getInventory().getItemInMainHand().clone();
ItemStack hand = p.getInventory().getItemInMainHand();
if (p.isSneaking() && is.getType().isBlock()) {
double v = getValue(e.getBlock());
int handSizeAfter = is.getAmount() - totalMap.get(p).size();
if (handSizeAfter >= 0) {
for (Block b : totalMap.get(p).keySet()) { // Block Placer
if (!canBlockPlace(p, b.getLocation())) {
Adapt.verbose("Player " + p.getName() + " doesn't have permission.");
continue;
}
BlockFace face = totalMap.get(p).get(b);
if (b.getWorld().getBlockAt(b.getRelative(face).getLocation()).getType() == Material.AIR) {
if (b.getRelative(face).getLocation() != e.getBlock().getLocation()) {
b.getWorld().setBlockData(b.getRelative(face).getLocation(), b.getBlockData());
getPlayer(p).getData().addStat("blocks.placed", 1);
getPlayer(p).getData().addStat("blocks.placed.value", v);
sp.play(b.getLocation(), Sound.BLOCK_AZALEA_BREAK, 0.4f, 0.25f);
xp(p, 2);
}
}
is.setAmount(is.getAmount() - 1);
hand.setAmount(is.getAmount());
}
totalMap.remove(p);
if (hand.getAmount() > 0) {
runPlayerViewport(getBlockFace(p), p.getTargetBlock(null, 5), p.getInventory().getItemInMainHand().getType(), p);
}
e.setCancelled(true);
} else {
Adapt.messagePlayer(p, C.RED + Localizer.dLocalize("architect", "placement", "lore1") + " " + C.GREEN + totalMap.get(p).size() + C.RED + " " + Localizer.dLocalize("architect", "placement", "lore2"));
}
var blocks = totalMap.get(p);
if (blocks == null || blocks.isEmpty())
return;

ItemStack hand = e.getItemInHand();
if (!hand.getType().isBlock() || blocks.keySet().getFirst().getType() != hand.getType())
return;

double v = getValue(e.getBlock());
Block ignored = blocks.keySet()
.stream()
.filter(b -> b.getRelative(blocks.get(b)).equals(e.getBlock()))
.findFirst()
.orElse(null);

if (hand.getAmount() < blocks.size()) {
Adapt.messagePlayer(p, C.RED + Localizer.dLocalize("architect", "placement", "lore1") + " " + C.GREEN + blocks.size() + C.RED + " " + Localizer.dLocalize("architect", "placement", "lore2"));
return;
}

blocks.remove(ignored);
for (Block b : blocks.keySet()) { // Block Placer
Block relative = b.getRelative(blocks.get(b));
if (!relative.getType().isAir())
continue;

if (!canBlockPlace(p, relative.getLocation())) {
Adapt.verbose("Player " + p.getName() + " doesn't have permission.");
continue;
}

relative.setBlockData(b.getBlockData());
getPlayer(p).getData().addStat("blocks.placed", 1);
getPlayer(p).getData().addStat("blocks.placed.value", v);
sp.play(b.getLocation(), Sound.BLOCK_AZALEA_BREAK, 0.4f, 0.25f);
xp(p, 2);

hand.setAmount(hand.getAmount() - 1);
}

if (ignored != null) {
e.getBlock().setBlockData(ignored.getBlockData());
getPlayer(p).getData().addStat("blocks.placed", 1);
getPlayer(p).getData().addStat("blocks.placed.value", v);
sp.play(ignored.getLocation(), Sound.BLOCK_AZALEA_BREAK, 0.4f, 0.25f);
xp(p, 2);

hand.setAmount(hand.getAmount() - 1);
} else e.setCancelled(true);

totalMap.remove(p);
if (hand.getAmount() > 0) {
runPlayerViewport(getBlockFace(p), p.getTargetBlock(null, 5), p.getInventory().getItemInMainHand().getType(), p);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.volmit.adapt.util.reflect.registries;

import com.volmit.adapt.util.cache.AtomicCache;
import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -18,6 +21,7 @@

@SuppressWarnings("unchecked")
public class RegistryUtil {
private static final AtomicCache<RegistryLookup> registryLookup = new AtomicCache<>();
private static final Map<Class<?>, Map<NamespacedKey, Keyed>> KEYED_REGISTRY = new HashMap<>();
private static final Map<Class<?>, Map<NamespacedKey, Object>> ENUM_REGISTRY = new HashMap<>();
private static final Map<Class<?>, Registry<Keyed>> REGISTRY = new HashMap<>();
Expand All @@ -41,7 +45,7 @@ public static <T> T find(@NonNull Class<T> typeClass, @Nullable Lookup<T> lookup
if (keys.length == 0) throw new IllegalArgumentException("Need at least one key");
Registry<Keyed> registry = null;
if (Keyed.class.isAssignableFrom(typeClass)) {
registry = Bukkit.getRegistry(typeClass.asSubclass(Keyed.class));
registry = getRegistry(typeClass.asSubclass(Keyed.class));
}
if (registry == null) {
registry = REGISTRY.computeIfAbsent(typeClass, t -> Arrays.stream(Registry.class.getDeclaredFields())
Expand Down Expand Up @@ -148,4 +152,56 @@ static <T> Lookup<T> combine(@NonNull Lookup<T>... lookups) {
};
}
}

@Nullable
private static <T extends Keyed> Registry<T> getRegistry(@NotNull Class<T> type) {
RegistryLookup lookup = registryLookup.aquire(() -> {
RegistryLookup bukkit;
try {
bukkit = Bukkit::getRegistry;
} catch (Throwable ignored) {
bukkit = null;
}
return new DefaultRegistryLookup(bukkit);
});
return lookup.find(type);
}

private interface RegistryLookup {
@Nullable
<T extends Keyed> Registry<T> find(@NonNull Class<T> type);
}

private static class DefaultRegistryLookup implements RegistryLookup {
private final RegistryLookup bukkit;
private final Map<Type, Object> registries;

private DefaultRegistryLookup(RegistryLookup bukkit) {
this.bukkit = bukkit;
registries = Arrays.stream(Registry.class.getDeclaredFields())
.filter(field -> Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers()))
.filter(field -> Registry.class.isAssignableFrom(field.getType()))
.map(field -> {
var type = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
try {
return Map.entry(type, field.get(null));
} catch (Throwable e) {
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a));
}

@Nullable
@Override
public <T extends Keyed> Registry<T> find(@NonNull Class<T> type) {
if (bukkit == null) return (Registry<T>) registries.get(type);
try {
return bukkit.find(type);
} catch (Throwable e) {
return (Registry<T>) registries.get(type);
}
}
}
}

0 comments on commit 2ecfccc

Please sign in to comment.