Skip to content

Commit

Permalink
Items merging.
Browse files Browse the repository at this point in the history
  • Loading branch information
cao-awa committed Dec 29, 2024
1 parent 2c4c4b2 commit 2a3cbb0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SepalsConfig {
public static final SepalsConfigKey<Boolean> NEAREST_LIVING_ENTITIES_SENSOR_USE_QUICK_SORT = SepalsConfigKey.create("nearestLivingEntitiesSensorUseQuickSort", true);
public static final SepalsConfigKey<Boolean> ENABLE_SEPALS_BIASED_LONG_JUMP_TASK = SepalsConfigKey.create("enableSepalsBiasedLongJumpTask", true);
public static final SepalsConfigKey<Boolean> ENABLE_SEPALS_ENTITIES_CRAMMING = SepalsConfigKey.create("enableSepalsEntitiesCramming", true);
public static final SepalsConfigKey<Boolean> ENABLE_SEPALS_ITEM_MERGE = SepalsConfigKey.create("enableSepalsItemMerge", true);

private final JSONObject config = new JSONObject();

Expand Down Expand Up @@ -64,6 +65,10 @@ public boolean isEnableSepalsEntitiesCramming() {
return getConfig(ENABLE_SEPALS_ENTITIES_CRAMMING);
}

public boolean isEnableSepalsItemMerge() {
return getConfig(ENABLE_SEPALS_ITEM_MERGE);
}

public <X> void setConfig(SepalsConfigKey<X> configKey, X value) {
this.config.put(configKey.name(), configKey.checkLimits(checkOrThrow(configKey, value)));
}
Expand Down Expand Up @@ -103,6 +108,7 @@ public void load() {
setConfig(NEAREST_LIVING_ENTITIES_SENSOR_USE_QUICK_SORT, config);
setConfig(ENABLE_SEPALS_BIASED_LONG_JUMP_TASK, config);
setConfig(ENABLE_SEPALS_ENTITIES_CRAMMING, config);
setConfig(ENABLE_SEPALS_ITEM_MERGE, config);
} catch (Exception e) {
LOGGER.warn("Config not found, use default values", e);
write();
Expand Down Expand Up @@ -132,6 +138,7 @@ public void copyFrom(@NotNull SepalsConfig config) {
setConfig(NEAREST_LIVING_ENTITIES_SENSOR_USE_QUICK_SORT, config.isNearestLivingEntitiesSensorUseQuickSort());
setConfig(ENABLE_SEPALS_BIASED_LONG_JUMP_TASK, config.isEnableSepalsBiasedLongJumpTask());
setConfig(ENABLE_SEPALS_ENTITIES_CRAMMING, config.isEnableSepalsEntitiesCramming());
setConfig(ENABLE_SEPALS_ITEM_MERGE, config.isEnableSepalsItemMerge());
}

public void print() {
Expand All @@ -142,7 +149,8 @@ public void print() {
LOGGER.info("Sepals 'enableSepalsLivingTargetCache' flag is {}", isEnableSepalsLivingTargetCache());
LOGGER.info("Sepals 'nearestLivingEntitiesSensorUseQuickSort' flag is {}", isNearestLivingEntitiesSensorUseQuickSort());
LOGGER.info("Sepals 'enableSepalsBiasedJumpLongTask' flag is {}", isEnableSepalsBiasedLongJumpTask());
LOGGER.info("Sepals 'enableEntitiesCramming' flag is {}", isEnableSepalsEntitiesCramming());
LOGGER.info("Sepals 'enableSepalsEntitiesCramming' flag is {}", isEnableSepalsEntitiesCramming());
LOGGER.info("Sepals 'enableSepalsItemMerge' flag is {}", isEnableSepalsItemMerge());
}

public Set<SepalsConfigKey<?>> collectEnabled() {
Expand Down Expand Up @@ -180,6 +188,10 @@ public Set<SepalsConfigKey<?>> collectEnabled() {
enabled.add(ENABLE_SEPALS_ENTITIES_CRAMMING);
}

if (isEnableSepalsItemMerge()) {
enabled.add(ENABLE_SEPALS_ITEM_MERGE);
}

return enabled;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.github.cao.awa.sepals.mixin.entity.item;

import com.github.cao.awa.sepals.Sepals;
import com.github.cao.awa.sepals.item.BoxedItemEntities;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.util.TypeFilter;
import net.minecraft.util.math.Box;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.function.Predicate;

// TODO preparing to optimization items.
@Mixin(ItemEntity.class)
public abstract class ItemEntityMixin extends Entity {
Expand All @@ -29,63 +36,66 @@ public ItemEntityMixin(EntityType<?> type, World world) {
@Shadow
protected abstract void tryMerge(ItemEntity other);

// @Inject(
// method = "tryMerge()V",
// at = @At(value = "HEAD"),
// cancellable = true
// )
// private void tryMerge(CallbackInfo ci) {
// if (canMerge()) {
// getBoxedEntities(
// getWorld(),
// getBoundingBox().expand(0.5, 0.0, 0.5),
// itemEntity -> {
// tryMerge(itemEntity);
// return isRemoved();
// }
// );
// }
// ci.cancel();
// }
//
// @Unique
// public void getBoxedEntities(World world, Box box, Predicate<ItemEntity> invalidate) {
// if (world instanceof BoxedItemEntities entities) {
// entities.setEntities(world.getEntitiesByType(
// TypeFilter.instanceOf(ItemEntity.class),
// box,
// entity -> entity != (Object) this && ((ItemEntityAccessor) entity).invokeCanMerge()
// )
// );
//
// for (ItemEntity entity : entities.entities()) {
// if (entity == (Object) this) {
// continue;
// }
//// ItemStack stack = entity.getStack();
////
//// if (stack.getMaxCount() == stack.getCount()) {
//// entities.invalidate(entity);
//// continue;
//// }
@Inject(
method = "tryMerge()V",
at = @At(value = "HEAD"),
cancellable = true
)
private void tryMerge(CallbackInfo ci) {
if (Sepals.CONFIG.isEnableSepalsItemMerge()) {
if (canMerge()) {
getBoxedEntities(
getWorld(),
getBoundingBox().expand(0.5, 0.0, 0.5),
itemEntity -> {
tryMerge(itemEntity);
return isRemoved();
}
);
}
ci.cancel();
}
}

@Unique
public void getBoxedEntities(World world, Box box, Predicate<ItemEntity> invalidate) {
if (world instanceof BoxedItemEntities entities) {
entities.setEntities(world.getEntitiesByType(
TypeFilter.instanceOf(ItemEntity.class),
box,
entity -> entity != (Object) this && ((ItemEntityAccessor) entity).invokeCanMerge()
)
);

for (ItemEntity entity : entities.entities()) {
if (entity == (Object) this) {
continue;
}
// ItemStack stack = entity.getStack();
//
// if (!((ItemEntityAccessor) entity).invokeCanMerge()) {
// if (stack.getMaxCount() == stack.getCount()) {
// entities.invalidate(entity);
// }
//
// if (!entity.getBoundingBox().intersects(box)) {
// continue;
// }
//
// entities.invalidate((ItemEntity) (Object) this);
//
// if (invalidate.test(entity)) {
// // Invalidate this item, because it no longer can be to other items.
// break;
// }
// }
// }
// }

if (!((ItemEntityAccessor) entity).invokeCanMerge()) {
entities.invalidate(entity);
continue;
}

if (!entity.getBoundingBox().intersects(box)) {
continue;
}

entities.invalidate((ItemEntity) (Object) this);

if (invalidate.test(entity)) {
// Invalidate this item, because it no longer can be to other items.
break;
}
}
}
}

@Inject(
method = "isFireImmune",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ public class ServerWorldMixin implements BoxedItemEntities {

@Unique
public Collection<ItemEntity> sepals$entitiesAndInvalidate(ItemEntity entity) {
Collection<ItemEntity> items = this.entities.values();

if (!items.contains(entity)) {
if (!this.entities.containsKey(entity.getId())) {
this.entities.remove(entity.getId());
}

return items;
return this.entities.values();
}

@Inject(
Expand Down

0 comments on commit 2a3cbb0

Please sign in to comment.