small restructure

This commit is contained in:
xGinko 2023-09-07 22:19:02 +02:00
parent 4d01238f4f
commit 2a9322d0a2
9 changed files with 23 additions and 25 deletions

View File

@ -96,7 +96,7 @@ public final class VillagerOptimizer extends JavaPlugin {
return OptimizationType.WORKSTATION;
}
}
return villagerCache.get(villager).getOptimizationType();
return villagerCache.getOrAdd(villager).getOptimizationType();
}
public void reloadPlugin() {

View File

@ -28,7 +28,7 @@ public class VillagerCache {
return wrappedVillager == null && Bukkit.getEntity(uuid) instanceof Villager villager ? add(villager) : wrappedVillager;
}
public @NotNull WrappedVillager get(@NotNull Villager villager) {
public @NotNull WrappedVillager getOrAdd(@NotNull Villager villager) {
WrappedVillager wrappedVillager = villagerCache.getIfPresent(villager.getUniqueId());
return wrappedVillager == null ? add(new WrappedVillager(villager)) : add(wrappedVillager);
}

View File

@ -23,14 +23,14 @@ public final class WrappedVillager {
}
public static @NotNull WrappedVillager fromCache(Villager villager) {
return VillagerOptimizer.getVillagerCache().get(villager);
return VillagerOptimizer.getVillagerCache().getOrAdd(villager);
}
public boolean isOptimized() {
return villagerData.has(Keys.OPTIMIZED.key());
}
public @NotNull OptimizationType computePossibleOptimization() {
public @NotNull OptimizationType computeOptimization() {
return VillagerOptimizer.computeOptimization(this);
}

View File

@ -63,7 +63,7 @@ public class BlockOptimization implements VillagerOptimizerModule, Listener {
placed.getRelative(BlockFace.UP).getLocation().getNearbyEntities(0.5,0.5,0.5).forEach(entity -> {
if (entity.getType().equals(EntityType.VILLAGER)) {
WrappedVillager wVillager = cache.get((Villager) entity);
WrappedVillager wVillager = cache.getOrAdd((Villager) entity);
if (!wVillager.isOptimized()) {
if (wVillager.setOptimization(OptimizationType.BLOCK)) {
if (shouldNotifyPlayer) {
@ -91,7 +91,7 @@ public class BlockOptimization implements VillagerOptimizerModule, Listener {
broken.getRelative(BlockFace.UP).getLocation().getNearbyEntities(0.5,0.5,0.5).forEach(entity -> {
if (entity.getType().equals(EntityType.VILLAGER)) {
WrappedVillager wVillager = cache.get((Villager) entity);
WrappedVillager wVillager = cache.getOrAdd((Villager) entity);
if (wVillager.getOptimizationType().equals(OptimizationType.BLOCK)) {
wVillager.setOptimization(OptimizationType.OFF);
if (shouldNotifyPlayer) {
@ -110,7 +110,7 @@ public class BlockOptimization implements VillagerOptimizerModule, Listener {
Entity interacted = event.getRightClicked();
if (!interacted.getType().equals(EntityType.VILLAGER)) return;
WrappedVillager wVillager = cache.get((Villager) interacted);
WrappedVillager wVillager = cache.getOrAdd((Villager) interacted);
final Location entityLegs = interacted.getLocation();
if (

View File

@ -60,7 +60,7 @@ public class NametagOptimization implements VillagerOptimizerModule, Listener {
if (name == null) return;
final String nameTag = PlainTextComponentSerializer.plainText().serialize(name);
WrappedVillager wVillager = cache.get((Villager) event.getEntity());
WrappedVillager wVillager = cache.getOrAdd((Villager) event.getEntity());
if (config.nametags.contains(nameTag.toLowerCase())) {
if (!wVillager.isOptimized()) {

View File

@ -11,11 +11,11 @@ import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
public class AntiVillagerDamage implements VillagerOptimizerModule, Listener {
public class PreventVillagerDamage implements VillagerOptimizerModule, Listener {
private final VillagerCache cache;
protected AntiVillagerDamage() {
protected PreventVillagerDamage() {
this.cache = VillagerOptimizer.getVillagerCache();
}
@ -38,7 +38,7 @@ public class AntiVillagerDamage implements VillagerOptimizerModule, Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onDamageReceive(EntityDamageEvent event) {
if (!event.getEntityType().equals(EntityType.VILLAGER)) return;
if (cache.get((Villager) event.getEntity()).isOptimized()) {
if (cache.getOrAdd((Villager) event.getEntity()).isOptimized()) {
event.setCancelled(true);
}
}
@ -46,7 +46,7 @@ public class AntiVillagerDamage implements VillagerOptimizerModule, Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPushByEntityAttack(EntityPushedByEntityAttackEvent event) {
if (!event.getEntityType().equals(EntityType.VILLAGER)) return;
if (cache.get((Villager) event.getEntity()).isOptimized()) {
if (cache.getOrAdd((Villager) event.getEntity()).isOptimized()) {
event.setCancelled(true);
}
}

View File

@ -13,11 +13,11 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
public class AntiVillagerTargetting implements VillagerOptimizerModule, Listener {
public class PreventVillagerTargetting implements VillagerOptimizerModule, Listener {
private final VillagerCache cache;
protected AntiVillagerTargetting() {
protected PreventVillagerTargetting() {
this.cache = VillagerOptimizer.getVillagerCache();
}
@ -39,14 +39,14 @@ public class AntiVillagerTargetting implements VillagerOptimizerModule, Listener
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onTarget(EntityTargetLivingEntityEvent event) {
if (event.getTarget() instanceof Villager villager && cache.get(villager).isOptimized()) {
if (event.getTarget() instanceof Villager villager && cache.getOrAdd(villager).isOptimized()) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onEntityTargetVillager(EntityPathfindEvent event) {
if (event.getTargetEntity() instanceof Villager villager && cache.get(villager).isOptimized()) {
if (event.getTargetEntity() instanceof Villager villager && cache.getOrAdd(villager).isOptimized()) {
event.setCancelled(true);
}
}
@ -56,7 +56,7 @@ public class AntiVillagerTargetting implements VillagerOptimizerModule, Listener
if (
event.getEntityType().equals(EntityType.VILLAGER)
&& event.getDamager() instanceof Mob attacker
&& cache.get((Villager) event.getEntity()).isOptimized()
&& cache.getOrAdd((Villager) event.getEntity()).isOptimized()
) {
attacker.setTarget(null);
}

View File

@ -12,13 +12,13 @@ import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent;
public class RestockOptimized implements VillagerOptimizerModule, Listener {
public class RestockTrades implements VillagerOptimizerModule, Listener {
private final VillagerCache cache;
private final long restock_delay;
private final boolean shouldLog;
public RestockOptimized() {
protected RestockTrades() {
this.cache = VillagerOptimizer.getVillagerCache();
Config config = VillagerOptimizer.getConfiguration();
config.addComment("optimization.trade-restocking.enable", """
@ -48,7 +48,7 @@ public class RestockOptimized implements VillagerOptimizerModule, Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onInteract(PlayerInteractEntityEvent event) {
if (!event.getRightClicked().getType().equals(EntityType.VILLAGER)) return;
WrappedVillager wVillager = cache.get((Villager) event.getRightClicked());
WrappedVillager wVillager = cache.getOrAdd((Villager) event.getRightClicked());
if (!wVillager.isOptimized()) return;

View File

@ -14,14 +14,12 @@ public interface VillagerOptimizerModule {
modules.forEach(VillagerOptimizerModule::disable);
modules.clear();
modules.add(new AntiVillagerDamage());
modules.add(new AntiVillagerTargetting());
modules.add(new ChunkLimit());
modules.add(new NametagOptimization());
modules.add(new BlockOptimization());
modules.add(new WorkstationOptimization());
modules.add(new ChunkLimit());
modules.add(new PreventVillagerDamage());
modules.add(new PreventVillagerTargetting());
for (VillagerOptimizerModule module : modules) {
if (module.shouldEnable()) module.enable();