Skip to content

Commit

Permalink
Add an item group containing sample takers for all entities that have…
Browse files Browse the repository at this point in the history
… a spawn egg
  • Loading branch information
OffsetMonkey538 committed Oct 5, 2023
1 parent d5cbcfa commit fc9f3b1
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.Box;
import top.offsetmonkey538.compactmobfarms.block.entity.CompactMobFarmBlockEntity;
import top.offsetmonkey538.compactmobfarms.item.SampleTakerItem;
import top.offsetmonkey538.compactmobfarms.item.FilledSampleTakerItem;

public class CompactMobFarmBlockEntityRenderer implements BlockEntityRenderer<CompactMobFarmBlockEntity> {
public CompactMobFarmBlockEntityRenderer(BlockEntityRendererFactory.Context context) {
Expand All @@ -24,7 +24,7 @@ public void render(CompactMobFarmBlockEntity blockEntity, float tickDelta, Matri
final ItemStack sampleTaker = blockEntity.getSampleTaker();
if (sampleTaker == null) return;

final EntityType<?> entityType = SampleTakerItem.getSampledEntityType(sampleTaker);
final EntityType<?> entityType = FilledSampleTakerItem.getSampledEntityType(sampleTaker);
if (entityType == null) return;

final Entity entity = entityType.create(blockEntity.getWorld());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import top.offsetmonkey538.compactmobfarms.accessor.EntityAccessor;
import top.offsetmonkey538.compactmobfarms.accessor.LivingEntityAccessor;
import top.offsetmonkey538.compactmobfarms.inventory.CompactMobFarmInventory;
import top.offsetmonkey538.compactmobfarms.item.SampleTakerItem;
import top.offsetmonkey538.compactmobfarms.item.FilledSampleTakerItem;
import top.offsetmonkey538.compactmobfarms.item.upgrade.CompactMobFarmUpgradeItem;
import top.offsetmonkey538.compactmobfarms.network.ModPackets;
import top.offsetmonkey538.compactmobfarms.screen.CompactMobFarmScreenHandler;
Expand Down Expand Up @@ -165,7 +165,7 @@ private boolean setCurrentEntity() {
final ItemStack sampleTaker = this.getSampleTaker();
if (sampleTaker == null) return false;

final EntityType<?> livingEntityType = SampleTakerItem.getSampledEntityType(sampleTaker);
final EntityType<?> livingEntityType = FilledSampleTakerItem.getSampledEntityType(sampleTaker);
sendEntityUpdatePacket(livingEntityType);
if (livingEntityType == null) return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGen
public void generateItemModels(ItemModelGenerator itemModelGenerator) {
itemModelGenerator.register(ModItems.SPIRIT_BOTTLE, Models.GENERATED);
itemModelGenerator.register(ModItems.SPAWNER_SHARD, Models.GENERATED);
itemModelGenerator.register(ModItems.FILLED_SAMPLE_TAKER, Models.GENERATED);

itemModelGenerator.writer.accept(ModelIds.getItemModelId(ModItems.COMPACT_MOB_FARM), new SimpleModelSupplier(ModelIds.getBlockModelId(ModBlocks.COMPACT_MOB_FARM)));
Models.GENERATED.upload(ModelIds.getItemModelId(ModItems.SAMPLE_TAKER), TextureMap.layer0(ModItems.SAMPLE_TAKER), itemModelGenerator.writer, this::sampleTakerJsonGenerator);
for (int i = 1; i <= 10; i++) {
for (int i = 1; i <= 9; i++) {
itemModelGenerator.register(ModItems.SAMPLE_TAKER, "_filled_" + i, Models.GENERATED);
}
}
Expand All @@ -44,7 +45,7 @@ private JsonObject sampleTakerJsonGenerator(Identifier id, Map<TextureKey, Ident
JsonObject json = Models.GENERATED.createJson(id, textures);

JsonArray overrides = new JsonArray();
for (int filled = 1; filled <= 10; filled++) {
for (int filled = 1; filled <= 9; filled++) {
JsonObject override = new JsonObject();
JsonObject predicate = new JsonObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ public ModEnglishLanguageProvider(FabricDataOutput dataOutput) {
@Override
public void generateTranslations(TranslationBuilder translationBuilder) {
translationBuilder.add("itemGroup.compact_mob_farms.main_group", "Compact Mob Farms");
translationBuilder.add("itemGroup.compact_mob_farms.filled_sample_takers_group", "Sample Takers");

translationBuilder.add(ModItems.SPIRIT_BOTTLE, "Bottle of Spirits");
translationBuilder.add(ModItems.SPAWNER_SHARD, "Spawner Shard");
translationBuilder.add(ModItems.COMPACT_MOB_FARM, "Compact Mob Farm");

translationBuilder.add(ModItems.SAMPLE_TAKER, "Sample taker");
translationBuilder.add(ModItems.SAMPLE_TAKER.getTranslationKey() + ".filled", "Filled sample taker");
translationBuilder.add(ModItems.SAMPLE_TAKER, "Sample Taker");
translationBuilder.add(ModItems.FILLED_SAMPLE_TAKER, "Filled Sample Taker");

translationBuilder.add(ModItems.SAMPLE_TAKER.getTranslationKey() + ".tooltip.amount", "Samples taken: %s");
translationBuilder.add(ModItems.SAMPLE_TAKER.getTranslationKey() + ".tooltip.type", "Sample type: %s");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ public ModEstonianLanguageProvider(FabricDataOutput dataOutput) {
@Override
public void generateTranslations(TranslationBuilder translationBuilder) {
translationBuilder.add("itemGroup.compact_mob_farms.main_group", "Kompaktsed Olendi Farmid");
translationBuilder.add("itemGroup.compact_mob_farms.filled_sample_takers_group", "Proovivõtjad");

translationBuilder.add(ModItems.SPIRIT_BOTTLE, "Pudel vaime");
translationBuilder.add(ModItems.SPAWNER_SHARD, "Tekitaja kild");
translationBuilder.add(ModItems.COMPACT_MOB_FARM, "Kompaktne Olendi Farm");

translationBuilder.add(ModItems.SAMPLE_TAKER, "Proovivõtja");
translationBuilder.add(ModItems.SAMPLE_TAKER.getTranslationKey() + ".filled", "Täidetud proovivõtja");
translationBuilder.add(ModItems.FILLED_SAMPLE_TAKER, "Täidetud proovivõtja");

translationBuilder.add(ModItems.SAMPLE_TAKER.getTranslationKey() + ".tooltip.amount", "Proove võetud: %s");
translationBuilder.add(ModItems.SAMPLE_TAKER.getTranslationKey() + ".tooltip.type", "Proovi tüüp: %s");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package top.offsetmonkey538.compactmobfarms.item;

import java.util.List;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class FilledSampleTakerItem extends Item {
public static final String SAMPLED_ENTITY_KEY = "SampledEntity";

public FilledSampleTakerItem(Settings settings) {
super(settings);
}

@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
final EntityType<?> sampledEntity = getSampledEntityType(stack);

if (sampledEntity == null) return;

tooltip.add(Text.translatable(ModItems.SAMPLE_TAKER.getTranslationKey() + ".tooltip.type", sampledEntity.getName()));
}

public static void setSampledEntity(ItemStack stack, Identifier entity) {
stack.getOrCreateNbt().putString(SAMPLED_ENTITY_KEY, entity.toString());
}

@Nullable
public static Identifier getSampledEntityId(ItemStack stack) {
if (stack.getNbt() == null || !stack.getNbt().contains(SAMPLED_ENTITY_KEY)) return null;
return new Identifier(stack.getNbt().getString(SAMPLED_ENTITY_KEY));
}

@Nullable
public static EntityType<?> getSampledEntityType(ItemStack stack) {
final Identifier id = getSampledEntityId(stack);

if (id == null || !Registries.ENTITY_TYPE.containsId(id)) return null;

return Registries.ENTITY_TYPE.get(id);
}

public ItemStack forEntity(@Nullable EntityType<?> entity) {
return forEntity(Registries.ENTITY_TYPE.getId(entity));
}

public ItemStack forEntity(Identifier entityId) {
final ItemStack stack = new ItemStack(this);

setSampledEntity(stack, entityId);

return stack;
}

@Override
public ItemStack getDefaultStack() {
return forEntity(Registries.ENTITY_TYPE.getDefaultId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ private ModItems() {

}

public static final BlockItem COMPACT_MOB_FARM = register("compact_mob_farm", new BlockItem(ModBlocks.COMPACT_MOB_FARM, new FabricItemSettings().rarity(Rarity.UNCOMMON)));
public static final SampleTakerItem SAMPLE_TAKER = register("sample_taker", new SampleTakerItem(new FabricItemSettings().maxCount(1)));
public static final Item SPIRIT_BOTTLE = register("spirit_bottle", new Item(new FabricItemSettings().maxCount(16).rarity(Rarity.COMMON).recipeRemainder(Items.GLASS_BOTTLE)));
public static final Item SPAWNER_SHARD = register("spawner_shard", new Item(new FabricItemSettings().rarity(Rarity.UNCOMMON)));
public static final BlockItem COMPACT_MOB_FARM = register("compact_mob_farm", new BlockItem(ModBlocks.COMPACT_MOB_FARM, new FabricItemSettings().rarity(Rarity.RARE)));
public static final SampleTakerItem SAMPLE_TAKER = register("sample_taker", new SampleTakerItem(new FabricItemSettings().maxCount(1)));
public static final FilledSampleTakerItem FILLED_SAMPLE_TAKER = register("filled_sample_taker", new FilledSampleTakerItem(new FabricItemSettings().maxCount(1).rarity(Rarity.UNCOMMON)));
public static final Item SPIRIT_BOTTLE = register("spirit_bottle", new Item(new FabricItemSettings().maxCount(16).rarity(Rarity.UNCOMMON).recipeRemainder(Items.GLASS_BOTTLE)));
public static final Item SPAWNER_SHARD = register("spawner_shard", new Item(new FabricItemSettings().rarity(Rarity.UNCOMMON)));

public static final CompactMobFarmUpgradeItem DEBUG_SPEED_UPGRADE = register("debug_speed_upgrade", new CompactMobFarmUpgradeItem() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ActionResult useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity
final List<UUID> samplesCollected = getSamplesCollected(stack);
final UUID targetUuid = targetEntity.getUuid();

if (samplesCollected.size() >= 10 || samplesCollected.contains(targetUuid) || (sampledEntiyIdentifier != null && !sampledEntiyIdentifier.equals(targetEntityIdentifier))) return super.useOnEntity(stack, user, targetEntity, hand);
if (samplesCollected.contains(targetUuid) || (sampledEntiyIdentifier != null && !sampledEntiyIdentifier.equals(targetEntityIdentifier))) return super.useOnEntity(stack, user, targetEntity, hand);


if (user.getWorld().isClient()) return ActionResult.SUCCESS;
Expand All @@ -45,6 +45,11 @@ public ActionResult useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity
samplesCollected.add(targetUuid);
setSamplesCollected(stack, samplesCollected);

if (samplesCollected.size() >= 10) {
user.setStackInHand(hand, ModItems.FILLED_SAMPLE_TAKER.forEntity(sampledEntiyIdentifier));
return ActionResult.CONSUME;
}

if (user.getAbilities().creativeMode) user.setStackInHand(hand, stack);

return ActionResult.CONSUME;
Expand Down Expand Up @@ -77,12 +82,6 @@ public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> too
tooltip.add(Text.translatable(getTranslationKey() + ".tooltip.type", sampledEntity.getName()));
}

@Override
public String getTranslationKey(ItemStack stack) {
if (getSamplesCollected(stack).size() >= 10) return super.getTranslationKey(stack) + ".filled";
return super.getTranslationKey(stack);
}

public static void setSampledEntity(ItemStack stack, Identifier entity) {
stack.getOrCreateNbt().putString(SAMPLED_ENTITY_KEY, entity.toString());
}
Expand All @@ -95,11 +94,13 @@ public static void setSamplesCollected(ItemStack stack, List<UUID> samplesCollec
stack.getOrCreateNbt().put(SAMPLES_COLLECTED_KEY, nbtList);
}

@Nullable
public static Identifier getSampledEntityId(ItemStack stack) {
if (stack.getNbt() == null || !stack.getNbt().contains(SAMPLED_ENTITY_KEY)) return null;
return new Identifier(stack.getNbt().getString(SAMPLED_ENTITY_KEY));
}

@Nullable
public static EntityType<?> getSampledEntityType(ItemStack stack) {
final Identifier id = getSampledEntityId(stack);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.text.Text;
Expand All @@ -26,6 +27,13 @@ private ModItemGroups() {
})
.build()
);
public static final ItemGroup FILLED_SAMPLE_TAKERS_ITEM_GROUP = register("filled_sample_takers_item_group",
FabricItemGroup.builder()
.icon(ModItems.FILLED_SAMPLE_TAKER::getDefaultStack)
.displayName(Text.translatable("itemGroup.compact_mob_farms.filled_sample_takers_group"))
.entries((displayContext, entries) -> SpawnEggItem.getAll().forEach(spawnEgg -> entries.add(ModItems.FILLED_SAMPLE_TAKER.forEntity(spawnEgg.getEntityType(null)))))
.build()
);


private static <T extends ItemGroup> T register(String name, T group) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import net.minecraft.util.Identifier;
import top.offsetmonkey538.compactmobfarms.block.ModBlocks;
import top.offsetmonkey538.compactmobfarms.block.entity.CompactMobFarmBlockEntity;
import top.offsetmonkey538.compactmobfarms.item.SampleTakerItem;
import top.offsetmonkey538.compactmobfarms.item.ModItems;
import top.offsetmonkey538.compactmobfarms.item.upgrade.CompactMobFarmUpgradeItem;

public class CompactMobFarmScreenHandler extends ScreenHandler {
Expand Down Expand Up @@ -60,7 +60,7 @@ public CompactMobFarmScreenHandler(int syncId, PlayerInventory playerInventory,
this.addSlot(new Slot(sampleTaker, 0, 35, 16) {
@Override
public boolean canInsert(ItemStack stack) {
return context.get((world, pos) -> SampleTakerItem.getSamplesCollected(stack).size() >= 10, false);
return context.get((world, pos) -> stack.isOf(ModItems.FILLED_SAMPLE_TAKER), false);
}
});

Expand Down

0 comments on commit fc9f3b1

Please sign in to comment.