Skip to content

Commit

Permalink
feat: Add timeout for check-location
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurelien30000 committed Dec 31, 2023
1 parent 56c879a commit d6bf5e1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.2.16-SNAPSHOT</version>
<version>7.3.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/uk/antiperson/stackmob/config/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class MainConfig extends SpecialConfigFile {
private final Map<EntityType, Integer> stack_threshold_amount = new EnumMap<>(EntityType.class);
private boolean default_stack_check_location_enabled;
private double default_stack_check_location_distance;
private int default_stack_check_location_timeout;
private boolean default_stack_on_spawn;
private final Map<EntityType, Boolean> stack_on_spawn = new EnumMap<>(EntityType.class);
private boolean default_stack_line_of_sight;
Expand Down Expand Up @@ -140,6 +141,7 @@ public void cache() {
default_stack_threshold_amount = getInt("stack.threshold.amount");
default_stack_check_location_enabled = getBoolean("stack.check-location.enabled");
default_stack_check_location_distance = getDouble("stack.check-location.distance");
default_stack_check_location_timeout = getInt("stack.check-location.timeout");
default_stack_on_spawn = getBoolean("stack.on-spawn");
default_stack_line_of_sight = getBoolean("stack.line-of-sight");
default_stack_nametag_mode = EntityConfig.NameTagStackMode.valueOf(getString("stack.nametag-mode"));
Expand Down Expand Up @@ -441,6 +443,10 @@ public double getCheckHasMovedDistance() {
return default_stack_check_location_distance;
}

public int getCheckHasMovedTimeout() {
return default_stack_check_location_timeout;
}

public boolean isStackOnSpawn(EntityType type) {
return stack_on_spawn.getOrDefault(type, default_stack_on_spawn);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/uk/antiperson/stackmob/entity/StackEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class StackEntity {
private boolean removed;
private Location lastLocation;
private int stackSize;
private int lastLocationTimeout;
private Set<ItemStack> equiptItems;
private Tag tag;

Expand Down Expand Up @@ -105,6 +106,15 @@ public void setLastLocation(Location lastLocation) {
this.lastLocation = lastLocation;
}

public boolean skipLastLocation() {
if (lastLocationTimeout == 0) {
lastLocationTimeout = sm.getMainConfig().getCheckHasMovedTimeout();
return true;
}
lastLocationTimeout--;
return false;
}

/**
* Returns whether the entity will have its stack data removed on spawn.
* See {@link #setForgetOnSpawn(boolean)} for further details.
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/uk/antiperson/stackmob/tasks/MergeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public void run() {
}
if (checkHasMoved) {
if (original.getEntity().getWorld().equals(original.getLastLocation().getWorld())) {
if (original.getEntity().getLocation().distance(original.getLastLocation()) < checkHasMovedDistance) {
continue;
if (!original.skipLastLocation()) {
if (original.getEntity().getLocation().distance(original.getLastLocation()) < checkHasMovedDistance) {
continue;
}
}
}
original.setLastLocation(original.getEntity().getLocation());
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ stack:
enabled: true
amount: 5
# Only find matches for this mob if it has moved from the last time nearby mobs were checked. (*)
# This may break grinders etc, which is why for now it is disabled by default.
# Please give feedback at https://github.com/Nathat23/StackMob-5/issues/198
# This may have unintended effects, so you should do some testing when modifying this section.
check-location:
enabled: true
# The distance threshold for the check
distance: 1.0
# Even if the entity hasn't moved, after (x) attempts then attempt a merge.
timeout: 20
# Stack entities when they spawn. (*)
on-spawn: false
# Only stack when two stacks can directly see each other
Expand Down

0 comments on commit d6bf5e1

Please sign in to comment.