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

Enchantment api #291

Draft
wants to merge 13 commits into
base: 1.19.4
Choose a base branch
from
7 changes: 7 additions & 0 deletions library/content_other/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id("qsl.library")
}

qslLibrary {
libraryName = "content_other"
}
18 changes: 18 additions & 0 deletions library/content_other/enchantment/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id("qsl.module")
}

qslModule {
name = "Quilt Enchantment API"
moduleName = "enchantment"
id = "quilt_enchantment"
description = "An "
library = "content_other"
Platymemo marked this conversation as resolved.
Show resolved Hide resolved
moduleDependencies {
core {
api("qsl_base")
testmodOnly("resource_loader")
}
}
accessWidener()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2023 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.enchantment.api;

import org.jetbrains.annotations.Contract;

import net.minecraft.item.ItemStack;
import net.minecraft.util.random.RandomGenerator;
import net.minecraft.world.World;

/**
* A class that contains contextual information about the enchantment process.
*/
public class EnchantingContext {
protected final int level;
protected final int power;
protected final ItemStack stack;
protected final World world;
protected final RandomGenerator random;
protected final boolean treasureAllowed;

public EnchantingContext(int level, int power, ItemStack stack, World world, RandomGenerator random, boolean treasureAllowed) {
this.level = level;
this.power = power;
this.stack = stack;
this.world = world;
this.random = random;
this.treasureAllowed = treasureAllowed;
}

@Contract("_->new")
public EnchantingContext withLevel(int level) {
return new EnchantingContext(level, this.power, this.stack, this.world, this.random, this.treasureAllowed);
}

@Contract("_->new")
public EnchantingContext withPower(int power) {
return new EnchantingContext(this.level, power, this.stack, this.world, this.random, this.treasureAllowed);
}

@Contract("_,_,_->new")
public EnchantingContext withCoreContext(ItemStack stack, RandomGenerator random, boolean treasureAllowed) {
return new EnchantingContext(this.level, this.power, stack, this.world, random, treasureAllowed);
}

/**
* @return an integer representing the current enchantment level being queried (ie. I, II, III, etc
*/
public int getLevel() {
return this.level;
}

/**
* @return the current power of the enchanting context
*/
public int getPower() {
return this.power;
}

/**
* @return the item stack for the enchantment to be applied to
*/
public ItemStack getStack() {
return this.stack;
}

/**
* @return the world in which the item is being enchanted
*/
public World getWorld() {
return this.world;
}

/**
* @return the random used for enchanting
*/
public RandomGenerator getRandom() {
return this.random;
}

/**
* @return {@code true} if treasure enchantments are allowed, or {@code false} otherwise
*/
public boolean treasureAllowed() {
return this.treasureAllowed;
}

/**
* @return {@code true} if this context ignores power (i.e. Applying enchantments in an anvil), or {@code false} otherwise
*/
public boolean ignoresPower() {
return this.power <= 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2023 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.enchantment.api;

import org.jetbrains.annotations.Contract;

import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.random.RandomGenerator;
import net.minecraft.world.World;

/**
* A class that contains contextual information about the enchantment process.
* <p>
* Contains further information about the entity performing the enchanting.
*/
public class EntityEnchantingContext<E extends Entity> extends EnchantingContext {
protected final E entity;

public EntityEnchantingContext(int level, int power, ItemStack stack, World world, RandomGenerator random, boolean treasureAllowed, E entity) {
super(level, power, stack, world, random, treasureAllowed);
this.entity = entity;
}

@Override
@Contract("_->new")
public EntityEnchantingContext<E> withLevel(int level) {
return new EntityEnchantingContext<>(level, this.power, this.stack, this.world, this.random, this.treasureAllowed, this.entity);
}

@Override
@Contract("_->new")
public EntityEnchantingContext<E> withPower(int power) {
return new EntityEnchantingContext<>(this.level, power, this.stack, this.world, this.random, this.treasureAllowed, this.entity);
}

@Override
@Contract("_,_,_->new")
public EnchantingContext withCoreContext(ItemStack stack, RandomGenerator random, boolean treasureAllowed) {
return new EntityEnchantingContext<>(this.level, this.power, stack, this.world, random, treasureAllowed, this.entity);
}

/**
* @return the entity performing the enchanting
*/
public E getEntity() {
return this.entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2023 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.enchantment.api;

import org.jetbrains.annotations.Contract;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.random.RandomGenerator;
import net.minecraft.world.World;

/**
* A class that contains contextual information about the enchantment process.
* <p>
* Contains further information about the player and block performing the enchanting.
*/
public class PlayerUsingBlockEnchantingContext extends EntityEnchantingContext<PlayerEntity> {
protected final BlockPos pos;

public PlayerUsingBlockEnchantingContext(int level, int power, ItemStack stack, World world, RandomGenerator random, boolean treasureAllowed, PlayerEntity player, BlockPos pos) {
super(level, power, stack, world, random, treasureAllowed, player);
this.pos = pos;
}

@Override
@Contract("_->new")
public PlayerUsingBlockEnchantingContext withLevel(int level) {
return new PlayerUsingBlockEnchantingContext(level, this.power, this.stack, this.world, this.random, this.treasureAllowed, this.entity, this.pos);
}

@Override
@Contract("_->new")
public EntityEnchantingContext<PlayerEntity> withPower(int power) {
return new PlayerUsingBlockEnchantingContext(this.level, power, this.stack, this.world, this.random, this.treasureAllowed, this.entity, this.pos);
}

@Override
@Contract("_,_,_->new")
public EnchantingContext withCoreContext(ItemStack stack, RandomGenerator random, boolean treasureAllowed) {
return new PlayerUsingBlockEnchantingContext(this.level, this.power, stack, this.world, random, treasureAllowed, this.entity, this.pos);
}

/**
* @return the player performing the enchanting
*/
public PlayerEntity getPlayer() {
return this.getEntity();
}

/**
* @return the position of the block being used
*/
public BlockPos getPos() {
return this.pos;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2023 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.enchantment.api;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;

import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.ItemGroup;

/**
* An extension of the default {@link Enchantment} class.
* <p>
* Allows for custom weighting in the enchantment table, as well as custom logic for application in the anvil.
*/
public abstract class QuiltEnchantment extends Enchantment {
public QuiltEnchantment(Rarity rarity, @Nullable EnchantmentTarget type, EquipmentSlot[] slotTypes) {
super(rarity, type, slotTypes);
}

/**
* Return an integer value that represents the weight of this enchantment given the current context.
* <p>
* If 0, then this enchantment won't be added.
* @param context the context of the enchanting
* @return the context-aware weight for the enchantment
*/
public @Range(from = 0, to = Integer.MAX_VALUE) int weightFromContext(EnchantingContext context) {
return this.getRarity().getWeight();
}

/**
* Determines if the given enchantment can be applied under the current context.
* @param context the context of the enchanting
* @return {@code true} if this enchantment can be applied, or {@code false} otherwise
*/
public boolean isAcceptableContext(EnchantingContext context) {
if (!context.ignoresPower() && this.isAcceptablePower(context.getLevel(), context.getPower())) {
return false;
}

return this.isAcceptableItem(context.getStack());
}

/**
* Determines if the provided power is within the valid range for this enchantment at the provided level.
* @param level the level of this enchantment being queried
* @param power the power of the current enchanting context
* @return {@code true} if the power is within the valid range, or {@code false} otherwise
*/
public boolean isAcceptablePower(int level, int power) {
return power >= this.getMinPower(level) && power <= this.getMaxPower(level);
}

/**
* Determines if the given enchantment should be visible in the creative tab.
* @param visibility in what context the stack visibility is tested
* @return {@code true} if this enchantment should be visible, or {@code false} otherwise
*/
public boolean isVisible(ItemGroup.Visibility visibility) {
return true;
}
}
Loading