Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为 AnvilCraftingContext 添加自由数据区 #1087

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.dubhe.anvilcraft.data.recipe.anvil;

import lombok.Getter;
import lombok.Setter;
import net.minecraft.core.BlockPos;
import net.minecraft.world.Container;
import net.minecraft.world.entity.item.FallingBlockEntity;
Expand All @@ -14,25 +13,29 @@
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* 铁砧合成上下文
*/
@Getter
public class AnvilCraftingContext
implements Container, StackedContentsCompatible {
@Getter
private final Level level;
@Getter
private final BlockPos pos;
@Getter
private final FallingBlockEntity entity;
@Setter
@Getter
private boolean isAnvilDamage = false;
private final Set<Map.Entry<Vec3, ItemStack>> outputs = new HashSet<>();
private final Map<String, Object> data = new HashMap<>();

/**
* 初始化 AnvilCraftingContainer
* 初始化 AnvilCraftingContext
*
* @param level 世界
* @param pos 位置
Expand Down Expand Up @@ -113,6 +116,15 @@
return this.outputs.add(Map.entry(pos, stack));
}

/**
* 设置是否强制铁砧损坏
*
* @param anvilDamage true 表示强制铁砧损坏,false 表示不强制铁砧损坏
*/
public void setAnvilDamage(boolean anvilDamage) {
isAnvilDamage = anvilDamage;
}

/**
* 向世界中输出物品实体
*/
Expand All @@ -126,4 +138,44 @@
this.level.addFreshEntity(entity);
}
}

/**
* 向数据集合中添加键值对
*
* @param key 键,用于唯一标识数据项
* @param value 值,可以是任意类型的数据
* @param <T> 泛型参数,表示值的类型
*/
public <T> void addData(String key, T value) {
this.data.put(key, value);
}


/**
* 根据键和类型从数据映射中获取数据
* <p>
* 此方法用于从存储对象的映射中安全地检索特定类型的数据
* <p>
* 如果键不存在,或者存储的值不是指定的类型,则返回 null
*
* @param key 要检索的数据项的键
* @param typeOfT 要检索的数据项的类型
* @return <T> 如果找到键且其值匹配指定类型,则返回该值;否则返回 null

Check warning on line 163 in common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. Raw Output: /home/runner/work/AnvilCraft/AnvilCraft/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java:163:0: warning: Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. (com.puppycrawl.tools.checkstyle.checks.javadoc.AtclauseOrderCheck)

Check warning on line 163 in common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. Raw Output: /home/runner/work/AnvilCraft/AnvilCraft/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java:163:0: warning: Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck)

Check warning on line 163 in common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. Raw Output: /home/runner/work/AnvilCraft/AnvilCraft/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java:163:0: warning: Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTagContinuationIndentationCheck)

Check warning on line 163 in common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. Raw Output: /home/runner/work/AnvilCraft/AnvilCraft/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java:163:0: warning: Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. (com.puppycrawl.tools.checkstyle.checks.javadoc.NonEmptyAtclauseDescriptionCheck)

Check warning on line 163 in common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. Raw Output: /home/runner/work/AnvilCraft/AnvilCraft/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java:163:0: warning: Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. (com.puppycrawl.tools.checkstyle.checks.javadoc.RequireEmptyLineBeforeBlockTagGroupCheck)

Check warning on line 163 in common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. Raw Output: /home/runner/work/AnvilCraft/AnvilCraft/common/src/main/java/dev/dubhe/anvilcraft/data/recipe/anvil/AnvilCraftingContext.java:163:0: warning: Javadoc comment at column 16 has parse error. Missed HTML close tag 'T'. Sometimes it means that close tag missed for one of previous tags. (com.puppycrawl.tools.checkstyle.checks.javadoc.SingleLineJavadocCheck)
*/
public <T> T getData(String key, Class<T> typeOfT) {
// 从映射中获取与键关联的对象
Object o = this.data.get(key);
// 如果对象不存在,则直接返回null
if (o == null) return null;
// 检查对象是否是请求的类型,如果不是,则返回null
if (!typeOfT.isInstance(o)) return null;
// 类型检查通过,安全地将对象转换为请求的类型并返回
//noinspection unchecked
return (T) o;
}

public AnvilCraftingContext clearData() {
this.data.clear();
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private void spawnEntities(
private void anvilProcess(AnvilRecipe recipe, AnvilCraftingContext context, AnvilFallOnLandEvent event) {
int counts = 0;
while (counts < AnvilCraft.config.anvilEfficiency) {
if (!recipe.craft(context)) break;
if (!recipe.craft(context.clearData())) break;
counts++;
}
if (context.isAnvilDamage()) event.setAnvilDamage(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ public class GiantAnvilLandingEventListener {
(blockPosList, level) -> {
for (BlockPos pos : blockPosList) {
BlockPos pos1 = pos.mutable();
AnvilCraftingContext container = new AnvilCraftingContext(level, pos1, null);
AnvilCraftingContext context = new AnvilCraftingContext(level, pos1, null);
Optional<AnvilRecipe> optional = AnvilRecipeManager.getAnvilRecipeList().stream()
.filter(recipe ->
recipe.getAnvilRecipeType() == AnvilRecipeType.BLOCK_SMASH
&& recipe.matches(container, level)
&& recipe.matches(context, level)
).findFirst();
if (optional.isPresent()) {
AnvilRecipe recipe = optional.get();
recipe.craft(container);
recipe.craft(context.clearData());
level.destroyBlock(pos.below(), true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void onLand(@NotNull GiantAnvilFallOnLandEvent event) {
private void anvilProcess(AnvilRecipe recipe, AnvilCraftingContext context) {
int counts = 0;
while (counts < AnvilCraft.config.anvilEfficiency) {
if (!recipe.craft(context)) break;
if (!recipe.craft(context.clearData())) break;
counts++;
}
context.spawnItemEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ private boolean isLoaded(String clazz) {
public void onLoad(String mixinPackage) {
hasZetaPiston = AnvilCraftMixinPlugin.class.getClassLoader()
.getResource("org/violetmoon/zeta/piston/ZetaPistonStructureResolver.class") != null;
hasCreate = this.isLoaded("com/simibubi/create/Create.class");
hasZetaPiston = this.isLoaded("org/violetmoon/zeta/piston/ZetaPistonStructureResolver.class");
hasReiScreen = this.isLoaded("me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.class");
}
Expand Down
Loading