Skip to content

Commit

Permalink
Add fossil items, incenses and item use logic config
Browse files Browse the repository at this point in the history
  • Loading branch information
KaptainWutax committed Jan 24, 2025
1 parent 45b5c87 commit 3edef65
Show file tree
Hide file tree
Showing 21 changed files with 240 additions and 2 deletions.
2 changes: 2 additions & 0 deletions common/src/main/java/abeshutt/staracademy/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import abeshutt.staracademy.data.entity.EntityPredicate;
import abeshutt.staracademy.data.item.ItemPredicate;
import abeshutt.staracademy.data.tile.TilePredicate;
import abeshutt.staracademy.util.ItemUseLogic;
import abeshutt.staracademy.world.roll.IntRoll;
import com.cobblemon.mod.common.api.pokemon.PokemonProperties;
import com.google.gson.Gson;
Expand Down Expand Up @@ -32,6 +33,7 @@ public abstract class Config {
.registerTypeAdapter(BlockPos.class, Adapters.BLOCK_POS)
.registerTypeAdapter(Identifier.class, Adapters.IDENTIFIER)
.registerTypeAdapter(PokemonProperties.class, Adapters.POKEMON_PROPERTIES)
.registerTypeAdapter(ItemUseLogic.class, Adapters.of(ItemUseLogic::new, false))
.create();

public abstract void write() throws IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package abeshutt.staracademy.config;

import abeshutt.staracademy.util.ItemUseLogic;
import com.google.gson.annotations.Expose;
import net.minecraft.item.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static abeshutt.staracademy.util.ItemUseLogic.CommandExecutionContext.PLAYER;
import static abeshutt.staracademy.util.ItemUseLogic.CommandExecutionContext.SERVER;

public class ItemLogicConfig extends FileConfig {

@Expose private List<ItemUseLogic> use;

@Override
public String getPath() {
return "item_logic";
}

public Optional<ItemUseLogic> getUseLogic(ItemStack stack) {
for(ItemUseLogic entry : this.use) {
if(entry.getPredicate().test(stack)) {
return Optional.of(entry);
}
}

return Optional.empty();
}

@Override
protected void reset() {
this.use = new ArrayList<>();
this.use.add(new ItemUseLogic("academy:hunt", PLAYER, "/hunt"));
this.use.add(new ItemUseLogic("academy:shiny_incense", SERVER, "/yacb boost ${user_uuid} 4 24000 shiny"));
this.use.add(new ItemUseLogic("academy:strong_shiny_incense", SERVER, "/yacb boost ${user_uuid} 8 36000 shiny"));
this.use.add(new ItemUseLogic("academy:uber_shiny_incense", SERVER, "/yacb boost ${user_uuid} 12 72000 shiny"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ModConfigs extends ModRegistries {
public static WardrobeConfig WARDROBE;
public static NPCConfig NPC;
public static DuelingConfig DUELING;
public static ItemLogicConfig ITEM_LOGIC;

public static void register(boolean initialization) {
TILE_GROUPS = new TileGroupsConfig().read();
Expand All @@ -36,6 +37,7 @@ public static void register(boolean initialization) {
WARDROBE = new WardrobeConfig().read();
NPC = new NPCConfig().read();
DUELING = new DuelingConfig().read();
ITEM_LOGIC = new ItemLogicConfig().read();

if(!initialization) {
ArrayList<Runnable> actions = new ArrayList<>(POST_LOAD);
Expand Down
16 changes: 14 additions & 2 deletions common/src/main/java/abeshutt/staracademy/init/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,31 @@
public class ModItems extends ModRegistries {

public static RegistrySupplier<Item> STAR_BADGE;
public static RegistrySupplier<HuntItem> HUNT;
public static RegistrySupplier<Item> HUNT;
public static RegistrySupplier<DuelingGloveItem> DUELING_GLOVE;
public static RegistrySupplier<OutfitItem> OUTFIT;
public static RegistrySupplier<SafariTicketItem> SAFARI_TICKET;
public static RegistrySupplier<SlingshotItem> SLINGSHOT;
public static RegistrySupplier<Item> HA_FOSSIL;
public static RegistrySupplier<Item> MAX_IV_FOSSIL;
public static RegistrySupplier<Item> SHINY_FOSSIL;
public static RegistrySupplier<Item> SHINY_INCENSE;
public static RegistrySupplier<Item> STRONG_SHINY_INCENSE;
public static RegistrySupplier<Item> UBER_SHINY_INCENSE;

public static void register() {
STAR_BADGE = register("star_badge", StarBadgeItem::new);
HUNT = register("hunt", HuntItem::new);
HUNT = register("hunt", () -> new Item(new Item.Settings().maxCount(1).fireproof()));
DUELING_GLOVE = register("dueling_glove", DuelingGloveItem::new);
OUTFIT = register("outfit", OutfitItem::new);
SAFARI_TICKET = register("safari_ticket", SafariTicketItem::new);
SLINGSHOT = register("slingshot", SlingshotItem::new);
HA_FOSSIL = register("ha_fossil", () -> new Item(new Item.Settings().maxCount(1)));
MAX_IV_FOSSIL = register("max_iv_fossil", () -> new Item(new Item.Settings().maxCount(1)));
SHINY_FOSSIL = register("shiny_fossil", () -> new Item(new Item.Settings().maxCount(1)));
SHINY_INCENSE = register("shiny_incense", () -> new Item(new Item.Settings().maxCount(1)));
STRONG_SHINY_INCENSE = register("strong_shiny_incense", () -> new Item(new Item.Settings().maxCount(1)));
UBER_SHINY_INCENSE = register("uber_shiny_incense", () -> new Item(new Item.Settings().maxCount(1)));
}

public static <V extends Item> RegistrySupplier<V> register(Identifier id, Supplier<V> item) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package abeshutt.staracademy.mixin;

import abeshutt.staracademy.init.ModConfigs;
import abeshutt.staracademy.util.ItemUseLogic;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import static abeshutt.staracademy.util.ItemUseLogic.CommandExecutionContext.*;

@Mixin(ItemStack.class)
public class MixinItemStack {

@Inject(method = "use", at = @At("HEAD"), cancellable = true)
public void use(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> ci) {
if(!user.getWorld().isClient() && user.getServer() != null) {
ItemUseLogic logic = ModConfigs.ITEM_LOGIC.getUseLogic((ItemStack)(Object)this).orElse(null);
if(logic == null) return;

for(String command : logic.getCommands()) {
command = command.replace("${user_uuid}", user.getUuid().toString());

if(logic.getContext() == PLAYER) {
user.getServer().getCommandManager().executeWithPrefix(user.getCommandSource(), command);
} else if(logic.getContext() == SERVER) {
user.getServer().getCommandManager().executeWithPrefix(user.getServer().getCommandSource(), command);
}
}

ci.setReturnValue(TypedActionResult.success(user.getStackInHand(hand)));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package abeshutt.staracademy.mixin;

import com.cobblemon.mod.common.util.PlayerExtensionsKt;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(PlayerExtensionsKt.class)
public class MixinPlayerEntity {

@Inject(method = "giveOrDropItemStack", at = @At("HEAD"))
private static void giveOrDropItemStack(PlayerEntity player, ItemStack stack, boolean playSound, CallbackInfo ci) {

}

}
78 changes: 78 additions & 0 deletions common/src/main/java/abeshutt/staracademy/util/ItemUseLogic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package abeshutt.staracademy.util;

import abeshutt.staracademy.data.adapter.Adapters;
import abeshutt.staracademy.data.item.ItemPredicate;
import abeshutt.staracademy.data.serializable.ISerializable;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.nbt.NbtCompound;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static abeshutt.staracademy.data.adapter.basic.EnumAdapter.Mode.NAME;

public class ItemUseLogic implements ISerializable<NbtCompound, JsonObject> {

private ItemPredicate predicate;
private final List<String> commands;
private CommandExecutionContext context;

public ItemUseLogic() {
this.commands = new ArrayList<>();
}

public ItemUseLogic(String predicate, CommandExecutionContext context, String... commands) {
this.predicate = ItemPredicate.of(predicate, true).orElseThrow();
this.context = context;
this.commands = new ArrayList<>(Arrays.asList(commands));
}

public ItemPredicate getPredicate() {
return this.predicate;
}

public List<String> getCommands() {
return this.commands;
}

public CommandExecutionContext getContext() {
return this.context;
}

@Override
public Optional<JsonObject> writeJson() {
return Optional.of(new JsonObject()).map(json -> {
Adapters.ITEM_PREDICATE.writeJson(this.predicate).ifPresent(tag -> json.add("predicate", tag));

JsonArray commands = new JsonArray();
this.commands.forEach(command -> Adapters.UTF_8.writeJson(command).ifPresent(commands::add));
json.add("commands", commands);
Adapters.ofEnum(CommandExecutionContext.class, NAME).writeJson(this.context)
.ifPresent(tag -> json.add("context", tag));
return json;
});
}

@Override
public void readJson(JsonObject json) {
this.predicate = Adapters.ITEM_PREDICATE.readJson(json.get("predicate")).orElse(ItemPredicate.FALSE);
this.commands.clear();

if(json.get("commands") instanceof JsonArray array) {
for(JsonElement element : array) {
Adapters.UTF_8.readJson(element).ifPresent(this.commands::add);
}
}

this.context = Adapters.ofEnum(CommandExecutionContext.class, NAME).readJson(json.get("context")).orElse(CommandExecutionContext.SERVER);
}

public enum CommandExecutionContext {
PLAYER, SERVER
}

}
1 change: 1 addition & 0 deletions common/src/main/resources/academy-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"MixinCommandManager",
"MixinEntity",
"MixinGameProfile",
"MixinItemStack",
"MixinLivingEntity",
"MixinPlayerScreenHandler",
"MixinServerWorld",
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/resources/assets/academy/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"item.academy.dueling_glove": "Dueling Glove",
"item.academy.safari_ticket": "Safari Ticket",
"item.academy.slingshot": "Slingshot",
"item.academy.ha_fossil": "HA Fossil",
"item.academy.max_iv_fossil": "Max IV Fossil",
"item.academy.shiny_fossil": "Shiny Fossil",
"item.academy.shiny_incense": "Shiny Incense",
"item.academy.strong_shiny_incense": "Strong Shiny Incense",
"item.academy.uber_shiny_incense": "Uber Shiny Incense",
"item.academy.outfit": "Outfit",
"item.academy.outfit.classy1_hat": "Classy Hat I",
"item.academy.outfit.classy1_shirt": "Classy Shirt I",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "academy:item/ha_fossil"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "academy:item/max_iv_fossil"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "academy:item/shiny_fossil"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "academy:item/shiny_incense"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "academy:item/strong_shiny_incense"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "academy:item/uber_shiny_incense"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3edef65

Please sign in to comment.