use of net.minecraft.world.Container in project SpongeCommon by SpongePowered.
the class InventoryEventFactory method displayContainer.
@org.checkerframework.checker.nullness.qual.Nullable
public static AbstractContainerMenu displayContainer(final ServerPlayer player, final Inventory inventory, final Component displayName) {
final net.minecraft.world.inventory.AbstractContainerMenu previousContainer = player.containerMenu;
final net.minecraft.world.inventory.AbstractContainerMenu container;
Optional<ViewableInventory> viewable = inventory.asViewable();
if (viewable.isPresent()) {
if (viewable.get() instanceof MenuProvider) {
MenuProvider namedContainerProvider = (MenuProvider) viewable.get();
if (displayName != null) {
namedContainerProvider = new SimpleMenuProvider(namedContainerProvider, SpongeAdventure.asVanilla(displayName));
}
player.openMenu(namedContainerProvider);
} else if (viewable.get() instanceof CarriedInventory) {
Optional carrier = ((CarriedInventory) viewable.get()).carrier();
if (carrier.get() instanceof AbstractHorse) {
player.openHorseInventory(((AbstractHorse) carrier.get()), ((Container) viewable.get()));
}
} else if (viewable.get() instanceof Merchant) {
Merchant merchant = (Merchant) viewable.get();
net.minecraft.network.chat.Component display = null;
int level = 0;
if (merchant instanceof Villager) {
display = ((Villager) merchant).getDisplayName();
level = ((Villager) merchant).getVillagerData().getLevel();
} else if (merchant instanceof WanderingTrader) {
display = ((WanderingTrader) merchant).getDisplayName();
level = 1;
}
if (displayName != null) {
display = SpongeAdventure.asVanilla(displayName);
}
OptionalInt containerId = player.openMenu(new SimpleMenuProvider((id, playerInv, p) -> new MerchantMenu(id, playerInv, merchant), display));
if (containerId.isPresent() && !merchant.getOffers().isEmpty()) {
player.sendMerchantOffers(containerId.getAsInt(), merchant.getOffers(), level, merchant.getVillagerXp(), merchant.showProgressBar(), merchant.canRestock());
}
}
}
container = player.containerMenu;
if (previousContainer == container) {
return null;
}
if (!InventoryEventFactory.callInteractContainerOpenEvent(player)) {
return null;
}
if (container instanceof ContainerBridge) {
// This overwrites the normal container behaviour and allows viewing
// inventories that are more than 8 blocks away
// This currently actually only works for the Containers mixed into
// by InteractableContainerMixin ; but throws no errors for other
// containers
// Allow viewing inventory; except when dead
((ContainerBridge) container).bridge$setCanInteractWith(p -> !p.removed);
}
return container;
}
use of net.minecraft.world.Container in project SpongeCommon by SpongePowered.
the class SpongeStonecuttingRecipeSerializer method fromJson.
@SuppressWarnings("unchecked")
@Override
public R fromJson(ResourceLocation recipeId, JsonObject json) {
final String group = GsonHelper.getAsString(json, Constants.Recipe.GROUP, "");
final Ingredient ingredient = IngredientUtil.spongeDeserialize(json.get(Constants.Recipe.STONECUTTING_INGREDIENT));
final Function<Container, ItemStack> resultFunction = IngredientResultUtil.deserializeResultFunction(json);
final ItemStack spongeStack = IngredientResultUtil.deserializeItemStack(json.getAsJsonObject(Constants.Recipe.SPONGE_RESULT));
if (spongeStack != null) {
return (R) new SpongeStonecuttingRecipe(recipeId, group, ingredient, spongeStack, resultFunction);
}
final String type = GsonHelper.getAsString(json, Constants.Recipe.RESULT);
final int count = GsonHelper.getAsInt(json, Constants.Recipe.COUNT);
final ItemStack itemstack = new ItemStack(Registry.ITEM.get(new ResourceLocation(type)), count);
return (R) new SpongeStonecuttingRecipe(recipeId, group, ingredient, itemstack, resultFunction);
}
use of net.minecraft.world.Container in project SpongeCommon by SpongePowered.
the class RecipeManagerMixin_API method findMatchingRecipe.
@Override
@SuppressWarnings("unchecked")
public Optional<Recipe> findMatchingRecipe(final Inventory inventory, final ServerWorld world) {
Preconditions.checkNotNull(inventory);
Preconditions.checkNotNull(world);
if (inventory instanceof AbstractFurnaceBlockEntity) {
final net.minecraft.world.item.crafting.RecipeType<? extends AbstractCookingRecipe> type = ((AbstractFurnaceBlockEntityAccessor) inventory).accessor$recipeType();
return this.<Container, AbstractCookingRecipe>shadow$getRecipeFor((net.minecraft.world.item.crafting.RecipeType<AbstractCookingRecipe>) type, (Container) inventory, (net.minecraft.world.level.Level) world).map(Recipe.class::cast);
}
if (inventory instanceof CampfireBlockEntity) {
return this.shadow$getRecipeFor(net.minecraft.world.item.crafting.RecipeType.CAMPFIRE_COOKING, (Container) inventory, (net.minecraft.world.level.Level) world).map(Recipe.class::cast);
}
if (inventory instanceof CraftingMenu) {
final CraftingContainer craftingInventory = ((CraftingMenuAccessor) inventory).accessor$craftSlots();
return this.shadow$getRecipeFor(net.minecraft.world.item.crafting.RecipeType.CRAFTING, craftingInventory, (net.minecraft.world.level.Level) world).map(Recipe.class::cast);
}
if (inventory instanceof InventoryMenu) {
final CraftingContainer craftingInventory = ((InventoryMenuAccessor) inventory).accessor$craftSlots();
return this.shadow$getRecipeFor(net.minecraft.world.item.crafting.RecipeType.CRAFTING, craftingInventory, (net.minecraft.world.level.Level) world).map(Recipe.class::cast);
}
if (inventory instanceof StonecutterMenu) {
final Container stonecutterInventory = ((StonecutterMenu) inventory).container;
return this.shadow$getRecipeFor(net.minecraft.world.item.crafting.RecipeType.STONECUTTING, stonecutterInventory, (net.minecraft.world.level.Level) world).map(Recipe.class::cast);
}
return Optional.empty();
}
use of net.minecraft.world.Container in project SpongeCommon by SpongePowered.
the class SpongeCookingRecipeSerializer method fromJson.
@Override
public R fromJson(final ResourceLocation recipeId, final JsonObject json) {
final String group = GsonHelper.getAsString(json, Constants.Recipe.GROUP, "");
final JsonElement jsonelement = GsonHelper.isArrayNode(json, Constants.Recipe.COOKING_INGREDIENT) ? GsonHelper.getAsJsonArray(json, Constants.Recipe.COOKING_INGREDIENT) : GsonHelper.getAsJsonObject(json, Constants.Recipe.COOKING_INGREDIENT);
final Ingredient ingredient = IngredientUtil.spongeDeserialize(jsonelement);
final String result = GsonHelper.getAsString(json, Constants.Recipe.RESULT);
final ResourceLocation resourcelocation = new ResourceLocation(result);
final ItemStack itemstack = new ItemStack(Registry.ITEM.getOptional(resourcelocation).orElseThrow(() -> new IllegalStateException("Item: " + result + " does not exist")));
final ItemStack spongeStack = IngredientResultUtil.deserializeItemStack(json.getAsJsonObject(Constants.Recipe.SPONGE_RESULT));
final Function<Container, ItemStack> resultFunction = IngredientResultUtil.deserializeResultFunction(json);
final float exp = GsonHelper.getAsFloat(json, Constants.Recipe.COOKING_EXP, 0.0F);
final int cookTime = GsonHelper.getAsInt(json, Constants.Recipe.COOKING_TIME, this.defaultCookingTime);
return this.create(recipeId, group, ingredient, spongeStack == null ? itemstack : spongeStack, exp, cookTime, resultFunction);
}
use of net.minecraft.world.Container in project SpongeCommon by SpongePowered.
the class SpongeSmithingRecipeSerializer method fromJson.
@SuppressWarnings("unchecked")
@Override
public R fromJson(ResourceLocation recipeId, JsonObject json) {
final Ingredient base = IngredientUtil.spongeDeserialize(json.get(Constants.Recipe.SMITHING_BASE_INGREDIENT));
final Ingredient addition = IngredientUtil.spongeDeserialize(json.get(Constants.Recipe.SMITHING_ADDITION_INGREDIENT));
final Function<Container, ItemStack> resultFunction = IngredientResultUtil.deserializeResultFunction(json);
ItemStack itemstack = ShapedRecipe.itemFromJson(GsonHelper.getAsJsonObject(json, Constants.Recipe.RESULT));
final ItemStack spongeStack = IngredientResultUtil.deserializeItemStack(json.getAsJsonObject(Constants.Recipe.SPONGE_RESULT));
return (R) new SpongeSmithingRecipe(recipeId, base, addition, spongeStack == null ? itemstack : spongeStack, resultFunction);
}
Aggregations