Skip to content

Commit

Permalink
Add batching
Browse files Browse the repository at this point in the history
  • Loading branch information
VidTu committed Jun 21, 2023
1 parent 6d8ebb8 commit 8540fe5
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 40 deletions.
32 changes: 25 additions & 7 deletions fabric-1.18.2/src/main/java/ru/femboypve/hcscr/HCsCRFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import net.minecraft.world.entity.boss.enderdragon.EndCrystal;
import net.minecraft.world.entity.monster.Slime;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import org.lwjgl.glfw.GLFW;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.*;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -115,15 +115,16 @@ public void onInitializeClient() {
/**
* Removes the entity client-side, if required.
*
* @param entity Target entity
* @param source Damage source
* @param amount Amount of damage
* @param entity Target entity
* @param source Damage source
* @param amount Amount of damage
* @param checked Set of already tested entities
* @return Whether the entity has been removed
*/
public static boolean removeClientSide(Entity entity, DamageSource source, float amount) {
public static boolean removeClientSide(Entity entity, DamageSource source, float amount, Set<Entity> checked) {
if (!HCsCR.enabled || HCsCR.serverDisabled || amount <= 0F || entity.isRemoved()
|| !entity.level.isClientSide() || !(source.getEntity() instanceof Player player)
|| entity.isInvulnerableTo(source)) return false;
|| entity.isInvulnerableTo(source) || !checked.add(entity)) return false;
if (entity instanceof EndCrystal) {
if (!HCsCR.removeCrystals) return false;
} else if (entity instanceof Slime slime) {
Expand All @@ -141,6 +142,23 @@ public static boolean removeClientSide(Entity entity, DamageSource source, float
instance.getEffect().removeAttributeModifiers(player, map, instance.getAmplifier());
}
if (amount < 0) return false;
if (HCsCR.batching != Batching.DISABLED) {
List<Entity> entities = new ArrayList<>(entity.level.getEntities(entity, entity.getBoundingBox()));
if (HCsCR.batching != Batching.INTERSECTING) {
AABB box = entity.getBoundingBox();
entities.removeIf(e -> {
AABB other = e.getBoundingBox();
if (other.minX >= box.minX && other.minY >= box.minY && other.minZ >= box.minZ &&
other.maxX <= box.maxX && other.maxY <= box.maxY && other.maxZ <= box.maxZ) return false;
return HCsCR.batching != Batching.CONTAINING_CONTAINED ||
!(box.minX >= other.minX) || !(box.minY >= other.minY) || !(box.minZ >= other.minZ) ||
!(box.maxX <= other.maxX) || !(box.maxY <= other.maxY) || !(box.maxZ <= other.maxZ);
});
}
for (Entity e : entities) {
removeClientSide(e, source, amount, checked);
}
}
if (HCsCR.delay > 0) {
SCHEDULE_REMOVAL.put(entity, System.nanoTime() + HCsCR.delay * 1_000_000L);
return true;
Expand Down
12 changes: 12 additions & 0 deletions fabric-1.18.2/src/main/java/ru/femboypve/hcscr/HCsCRScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ protected void init() {
width / 2 - 12 - font.width(new TranslatableComponent("hcscr.screen.absolutePrecision")) / 2, 184,
24 + font.width(new TranslatableComponent("hcscr.screen.absolutePrecision")), 20,
new TranslatableComponent("hcscr.screen.absolutePrecision"), HCsCR.absolutePrecision));
addRenderableWidget(new Button(width / 2 - 100, 208, 200, 20, CommonComponents.optionNameValue(
new TranslatableComponent("hcscr.screen.batching"),
new TranslatableComponent("hcscr.screen.batching.".concat(HCsCR.batching.id()))
), button -> {
int i = HCsCR.batching.ordinal() + 1;
if (i >= Batching.values().length) i = 0;
HCsCR.batching = Batching.values()[i];
button.setMessage(CommonComponents.optionNameValue(
new TranslatableComponent("hcscr.screen.batching"),
new TranslatableComponent("hcscr.screen.batching.".concat(HCsCR.batching.id()))
));
}));
addRenderableWidget(new Button(width / 2 - 75, height - 24, 150, 20, CommonComponents.GUI_DONE, button -> minecraft.setScreen(parent)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import ru.femboypve.hcscr.HCsCRFabric;

import java.util.HashSet;

/**
* Mixin that speeds up entity removing.
*
Expand All @@ -37,6 +39,6 @@ private PlayerMixin() {

@Redirect(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"))
public boolean hcscr$attack$hurt(Entity entity, DamageSource source, float amount) {
return entity.hurt(source, amount) | HCsCRFabric.removeClientSide(entity, source, amount);
return entity.hurt(source, amount) | HCsCRFabric.removeClientSide(entity, source, amount, new HashSet<>());
}
}
32 changes: 25 additions & 7 deletions fabric-1.19.2/src/main/java/ru/femboypve/hcscr/HCsCRFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
import net.minecraft.world.entity.boss.enderdragon.EndCrystal;
import net.minecraft.world.entity.monster.Slime;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import org.lwjgl.glfw.GLFW;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.*;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -114,15 +114,16 @@ public void onInitializeClient() {
/**
* Removes the entity client-side, if required.
*
* @param entity Target entity
* @param source Damage source
* @param amount Amount of damage
* @param entity Target entity
* @param source Damage source
* @param amount Amount of damage
* @param checked Set of already tested entities
* @return Whether the entity has been removed
*/
public static boolean removeClientSide(Entity entity, DamageSource source, float amount) {
public static boolean removeClientSide(Entity entity, DamageSource source, float amount, Set<Entity> checked) {
if (!HCsCR.enabled || HCsCR.serverDisabled || amount <= 0F || entity.isRemoved()
|| !entity.level.isClientSide() || !(source.getEntity() instanceof Player player)
|| entity.isInvulnerableTo(source)) return false;
|| entity.isInvulnerableTo(source) || !checked.add(entity)) return false;
if (entity instanceof EndCrystal) {
if (!HCsCR.removeCrystals) return false;
} else if (entity instanceof Slime slime) {
Expand All @@ -140,6 +141,23 @@ public static boolean removeClientSide(Entity entity, DamageSource source, float
instance.getEffect().removeAttributeModifiers(player, map, instance.getAmplifier());
}
if (amount < 0) return false;
if (HCsCR.batching != Batching.DISABLED) {
List<Entity> entities = new ArrayList<>(entity.level.getEntities(entity, entity.getBoundingBox()));
if (HCsCR.batching != Batching.INTERSECTING) {
AABB box = entity.getBoundingBox();
entities.removeIf(e -> {
AABB other = e.getBoundingBox();
if (other.minX >= box.minX && other.minY >= box.minY && other.minZ >= box.minZ &&
other.maxX <= box.maxX && other.maxY <= box.maxY && other.maxZ <= box.maxZ) return false;
return HCsCR.batching != Batching.CONTAINING_CONTAINED ||
!(box.minX >= other.minX) || !(box.minY >= other.minY) || !(box.minZ >= other.minZ) ||
!(box.maxX <= other.maxX) || !(box.maxY <= other.maxY) || !(box.maxZ <= other.maxZ);
});
}
for (Entity e : entities) {
removeClientSide(e, source, amount, checked);
}
}
if (HCsCR.delay > 0) {
SCHEDULE_REMOVAL.put(entity, System.nanoTime() + HCsCR.delay * 1_000_000L);
return true;
Expand Down
12 changes: 12 additions & 0 deletions fabric-1.19.2/src/main/java/ru/femboypve/hcscr/HCsCRScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ protected void init() {
width / 2 - 12 - font.width(Component.translatable("hcscr.screen.absolutePrecision")) / 2, 184,
24 + font.width(Component.translatable("hcscr.screen.absolutePrecision")), 20,
Component.translatable("hcscr.screen.absolutePrecision"), HCsCR.absolutePrecision));
addRenderableWidget(new Button(width / 2 - 100, 208, 200, 20, CommonComponents.optionNameValue(
Component.translatable("hcscr.screen.batching"),
Component.translatable("hcscr.screen.batching.".concat(HCsCR.batching.id()))
), button -> {
int i = HCsCR.batching.ordinal() + 1;
if (i >= Batching.values().length) i = 0;
HCsCR.batching = Batching.values()[i];
button.setMessage(CommonComponents.optionNameValue(
Component.translatable("hcscr.screen.batching"),
Component.translatable("hcscr.screen.batching.".concat(HCsCR.batching.id()))
));
}));
addRenderableWidget(new Button(width / 2 - 75, height - 24, 150, 20, CommonComponents.GUI_DONE, button -> minecraft.setScreen(parent)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import ru.femboypve.hcscr.HCsCRFabric;

import java.util.HashSet;

/**
* Mixin that speeds up entity removing.
*
Expand All @@ -37,6 +39,6 @@ private PlayerMixin() {

@Redirect(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"))
public boolean hcscr$attack$hurt(Entity entity, DamageSource source, float amount) {
return entity.hurt(source, amount) | HCsCRFabric.removeClientSide(entity, source, amount);
return entity.hurt(source, amount) | HCsCRFabric.removeClientSide(entity, source, amount, new HashSet<>());
}
}
32 changes: 25 additions & 7 deletions fabric-1.19.3/src/main/java/ru/femboypve/hcscr/HCsCRFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
import net.minecraft.world.entity.boss.enderdragon.EndCrystal;
import net.minecraft.world.entity.monster.Slime;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import org.lwjgl.glfw.GLFW;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.*;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -114,15 +114,16 @@ public void onInitializeClient() {
/**
* Removes the entity client-side, if required.
*
* @param entity Target entity
* @param source Damage source
* @param amount Amount of damage
* @param entity Target entity
* @param source Damage source
* @param amount Amount of damage
* @param checked Set of already tested entities
* @return Whether the entity has been removed
*/
public static boolean removeClientSide(Entity entity, DamageSource source, float amount) {
public static boolean removeClientSide(Entity entity, DamageSource source, float amount, Set<Entity> checked) {
if (!HCsCR.enabled || HCsCR.serverDisabled || amount <= 0F || entity.isRemoved()
|| !entity.level.isClientSide() || !(source.getEntity() instanceof Player player)
|| entity.isInvulnerableTo(source)) return false;
|| entity.isInvulnerableTo(source) || !checked.add(entity)) return false;
if (entity instanceof EndCrystal) {
if (!HCsCR.removeCrystals) return false;
} else if (entity instanceof Slime slime) {
Expand All @@ -140,6 +141,23 @@ public static boolean removeClientSide(Entity entity, DamageSource source, float
instance.getEffect().removeAttributeModifiers(player, map, instance.getAmplifier());
}
if (amount < 0) return false;
if (HCsCR.batching != Batching.DISABLED) {
List<Entity> entities = new ArrayList<>(entity.level.getEntities(entity, entity.getBoundingBox()));
if (HCsCR.batching != Batching.INTERSECTING) {
AABB box = entity.getBoundingBox();
entities.removeIf(e -> {
AABB other = e.getBoundingBox();
if (other.minX >= box.minX && other.minY >= box.minY && other.minZ >= box.minZ &&
other.maxX <= box.maxX && other.maxY <= box.maxY && other.maxZ <= box.maxZ) return false;
return HCsCR.batching != Batching.CONTAINING_CONTAINED ||
!(box.minX >= other.minX) || !(box.minY >= other.minY) || !(box.minZ >= other.minZ) ||
!(box.maxX <= other.maxX) || !(box.maxY <= other.maxY) || !(box.maxZ <= other.maxZ);
});
}
for (Entity e : entities) {
removeClientSide(e, source, amount, checked);
}
}
if (HCsCR.delay > 0) {
SCHEDULE_REMOVAL.put(entity, System.nanoTime() + HCsCR.delay * 1_000_000L);
return true;
Expand Down
12 changes: 12 additions & 0 deletions fabric-1.19.3/src/main/java/ru/femboypve/hcscr/HCsCRScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ protected void init() {
width / 2 - 12 - font.width(Component.translatable("hcscr.screen.absolutePrecision")) / 2, 184,
24 + font.width(Component.translatable("hcscr.screen.absolutePrecision")), 20,
Component.translatable("hcscr.screen.absolutePrecision"), HCsCR.absolutePrecision));
addRenderableWidget(Button.builder(CommonComponents.optionNameValue(
Component.translatable("hcscr.screen.batching"),
Component.translatable("hcscr.screen.batching.".concat(HCsCR.batching.id()))
), button -> {
int i = HCsCR.batching.ordinal() + 1;
if (i >= Batching.values().length) i = 0;
HCsCR.batching = Batching.values()[i];
button.setMessage(CommonComponents.optionNameValue(
Component.translatable("hcscr.screen.batching"),
Component.translatable("hcscr.screen.batching.".concat(HCsCR.batching.id()))
));
}).bounds(width / 2 - 100, 208, 200, 20).build());
addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> minecraft.setScreen(parent)).bounds(width / 2 - 75, height - 24, 150, 20).build());
removeInteractions.setTooltip(Tooltip.create(Component.translatable("hcscr.screen.removeInteractions.version")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import ru.femboypve.hcscr.HCsCRFabric;

import java.util.HashSet;

/**
* Mixin that speeds up entity removing.
*
Expand All @@ -37,6 +39,6 @@ private PlayerMixin() {

@Redirect(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"))
public boolean hcscr$attack$hurt(Entity entity, DamageSource source, float amount) {
return entity.hurt(source, amount) | HCsCRFabric.removeClientSide(entity, source, amount);
return entity.hurt(source, amount) | HCsCRFabric.removeClientSide(entity, source, amount, new HashSet<>());
}
}
32 changes: 25 additions & 7 deletions fabric-1.19.4/src/main/java/ru/femboypve/hcscr/HCsCRFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import net.minecraft.world.entity.boss.enderdragon.EndCrystal;
import net.minecraft.world.entity.monster.Slime;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import org.lwjgl.glfw.GLFW;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.*;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -115,15 +115,16 @@ public void onInitializeClient() {
/**
* Removes the entity client-side, if required.
*
* @param entity Target entity
* @param source Damage source
* @param amount Amount of damage
* @param entity Target entity
* @param source Damage source
* @param amount Amount of damage
* @param checked Set of already tested entities
* @return Whether the entity has been removed
*/
public static boolean removeClientSide(Entity entity, DamageSource source, float amount) {
public static boolean removeClientSide(Entity entity, DamageSource source, float amount, Set<Entity> checked) {
if (!HCsCR.enabled || HCsCR.serverDisabled || amount <= 0F || entity.isRemoved()
|| !entity.level.isClientSide() || !(source.getEntity() instanceof Player player)
|| entity.isInvulnerableTo(source)) return false;
|| entity.isInvulnerableTo(source) || !checked.add(entity)) return false;
if (entity instanceof EndCrystal) {
if (!HCsCR.removeCrystals) return false;
} else if (entity instanceof Slime slime) {
Expand All @@ -143,6 +144,23 @@ public static boolean removeClientSide(Entity entity, DamageSource source, float
instance.getEffect().removeAttributeModifiers(player, map, instance.getAmplifier());
}
if (amount < 0) return false;
if (HCsCR.batching != Batching.DISABLED) {
List<Entity> entities = new ArrayList<>(entity.level.getEntities(entity, entity.getBoundingBox()));
if (HCsCR.batching != Batching.INTERSECTING) {
AABB box = entity.getBoundingBox();
entities.removeIf(e -> {
AABB other = e.getBoundingBox();
if (other.minX >= box.minX && other.minY >= box.minY && other.minZ >= box.minZ &&
other.maxX <= box.maxX && other.maxY <= box.maxY && other.maxZ <= box.maxZ) return false;
return HCsCR.batching != Batching.CONTAINING_CONTAINED ||
!(box.minX >= other.minX) || !(box.minY >= other.minY) || !(box.minZ >= other.minZ) ||
!(box.maxX <= other.maxX) || !(box.maxY <= other.maxY) || !(box.maxZ <= other.maxZ);
});
}
for (Entity e : entities) {
removeClientSide(e, source, amount, checked);
}
}
if (HCsCR.delay > 0) {
SCHEDULE_REMOVAL.put(entity, System.nanoTime() + HCsCR.delay * 1_000_000L);
return true;
Expand Down
12 changes: 12 additions & 0 deletions fabric-1.19.4/src/main/java/ru/femboypve/hcscr/HCsCRScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ protected void init() {
width / 2 - 12 - font.width(Component.translatable("hcscr.screen.absolutePrecision")) / 2, 184,
24 + font.width(Component.translatable("hcscr.screen.absolutePrecision")), 20,
Component.translatable("hcscr.screen.absolutePrecision"), HCsCR.absolutePrecision));
addRenderableWidget(Button.builder(CommonComponents.optionNameValue(
Component.translatable("hcscr.screen.batching"),
Component.translatable("hcscr.screen.batching.".concat(HCsCR.batching.id()))
), button -> {
int i = HCsCR.batching.ordinal() + 1;
if (i >= Batching.values().length) i = 0;
HCsCR.batching = Batching.values()[i];
button.setMessage(CommonComponents.optionNameValue(
Component.translatable("hcscr.screen.batching"),
Component.translatable("hcscr.screen.batching.".concat(HCsCR.batching.id()))
));
}).bounds(width / 2 - 100, 208, 200, 20).build());
addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> minecraft.setScreen(parent)).bounds(width / 2 - 75, height - 24, 150, 20).build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import ru.femboypve.hcscr.HCsCRFabric;

import java.util.HashSet;

/**
* Mixin that speeds up entity removing.
*
Expand All @@ -37,6 +39,6 @@ private PlayerMixin() {

@Redirect(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"))
public boolean hcscr$attack$hurt(Entity entity, DamageSource source, float amount) {
return entity.hurt(source, amount) | HCsCRFabric.removeClientSide(entity, source, amount);
return entity.hurt(source, amount) | HCsCRFabric.removeClientSide(entity, source, amount, new HashSet<>());
}
}
Loading

0 comments on commit 8540fe5

Please sign in to comment.