Skip to content

Commit

Permalink
fix stacked mobs exp
Browse files Browse the repository at this point in the history
  • Loading branch information
alvindimas05 committed Aug 15, 2024
1 parent 3989a37 commit a00b062
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 48 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions src/main/java/org/alvindimas05/lagassist/stacker/StackChunk.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package org.alvindimas05.lagassist.stacker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -142,14 +137,14 @@ private static Entity getMatch(Map<EntityType, HashSet<Entity>> ents, Location l
list.add(optional);
return optional;
}

for (Entity ent : list) {
boolean similar = StackComparer.isSimilar(ent, optional);
if (similar) {
return ent;
}
}

list.add(optional);
return optional;

Expand Down Expand Up @@ -199,7 +194,7 @@ public static void setSize(Entity ent, int size) {
ent.setMetadata("lagassist.stacksize", new FixedMetadataValue(Main.p, size));
ent.setCustomName(formatted);
ent.setCustomNameVisible(Main.config.getBoolean("smart-stacker.gameplay.tag-visibility"));
}
}

public static void setDrops(EntityDeathEvent e) {
Entity ent = e.getEntity();
Expand All @@ -226,9 +221,15 @@ public static void setDrops(EntityDeathEvent e) {
}
}

int droppedExp = e.getDroppedExp();

if(droppedExp < 1){
droppedExp = StackExp.getExp(e.getEntity());
}

e.getDrops().clear();
e.getDrops().addAll(drops);
e.setDroppedExp(e.getDroppedExp() * stack);
e.setDroppedExp(droppedExp * stack);
}

public static void runShutdown() {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/org/alvindimas05/lagassist/stacker/StackExp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.alvindimas05.lagassist.stacker;


import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;

// Sorry, the exp is hard coded here
// Based on https://minecraft.wiki/w/Experience
public class StackExp {
static String[] commonFarmMobs = {
"CREEPER", "DROWNED", "ENDERMEN", "HOGLIN", "HUSK", "PHANTOM", "PILLAGER",
"SILVERFISH", "SKELETON", "STRAY", "VEX", "VINDICATOR", "WITCH",
"WITHER_SKELETON", "ZOMBIE", "ZOMBIE_VILLAGER", "ZOGLIN", "ZOMBIFIED_PIGLIN",
};
static int commonFarmExp = 5;

static String[] uncommonFarmMobs = {
"BLAZE", "ELDER_GUARDIAN", "EVOKER", "GUARDIAN"
};
static int uncommonFarmExp = 10;

static String[] rareFarmMobs = {
"RAVAGER", "PIGLIN_BRUTE"
};
static int rareFarmExp = 20;

static String[] commonFarmAnimals = {
"CHICKEN", "COD", "COW", "MOOSHROOM", "PIG", "RABBIT", "SALMON", "SHEEP"
};
static int minCommonFarmAnimalExp = 1;
static int maxCommonFarmAnimalExp = 3;

public static int getExp(Entity ent){
String type = ent.getType().toString().toUpperCase();

if(Arrays.asList(commonFarmMobs).contains(type)){
return commonFarmExp;
} else if(Arrays.asList(uncommonFarmMobs).contains(type)){
return uncommonFarmExp;
} else if(Arrays.asList(rareFarmMobs).contains(type)){
return rareFarmExp;
} else if(Arrays.asList(commonFarmAnimals).contains(type)){
return ThreadLocalRandom.current().nextInt(minCommonFarmAnimalExp, maxCommonFarmAnimalExp);
}

return 0;
}
}
74 changes: 37 additions & 37 deletions src/main/java/org/alvindimas05/lagassist/stacker/StackManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public static void Enabler(boolean reload) {
if (!reload) {
Main.p.getServer().getPluginManager().registerEvents(new StackManager(), Main.p);
Main.p.getServer().getPluginManager().registerEvents(new StackManipulator(), Main.p);

if (Main.paper) {
Main.p.getServer().getPluginManager().registerEvents(new PaperOnly(), Main.p);
}
}

StackChunk.runStart();
Bukkit.getLogger().info(" §e[§a✔§e] §fSmart Mob Stacker.");
}

public static boolean isStacked(Entity ent) {
return StackChunk.getStack(ent) > 1;
}
Expand All @@ -72,44 +72,44 @@ public void onCreatureSpawn(CreatureSpawnEvent e) {
if (!smartstacker) {
return;
}

if (WorldMgr.isBlacklisted(e.getLocation().getWorld())) {
return;
}

e.getEntity().setMetadata("lagassist.spawnreason", new FixedMetadataValue(Main.p, e.getSpawnReason()));
}


@EventHandler(priority = EventPriority.HIGH)
public void onSpawn(EntitySpawnEvent e) {

if (e.isCancelled()) {
return;
}

if (!smartstacker) {
return;
}

if (WorldMgr.isBlacklisted(e.getLocation().getWorld())) {
return;
}

if(StackManipulator.isDeadEntity()) {
return;
}

Entity ent = e.getEntity();

if (!Main.config.getBoolean("smart-stacker.checks.spawn-check")) {
return;
}

// if (!(ent instanceof LivingEntity)) {
// return;
// }

if (StackChunk.tryStacking(ent.getLocation(), ent.getType(), ent)) {
// e.setCancelled(true);
ent.remove();
Expand All @@ -125,14 +125,14 @@ public void onDeath(EntityDeathEvent e) {
if (!smartstacker) {
return;
}

if (!Main.config.getBoolean("smart-stacker.technical.drops-fix")) {
return;
}

StackChunk.setDrops(e);
}

private static class PaperOnly implements Listener {
// Ugly so it hopefully doesn't break.
@EventHandler(priority = EventPriority.HIGH)
Expand All @@ -144,11 +144,11 @@ public void onPreSpawn(CreatureSpawnEvent e) {
if (!smartstacker) {
return;
}

if (WorldMgr.isBlacklisted(e.getLocation().getWorld())) {
return;
}

if(StackManipulator.isDeadEntity()) {
return;
}
Expand All @@ -161,7 +161,7 @@ public void onPreSpawn(CreatureSpawnEvent e) {
e.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.HIGH)
public void onAnvil(PrepareAnvilEvent e) {
if (!smartstacker) {
Expand All @@ -177,20 +177,20 @@ public void onAnvil(PrepareAnvilEvent e) {
if (!result.hasItemMeta()) {
return;
}

ItemMeta imeta = result.getItemMeta();

if (!imeta.hasDisplayName()) {
return;
}

Pattern pat = Pattern.compile(StackChunk.regexpat.replace("{type}", "(.*.)"), Pattern.MULTILINE);
Matcher mtch = pat.matcher(imeta.getDisplayName());

if (!mtch.find()) {
return;
}

e.setResult(null);
}
}
Expand All @@ -203,11 +203,11 @@ public void onSplitChange(SplitChangeEvent e) {
if (!smartstacker) {
return;
}

if (!Main.config.getBoolean("smart-stacker.checks.split-change-check")) {
return;
}

if (WorldMgr.isBlacklisted(e.getFrom().getWorld())) {
return;
}
Expand All @@ -218,7 +218,7 @@ public void onSplitChange(SplitChangeEvent e) {
if (illegal.contains(etype)) {
return;
}

// Not a stackable entity.
// if (!(ent instanceof LivingEntity)) {
// return;
Expand All @@ -239,7 +239,7 @@ public void onChunkLoad(ChunkLoadEvent e) {
if (Main.config.getBoolean("smart-stacker.technical.shutdown-clean")) {
return;
}

if (WorldMgr.isBlacklisted(e.getWorld())) {
return;
}
Expand All @@ -248,7 +248,7 @@ public void onChunkLoad(ChunkLoadEvent e) {

StackChunk.loadChunk(chk);
}

public void onChunkLoad(ChunkUnloadEvent e) {
if (!smartstacker) {
return;
Expand All @@ -257,37 +257,37 @@ public void onChunkLoad(ChunkUnloadEvent e) {
if (WorldMgr.isBlacklisted(e.getWorld())) {
return;
}

Chunk chk = e.getChunk();

StackChunk.unloadChunk(chk);
}

public static boolean isStackable(Entity ent) {
if (ent == null) {
return false;
}

// if (!(ent instanceof LivingEntity)) {
// return false;
// }

if (ent instanceof Player) {
return false;
}

if (!Main.config.getStringList("smart-stacker.gameplay.stackable").contains(ent.getType().toString().toUpperCase())) {
return false;
}

String spawnreason = ent.hasMetadata("lagassist.spawnreason") ? ent.getMetadata("lagassist.spawnreason").get(0).value().toString().toUpperCase() : "UNKNOWN";

if(!(Main.config.getStringList("smart-stacker.gameplay.spawn-reasons").contains("ALL") || Main.config.getStringList("smart-stacker.gameplay.spawn-reasons").contains(spawnreason))) {
return false;
}

int stacksize = StackChunk.getStack(ent);

if (stacksize < 0) {
return false;
}
Expand Down

0 comments on commit a00b062

Please sign in to comment.