Skip to content

Commit

Permalink
switch most custom item cooldowns to native cooldowns
Browse files Browse the repository at this point in the history
  • Loading branch information
Abbie5 authored and artemisSystem committed Dec 10, 2024
1 parent ca6264b commit 8654e44
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,17 @@ public static void init(TriConsumer<ItemLike, ResourceLocation, ClampedItemPrope
consumer.accept(BotaniaItems.manaweaveBoots, holidayId, holidayGetter);
consumer.accept(BotaniaItems.manaweaveLegs, holidayId, holidayGetter);

ClampedItemPropertyFunction ringOnGetter = (stack, worldIn, entityIn, seed) -> RingOfMagnetizationItem.getCooldown(stack) <= 0 ? 1 : 0;
ClampedItemPropertyFunction ringOnGetter = (stack, worldIn, entityIn, seed) -> {
if (entityIn instanceof Player player) {
if (player.getCooldowns().isOnCooldown(stack.getItem())) {
return 0;
} else {
return 1;
}
} else {
return 0;
}
};
consumer.accept(BotaniaItems.magnetRing, botaniaRL("active"), ringOnGetter);
consumer.accept(BotaniaItems.magnetRingGreater, botaniaRL("active"), ringOnGetter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void onDrawScreenPost(GuiGraphics gui, float partialTicks) {
ItemStack tiara = EquipmentHandler.findOrEmpty(BotaniaItems.flightTiara, mc.player);
if (!tiara.isEmpty()) {
profiler.push("flugelTiara");
FlugelTiaraItem.ClientLogic.renderHUD(gui, mc.player, tiara);
FlugelTiaraItem.ClientLogic.renderHUD(gui, partialTicks, mc.player, tiara);
profiler.pop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class CloakOfVirtueItem extends BaubleItem {
private static final ResourceLocation texture = ResourceLocation.parse(ResourcesLib.MODEL_HOLY_CLOAK);
private static final ResourceLocation textureGlow = ResourceLocation.parse(ResourcesLib.MODEL_HOLY_CLOAK_GLOW);

private static final String TAG_COOLDOWN = "cooldown";
private static final String TAG_IN_EFFECT = "inEffect";

public CloakOfVirtueItem(Properties props) {
Expand All @@ -57,13 +56,13 @@ public float onPlayerDamage(Player player, DamageSource src, float amount) {

if (!stack.isEmpty() && !isInEffect(stack)) {
CloakOfVirtueItem cloak = (CloakOfVirtueItem) stack.getItem();
int cooldown = getCooldown(stack);
boolean isOnCooldown = player.getCooldowns().isOnCooldown(this);

// Used to prevent StackOverflows with mobs that deal damage when damaged
setInEffect(stack, true);
MutableFloat mutAmount = new MutableFloat(amount);
if (cooldown == 0 && cloak.effectOnDamage(src, mutAmount, player, stack)) {
setCooldown(stack, cloak.getCooldownTime(stack));
if (!isOnCooldown && cloak.effectOnDamage(src, mutAmount, player, stack)) {
player.getCooldowns().addCooldown(cloak, cloak.getCooldownTime(stack));
}
setInEffect(stack, false);
return mutAmount.getValue();
Expand All @@ -72,14 +71,6 @@ public float onPlayerDamage(Player player, DamageSource src, float amount) {
return amount;
}

@Override
public void onWornTick(ItemStack stack, LivingEntity living) {
int cooldown = getCooldown(stack);
if (cooldown > 0) {
setCooldown(stack, cooldown - 1);
}
}

public static class Renderer implements AccessoryRenderer {
private static CloakModel model = null;

Expand Down Expand Up @@ -127,14 +118,6 @@ public int getCooldownTime(ItemStack stack) {
return 200;
}

public static int getCooldown(ItemStack stack) {
return ItemNBTHelper.getInt(stack, TAG_COOLDOWN, 0);
}

public static void setCooldown(ItemStack stack, int cooldown) {
ItemNBTHelper.setInt(stack, TAG_COOLDOWN, cooldown);
}

public static boolean isInEffect(ItemStack stack) {
return ItemNBTHelper.getBoolean(stack, TAG_IN_EFFECT, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,18 @@ public void onWornTick(ItemStack stack, LivingEntity living) {
&& !ItemNBTHelper.getBoolean(stack, TAG_INFINITE_FLIGHT, false)) {
newTime--;
}
final int maxCd = 80;
int cooldown = ItemNBTHelper.getInt(stack, TAG_DASH_COOLDOWN, 0);
if (!wasSprting && isSprinting && cooldown == 0) {
final int maxCd = 40;
boolean isOnCooldown = player.getCooldowns().isOnCooldown(this);
if (!wasSprting && isSprinting && !isOnCooldown) {
player.setDeltaMovement(player.getDeltaMovement().add(look.x, 0, look.z));
player.level().playSound(null, player.getX(), player.getY(), player.getZ(), BotaniaSounds.dash, SoundSource.PLAYERS, 1F, 1F);
ItemNBTHelper.setInt(stack, TAG_DASH_COOLDOWN, maxCd);
player.getCooldowns().addCooldown(this, maxCd);
ItemNBTHelper.setBoolean(stack, TAG_BOOST_PENDING, true);
} else if (cooldown > 0) {
} else if (isOnCooldown) {
if (ItemNBTHelper.getBoolean(stack, TAG_BOOST_PENDING, false)) {
living.moveRelative(5F, new Vec3(0F, 0F, 1F));
ItemNBTHelper.removeEntry(stack, TAG_BOOST_PENDING);
}
ItemNBTHelper.setInt(stack, TAG_DASH_COOLDOWN, cooldown - 2);
}
} else {
boolean wasGliding = ItemNBTHelper.getBoolean(stack, TAG_GLIDING, false);
Expand Down Expand Up @@ -493,7 +492,7 @@ private static int estimateAdditionalNumRowsRendered(Player player) {
return 0;
}

public static void renderHUD(GuiGraphics gui, Player player, ItemStack stack) {
public static void renderHUD(GuiGraphics gui, float partialTicks, Player player, ItemStack stack) {
int u = Math.max(1, getVariant(stack)) * 9 - 9;
int v = 0;

Expand All @@ -520,7 +519,7 @@ public static void renderHUD(GuiGraphics gui, Player player, ItemStack stack) {
}

if (player.getAbilities().flying) {
int width = ItemNBTHelper.getInt(stack, TAG_DASH_COOLDOWN, 0);
int width = (int)(player.getCooldowns().getCooldownPercent(stack.getItem(), partialTicks) * 80);
RenderSystem.setShaderColor(1F, 1F, 1F, 1F);
if (width > 0) {
gui.fill(xo, y - 2, xo + 80, y - 1, 0x88000000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

public class RingOfDexterousMotionItem extends BaubleItem {

public static final String TAG_DODGE_COOLDOWN = "dodgeCooldown";
public static final int MAX_CD = 20;

private static boolean oldLeftDown, oldRightDown, oldForwardDown, oldBackDown;
Expand All @@ -46,7 +45,7 @@ public static void onKeyDown() {
}

ItemStack ringStack = EquipmentHandler.findOrEmpty(BotaniaItems.dodgeRing, mc.player);
if (ringStack.isEmpty() || ItemNBTHelper.getInt(ringStack, TAG_DODGE_COOLDOWN, 0) > 0) {
if (ringStack.isEmpty() || mc.player.getCooldowns().isOnCooldown(ringStack.getItem())) {
return;
}

Expand Down Expand Up @@ -92,8 +91,7 @@ public static void renderHUD(GuiGraphics gui, Player player, ItemStack stack, fl
int y = Minecraft.getInstance().getWindow().getGuiScaledHeight() / 2 + 20;

if (!player.getAbilities().flying) {
int cd = ItemNBTHelper.getInt(stack, TAG_DODGE_COOLDOWN, 0);
int width = Math.min((int) ((cd - pticks) * 2), 40);
int width = (int) (player.getCooldowns().getCooldownPercent(stack.getItem(), pticks) * 40);
RenderSystem.setShaderColor(1F, 1F, 1F, 1F);
if (width > 0) {
gui.fill(xo, y - 2, xo + 40, y - 1, 0x88000000);
Expand All @@ -105,14 +103,6 @@ public static void renderHUD(GuiGraphics gui, Player player, ItemStack stack, fl
}
}

@Override
public void onWornTick(ItemStack stack, LivingEntity living) {
int cd = ItemNBTHelper.getInt(stack, TAG_DODGE_COOLDOWN, 0);
if (cd > 0) {
ItemNBTHelper.setInt(stack, TAG_DODGE_COOLDOWN, cd - 1);
}
}

private static void dodge(Player player, Direction dir) {
if (player.getAbilities().flying || !player.onGround() || dir == Direction.UP || dir == Direction.DOWN) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

public class RingOfMagnetizationItem extends BaubleItem {

private static final String TAG_COOLDOWN = "cooldown";

private final int range;

public RingOfMagnetizationItem(Properties props) {
Expand All @@ -46,7 +44,7 @@ public RingOfMagnetizationItem(Properties props, int range) {
public static void onTossItem(Player player) {
ItemStack ring = EquipmentHandler.findOrEmpty(s -> s.getItem() instanceof RingOfMagnetizationItem, player);
if (!ring.isEmpty()) {
setCooldown(ring, 100);
player.getCooldowns().addCooldown(ring.getItem(), 100);
}
}

Expand All @@ -58,44 +56,43 @@ public void onWornTick(ItemStack stack, LivingEntity living) {
return;
}

int cooldown = getCooldown(stack);

if (BotaniaAPI.instance().hasSolegnoliaAround(living)) {
if (cooldown < 0) {
setCooldown(stack, 2);
if (living instanceof Player player) {
boolean isOnCooldown = player.getCooldowns().isOnCooldown(this);
if (BotaniaAPI.instance().hasSolegnoliaAround(living)) {
if (!isOnCooldown) {
player.getCooldowns().addCooldown(this, 2);
}
return;
}
return;
}

if (cooldown <= 0) {
if (living.isShiftKeyDown() == BotaniaConfig.common().invertMagnetRing()) {
double x = living.getX();
double y = living.getY() + 0.75;
double z = living.getZ();

int range = ((RingOfMagnetizationItem) stack.getItem()).range;
List<ItemEntity> items = living.level().getEntitiesOfClass(ItemEntity.class, new AABB(x - range, y - range, z - range, x + range, y + range, z + range));
int pulled = 0;
for (ItemEntity item : items) {
if (((RingOfMagnetizationItem) stack.getItem()).canPullItem(item)) {
if (pulled > 200) {
break;
if (!isOnCooldown) {
if (living.isShiftKeyDown() == BotaniaConfig.common().invertMagnetRing()) {
double x = living.getX();
double y = living.getY() + 0.75;
double z = living.getZ();

int range = ((RingOfMagnetizationItem) stack.getItem()).range;
List<ItemEntity> items = living.level().getEntitiesOfClass(ItemEntity.class, new AABB(x - range, y - range, z - range, x + range, y + range, z + range));
int pulled = 0;
for (ItemEntity item : items) {
if (((RingOfMagnetizationItem) stack.getItem()).canPullItem(item)) {
if (pulled > 200) {
break;
}

MathHelper.setEntityMotionFromVector(item, new Vec3(x, y, z), 0.45F);
if (living.level().isClientSide) {
boolean red = living.level().random.nextBoolean();
float r = red ? 1F : 0F;
float b = red ? 0F : 1F;
SparkleParticleData data = SparkleParticleData.sparkle(1F, r, 0F, b, 3);
living.level().addParticle(data, item.getX(), item.getY(), item.getZ(), 0, 0, 0);
}
pulled++;
}

MathHelper.setEntityMotionFromVector(item, new Vec3(x, y, z), 0.45F);
if (living.level().isClientSide) {
boolean red = living.level().random.nextBoolean();
float r = red ? 1F : 0F;
float b = red ? 0F : 1F;
SparkleParticleData data = SparkleParticleData.sparkle(1F, r, 0F, b, 3);
living.level().addParticle(data, item.getX(), item.getY(), item.getZ(), 0, 0, 0);
}
pulled++;
}
}
}
} else {
setCooldown(stack, cooldown - 1);
}
}

Expand Down Expand Up @@ -128,11 +125,4 @@ private boolean canPullItem(ItemEntity item) {
return true;
}

public static int getCooldown(ItemStack stack) {
return ItemNBTHelper.getInt(stack, TAG_COOLDOWN, 0);
}

public static void setCooldown(ItemStack stack, int cooldown) {
ItemNBTHelper.setInt(stack, TAG_COOLDOWN, cooldown);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class ShadedMesaRodItem extends Item {
private static final Predicate<Entity> CAN_TARGET = e -> !e.isSpectator() && e.isAlive() && !e.getType().is(BLACKLIST);

private static final String TAG_TICKS_TILL_EXPIRE = "ticksTillExpire";
private static final String TAG_TICKS_COOLDOWN = "ticksCooldown";
private static final String TAG_TARGET = "target";
private static final String TAG_DIST = "dist";

Expand Down Expand Up @@ -81,22 +80,16 @@ public void inventoryTick(ItemStack stack, Level world, Entity entity, int slot,
}

int ticksTillExpire = ItemNBTHelper.getInt(stack, TAG_TICKS_TILL_EXPIRE, 0);
int ticksCooldown = ItemNBTHelper.getInt(stack, TAG_TICKS_COOLDOWN, 0);

if (ticksTillExpire == 0) {
ItemNBTHelper.setInt(stack, TAG_TARGET, -1);
ItemNBTHelper.setDouble(stack, TAG_DIST, -1);
}

if (ticksCooldown > 0) {
ticksCooldown--;
}

if (ticksTillExpire >= 0) {
ticksTillExpire--;
}
ItemNBTHelper.setInt(stack, TAG_TICKS_TILL_EXPIRE, ticksTillExpire);
ItemNBTHelper.setInt(stack, TAG_TICKS_COOLDOWN, ticksCooldown);
}

@SoftImplement("IItemExtension")
Expand Down Expand Up @@ -127,10 +120,9 @@ public static InteractionResult onAttack(Player player, Level level, Interaction
public InteractionResultHolder<ItemStack> use(Level world, Player player, @NotNull InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
int targetID = ItemNBTHelper.getInt(stack, TAG_TARGET, -1);
int ticksCooldown = ItemNBTHelper.getInt(stack, TAG_TICKS_COOLDOWN, 0);
double length = ItemNBTHelper.getDouble(stack, TAG_DIST, -1);

if (ticksCooldown == 0) {
if (!player.getCooldowns().isOnCooldown(this)) {
Entity target = null;
if (targetID != -1 && player.level().getEntity(targetID) != null) {
Entity taritem = player.level().getEntity(targetID);
Expand Down Expand Up @@ -268,7 +260,7 @@ private static void leftClick(Player player) {
}
target.setDeltaMovement(moveVector.multiply(3, 1.5, 3));
}
ItemNBTHelper.setInt(stack, TAG_TICKS_COOLDOWN, 10);
player.getCooldowns().addCooldown(stack.getItem(), 10);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void handle(MinecraftServer server, ServerPlayer player) {
}

player.causeFoodExhaustion(0.3F);
ItemNBTHelper.setInt(ringStack, RingOfDexterousMotionItem.TAG_DODGE_COOLDOWN, RingOfDexterousMotionItem.MAX_CD);
player.getCooldowns().addCooldown(ringStack.getItem(), RingOfDexterousMotionItem.MAX_CD);
});
}
}

0 comments on commit 8654e44

Please sign in to comment.