Skip to content

Commit 2ecfccc

Browse files
Merge pull request #534 from VolmitSoftware/Development
1.16.4
2 parents dc4ea60 + 588c9d4 commit 2ecfccc

File tree

3 files changed

+124
-46
lines changed

3 files changed

+124
-46
lines changed

src/main/java/com/volmit/adapt/api/advancement/AdvancementManager.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.volmit.adapt.api.skill.Skill;
1111
import com.volmit.adapt.api.world.AdaptPlayer;
1212
import com.volmit.adapt.util.J;
13+
import org.bukkit.Bukkit;
14+
import org.bukkit.entity.Player;
1315

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

3537
public void grant(AdaptPlayer player, String key, boolean toast) {
3638
player.getData().ensureGranted(key);
37-
if (!AdaptConfig.get().isAdvancements() || !enabled.get()) return;
39+
Player p = player.getPlayer();
40+
if (!AdaptConfig.get().isAdvancements() || !enabled.get() || p == null || !p.isOnline()) return;
3841
Advancement advancement = advancements.get(key);
3942
try {
40-
J.s(() -> advancement.grant(player.getPlayer(), true), 5);
43+
J.s(() -> {
44+
if (!p.isOnline()) return;
45+
advancement.grant(player.getPlayer(), true);
46+
}, 5);
4147
} catch (Exception e) {
4248
Adapt.error("Failed to grant advancement " + key);
4349
}
4450

4551
if (toast) {
46-
if (player.getPlayer() != null) {
47-
try {
48-
advancement.displayToastToPlayer(player.getPlayer());
49-
} catch (Exception e) {
50-
Adapt.error("Failed to grant advancement " + key + " Reattaching!");
51-
}
52+
try {
53+
advancement.displayToastToPlayer(p);
54+
} catch (Exception e) {
55+
Adapt.error("Failed to grant advancement " + key + " Reattaching!");
5256
}
5357
}
5458
}

src/main/java/com/volmit/adapt/content/adaptation/architect/ArchitectPlacement.java

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,48 +76,66 @@ public void on(PlayerQuitEvent e) {
7676
totalMap.remove(p);
7777
}
7878

79-
@EventHandler(priority = EventPriority.HIGHEST)
79+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
8080
public void on(BlockPlaceEvent e) {
81-
if (e.isCancelled()) {
82-
return;
83-
}
8481
Player p = e.getPlayer();
8582
SoundPlayer sp = SoundPlayer.of(p);
83+
if (!hasAdaptation(p) || !p.isSneaking())
84+
return;
8685

87-
if (hasAdaptation(p) && !totalMap.isEmpty() && totalMap.get(p) != null && totalMap.get(p).size() > 0) {
88-
ItemStack is = p.getInventory().getItemInMainHand().clone();
89-
ItemStack hand = p.getInventory().getItemInMainHand();
90-
if (p.isSneaking() && is.getType().isBlock()) {
91-
double v = getValue(e.getBlock());
92-
int handSizeAfter = is.getAmount() - totalMap.get(p).size();
93-
if (handSizeAfter >= 0) {
94-
for (Block b : totalMap.get(p).keySet()) { // Block Placer
95-
if (!canBlockPlace(p, b.getLocation())) {
96-
Adapt.verbose("Player " + p.getName() + " doesn't have permission.");
97-
continue;
98-
}
99-
BlockFace face = totalMap.get(p).get(b);
100-
if (b.getWorld().getBlockAt(b.getRelative(face).getLocation()).getType() == Material.AIR) {
101-
if (b.getRelative(face).getLocation() != e.getBlock().getLocation()) {
102-
b.getWorld().setBlockData(b.getRelative(face).getLocation(), b.getBlockData());
103-
getPlayer(p).getData().addStat("blocks.placed", 1);
104-
getPlayer(p).getData().addStat("blocks.placed.value", v);
105-
sp.play(b.getLocation(), Sound.BLOCK_AZALEA_BREAK, 0.4f, 0.25f);
106-
xp(p, 2);
107-
}
108-
}
109-
is.setAmount(is.getAmount() - 1);
110-
hand.setAmount(is.getAmount());
111-
}
112-
totalMap.remove(p);
113-
if (hand.getAmount() > 0) {
114-
runPlayerViewport(getBlockFace(p), p.getTargetBlock(null, 5), p.getInventory().getItemInMainHand().getType(), p);
115-
}
116-
e.setCancelled(true);
117-
} else {
118-
Adapt.messagePlayer(p, C.RED + Localizer.dLocalize("architect", "placement", "lore1") + " " + C.GREEN + totalMap.get(p).size() + C.RED + " " + Localizer.dLocalize("architect", "placement", "lore2"));
119-
}
86+
var blocks = totalMap.get(p);
87+
if (blocks == null || blocks.isEmpty())
88+
return;
89+
90+
ItemStack hand = e.getItemInHand();
91+
if (!hand.getType().isBlock() || blocks.keySet().getFirst().getType() != hand.getType())
92+
return;
93+
94+
double v = getValue(e.getBlock());
95+
Block ignored = blocks.keySet()
96+
.stream()
97+
.filter(b -> b.getRelative(blocks.get(b)).equals(e.getBlock()))
98+
.findFirst()
99+
.orElse(null);
100+
101+
if (hand.getAmount() < blocks.size()) {
102+
Adapt.messagePlayer(p, C.RED + Localizer.dLocalize("architect", "placement", "lore1") + " " + C.GREEN + blocks.size() + C.RED + " " + Localizer.dLocalize("architect", "placement", "lore2"));
103+
return;
104+
}
105+
106+
blocks.remove(ignored);
107+
for (Block b : blocks.keySet()) { // Block Placer
108+
Block relative = b.getRelative(blocks.get(b));
109+
if (!relative.getType().isAir())
110+
continue;
111+
112+
if (!canBlockPlace(p, relative.getLocation())) {
113+
Adapt.verbose("Player " + p.getName() + " doesn't have permission.");
114+
continue;
120115
}
116+
117+
relative.setBlockData(b.getBlockData());
118+
getPlayer(p).getData().addStat("blocks.placed", 1);
119+
getPlayer(p).getData().addStat("blocks.placed.value", v);
120+
sp.play(b.getLocation(), Sound.BLOCK_AZALEA_BREAK, 0.4f, 0.25f);
121+
xp(p, 2);
122+
123+
hand.setAmount(hand.getAmount() - 1);
124+
}
125+
126+
if (ignored != null) {
127+
e.getBlock().setBlockData(ignored.getBlockData());
128+
getPlayer(p).getData().addStat("blocks.placed", 1);
129+
getPlayer(p).getData().addStat("blocks.placed.value", v);
130+
sp.play(ignored.getLocation(), Sound.BLOCK_AZALEA_BREAK, 0.4f, 0.25f);
131+
xp(p, 2);
132+
133+
hand.setAmount(hand.getAmount() - 1);
134+
} else e.setCancelled(true);
135+
136+
totalMap.remove(p);
137+
if (hand.getAmount() > 0) {
138+
runPlayerViewport(getBlockFace(p), p.getTargetBlock(null, 5), p.getInventory().getItemInMainHand().getType(), p);
121139
}
122140
}
123141

src/main/java/com/volmit/adapt/util/reflect/registries/RegistryUtil.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.volmit.adapt.util.reflect.registries;
22

3+
import com.volmit.adapt.util.cache.AtomicCache;
34
import lombok.NonNull;
45
import org.bukkit.Bukkit;
56
import org.bukkit.Keyed;
67
import org.bukkit.NamespacedKey;
78
import org.bukkit.Registry;
9+
import org.jetbrains.annotations.NotNull;
810
import org.jetbrains.annotations.Nullable;
911

1012
import java.lang.reflect.Modifier;
1113
import java.lang.reflect.ParameterizedType;
14+
import java.lang.reflect.Type;
1215
import java.util.Arrays;
1316
import java.util.HashMap;
1417
import java.util.Map;
@@ -18,6 +21,7 @@
1821

1922
@SuppressWarnings("unchecked")
2023
public class RegistryUtil {
24+
private static final AtomicCache<RegistryLookup> registryLookup = new AtomicCache<>();
2125
private static final Map<Class<?>, Map<NamespacedKey, Keyed>> KEYED_REGISTRY = new HashMap<>();
2226
private static final Map<Class<?>, Map<NamespacedKey, Object>> ENUM_REGISTRY = new HashMap<>();
2327
private static final Map<Class<?>, Registry<Keyed>> REGISTRY = new HashMap<>();
@@ -41,7 +45,7 @@ public static <T> T find(@NonNull Class<T> typeClass, @Nullable Lookup<T> lookup
4145
if (keys.length == 0) throw new IllegalArgumentException("Need at least one key");
4246
Registry<Keyed> registry = null;
4347
if (Keyed.class.isAssignableFrom(typeClass)) {
44-
registry = Bukkit.getRegistry(typeClass.asSubclass(Keyed.class));
48+
registry = getRegistry(typeClass.asSubclass(Keyed.class));
4549
}
4650
if (registry == null) {
4751
registry = REGISTRY.computeIfAbsent(typeClass, t -> Arrays.stream(Registry.class.getDeclaredFields())
@@ -148,4 +152,56 @@ static <T> Lookup<T> combine(@NonNull Lookup<T>... lookups) {
148152
};
149153
}
150154
}
155+
156+
@Nullable
157+
private static <T extends Keyed> Registry<T> getRegistry(@NotNull Class<T> type) {
158+
RegistryLookup lookup = registryLookup.aquire(() -> {
159+
RegistryLookup bukkit;
160+
try {
161+
bukkit = Bukkit::getRegistry;
162+
} catch (Throwable ignored) {
163+
bukkit = null;
164+
}
165+
return new DefaultRegistryLookup(bukkit);
166+
});
167+
return lookup.find(type);
168+
}
169+
170+
private interface RegistryLookup {
171+
@Nullable
172+
<T extends Keyed> Registry<T> find(@NonNull Class<T> type);
173+
}
174+
175+
private static class DefaultRegistryLookup implements RegistryLookup {
176+
private final RegistryLookup bukkit;
177+
private final Map<Type, Object> registries;
178+
179+
private DefaultRegistryLookup(RegistryLookup bukkit) {
180+
this.bukkit = bukkit;
181+
registries = Arrays.stream(Registry.class.getDeclaredFields())
182+
.filter(field -> Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers()))
183+
.filter(field -> Registry.class.isAssignableFrom(field.getType()))
184+
.map(field -> {
185+
var type = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
186+
try {
187+
return Map.entry(type, field.get(null));
188+
} catch (Throwable e) {
189+
return null;
190+
}
191+
})
192+
.filter(Objects::nonNull)
193+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a));
194+
}
195+
196+
@Nullable
197+
@Override
198+
public <T extends Keyed> Registry<T> find(@NonNull Class<T> type) {
199+
if (bukkit == null) return (Registry<T>) registries.get(type);
200+
try {
201+
return bukkit.find(type);
202+
} catch (Throwable e) {
203+
return (Registry<T>) registries.get(type);
204+
}
205+
}
206+
}
151207
}

0 commit comments

Comments
 (0)