Port to 1.18.2 (#2824)

* Port to 1.18.2-pre1

* 1.18.2-pre3

* Fix build.

* Fixes and cleanup

* Build fix?

* Update REI
This commit is contained in:
modmuss50 2022-02-28 09:18:27 +00:00 committed by GitHub
parent 4465ffa1aa
commit f7d5139332
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 406 additions and 393 deletions

View file

@ -378,9 +378,9 @@ public class GuiBase<T extends ScreenHandler> extends HandledScreen<T> {
}
@Override
public void onClose() {
public void close() {
closeSelectedTab();
super.onClose();
super.close();
}
@Nullable

View file

@ -30,10 +30,14 @@ import net.minecraft.item.Item;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.ShapedRecipe;
import net.minecraft.tag.Tag;
import net.minecraft.tag.TagKey;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryEntry;
import java.util.Map;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class ShapedRecipeHelper {
public static JsonObject rewriteForNetworkSync(JsonObject shapedRecipeJson) {
@ -49,8 +53,12 @@ public class ShapedRecipeHelper {
if (ingredientEntry instanceof Ingredient.StackEntry stackEntry) {
entries.add(stackEntry.toJson());
} else if (ingredientEntry instanceof Ingredient.TagEntry tagEntry) {
final Tag<Item> tag = tagEntry.tag;
final Item[] items = tag.values().toArray(new Item[0]);
final TagKey<Item> tag = tagEntry.tag;
final Item[] items = streamItemsFromTag(tag).toArray(Item[]::new);
if (items.length == 0) {
throw new IllegalStateException("No items in %s tag".formatted(tag.id()));
}
for (Item item : items) {
JsonObject jsonObject = new JsonObject();
@ -62,6 +70,10 @@ public class ShapedRecipeHelper {
}
}
if (entries.size() == 0) {
throw new IllegalStateException("Cannot write no entries");
}
keys.add(entry.getKey(), entries);
}
@ -69,4 +81,9 @@ public class ShapedRecipeHelper {
shapedRecipeJson.add("key", keys);
return shapedRecipeJson;
}
private static Stream<Item> streamItemsFromTag(TagKey<Item> tag) {
return StreamSupport.stream(Registry.ITEM.iterateEntries(tag).spliterator(), false)
.map(RegistryEntry::value);
}
}

View file

@ -1,56 +0,0 @@
/*
* This file is part of RebornCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 TeamReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package reborncore.common.crafting.ingredient;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import java.util.Collections;
import java.util.List;
public class SimpleTag<T> implements Tag.Identified<T> {
private final List<T> entries;
private final Identifier identifier;
public SimpleTag(List<T> entries, Identifier identifier) {
this.entries = entries;
this.identifier = identifier;
}
@Override
public boolean contains(T entry) {
return entries.contains(entry);
}
@Override
public List<T> values() {
return Collections.unmodifiableList(entries);
}
@Override
public Identifier getId() {
return identifier;
}
}

View file

@ -38,31 +38,26 @@ import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
public class StackIngredient extends RebornIngredient {
private final List<ItemStack> stacks;
private final ItemStack stack;
private final Optional<Integer> count;
private final Optional<NbtCompound> nbt;
private final boolean requireEmptyNbt;
public StackIngredient(List<ItemStack> stacks, Optional<Integer> count, Optional<NbtCompound> nbt, boolean requireEmptyNbt) {
this.stacks = stacks;
this.count = count;
this.nbt = nbt;
public StackIngredient(ItemStack stack, Optional<Integer> count, Optional<NbtCompound> nbt, boolean requireEmptyNbt) {
this.stack = stack;
this.count = Objects.requireNonNull(count);
this.nbt = Objects.requireNonNull(nbt);
this.requireEmptyNbt = requireEmptyNbt;
Validate.isTrue(stacks.size() == 1, "stack size must 1");
}
public StackIngredient(List<ItemStack> stacks, int count, @Nullable NbtCompound nbt, boolean requireEmptyNbt) {
this(stacks, count > 1 ? Optional.of(count) : Optional.empty(), Optional.ofNullable(nbt), requireEmptyNbt);
Validate.isTrue(!stack.isEmpty(), "ingredient must not empty");
}
public static RebornIngredient deserialize(JsonObject json) {
@ -87,7 +82,7 @@ public class StackIngredient extends RebornIngredient {
}
}
return new StackIngredient(Collections.singletonList(new ItemStack(item)), stackSize, tag, requireEmptyTag);
return new StackIngredient(new ItemStack(item), stackSize, tag, requireEmptyTag);
}
@ -96,12 +91,15 @@ public class StackIngredient extends RebornIngredient {
if (itemStack.isEmpty()) {
return false;
}
if (stacks.stream().noneMatch(recipeStack -> recipeStack.getItem() == itemStack.getItem())) {
if (stack.getItem() != itemStack.getItem()) {
return false;
}
if (count.isPresent() && count.get() > itemStack.getCount()) {
return false;
}
if (nbt.isPresent()) {
if (!itemStack.hasNbt()) {
return false;
@ -118,6 +116,7 @@ public class StackIngredient extends RebornIngredient {
return false;
}
}
return !requireEmptyNbt || !itemStack.hasNbt();
}
@ -128,19 +127,17 @@ public class StackIngredient extends RebornIngredient {
@Override
public List<ItemStack> getPreviewStacks() {
return Collections.unmodifiableList(
stacks.stream()
.map(ItemStack::copy)
.peek(itemStack -> itemStack.setCount(count.orElse(1)))
.peek(itemStack -> itemStack.setNbt(nbt.orElse(null)))
.collect(Collectors.toList()));
ItemStack copy = stack.copy();
copy.setCount(count.orElse(1));
copy.setNbt(nbt.orElse(null));
return Collections.singletonList(copy);
}
@Override
public JsonObject toJson(boolean networkSync) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("item", Registry.ITEM.getId(stacks.get(0).getItem()).toString());
jsonObject.addProperty("item", Registry.ITEM.getId(stack.getItem()).toString());
count.ifPresent(integer -> jsonObject.addProperty("count", integer));
if (requireEmptyNbt) {

View file

@ -25,43 +25,40 @@
package reborncore.common.crafting.ingredient;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.fabricmc.fabric.api.tag.TagFactory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.recipe.Ingredient;
import net.minecraft.tag.ServerTagManagerHolder;
import net.minecraft.tag.Tag;
import net.minecraft.tag.TagKey;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryEntry;
import org.apache.commons.lang3.Validate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class TagIngredient extends RebornIngredient {
private final Tag.Identified<Item> tag;
private final TagKey<Item> tag;
private final Optional<Integer> count;
public TagIngredient(Tag.Identified<Item> tag, Optional<Integer> count) {
public TagIngredient(TagKey<Item> tag, Optional<Integer> count) {
this.tag = tag;
this.count = count;
}
public TagIngredient(Tag.Identified<Item> tag, int count) {
this(tag, count > 1 ? Optional.of(count) : Optional.empty());
}
@Override
public boolean test(ItemStack itemStack) {
if (count.isPresent() && count.get() > itemStack.getCount()) {
return false;
}
return tag.contains(itemStack.getItem());
return itemStack.isIn(tag);
}
@Override
@ -71,7 +68,7 @@ public class TagIngredient extends RebornIngredient {
@Override
public List<ItemStack> getPreviewStacks() {
return tag.values().stream().map(ItemStack::new).peek(itemStack -> itemStack.setCount(count.orElse(1))).collect(Collectors.toList());
return streamItems().map(ItemStack::new).peek(itemStack -> itemStack.setCount(count.orElse(1))).collect(Collectors.toList());
}
public static RebornIngredient deserialize(JsonObject json) {
@ -89,17 +86,14 @@ public class TagIngredient extends RebornIngredient {
Validate.isTrue(item != Items.AIR, "item cannot be air");
items.add(item);
}
return new TagIngredient(new SimpleTag<>(items, tagIdent), count);
return new Synced(TagKey.of(Registry.ITEM_KEY, tagIdent), count, items);
}
Identifier identifier = new Identifier(JsonHelper.getString(json, "tag"));
Tag.Identified<Item> tag = TagFactory.of(() -> ServerTagManagerHolder.getTagManager().getOrCreateTagGroup(Registry.ITEM_KEY)).create(identifier);
if (tag == null) {
throw new JsonSyntaxException("Unknown item tag '" + identifier + "'");
}
return new TagIngredient(tag, count);
TagKey<Item> tagKey = TagKey.of(Registry.ITEM_KEY, identifier);
return new TagIngredient(tagKey, count);
}
@Override
@ -109,7 +103,7 @@ public class TagIngredient extends RebornIngredient {
}
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tag", tag.getId().toString());
jsonObject.addProperty("tag", tag.id().toString());
return jsonObject;
}
@ -118,17 +112,41 @@ public class TagIngredient extends RebornIngredient {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tag_server_sync", true);
Item[] items = tag.values().toArray(new Item[0]);
Item[] items = streamItems().toArray(Item[]::new);
jsonObject.addProperty("items", items.length);
for (int i = 0; i < items.length; i++) {
jsonObject.addProperty("item_" + i, Registry.ITEM.getId(items[i]).toString());
}
count.ifPresent(integer -> jsonObject.addProperty("count", integer));
jsonObject.addProperty("tag_identifier", tag.getId().toString());
jsonObject.addProperty("tag_identifier", tag.id().toString());
return jsonObject;
}
protected Stream<Item> streamItems() {
return StreamSupport.stream(Registry.ITEM.iterateEntries(tag).spliterator(), false)
.map(RegistryEntry::value);
}
private static class Synced extends TagIngredient {
private final List<Item> items;
public Synced(TagKey<Item> tag, Optional<Integer> count, List<Item> items) {
super(tag, count);
this.items = items;
}
@Override
public boolean test(ItemStack itemStack) {
return items.contains(itemStack.getItem());
}
@Override
protected Stream<Item> streamItems() {
return items.stream();
}
}
@Override
public int getCount() {
return count.orElse(1);

View file

@ -24,11 +24,11 @@
package reborncore.common.misc;
import net.fabricmc.fabric.api.tag.TagFactory;
import net.minecraft.item.Item;
import net.minecraft.tag.Tag;
import net.minecraft.tag.TagKey;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
public class RebornCoreTags {
public static final Tag.Identified<Item> WATER_EXPLOSION_ITEM = TagFactory.ITEM.create(new Identifier("reborncore", "water_explosion"));
public static final TagKey<Item> WATER_EXPLOSION_ITEM = TagKey.of(Registry.ITEM_KEY, new Identifier("reborncore", "water_explosion"));
}

View file

@ -26,7 +26,7 @@ package reborncore.common.misc;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.tag.Tag;
import net.minecraft.tag.TagKey;
/**
* Tells if an item, block etc. has a tag solely for compatibility with other mods.
@ -41,6 +41,6 @@ public interface TagConvertible<T> {
*
* @return the common tag of this object
*/
Tag.Identified<T> asTag();
TagKey<T> asTag();
}

View file

@ -49,7 +49,7 @@ public abstract class MixinItemEntity extends Entity {
@Inject(method = "tick", at = @At("RETURN"))
public void tick(CallbackInfo info) {
if (!world.isClient && isTouchingWater() && !getStack().isEmpty()) {
if (RebornCoreTags.WATER_EXPLOSION_ITEM.contains(getStack().getItem())) {
if (getStack().isIn(RebornCoreTags.WATER_EXPLOSION_ITEM)) {
world.createExplosion(this, getX(), getY(), getZ(), 2F, Explosion.DestructionType.BREAK);
this.remove(RemovalReason.KILLED);
}

View file

@ -25,10 +25,11 @@
"reborncore.common.mixins.json"
],
"depends": {
"fabricloader": ">=0.12.12",
"fabricloader": ">=0.13.3",
"fabric": ">=0.40.0",
"team_reborn_energy": ">=2.2.0",
"fabric-biome-api-v1": ">=3.0.0"
"fabric-biome-api-v1": ">=3.0.0",
"minecraft": "~1.18.2-beta.1"
},
"authors": [
"Team Reborn",

View file

@ -8,7 +8,7 @@ accessible class net/minecraft/recipe/Ingredient$Entry
accessible class net/minecraft/recipe/Ingredient$TagEntry
accessible class net/minecraft/recipe/Ingredient$StackEntry
accessible field net/minecraft/recipe/Ingredient entries [Lnet/minecraft/recipe/Ingredient$Entry;
accessible field net/minecraft/recipe/Ingredient$TagEntry tag Lnet/minecraft/tag/Tag;
accessible field net/minecraft/recipe/Ingredient$TagEntry tag Lnet/minecraft/tag/TagKey;
accessible method net/minecraft/client/gui/screen/ingame/HandledScreen getSlotAt (DD)Lnet/minecraft/screen/slot/Slot;