Untitled

mail@pastecode.io avatar
unknown
java
a month ago
3.0 kB
11
Indexable
Never
private void onInteractPainting(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
        if ((Object) this instanceof PaintingEntity painting) {
			Item itemUsed = player.getMainHandStack().getItem();
			PaintingVariant thisVariant = painting.getVariant().value();

            // BRUSHING LOGIC -----------------------------------------------------------------------
            if (itemUsed.equals(Items.BRUSH)) {

                // Generate list of painting variants
                ArrayList<PaintingVariant> list = new ArrayList<>();
                player.getWorld().getRegistryManager().get(RegistryKeys.PAINTING_VARIANT).forEach(list::add);

				ArrayList<PaintingVariant> filteredList = new ArrayList<>();

				// Sort list down into paintings of the same size
				for (int i = 0; i < list.size(); i++) {
					PaintingVariant variant = list.get(i);
					// Remove the ones that don't match sizing
					if (variant.width() == thisVariant.width()) {
						if (variant.height() == thisVariant.height()) {
							filteredList.add(list.get(i));
						}
					}
				}

				// add any unlocked paintings to the list
				if (player instanceof ServerPlayerEntity) {
					ArrayList<Identifier> playerUnlocks = player.getComponent(PaintingFramesComponents.PAINTING_UNLOCKS).getValue();
					for (PaintingVariant entry : list) {
						if (playerUnlocks.contains(entry.assetId())) {
							if (entry.width() == thisVariant.width()) {
								if (entry.height() == thisVariant.height()) {
									filteredList.add(entry);
								}
							}
						}
					}
				}

				int indexOfCurrent = filteredList.indexOf(thisVariant);
				if (indexOfCurrent == filteredList.size()-1) {
					indexOfCurrent = 0;
				}

				PaintingVariant nextVariant = filteredList.get((indexOfCurrent+1));
				painting.setVariant(RegistryEntry.of(nextVariant));

				SoundEvent soundEvent = SoundEvents.ITEM_BRUSH_BRUSHING_GENERIC;
				player.getWorld().playSound(player, painting.getBlockPos(), soundEvent, SoundCategory.BLOCKS);
				PaintingFrames.LOGGER.info(filteredList.toString());
				cir.setReturnValue(ActionResult.SUCCESS);
			}

			Frame frame = FrameVariants.frameFromItem(itemUsed);
			// Make sure frame is not null and that it isn't the current one.
			if (frame != null && !frame.equals(FrameVariants.getFrame(painting.getComponent(PaintingFramesComponents.FRAME_TYPE).getValue()))) {
				if (!player.getAbilities().creativeMode) {
					player.getMainHandStack().decrement(1);
				}

				FrameComponent frameComponent = (FrameComponent) painting.getComponent(PaintingFramesComponents.FRAME_TYPE);
				PaintingFrames.LOGGER.info("Painting dyed with " + frame.toString());
				frameComponent.setValue(frame);
				SoundEvent soundEvent = SoundEvents.ITEM_DYE_USE;
				player.getWorld().playSound(player, painting.getBlockPos(), soundEvent, SoundCategory.BLOCKS);
				cir.setReturnValue(ActionResult.SUCCESS);

			}
        }
Leave a Comment